/* ___ ___ ___ _ _ ___ ___ ____ ___ ____ * / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \| \ *| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | | * \___/(___/ \___/ \__ |\___/ \___(_)____)___/|_|_|_| * (____/ * Osoyoo Wifi Arduino Shield Testing Project * Get remote Temperature and toggle LED on/off with web browser * tutorial url: http://osoyoo.com/?p=21094 */ #include dht DHT; #define DHT11_PIN 5 // DHT11 sensor data pin connect to D5 int ledPin=13; //LED connect to D13 int ledStatus=0; String ledStr="OFF"; String toggleStr="
"; #define DEBUG true #include "WiFiEsp.h" // Emulate Serial1 on pins 3/2 by default // If you want to use Hard Serial1 in Mega2560 or Leonardo, please remove the wifi shield jumper cap on TX->D3 RX->D2 #ifndef HAVE_HWSERIAL1 #include "SoftwareSerial.h" SoftwareSerial Serial1(3, 2); // RX, TX #endif char ssid[] = "wifi_hotspot_ssid"; // your network SSID (name) char pass[] = "wifi_password"; // your network password int status = WL_IDLE_STATUS; // the Wifi radio's status int connectionId; void setup() { pinMode(ledPin,OUTPUT); // initialize serial for debugging Serial.begin(9600); // initialize serial for ESP module Serial1.begin(9600); // initialize ESP module WiFi.init(&Serial1); // check for the presence of the shield if (WiFi.status() == WL_NO_SHIELD) { Serial.println("WiFi shield not present"); // don't continue while (true); } // attempt to connect to WiFi network while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to WPA SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network status = WiFi.begin(ssid, pass); } sendDataToESP8266("AT+CIPMUX=1\r\n",false); // configure for multiple connections sendDataToESP8266("AT+CIPSERVER=1,80\r\n",true); // turn on http server on port 80 Serial.println("You're connected to the network"); Serial.println(); printCurrentNet(); printWifiData(); } void loop() { DHT.read11(DHT11_PIN); delay(200); float temp=DHT.temperature; float humid=DHT.humidity; if(Serial1.available()) { if(Serial1.find("+IPD,")) { delay(300); connectionId = Serial1.read()-48; if(Serial1.find("led=TURN_O")) { int led_sign=Serial1.read(); if (led_sign==78) { ledStatus=1; toggleStr="
"; } else { ledStatus=0; toggleStr="
"; } // Serial.print("LED value="); // Serial.println(ledStatus); digitalWrite(ledPin,ledStatus); } if (ledStatus==1) ledStr="ON"; else ledStr="OFF"; //Sending data to browser String webpage=""; webpage +="Temp="+String(temp); webpage +="C Humid="+String(humid)+"%
"; webpage +="LED is "+ledStr+"
"; webpage +=toggleStr; webpage +=""; sendWebpageToESP8266(webpage); String closeCommand = "AT+CIPCLOSE="; ////////////////close the socket connection////esp command closeCommand+=connectionId; // append connection id closeCommand+="\r\n"; sendDataToESP8266(closeCommand,true); } } if(Serial.available()) { String serialCommand=Serial.readString(); sendDataToESP8266(serialCommand,true); } } void printWifiData() { // print your WiFi shield's IP address IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip); // print your MAC address byte mac[6]; WiFi.macAddress(mac); char buf[20]; sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]); Serial.print("MAC address: "); Serial.println(buf); } void printCurrentNet() { // print the SSID of the network you're attached to Serial.print("SSID: "); Serial.println(WiFi.SSID()); // print the MAC address of the router you're attached to byte bssid[6]; WiFi.BSSID(bssid); char buf[20]; sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]); Serial.print("BSSID: "); Serial.println(buf); // print the received signal strength long rssi = WiFi.RSSI(); Serial.print("Signal strength (RSSI): "); Serial.println(rssi); } void sendWebpageToESP8266(String content) { String cipSendStr = "AT+CIPSEND="; cipSendStr += connectionId; cipSendStr += ","; cipSendStr +=content.length(); cipSendStr +="\r\n"; sendDataToESP8266(cipSendStr,true); sendDataToESP8266(content,true); } void sendDataToESP8266(String messageData,boolean debug) { String response; Serial1.print(messageData); response= Serial1.readString(); // read response from esp8266 if (debug) Serial.println(response); //displays the esp response messages in arduino Serial monitor }