├── .DS_Store ├── .gitattributes ├── Ep-1_ESPAsyncWebServer_mDNS └── Ep-1_ESPAsyncWebServer_mDNS.ino ├── Ep-2_HTML_Page_with_buttons └── Ep-2_HTML_Page_with_buttons.ino ├── Ep-3_WebSockets_JavaScript └── Ep-3_WebSockets_JavaScript.ino ├── Ep-4_JSON_WebSockets └── Ep-4_JSON_WebSockets.ino └── Ep-5_Send Sensor ├── Send_Sensor_ESP32 └── Send_Sensor_ESP32.ino └── Send_Sensor_NodeMCU └── Send_Sensor_NodeMCU.ino /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techiesms/NodeMCU-ESP32-Series/f56223dc0fac7e48f80eccb8e980f10235fde5f8/.DS_Store -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Ep-1_ESPAsyncWebServer_mDNS/Ep-1_ESPAsyncWebServer_mDNS.ino: -------------------------------------------------------------------------------- 1 | #ifdef ESP8266 2 | #include 3 | #include 4 | #elif defined(ESP32) 5 | #include 6 | #include 7 | #else 8 | #error "Board not found" 9 | #endif 10 | 11 | 12 | 13 | #include 14 | 15 | AsyncWebServer server(80); // server port 80 16 | 17 | void notFound(AsyncWebServerRequest *request) 18 | { 19 | request->send(404, "text/plain", "Page Not found"); 20 | } 21 | 22 | void setup(void) 23 | { 24 | 25 | Serial.begin(115200); 26 | 27 | WiFi.softAP("techiesms", ""); 28 | Serial.println("softap"); 29 | Serial.println(""); 30 | Serial.println(WiFi.softAPIP()); 31 | 32 | 33 | if (MDNS.begin("ESP")) { //esp.local/ 34 | Serial.println("MDNS responder started"); 35 | } 36 | 37 | 38 | 39 | server.on("/", [](AsyncWebServerRequest * request) 40 | { 41 | String message = "hello world"; 42 | request->send(200, "text/plain", message); 43 | }); 44 | 45 | server.on("/page1", HTTP_GET, [](AsyncWebServerRequest * request) 46 | { 47 | String message = "Welcome to page1"; 48 | request->send(200, "text/plain", message); 49 | }); 50 | 51 | server.onNotFound(notFound); 52 | 53 | server.begin(); // it will start webserver 54 | } 55 | 56 | 57 | void loop(void) 58 | { 59 | } 60 | -------------------------------------------------------------------------------- /Ep-2_HTML_Page_with_buttons/Ep-2_HTML_Page_with_buttons.ino: -------------------------------------------------------------------------------- 1 | #ifdef ESP8266 2 | #include 3 | #include 4 | #elif defined(ESP32) 5 | #include 6 | #include 7 | #else 8 | #error "Board not found" 9 | #endif 10 | 11 | 12 | #define LED1 15 13 | #define LED2 12 14 | 15 | char webpage[] PROGMEM = R"=====( 16 | 17 | 18 | 19 | 20 | 21 |
22 |

My Home Automation

23 | 24 |

LED 1

25 | 26 |

LED 2

27 | 28 | 29 |
30 | 31 | 32 | 33 | )====="; 34 | 35 | // ipaddress/led1/on 36 | //ipaddress/led1/off 37 | 38 | // ipaddress/led2/on 39 | //ipaddress/led2/off 40 | #include 41 | 42 | AsyncWebServer server(80); // server port 80 43 | 44 | void notFound(AsyncWebServerRequest *request) 45 | { 46 | request->send(404, "text/plain", "Page Not found"); 47 | } 48 | 49 | void setup(void) 50 | { 51 | 52 | Serial.begin(115200); 53 | pinMode(LED1,OUTPUT); 54 | pinMode(LED2,OUTPUT); 55 | 56 | WiFi.softAP("techiesms", ""); 57 | Serial.println("softap"); 58 | Serial.println(""); 59 | Serial.println(WiFi.softAPIP()); 60 | 61 | 62 | if (MDNS.begin("ESP")) { //esp.local/ 63 | Serial.println("MDNS responder started"); 64 | } 65 | 66 | 67 | 68 | server.on("/", [](AsyncWebServerRequest * request) 69 | { 70 | 71 | request->send_P(200, "text/html", webpage); 72 | }); 73 | 74 | server.on("/led1/on", HTTP_GET, [](AsyncWebServerRequest * request) 75 | { 76 | digitalWrite(LED1,HIGH); 77 | request->send_P(200, "text/html", webpage); 78 | }); 79 | 80 | server.onNotFound(notFound); 81 | 82 | server.begin(); // it will start webserver 83 | } 84 | 85 | 86 | void loop(void) 87 | { 88 | } 89 | -------------------------------------------------------------------------------- /Ep-3_WebSockets_JavaScript/Ep-3_WebSockets_JavaScript.ino: -------------------------------------------------------------------------------- 1 | #ifdef ESP8266 2 | #include 3 | #include 4 | #elif defined(ESP32) 5 | #include 6 | #include 7 | #else 8 | #error "Board not found" 9 | #endif 10 | 11 | #include 12 | 13 | 14 | 15 | #define LED1 13 16 | #define LED2 12 17 | 18 | char webpage[] PROGMEM = R"=====( 19 | 20 | 21 | 22 | 23 | 41 | 42 | 43 |
44 |

My Home Automation

45 | 46 |

LED 1

47 | 48 |

LED 2

49 | 50 | 51 |
52 | 53 | 54 | 55 | )====="; 56 | 57 | // ipaddress/led1/on 58 | //ipaddress/led1/off 59 | 60 | // ipaddress/led2/on 61 | //ipaddress/led2/off 62 | #include 63 | 64 | AsyncWebServer server(80); // server port 80 65 | WebSocketsServer websockets(81); 66 | 67 | void notFound(AsyncWebServerRequest *request) 68 | { 69 | request->send(404, "text/plain", "Page Not found"); 70 | } 71 | 72 | 73 | void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { 74 | 75 | switch (type) 76 | { 77 | case WStype_DISCONNECTED: 78 | Serial.printf("[%u] Disconnected!\n", num); 79 | break; 80 | case WStype_CONNECTED: { 81 | IPAddress ip = websockets.remoteIP(num); 82 | Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); 83 | 84 | // send message to client 85 | websockets.sendTXT(num, "Connected from server"); 86 | } 87 | break; 88 | case WStype_TEXT: 89 | Serial.printf("[%u] get Text: %s\n", num, payload); 90 | String message = String((char*)( payload)); 91 | Serial.println(message); 92 | 93 | if(message == "LED 1 is OFF"){ 94 | digitalWrite(LED1,LOW); 95 | } 96 | 97 | if(message == "LED 1 is ON"){ 98 | digitalWrite(LED1,HIGH); 99 | } 100 | 101 | 102 | 103 | } 104 | } 105 | 106 | void setup(void) 107 | { 108 | 109 | Serial.begin(115200); 110 | pinMode(LED1,OUTPUT); 111 | pinMode(LED2,OUTPUT); 112 | 113 | WiFi.softAP("techiesms", ""); 114 | Serial.println("softap"); 115 | Serial.println(""); 116 | Serial.println(WiFi.softAPIP()); 117 | 118 | 119 | if (MDNS.begin("ESP")) { //esp.local/ 120 | Serial.println("MDNS responder started"); 121 | } 122 | 123 | 124 | 125 | server.on("/", [](AsyncWebServerRequest * request) 126 | { 127 | 128 | request->send_P(200, "text/html", webpage); 129 | }); 130 | 131 | server.on("/led1/on", HTTP_GET, [](AsyncWebServerRequest * request) 132 | { 133 | digitalWrite(LED1,HIGH); 134 | request->send_P(200, "text/html", webpage); 135 | }); 136 | 137 | server.onNotFound(notFound); 138 | 139 | server.begin(); // it will start webserver 140 | websockets.begin(); 141 | websockets.onEvent(webSocketEvent); 142 | 143 | } 144 | 145 | 146 | void loop(void) 147 | { 148 | websockets.loop(); 149 | } 150 | -------------------------------------------------------------------------------- /Ep-4_JSON_WebSockets/Ep-4_JSON_WebSockets.ino: -------------------------------------------------------------------------------- 1 | #ifdef ESP8266 2 | #include 3 | #include 4 | #elif defined(ESP32) 5 | #include 6 | #include 7 | #else 8 | #error "Board not found" 9 | #endif 10 | 11 | #include 12 | 13 | #include 14 | 15 | 16 | 17 | #define LED1 13 18 | #define LED2 12 19 | 20 | char webpage[] PROGMEM = R"=====( 21 | 22 | 23 | 24 | 25 | 68 | 69 | 70 |
71 |

My Home Automation

72 | 73 |

LED 1

74 | 75 |

LED 2

76 | 77 | 78 |
79 | 80 | 81 | 82 | )====="; 83 | 84 | // ipaddress/led1/on 85 | //ipaddress/led1/off 86 | 87 | // ipaddress/led2/on 88 | //ipaddress/led2/off 89 | #include 90 | 91 | AsyncWebServer server(80); // server port 80 92 | WebSocketsServer websockets(81); 93 | 94 | void notFound(AsyncWebServerRequest *request) 95 | { 96 | request->send(404, "text/plain", "Page Not found"); 97 | } 98 | 99 | 100 | void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { 101 | 102 | switch (type) 103 | { 104 | case WStype_DISCONNECTED: 105 | Serial.printf("[%u] Disconnected!\n", num); 106 | break; 107 | case WStype_CONNECTED: { 108 | IPAddress ip = websockets.remoteIP(num); 109 | Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); 110 | 111 | // send message to client 112 | websockets.sendTXT(num, "Connected from server"); 113 | } 114 | break; 115 | case WStype_TEXT: 116 | Serial.printf("[%u] get Text: %s\n", num, payload); 117 | String message = String((char*)( payload)); 118 | Serial.println(message); 119 | 120 | 121 | DynamicJsonDocument doc(200); 122 | // deserialize the data 123 | DeserializationError error = deserializeJson(doc, message); 124 | // parse the parameters we expect to receive (TO-DO: error handling) 125 | // Test if parsing succeeds. 126 | if (error) { 127 | Serial.print("deserializeJson() failed: "); 128 | Serial.println(error.c_str()); 129 | return; 130 | } 131 | 132 | int LED1_status = doc["LED1"]; 133 | int LED2_status = doc["LED2"]; 134 | digitalWrite(LED1,LED1_status); 135 | digitalWrite(LED2,LED2_status); 136 | 137 | 138 | 139 | 140 | } 141 | } 142 | 143 | void setup(void) 144 | { 145 | 146 | Serial.begin(115200); 147 | pinMode(LED1,OUTPUT); 148 | pinMode(LED2,OUTPUT); 149 | 150 | WiFi.softAP("techiesms", ""); 151 | Serial.println("softap"); 152 | Serial.println(""); 153 | Serial.println(WiFi.softAPIP()); 154 | 155 | 156 | if (MDNS.begin("ESP")) { //esp.local/ 157 | Serial.println("MDNS responder started"); 158 | } 159 | 160 | 161 | 162 | server.on("/", [](AsyncWebServerRequest * request) 163 | { 164 | 165 | request->send_P(200, "text/html", webpage); 166 | }); 167 | 168 | server.on("/led1/on", HTTP_GET, [](AsyncWebServerRequest * request) 169 | { 170 | digitalWrite(LED1,HIGH); 171 | request->send_P(200, "text/html", webpage); 172 | }); 173 | 174 | server.onNotFound(notFound); 175 | 176 | server.begin(); // it will start webserver 177 | websockets.begin(); 178 | websockets.onEvent(webSocketEvent); 179 | 180 | } 181 | 182 | 183 | void loop(void) 184 | { 185 | websockets.loop(); 186 | } 187 | -------------------------------------------------------------------------------- /Ep-5_Send Sensor/Send_Sensor_ESP32/Send_Sensor_ESP32.ino: -------------------------------------------------------------------------------- 1 | #ifdef ESP8266 2 | #include 3 | #include 4 | #elif defined(ESP32) 5 | #include 6 | #include 7 | #else 8 | #error "Board not found" 9 | #endif 10 | 11 | #include 12 | 13 | #include 14 | #include //DHT and Adafruit Sensor library(https://github.com/adafruit/Adafruit_Sensor) 15 | #include //https://github.com/sstaub/Ticker 16 | 17 | #define DHTPIN 4 18 | #define LED1 13 19 | #define LED2 12 20 | 21 | #define DHTTYPE DHT11 22 | DHT dht(DHTPIN, DHTTYPE); 23 | 24 | void send_sensor(); 25 | 26 | Ticker timer; 27 | 28 | char webpage[] PROGMEM = R"=====( 29 | 30 | 31 | 32 | 33 | 90 | 91 | 92 |
93 |

My Home Automation

94 | 95 |

LED 1

96 | 97 |

LED 2

98 | 99 |
100 | 101 |
102 |

Temperature

2

103 |

Humidity

2

104 | 105 | 106 | 107 | 108 | )====="; 109 | 110 | // ipaddress/led1/on 111 | //ipaddress/led1/off 112 | 113 | // ipaddress/led2/on 114 | //ipaddress/led2/off 115 | #include 116 | 117 | AsyncWebServer server(80); // server port 80 118 | WebSocketsServer websockets(81); 119 | 120 | void notFound(AsyncWebServerRequest *request) 121 | { 122 | request->send(404, "text/plain", "Page Not found"); 123 | } 124 | 125 | 126 | void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { 127 | 128 | switch (type) 129 | { 130 | case WStype_DISCONNECTED: 131 | Serial.printf("[%u] Disconnected!\n", num); 132 | break; 133 | case WStype_CONNECTED: { 134 | IPAddress ip = websockets.remoteIP(num); 135 | Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); 136 | } 137 | break; 138 | case WStype_TEXT: 139 | Serial.printf("[%u] get Text: %s\n", num, payload); 140 | String message = String((char*)( payload)); 141 | Serial.println(message); 142 | 143 | 144 | DynamicJsonDocument doc(200); 145 | // deserialize the data 146 | DeserializationError error = deserializeJson(doc, message); 147 | // parse the parameters we expect to receive (TO-DO: error handling) 148 | // Test if parsing succeeds. 149 | if (error) { 150 | Serial.print("deserializeJson() failed: "); 151 | Serial.println(error.c_str()); 152 | return; 153 | } 154 | 155 | int LED1_status = doc["LED1"]; 156 | int LED2_status = doc["LED2"]; 157 | digitalWrite(LED1,LED1_status); 158 | digitalWrite(LED2,LED2_status); 159 | 160 | 161 | 162 | 163 | } 164 | } 165 | 166 | void setup(void) 167 | { 168 | 169 | Serial.begin(115200); 170 | pinMode(LED1,OUTPUT); 171 | pinMode(LED2,OUTPUT); 172 | dht.begin(); 173 | 174 | WiFi.softAP("techiesms", ""); 175 | Serial.println("softap"); 176 | Serial.println(""); 177 | Serial.println(WiFi.softAPIP()); 178 | 179 | 180 | if (MDNS.begin("ESP")) { //esp.local/ 181 | Serial.println("MDNS responder started"); 182 | } 183 | 184 | 185 | 186 | server.on("/", [](AsyncWebServerRequest * request) 187 | { 188 | 189 | request->send_P(200, "text/html", webpage); 190 | }); 191 | 192 | server.on("/led1/on", HTTP_GET, [](AsyncWebServerRequest * request) 193 | { 194 | digitalWrite(LED1,HIGH); 195 | request->send_P(200, "text/html", webpage); 196 | }); 197 | 198 | server.onNotFound(notFound); 199 | 200 | server.begin(); // it will start webserver 201 | websockets.begin(); 202 | websockets.onEvent(webSocketEvent); 203 | timer.attach(2,send_sensor); 204 | 205 | } 206 | 207 | 208 | void loop(void) 209 | { 210 | websockets.loop(); 211 | } 212 | 213 | void send_sensor() 214 | { 215 | // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) 216 | float h = dht.readHumidity(); 217 | // Read temperature as Celsius (the default) 218 | float t = dht.readTemperature(); 219 | 220 | if (isnan(h) || isnan(t) ) { 221 | Serial.println(F("Failed to read from DHT sensor!")); 222 | return; 223 | } 224 | // JSON_Data = {"temp":t,"hum":h} 225 | String JSON_Data = "{\"temp\":"; 226 | JSON_Data += t; 227 | JSON_Data += ",\"hum\":"; 228 | JSON_Data += h; 229 | JSON_Data += "}"; 230 | Serial.println(JSON_Data); 231 | websockets.broadcastTXT(JSON_Data); 232 | } 233 | -------------------------------------------------------------------------------- /Ep-5_Send Sensor/Send_Sensor_NodeMCU/Send_Sensor_NodeMCU.ino: -------------------------------------------------------------------------------- 1 | #ifdef ESP8266 2 | #include 3 | #include 4 | #elif defined(ESP32) 5 | #include 6 | #include 7 | #else 8 | #error "Board not found" 9 | #endif 10 | 11 | #include 12 | 13 | #include 14 | #include //https://github.com/sstaub/Ticker 15 | 16 | 17 | #define LED1 13 18 | #define LED2 12 19 | 20 | 21 | void send_sensor(); 22 | Ticker timer; 23 | 24 | char webpage[] PROGMEM = R"=====( 25 | 26 | 27 | 28 | 29 | 82 | 83 | 84 |
85 |

My Home Automation

86 | 87 |

LED 1

88 | 89 |

LED 2

90 | 91 |
92 | 93 |
94 |

Proximity Sensor

2

95 | 96 | 97 | 98 | 99 | )====="; 100 | 101 | // ipaddress/led1/on 102 | //ipaddress/led1/off 103 | 104 | // ipaddress/led2/on 105 | //ipaddress/led2/off 106 | #include 107 | 108 | AsyncWebServer server(80); // server port 80 109 | WebSocketsServer websockets(81); 110 | 111 | void notFound(AsyncWebServerRequest *request) 112 | { 113 | request->send(404, "text/plain", "Page Not found"); 114 | } 115 | 116 | 117 | void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { 118 | 119 | switch (type) 120 | { 121 | case WStype_DISCONNECTED: 122 | Serial.printf("[%u] Disconnected!\n", num); 123 | break; 124 | case WStype_CONNECTED: { 125 | IPAddress ip = websockets.remoteIP(num); 126 | Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); 127 | } 128 | break; 129 | case WStype_TEXT: 130 | Serial.printf("[%u] get Text: %s\n", num, payload); 131 | String message = String((char*)( payload)); 132 | Serial.println(message); 133 | 134 | 135 | DynamicJsonDocument doc(200); 136 | // deserialize the data 137 | DeserializationError error = deserializeJson(doc, message); 138 | // parse the parameters we expect to receive (TO-DO: error handling) 139 | // Test if parsing succeeds. 140 | if (error) { 141 | Serial.print("deserializeJson() failed: "); 142 | Serial.println(error.c_str()); 143 | return; 144 | } 145 | 146 | int LED1_status = doc["LED1"]; 147 | int LED2_status = doc["LED2"]; 148 | digitalWrite(LED1,LED1_status); 149 | digitalWrite(LED2,LED2_status); 150 | 151 | 152 | 153 | 154 | } 155 | } 156 | 157 | void setup(void) 158 | { 159 | 160 | Serial.begin(115200); 161 | pinMode(LED1,OUTPUT); 162 | pinMode(LED2,OUTPUT); 163 | WiFi.softAP("techiesms", ""); 164 | Serial.println("softap"); 165 | Serial.println(""); 166 | Serial.println(WiFi.softAPIP()); 167 | 168 | 169 | if (MDNS.begin("ESP")) { //esp.local/ 170 | Serial.println("MDNS responder started"); 171 | } 172 | 173 | 174 | 175 | server.on("/", [](AsyncWebServerRequest * request) 176 | { 177 | 178 | request->send_P(200, "text/html", webpage); 179 | }); 180 | 181 | server.on("/led1/on", HTTP_GET, [](AsyncWebServerRequest * request) 182 | { 183 | digitalWrite(LED1,HIGH); 184 | request->send_P(200, "text/html", webpage); 185 | }); 186 | 187 | server.onNotFound(notFound); 188 | 189 | server.begin(); // it will start webserver 190 | websockets.begin(); 191 | websockets.onEvent(webSocketEvent); 192 | timer.attach(0.5,send_sensor); 193 | pinMode(D5,INPUT); 194 | 195 | } 196 | 197 | 198 | void loop(void) 199 | { 200 | websockets.loop(); 201 | } 202 | 203 | void send_sensor() 204 | { 205 | int sens = digitalRead(D5); 206 | // JSON_Data = {"temp":t,"hum":h}" 207 | String JSON_Data = "{\"sens\":"; 208 | JSON_Data += sens; 209 | JSON_Data += "}"; 210 | Serial.println(JSON_Data); 211 | websockets.broadcastTXT(JSON_Data); 212 | } 213 | --------------------------------------------------------------------------------