2022年4月25日 星期一

ESP8266 WiFi模組+App Inventor 2 控制智高積木馬達

AI2程式碼

ESP8266程式碼
/* * This sketch demonstrates how to set up a simple HTTP-like server. * The server will set a GPIO pin depending on the request * http://server_ip/gpio/0 will set the GPIO2 low, * http://server_ip/gpio/1 will set the GPIO2 high * server_ip is the IP address of the ESP8266 module, will be * printed to Serial when the module is connected. */ #include <ESP8266WiFi.h> const char *ssid = "Gigo-Motor-TEST"; const char *password = "12345678"; // Create an instance of the server // specify the port to listen on as an argument WiFiServer server(80); void setup() { IPAddress Ip(192, 168, 255, 1); IPAddress NMask(255, 0, 0, 0); // prepare GPIO0 pinMode(0, OUTPUT); pinMode(2, OUTPUT); digitalWrite(0, 1); // prepare GPIO2 digitalWrite(2, 1); Serial.begin(115200); delay(10); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Configuring access point..."); WiFi.softAPConfig(Ip, Ip, NMask); /* You can remove the password parameter if you want the AP to be open. */ WiFi.softAP(ssid, password); IPAddress myIP = WiFi.softAPIP(); Serial.print("AP IP address: "); Serial.println(myIP); server.begin(); Serial.println("HTTP server started"); } void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Wait until the client sends some data Serial.println("new client"); while(!client.available()){ delay(1); } // Read the first line of the request String req = client.readStringUntil('\r'); Serial.println(req); client.flush(); // Match the request int val; if (req.indexOf("/gpio/0") != -1) val = 0; else if (req.indexOf("/gpio/1") != -1) val = 1; else if (req.indexOf("/gpio/2") != -1) val = 2; else { Serial.println("invalid request"); client.stop(); return; } switch(val) { case 0: digitalWrite(0, 1); digitalWrite(2, 1); break; case 1: digitalWrite(0, 1); digitalWrite(2, 0); break; case 2: digitalWrite(0, 0); digitalWrite(2, 1); break; } client.flush(); String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n"; s += "OK"; s += "</html>\n"; // Send the response to the client client.print(s); delay(1); Serial.println("Client disonnected"); // The client will actually be disconnected // when the function returns and 'client' object is detroyed }















































































執行結果




沒有留言:

張貼留言

使Arduino Nano Every在ARDUINO IDE支援ATmega4809 48pin包裝

軟體環境 ARDUINO IDE 2.3.2 板函式庫MegaCoreX 1.1.2, https://github.com/MCUdude/MegaCoreX ARDUINO IDE 選擇48pin,編譯時會產生以下錯誤 修正錯誤 修改C:\Users\<your_acc...