AskSensors IoT Platform Documentation


Solutions and Projects made with AskSensors


The following tutorials walk you through the steps to build your own weather station and connect it the AskSensors IoT platform. Make sure that you have created your AskSensors account and read this getting started guide.

Here is a list of project examples made with AskSensors. Choose your preferred material:

1-Weather stations using ESP8266 and DHT

2-Weather stations using ESP32 and DHT

3-Low Cost Weather stations using IoT MCU ESP-01S



2-Weather stations using ESP32 and DHT

This section explains how to monitor the temperature and humidity of your room or desk using the DHT11 and the ESP32 connected to the cloud.

AskSensors : ESP32 weather station


a) Prerequisites :

b) Hardware requirements :
  • ESP32 WiFi module
  • DHT11 sensor, The DHT22 is also an alternative.
  • 47K Resistor
  • Wires for connections between the DHT11 and the ESP32
  • USB Micro cable to connect the ESP32 to your computer.
The connections between the DHT and the ESP32 are:
  • DHT VCC to ESP32 5V.
  • DHT Ground to ESP32 Ground.
  • DHT Data to ESP32 IO4 (specified in the code).
  • Connect the Data (IO4) pin and the 5V with a 47K or 10K pull up resistors.

c) DHT Sensor Specifications:
The following application was tested with a DHT11 module. here are the main specifications:
  • Operating Voltage: 3.5V to 5.5V
  • Operating current: 0.3mA (measuring) 60uA (standby)
  • Output: Serial data
  • Temperature Range: 0°C to 50°C
  • Humidity Range: 20% to 90%
  • Resolution: Temperature and Humidity both are 16-bit
  • Accuracy: ±2°C and ±5%

d) Install ESP32 in Arduino IDE :
    Follow the instructions below to install the ESP32 board in your Arduino IDE:
  • Install the latest version of Arduino IDE software (1.8.7 or higher).
  • Open the preferences window from the Arduino IDE : File> Preferences.
  • Go to the "Additional Board Manager URLs" field, Enter the following URL:
    https://dl.espressif.com/dl/package_esp32_index.json
    If you already have the ESP8266 boards URL, separate the URLs with a comma as show below:
    https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json
    Now, open boards manager (Tools > Board > Boards Manager), search for ESP32 and click the install button for the "ESP32 by Espressif Systems".

e) Software :
  • Install the Adafruit DHT Library.
  • Get this example sketch from the AskSensors Github page.
  • Modify the Wi-Fi SSID and password, the Api Key In:
    const char* wifi_ssid = ".........."; // SSID
    const char* wifi_password = ".........."; // WIFI
    const char* apiKeyIn = ".........."; // API KEY IN

f) Run the code :
Now, let's connect your ESP32 weather station to the cloud.
  • Connect the ESP32 to your computer through a USB cable.
  • Open Arduino IDE and upload the code.
  • Open a serial terminal. You should see your ESP32 connecting to internet through WiFi, Then, the ESP32 will periodically reads the temperature and the humidity and send it to AskSensors.

g) Source Code :
A basic source code is shown below. Please refer to the AskSensors Github page to get the latest version and updates.
/*
* DHT Temperature and humidity monitoring using ESP32 and askSensors Iot service
* Description: This examples connects the ESP32 to wifi, and sends Temperature and humidity to askSensors IoT platfom over HTTP GET Request.
* Author: https://asksensors.com, 2018
* github: https://github.com/asksensors
*/
// includes
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
WiFiMulti WiFiMulti;
HTTPClient ask;
// user config: TODO
const char* ssid = "........"; // SSID
const char* password = ".........."; // WIFI
const char* apiKeyIn = ".............."; // API KEY IN
// ASKSENSORS config.
const char* host = "api.asksensors.com"; // host
const int httpPort = 80; // port
// DHT config.
#define DHTPIN 4 // Pin which is connected to the DHT sensor.
// Uncomment the type of sensor in use:
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
int status = WL_IDLE_STATUS;
float myTemperature = 0, myHumidity = 0;
// create ASKSENSORS client
//
void setup() {
// open serial
Serial.begin(115200);
Serial.println("*****************************************************");
Serial.println("********** Program Start : Connect ESP32 and DHT to AskSensors.");
Serial.println("Wait for WiFi... ");
// connecting to the WiFi network
WiFiMulti.addAP(ssid, password);
while (WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
// connected
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Initialize device.
dht.begin();
Serial.println("DHTxx Unified Sensor Example");
// Print temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Temperature");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C");
Serial.println("------------------------------------");
// Print humidity sensor details.
dht.humidity().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Humidity");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%");
Serial.println("------------------------------------");
// Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 1000;
}
void loop() {
Serial.println("*****************************************************");
Serial.println("********** Start ");
// Read data from DHT
// Delay between measurements.
delay(delayMS);
// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
}
else {
// Update temperature and humidity
myTemperature = (float)event.temperature;
Serial.print("Temperature: ");
Serial.print(myTemperature);
Serial.println(" C");
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println("Error reading humidity!");
}
else {
myHumidity = (float)event.relative_humidity;
Serial.print("Humidity: ");
Serial.print(myHumidity);
Serial.println("%");
}
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}else {
// Create a URL for the request
String url = "https://api.asksensors.com/write/";
url += apiKeyIn;
url += "?module1=";
url += myTemperature;
url += "&module2=";
url += myHumidity;
Serial.print("********** requesting URL: ");
Serial.println(url);
ask.begin(url); //Specify the URL
//Check for the returning code
int httpCode = ask.GET();
if (httpCode > 0) {
String payload = ask.getString();
Serial.println(httpCode);
Serial.println(payload);
} else {
Serial.println("Error on HTTP request");
}
ask.end(); //End
Serial.println("********** End ");
Serial.println("*****************************************************");
}
client.stop(); // stop client
delay(20000); // delay
}