AI2程式碼
/*
* 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
}
執行結果



沒有留言:
張貼留言