// WiFiWebServer Temperature Humidity 198.168.1.180:9701 // Serves up tempeature and humidity // Run this on an ESP8266=01 using GPIO_0 and GPIO_2 for I2C sensor // or run on NodeMCU, or any other flavor of ESP-8266 // // 3.3v NodeMCU_V1 // D3 GPIO_0 SDA internal pull-up 12k // D4 GPIO_2 SDA internal pull-up 12k // D0 GPIO_16 pulled HI through LED (LOW = ON) // // ESP8266-01 // GPIO_0 SDA // GPIO_2 SDA // // had to add delays to get program to run through // might need more?? to make program stable // // WZ 2-5-2016 /* * https://github.com/internetofhomethings/ESP8266-Weather-Sensor-Webserver * This sketch based on the example simple HTTP-like server. * The server will perform 4 functions depending on the request * * 1. http://ESP8266-IP:SVRPORT/gpio/0 will set the GPIO16 low, * http://192.168.1.180:9701/gpio/0 * **Do NOT use this call if running code on an ESP8266-01** * * 2. http://ESP8266-IP:SVRPORT/gpio/1 will set the GPIO16 high * http://192.168.1.180:9701/gpio/1 * **Do NOT use this call if running code on an ESP8266-01** * * 3. http://ESP8266-IP:SVRPORT/?request=GetSensors will return sensor values * http://192.168.1.180:9701/?request=GetSensors * call this from a PC, tablet, or smartphone to get instantaneous temp/humidity * * 4. http://ESP8266-IP:SVRPORT/?request=GetSensors_Non-HTML will return a string with sensor values * http://192.168.1.180:9701/?request=GetSensors_Non-HTML * call this from another microcontroller to get raw data * * NOTE: * * Run this code on a NodeMCU, or any of the ESP8266 * family that contains a GPIO that can be used to light an LED * (or switch a relay/SSR, etc.) * * If running on an ESP8266-01 only 3. and 4. above will work * * ESP8266-IP is the IP address of the ESP8266 module * SVRPORT is the TCP port number the server listens for connections on */ #include #include extern "C" { #include "user_interface.h" } //Server actions #define SET_LED_OFF 0 #define SET_LED_ON 1 #define Get_SENSORS 2 #define SERBAUD 115200 #define SVRPORT 9701 #define ONEJSON 0 #define FIRSTJSON 1 #define NEXTJSON 2 #define LASTJSON 3 //GPIO pin assignments #define I2C_SCL 2 #define I2C_SDA 0 #define LED_IND 16 // LED on NodeMCU //========================================== PERSONAL DATA =============== #include /* * my_info.h contains my personal information * instead use the following lines: * #define myssid "x" // Replace x with YOUR ssid * #define mypassword "y" // Replace y with YOUR password */ //========================================== PERSONAL DATA =============== const uint8_t ipadd[4] = {192,168,1,180}; //static ip assigned to ESP8266 const uint8_t ipgat[4] = {192,168,1,1}; //local router gateway ip const uint8_t ipsub[4] = {255,255,255,0}; #include // This code also works with the HDC1080 HDC1000 mySensor; //HDC1000 mySensor(addr, drdyn_pin) <--- you can change default options if you want. float temperature = 0; float tempF = 0; float humidity = 0; char buf [10]; int lc=0; bool complete=false; uint32_t state=0; char szT[30]; // Create an instance of the server // specify the port to listen on as an argument WiFiServer server(SVRPORT); void printStatus(char * status, int s) { Serial.print("Free heap: "); Serial.print(system_get_free_heap_size()); Serial.print(" "); delay(100); Serial.print(" Time(sec): "); delay(100); Serial.print(millis()/1000); Serial.print(" "); delay(100); Serial.print("status: "); Serial.print(status); delay(100); if(s>=0) Serial.println(s); else Serial.println(""); delay(100); } void startWIFI(void) { // Connect to WiFi network Serial.println(); delay(10); Serial.println(); delay(10); Serial.print("Connecting to "); delay(10); Serial.println(myssid); delay(10); WiFi.config(ipadd, ipgat, ipsub); //dsa added 12.04.2015 WiFi.begin(myssid, mypassword); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); // Print the IP address Serial.print("ESP8266 IP: "); Serial.println(WiFi.localIP()); Serial.print("ESP8266 WebServer Port: "); Serial.println(SVRPORT); delay(300); } //------------------------------------------ void jsonAdd(String *s, String key,String val) { *s += '"' + key + '"' + ":" + '"' + val + '"'; } void jsonEncode(int pos, String * s, String key, String val) { switch (pos) { case ONEJSON: case FIRSTJSON: *s += "{\r\n"; jsonAdd(s,key,val); *s+= (pos==ONEJSON) ? "\r\n}" : ",\r\n"; break; case NEXTJSON: jsonAdd(s,key,val); *s+= ",\r\n"; break; case LASTJSON: jsonAdd(s,key,val); *s+= "\r\n}"; break; } } void killclient(WiFiClient client, bool *busy) { lc=0; delay(1); client.flush(); client.stop(); complete=false; *busy = false; } //===================================================== // ===================================================== // Use this section to get readings from the HDC1080 void readHDC1000() { yield(); Serial.print("Temperature: "); //too bad the Arduino version of sprintf Serial.print(((mySensor.getTemp()*1.8) + 32),1); //doesn't support floats, tempF = ((mySensor.getTemp()*1.8) + 32); delay(500); yield(); Serial.print("F, Humidity: "); //I hate Serial.print(). Serial.print(mySensor.getHumi(),1); humidity = (mySensor.getHumi()); Serial.println("%"); delay(500); Serial.println(); Serial.println("========================"); delay(50); ESP.wdtFeed(); yield(); return; } // ===================================================== //===================================================== void sysloop() { static bool busy=false; static int timeout_busy=0; String tempFstring = ""; String humidstring = ""; String thstring = ""; sprintf (buf, "%f", tempF); tempFstring = String(dtostrf (tempF,4, 1, buf)); sprintf (buf, "%f", humidity); humidstring = String(dtostrf (humidity,4, 1, buf)); thstring = "180," + tempFstring + "," + humidstring; //connect wifi if not connected if (WiFi.status() != WL_CONNECTED) { delay(1); startWIFI(); return; } //return if busy if(busy) { delay(1); if(timeout_busy++ >10000) { printStatus((char *)" Status: Busy timeout-resetting..",-1); ESP.reset(); busy = false; } return; } else { busy = true; timeout_busy=0; } delay(1); //Read 1 sensor every 2.5 seconds if(lc++>2500) { lc=0; printStatus((char *)" ",state); // readSensorIsr(); readHDC1000(); busy = false; return; } // Check if a client has connected WiFiClient client = server.available(); if (!client) { busy = false; return; } // Wait until the client sends some data while((!client.available())&&(timeout_busy++<5000)){ delay(1); if(complete==true) { killclient(client, &busy); return; } } //kill client if timeout if(timeout_busy>=5000) { killclient(client, &busy); return; } complete=false; ESP.wdtFeed(); // Read the first line of the request String req = client.readStringUntil('\r'); client.flush(); if (req.indexOf("/favicon.ico") != -1) { client.stop(); complete=true; busy = false; return; } Serial.print("Recv http: "); Serial.println(req); delay(100); // Match the request int val; if (req.indexOf("/gpio/0") != -1) val = SET_LED_OFF; else if (req.indexOf("/gpio/1") != -1) val = SET_LED_ON; else if (req.indexOf("/?request=GetSensors") != -1) { val = Get_SENSORS; Serial.println("Get Sensor Values Requested"); } else if (req.indexOf("/?request=GetSensor_Non-HTML") != -1) { val = Get_SENSORS; Serial.println(thstring); client.println(thstring); // Send the response to the client } else { Serial.println("invalid request"); client.stop(); complete=true; busy = false; return; } client.flush(); // Prepare Response header String s = "HTTP/1.1 200 OK\r\n"; String v =""; ESP.wdtFeed(); switch (val) { case SET_LED_OFF: case SET_LED_ON: // Set GPIO_16 according to the request digitalWrite(LED_IND , val); // Prepare the response for GPIO state s += "Content-Type: text/html\r\n\r\n"; s += ""; s += "\r\nGPIO is now "; s += (val)?"high":"low"; s += "\n"; // Send the response to the client client.print(s); break; case Get_SENSORS: // Prepare the response for Get_SENSORS s += "Content-Type: text/html\r\n\r\n"; s += ""; s += "\r\n"; s += "tempF = "; s += tempFstring; s += "
"; // start a new line s += "Hum% = "; s += humidstring; s += "
"; // start a new line s += "
"; // start a new line s += "S/N = 180"; client.print(s); // Send the response to the client /* //Create JSON return string s += "Content-Type: application/json\r\n\r\n"; jsonEncode(FIRSTJSON,&s,"tempF ", tempFstring); jsonEncode(NEXTJSON,&s,"Humidity", humidstring); // jsonEncode(FIRSTJSON,&s,"B_Pressure", bprs); jsonEncode(NEXTJSON,&s,"B_Temperature", btmp); jsonEncode(NEXTJSON,&s,"B_Altitude", balt); jsonEncode(NEXTJSON,&s,"DS_TempInside", tin); jsonEncode(NEXTJSON,&s,"DS_TempOutside", tout); jsonEncode(NEXTJSON,&s,"DS_TempAttic", tatt); jsonEncode(NEXTJSON,&s,"DH_Humidity", dhhumi); jsonEncode(NEXTJSON,&s,"DH_Temperature", dhtemp); v = system_get_free_heap_size(); jsonEncode(NEXTJSON,&s,"SYS_Heap", v); v = millis()/1000; jsonEncode(LASTJSON,&s,"SYS_Time", v); // Send the response to the client client.print(s); */ yield(); //ESP.wdtFeed(); break; // default: break; } delay(1); v =""; s=""; val=0; Serial.println("Ending it: Client disconnected"); delay(150); complete=true; busy = false; ESP.wdtFeed(); // The client will actually be disconnected // when the function returns and 'client' object is destroyed } void setup() { Serial.begin(SERBAUD); delay(10); startWIFI(); // Set Indicator LED as output pinMode(LED_IND , OUTPUT); digitalWrite(LED_IND, 1); // LED on NOdeMCU is sunk by pin = OFF Wire.begin(I2C_SDA, I2C_SCL); // Print Free Heap printStatus((char *)" Status: End of Setup",-1); delay(500); } void loop() { sysloop(); }