AskSensors IoT Platform Documentation


Connect Hardware to AskSensors over HTTP/HTTPS


AskSensors HTTPS Hardware Summary:

1-HTTP/HTTPS Write/Read API

2-Connect ESP8266 over HTTP/HTTPS

3-Connect ESP32 over HTTP/HTTPS

4-Connect Arduino Ethernet over HTTP/HTTPS

5-Connect Arduino WiFi over HTTP/HTTPS

6-Connect Raspberry Pi over HTTP/HTTPS

7-Connect Regular PC Node.js over HTTP/HTTPS

8-Publish ESP32 data with timestamp over HTTP/HTTPS



8-Publish ESP32 data with timestamp over HTTP/HTTPS

This section goes through the main steps you need to timestamp your ESP32 data using NTP servers(Network Time Protocol), and send the timestamped data to the AskSensors IoT cloud over HTTP/HTTPS.
The HTTP/HTTPS write API with timestamp is described here.


a) Prerequisites :
  • Active AskSensors account: Subscribe to a 15 days free trial(SIMPLE, PRO or GURU).
  • Create a new sensor. For additional details on how to setup a sensor, simply click here and follow the simple process.
  • Follow these steps to get familiar with AskSensors.
  • Connect ESP32 to the AskSensors Cloud as shown in the this section.

b) Required Material :
  • Computer running Arduino software (version 1.8.7 or higher).
  • ESP32 board.
  • USB micro cable to connect the ESP32 board to the computer.

c) Install ESP32 Library in Arduino IDE:
If you haven’t already installed the ESP32 board in your Arduino IDE, follow these next instructions::
  • 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
    search for ESP32 word in the Arduino IDE Boards Manager (Tools > Board > Boards Manager). Click the install button for the "ESP32 by Espressif Systems".

d) Required Software :
  • Install the NTP Client library for the Arduino IDE: Navigate to the Sketch > Include Library > Manage Libraries.
    Wait for Library Manager to download libraries index and updated list of installed libraries.
    Filter your search by typing ‘ntpclient’. There should be a couple entries. Look for NTPClient by Fabrice Weinberg. Click on that entry, and then select Install.
  • Download this demo from the AskSensors Github page.
The provided code includes the libraries for both connecting to a WiFi network and to publish data with NTP timestamp to AskSensors over HTTP.
You need to to modify the following variables:

  • Your WIFI SSID and password.
  • The Api Key In given by AskSensors.
  • The time interval between two successive updates.
  •   const char* wifi_ssid = ".........."; // SSID
      const char* wifi_password = ".........."; // WIFI
      const char* apiKeyIn = ".........."; // API KEY IN
      const unsigned int writeInterval = 25000; // write interval (in ms)

e) Run the code in your ESP32
  • Connect your ESP32 board to the computer via serial/USB and upload the code using the Arduino IDE.
  • Check your Sensor data stream on your AskSensors workspace.
  • Open a serial terminal. You can cross-check the AskSensors graph readings with the values being printed on your ESP32 Terminal.

f) Source Code :
A basic source code is shown below. Please refer to the AskSensors Github page to get the latest version and updates.

/*
Connect ESP32 to AskSensors
* Description: This sketch publishing data with timestamp to IOT cloud (https://asksensors.com) using an ESP32 Wifi module.
* Author: https://asksensors.com, 2019
* github: https://github.com/asksensors
*/
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

WiFiMulti WiFiMulti;
HTTPClient ask;
WiFiUDP ntpUDP; // UDP client
NTPClient timeClient(ntpUDP); // NTP client

// TODO: user config
const char* ssid = "............."; //Wifi SSID
const char* password = "............."; //Wifi Password
const char* apiKeyIn = "..................."; // API KEY IN
const unsigned int writeInterval = 25000; // write interval (in ms)
// ASKSENSORS API host config
const char* host = "api.asksensors.com"; // API host name
const int httpPort = 80; // port

void setup(){

  // open serial
  Serial.begin(115200);
  Serial.println("*****************************************************");
  Serial.println("********** Program Start : Connect ESP32 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());

  timeClient.begin(); // init NTP
  timeClient.setTimeOffset(0); // 0= GMT, 3600 = GMT+1
}

void loop(){

   // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, httpPort)) {
     Serial.println("connection failed");
    return;
  }else {
    Serial.println("********** Send data with timestamp to ASKSENSORS over HTTP");
    // timestamp
    while(!timeClient.update()) {
    timeClient.forceUpdate();
  }
  // get Epoch time
  Serial.print("> NTP Time:");
  Serial.println(timeClient.getFormattedTime());
  long unsigned int timestamp = timeClient.getEpochTime();

   // Create a URL for updating module1 and module 2
  String url = "http://api.asksensors.com/write/";
  url += apiKeyIn;
  url += "?module1=";
  url += random(10, 100);
  url += "&module2=";
  url += random(10, 100);
  url += "&t=";
  url += timestamp;

  Serial.print("********** requesting URL: ");
  Serial.println(url);
  // send data
  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(writeInterval); // delay
}


Previous : Node.js HTTPS