Getting started with askSensors using ESP8266


This tutorial will cover the basic steps that you need to follow to connect an ESP8266 to askSensors IoT Platform.

STEP 1: MATERIALS.

The hardware used in this tutorial is an ESP8266 ESP-12E NodeMCU. A tutorial showing how to Program the ESP8266 ESP-12E NodeMCU using Arduino IDE is shown here. Feel free to test with any other ESP8266 module listed here.

Image result for esp node mcu

STEP 2: CREATE ASKSENSORS ACCOUNT.

You need to sign up for an account at askSensors, set up a sensor with a unique API Key, so you will be able to send data to and view it in graphs.

This tutorial demonstrates how to create an account, set a sensor and collect and visualize data in graphs.

Next, We will focus mainly on how to make the ESP-12 behaves as a WiFi client by connecting to your wireless network, connect and send dummy data to askSensors, which you can replace with your real sensor data.

STEP 3: SOFTWARE

We suggest the ESP8266WiFi library, which is part of ESP8266 core for Arduino and could be used to directly program the ESP8266 via the Arduino IDE. The library is available for download in github.

All you need is to modify the following:

const char* ssid = "........";
const char* password = "........";
const char* host = "api.github.com";
const int httpsPort = 443;
// SHA1 fingerprint of the certificate
const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C";

by

const char* ssid = "........";
const char* password = "........";
const char* host = "asksensors.com";
const int httpsPort = 443;
// SHA1 fingerprint of the certificat
const char* fingerprint = " B5 C3 1B 2C 0D 5D 9B E5 D6 7C B6 EF 50 3A AD 3F 9F 1E 44 75 ";


Then, set the URL in this form:

"/api.askSensors/write/ApiKeyIn?module1=value1";

·         ApiKeyIn :is the API KEY IN given by askSensors

·         value1 is the value of sensor modules values. you can add more at the end of the URL (&module2=value2&module3=value3&module4=value4)

As follows a working sketch that you can use:


/*
* askSensors HTTPS Request
* Description: This examples connects the ESP to wifi, and sends dummy data to askSensors IoT platfom over HTTPS GET Request.
* Author: https://asksensors.com, 2018
* original created by Ivan Grokhotkov, 2015.
*/
// includes
#include
#include
// Wifi config
const char* wifi_ssid = ".........."; // TODO: SSID
const char* wifi_password = ".........."; // TODO: WIFI
// ASKSENSORS config.
const char* https_host = "asksensors.com"; // ASKSENSORS host name
const int https_port = 443; // https port
const char* https_fingerprint = "B5 C3 1B 2C 0D 5D 9B E5 D6 7C B6 EF 50 3A AD 3F 9F 1E 44 75"; // ASKSENSORS HTTPS SHA1 certificate
const char* apiKeyIn = "..........................."; //TOD: Insert your API KEY IN from asksensors.com
// create ASKSENSORS client
WiFiClientSecure client;
int module1_value = 0;
//
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("********** connecting to WIFI : ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_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() {
// Use WiFiClientSecure class to create TLS connection
Serial.print("********** connecting to HOST : ");
Serial.println(https_host);
if (!client.connect(https_host, https_port)) {
Serial.println("-> connection failed");
return;
}

if (client.verify(https_fingerprint, https_host)) {
Serial.println("-> certificate matches");
} else {
Serial.println("-> certificate doesn't match");
}
// Create a URL for the request
String url = "/askSensorApi/write/";
url += apiKeyIn;
url += "?module1=";
url += module1_value++;

Serial.print("********** requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + https_host + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println("> Request sent to ASKSENSORS");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
String line = client.readStringUntil('\n');
Serial.println("********** ASKSENSORS replay:");
Serial.println(line);
Serial.println("********** closing connection");

break;
}
}
delay(25000); // delay 25sec
}

STEP 3: PROGRAM YOUR HARDWARE

Now you have the software successfully built and the NodeMCU ready to be programmed. Once the firmware load is complete, open the arduino IDE serial terminal. You should see the ESP connecting to asksensors, and sending data every 25 seconds over HTTPS GET requests. The asksensors server acknowledge the reception by returning the number of modules successfully updated (one module in this example). refer to picture below.



STEP 4: VISUALIZE YOUR SENSOR DATA!

To see the received data in graphs, click on "Visualize" on the appropriate sensor row.



Therefore, you will be able to visualize your sensor data in graphs as following:


STEP 5: DONE!

Congratulations! You have successfully connected your ESP to askSensors and posted sensor data using the ESP8266 and GET requests.