Using the ESP8266, you can log sensor data to online servers for monitoring and control purposes. In this recipe, we will be looking at how to do that. We will use an ESP8266 board to post and store sensor data on dweet.io
.
To do this tutorial, you will need an ESP8266 board and a USB cable among several other things, which include:
The hardware setup will resemble the one in the previous recipe.
In addition to setting up the hardware, you will set up the online platform where the sensor data will be posted. Luckily the platform we are using, dweet.io
, is simple to use and requires no setup or signup. All you need to do is choose a name for your thing, which in this case is your ESP8266, then you can start publishing data to dweet.io
.
Posting data to dweet.io
is simple. It is done by calling a URL such as https://dweet.io/dweet/for/my-thing-name?hello=world
. You are required to change my-thing-name
to the name of your thing, hello
to the name of the parameter that you are posting, and world
to the value of the parameter that you are posting online.
In our case, we will call our thing garden-monitor-11447
, and the names of the parameters we will be posting are humidity
, temperature
, and moisture
. Our URL will look like this: https://dweet.io/dweet/for/garden-monitor-11447?humidity=<humidity value>&temperature=<temperature value>&moisture=<moisture value>
.
Once you have successfully generated the URL:
humidity
, temperature
, and soil moisture
values from the sensors, as done in the previous recipe.<humidity value>,
<temperature value>
, and <moisture value>
in the preceding URL with the readings obtained from the sensors. The URL will be used to send a http
request to the dweet.io
server.http
request, the sensor data will be published on the dweet.io
platform. To view the data, visit this URL from any web browser: https://dweet.io/follow/garden-monitor-11447:// Libraries #include <ESP8266WiFi.h> #include "DHT.h"
ssid
and password
is set in this section:// Wi-Fi network SSID and password const char* ssid = "your-ssid"; const char* password = "your-password";
host
name of the cloud server:// Host const char* host = "dweet.io";
DHT11
signal pin and the type of DHT
sensor that we are using:#define DHTPIN 2 // what digital pin DHT11 is connected to #define DHTTYPE DHT11 // DHT 11 sensor
dht
object:DHT dht(DHTPIN, DHTTYPE);
moistureReading
:int moistureReading = 0; // holds value soil moisture sensor reading
DHT
object. Configure the ssid
and password
of the Wi-Fi network and connect the ESP8266 to it:void setup() { Serial.begin(115200); // initialize serial interface dht.begin(); // initialize DHT11 sensor delay(10); // We start by connecting to a WiFi network 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("IP address: "); Serial.println(WiFi.localIP()); }
void loop() { delay(5000); Serial.print("connecting to "); Serial.println(host);
host
server. Retry if not successful:// Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; }
DHT11
sensor and soil moisture
sensor:// Read sensor inputs // get humidity reading float h = dht.readHumidity(); // get temperature reading in Celsius float t = dht.readTemperature(); // Check if any reads failed and exit early (to try again). while (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); delay(2000); // delay before next measurements //get the measurements once more h = dht.readHumidity(); t = dht.readTemperature(); } //get soil moisture reading moistureReading = analogRead(A0);
GET
request we will send to the host server. The URL will include the sensor readings:// We now create a URI for the request String url = "/dweet/for/garden-monitor-11447?humidity="; url += String(h); url += "&temperature="; url += String(t); url += "&moisture="; url += String(moistureReading);
GET
request to the server and check whether the request has been received, or if it has been timed out:// Send request Serial.print("Requesting URL: "); Serial.println(url); client.print(String("GET ") + url + " HTTP/1.1 " + "Host: " + host + " " + "Connection: close "); unsigned long timeout = millis(); while (client.available() == 0) { if (millis() - timeout > 5000) { Serial.println(">>> Client Timeout !"); client.stop(); return; } }
host
server line by line and display the data on the serial monitor. Close the connection after all the data has been received from the server:// Read all the lines from the answer while(client.available()){ String line = client.readStringUntil(' '); Serial.print(line); } // Close connecting Serial.println(); Serial.println("closing connection"); }
ssid
in the code from your-ssid
to the name of your Wi-Fi network, and the password
from your-password
to the password of your Wi-Fi network.The program connects to the Wi-Fi network using the provided password
and ssid
. It then proceeds to connect to the provided cloud/host server using the client.connect()
function. Once the ESP8266 connects successfully, data from the sensors is read and a URL is generated that includes the updated sensor data. The URL is then sent to the host server using the client.print()
function.
Once the data has been successfully sent, the sketch waits for a reply from the server. It does this with the client.available()
function, which checks whether there is incoming data from the server. If there is data available, the sketch reads it and displays it on the serial monitor. The ESP8266 posts sensor data to dweet.io
every five seconds.
18.225.10.116