ÿþ// ARDUINO IDE 1.6.7,,,scratch mDNS_Web_Server //for ESP8266,,,,,If arduino - you change code for WIFI and ... //functions: // temperature on thingspeak // find out public ip // extract ip or mac address on parts // sending external IP address on Thingspeak to four fields // sending test msg on THINGSPEAK Status // read from thingspeak-- no complet=example // read one field // deep sleeping for ESP8226 - if you enable //source: //https://stackoverflow.com/questions/35227449/convert-ip-or-mac-address-from-string-to-byte-array-arduino-or-c //https://www.ipify.org //https://github.com/mathworks/thingspeak-arduino //https://uk.mathworks.com/help/thingspeak/get-a-channel-feed.html // //setting for ARDUINO ide // boards http://arduino.esp8266.com/package_esp8266com_index.json // board: GENERIC ESP8266 MODULE or ADAFRUIT HUZZAH ESP8266 or for NODEMCU // settings QIO 40MHz SERIAL 80MHz 4M ck 115200 com? AVRSIPmkll //sleeping for ESP8266 E12 : it is on end of LOOP //ESP.deepSleep(100e6); // 20e6 is 20 seconds,connect pin GPIO16=D0 on RST /* ESP8266 mDNS responder sample This is an example of an HTTP server that is accessible via http://esp8266.local URL thanks to mDNS responder. Instructions: - Update WiFi SSID and password as necessary. - Flash the sketch to the ESP8266 board - Install host software: - For Linux, install Avahi (http://avahi.org/). - For Windows, install Bonjour (http://www.apple.com/support/bonjour/). - For Mac OSX and iOS support is built in through Bonjour already. - Point your browser to http://esp8266.local, you should see a response. */ #include <ESP8266WebServer.h> #include <ESP8266WiFi.h> #include <ESP8266mDNS.h> #include <WiFiClient.h> #include <OneWire.h> #include <DallasTemperature.h> //define Data Types byte bytes[4]; String ddd = "111.111.111.111"; //after power on=reset only String line = ""; // replace with your channel s thingspeak API key, String apiKey = "X274FW5NIT3BAHW4"; // replace with your NAME=SSID and PASSWORD for router,,with " " const char* ssid = "virginmedia3878031"; const char* password = "hbmzxlur"; //server THINGSPEAK const char* server = "api.thingspeak.com"; // DS18B20 plug to pin GPIO12 ESP8266-12 = D6(MISO) #define ONE_WIRE_BUS 2 // define resolution for sensors 9bit/0.5 10bit/0.25 11bit/0.125 12bit/0.0625 #define TEMPERATURE_PRECISION 11 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); // arrays to hold device addresses DeviceAddress insideThermometer, outsideThermometer; float teplota; String termOUT; String termIN; WiFiClient client; void setup() { Serial.begin(115200); delay(10); WiFi.begin(ssid, password); Serial.println(""); Serial.println(); 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.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // Start up the library sensors.begin(); // locate devices on the bus Serial.print("Found "); Serial.print(sensors.getDeviceCount(), DEC); Serial.println(" devices."); // report parasite power requirements Serial.print("Parasite power is: "); if (sensors.isParasitePowerMode()) Serial.println("ON"); else Serial.println("OFF"); // search for devices on the bus and assign based on an index. if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1"); // set the resolution to 9 bit sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION); sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION); Serial.print("Device 0 Resolution: "); Serial.print(sensors.getResolution(insideThermometer), DEC); Serial.println(); Serial.print("Device 1 Resolution: "); Serial.print(sensors.getResolution(outsideThermometer), DEC); Serial.println(); } // function to print a device address void printAddress(DeviceAddress deviceAddress) { for (uint8_t i = 0; i < 8; i++) { if (deviceAddress[i] < 16) Serial.print("0"); Serial.print(deviceAddress[i], HEX); } } // function to print the temperature for a device void printTemperature(DeviceAddress deviceAddress) { float tempC = sensors.getTempC(deviceAddress); Serial.print("Temp C: "); Serial.print(tempC); Serial.print(" Temp F: "); Serial.print(DallasTemperature::toFahrenheit(tempC)); } void printResolution(DeviceAddress deviceAddress) { Serial.print("Resolution: "); Serial.print(sensors.getResolution(deviceAddress)); Serial.println(); } // main function to print information about a device void printData(DeviceAddress deviceAddress) { Serial.print("Device Address: "); printAddress(deviceAddress); Serial.print(" "); printTemperature(deviceAddress); Serial.print(" "); printResolution(deviceAddress); Serial.println(); } void loop(void) { // call sensors.requestTemperatures() to issue a global temperature // request to all devices on the bus Serial.print("Requesting temperatures..."); sensors.requestTemperatures(); Serial.println("DONE"); // print the device information Serial.print("Teplota vnutri: "); printData(insideThermometer); Serial.print("Teplota vonku: "); printData(outsideThermometer); float tint = sensors.getTempC(insideThermometer); float tout = sensors.getTempC(outsideThermometer); // thingspeak needs minimum 15 sec delay between updates //change delay for next update delay(20000); // find out public ip // it finds out ext.IP address with html for arduino,,It should only send an address without html text,,,read more on ipify.org //you can use other IP check servers to find out external IP address if (client.connect("api.ipify.org", 80)) { //Serial.println("connected"); client.println("GET /?format=txt HTTP/1.0"); client.println("Host: api.ipify.org"); client.println(); } else { Serial.println("connection failed"); } delay(50); int b = 1; while (client.connected()) { b = b + 1; line = client.readStringUntil('\n'); // part for IP address Serial.println(line); } // find out public ip--end // String to char for IP //how it works---see sketch "extract ip or mac address on parts.txt" char ipstr[20]; line.toCharArray(ipstr, 20); const char* str = ipstr; char sep = '.'; int maxBytes = 4; int base = 10; for (int i = 0; i < maxBytes; i++) { bytes[i] = strtoul(str, NULL, base); // Convert byte Serial.println(bytes[i]); str = strchr(str, sep); // Find next separator if (str == NULL || *str == '\0') { break; // No more separators, exit } str++; // Point to next character after separator } // String to char for IP--end //preparing string - IP to String for Thingspeak String abc = "&field5="; abc += String(bytes[0]); abc +="&field6="; abc += String(bytes[1]); abc +="&field7="; abc += String(bytes[2]); abc +="&field8="; abc += String(bytes[3]); // if it does not find out IP,it will not send ZERO //CHANGE code if you want to send new IP address at a change only without minute update if (bytes[0] > 0 ) { ddd = abc; } client.stop(); // find out public ip---end // data to thingspeak if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com String postStr = apiKey; postStr +="&field1="; postStr += String(tint); postStr +="&field2="; postStr += String(tout); //add IP postStr += ddd; //you can write msg on THINGSPEAK, max 255 characters,,enable status on THINGSPEAK postStr += "&status="; postStr += "I love you"; postStr += "\r\n\r\n"; client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(postStr.length()); client.print("\n\n"); client.print(postStr); } Serial.println("thing text"); client.stop(); // data to thingspeak - end // read from thingspeak Serial.println("read TS& "); if (client.connect(server,80)){ client.println("GET /channels/179963/fields/8/last") ; client.println("Host: api.thingspeak.com"); client.println(); } else { Serial.println("connection failed"); } delay(50); int a = 1; while (client.connected()) { a = a + 1; String thinga = client.readStringUntil('\n'); Serial.println(thinga); } client.stop(); Serial.println("stop connection& "); // read from thingspeak - end //sleeping enable/disable // 20e6 is 20 seconds,connect pin GPIO16=D0 on RST //ESP.deepSleep(100e6); }