#include const char* ssid = "ssid_name"; const char* password = "abc123"; WiFiServer server(1234); #define NO_ACTION 0 #define BAD_ACTION 1 #define TOGGLE_DOOR1 D1 // weMOS relay pin #define READ_DISTANCE1 D2 typedef struct { const char* actionWord; int actionToken; } ActionArray; #define NUM_ACTIONS 2 ActionArray actionArray[NUM_ACTIONS] { {"toggle1", TOGGLE_DOOR1}, {"distance1", READ_DISTANCE1} }; void setup() { Serial.begin(115200); delay(10); Serial.print("Connecting to "); Serial.println( ssid ); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println( "." ); Serial.println( "WiFi connected."); Serial.print( "IP address: "); Serial.println( WiFi.localIP()); pinMode(BUILTIN_LED, OUTPUT); // initialize onboard LED as output pinMode(TOGGLE_DOOR1, OUTPUT); // Start the server server.begin(); Serial.println("Server started"); } void loop() { digitalWrite(BUILTIN_LED, HIGH); // turn on LED with voltage HIGH delay(1000); // wait one second digitalWrite(BUILTIN_LED, LOW); // turn off LED with voltage LOW delay(1000); // wait one second // Check if a client has connected WiFiClient client = server.available(); if (!client) { // Serial.println("no 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(); int action = NO_ACTION; for( int i=0;i30?"open":"closed"); } else { Serial.println( "no matching action" ); } } long microsecondsToCentimeters(long microseconds) { // The speed of sound is 340 m/s or 29 microseconds per centimeter. // The ping travels out and back, so to find the distance of the // object we take half of the distance travelled. return microseconds / 29 / 2; }