2- Connect ESP8266 over HTTPS

The ESP8266 chip from Espressif was the new generation of low-cost WiFi chips after the TI CC3000/CC3200. This small chip not only integrates the whole WiFi features, but also a powerful programmable MCU.

Depending on the board layout (ESP-01, ESP-03, ESP-07, ESP12, etc) it is attached to a programmable flash, ranging from 512K to 4M. This increases the available user code space, and make possible other cool features like a small file system, or OTA updates.

This devices can be directly programmed from the Arduino IDE. The only requirement is to install the board via the Arduino Boards Manager.

Connect ESP8266 To AskSensors Over HTTPS

a) Prerequisites :

b) Required Hardwares :

  • Computer running Arduino software (version 1.8.7 or higher).
  • ESP8266 board.
  • USB micro cable to connect the ESP8266 board to the computer.

c) Install ESP8266 in Arduino IDE :

  • Install the latest version of Arduino IDE software (1.8.7 or higher) from the official Arduino download page
  • Open the preferences window from the Arduino IDE : File> Preferences.
  • Go to the “Additional Board Manager URLs” field, Enter the following URL:
    http://arduino.esp8266.com/stable/package_esp8266com_index.json
  • Open boards manager (Tools > Board > Boards Manager), search for ESP8266 and click the install button for the “ESP8266 by Espressif Systems”. It takes some few seconds.

d) Software :

Example sketch and ESP8266 WIFI libraries are available in Github.
The provided code is ready to use as is. It connects the ESP8266 to wireless network as HTTP client, and then push data to AskSensors.

Download the code from github, decompress the zip folder and open the .ino file in Arduino IDE. You need to fill the following:

  • Your WIFI SSID and password.
  • The Api Key In generated before by AskSensors.
  • If needed, the time period between two successive data updates (set to 25 second in this example).
  • 
      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 :

Now it’s time to connect your board.

  • Connect your ESP8266 board to the computer via serial/USB and upload the code using the Arduino IDE.
  • Return back to your sensor page on AskSensors, Click on ‘Show Graph’ and select the chart type (such as Line, Bar, Binary..) to visualize your sensor data stream in graph.
  • Open a serial terminal. You can cross-check the AskSensors graph readings with the values being printed on your Arduino 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.


/*
* AskSensors HTTP Request
* Description: Connect ESP8266 to AskSensors over HTTP
* Author: https://asksensors.com, 2018
* github: https://github.com/asksensors/AskSensors-ESP8266-API
*/ #include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
// TODO: ESP8266 user config.
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)
// ASKSENSORS API host name
String host = "http://api.asksensors.com";
ESP8266WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
Serial.println("*****************************************************");
Serial.println("********** Program Start : Connect ESP8266 to AskSensors over HTTP");
Serial.println("Wait for WiFi... ");
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() {
// wait for WiFi connection
if (WiFi.status() == WL_CONNECTED){
  HTTPClient http;
  Serial.print("[HTTP] begin...\n");
  // Create a URL for updating module 1 and module 2
  String url = "";
  url += host;
  url += "/write/";
  url += apiKeyIn;
  url += "?module1=";
  url += random(10,100);
  url += "&module2=";
  url += random(10,100);
  Serial.print("********** requesting URL: ");
  Serial.println(url);
  http.begin(url); //HTTP
  Serial.println("> Request sent to ASKSENSORS");
  Serial.print("[HTTP] GET...\n");
  // start connection and send HTTP header
  int httpCode = http.GET();
  // httpCode will be negative on error
  if(httpCode > 0) {
    // HTTP header has been send and Server response header has been handled
    Serial.printf("[HTTP] GET... code: %d\n", httpCode);
    // file found at server
    if(httpCode == HTTP_CODE_OK) {
      String payload = http.getString();
      Serial.println(payload);
    }
  } else {
    Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  }
  http.end();
  Serial.println("********** End ");
  Serial.println("*****************************************************");
}
delay(writeInterval); // wait for writeInterval
}
Was this article helpful to you? Yes No

How can we help?