#include #include #define NO_ACTION 0 #define BAD_ACTION 1 #define TOGGLE_DOOR1 D1 // weMOS relay pin #define READ_DISTANCE1 D2 #define READ_TEMP1 D3 #define FORCE_SEND_DATA 9999 #define SERVER_PORT 1234 #define ONEMIN (1000UL * 60) // #define pulseTIMER (ONEMIN / 4) #define pulseTIMER (ONEMIN * 10) /* * Server and oneWire devices */ WiFiServer server(SERVER_PORT); OneWire ds(D3); /* * supported actions */ typedef struct { const char* actionWord; int actionToken; } ActionArray; #define NUM_ACTIONS 4 ActionArray actionArray[NUM_ACTIONS] { {"toggle1", TOGGLE_DOOR1}, {"distance1", READ_DISTANCE1}, {"temperature1", READ_TEMP1}, {"forceSend", FORCE_SEND_DATA} }; /* * WIFI connect info */ const char* ssid = "ssid_name"; const char* password = "abc123"; void setup() { Serial.begin(9600); delay(10); Serial.println(""); 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); digitalWrite(TOGGLE_DOOR1, LOW); // Start the server server.begin(); Serial.println("Server started"); } void loop() { // send data every pulseTIMER minutes unsigned static long rolltime = millis() + pulseTIMER; if((long)(millis() - rolltime) >= 0) { sendData(); rolltime += pulseTIMER; } // blink on board led to show the device is working 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) { 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'); client.flush(); handleRequest(client, req); } void handleRequest(WiFiClient& client, String& req) { int action = NO_ACTION; for( int i=0;i