We are now going to see how to log the temperature and humidity measurements in the cloud. We will use the Dweet.io cloud service here, which is very convenient for logging data online:
As the code for this part is very long, we will only see the important parts here. You can of course get all the code from the GitHub repository for this project at https://github.com/openhomeautomation/iot-esp8266.
Again all the measurements are done inside the loop()
function of the sketch, which makes the code repeat every 10 seconds, using a delay()
function.
Inside this loop, we connect to the Dweet.io server with:
WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; }
Then, we read the data from the sensor with:
int h = dht.readHumidity(); int t = dht.readTemperature();
After that, we send data to the Dweet.io server with:
client.print(String("GET /dweet/for/myesp8266?temperature=") +String(t) + "&humidity=" + String(h) + " HTTP/1.1 " +"Host: " + host + " " +"Connection: close ");
You might want to replace myesp8266
here, which is your device name on Dweet.io. Use a complicated name (just like a password) to make sure you are creating a unique device on Dweet.io.
We also print any data received on the serial port with:
while(client.available()){ String line = client.readStringUntil(' '); Serial.print(line); }
You should see that every 10 seconds, the request is sent to the Dweet.io server
, and you get the answer back:
HTTP/1.1 200 OK Access-Control-Allow-Origin: * Content-Type: application/json Content-Length: 147 Date: Mon, 16 Mar 2015 10:03:37 GMT Connection: keep-alive {"this":"succeeded","by":"dweeting","the":"dweet","with":{"thing":"myesp8266","created":"2015-03-16T10:03:37.053Z","content":{"temperature":24, "humidity":39} } }
If you can see the succeeded
message, congratulations, you just logged data in the cloud with your ESP8266 chip!
18.217.147.193