3- Connect ESP32 over HTTPS

After the big success of the ESP8266, the ESP32 is a new chip from Espressif that combines WiFi and Bluetooth wireless capabilities with two CPU cores and a decent hardware peripheral set.

This section shows how to connect your ESP32 to the AskSensors cloud

Connect ESP32 To AskSensors Over HTTPS

a) Prerequisites :

b) Required Hardwares :
To follow along with this tutorial you will simply need:

  • Computer running Arduino software.
  • ESP32 development board.
  • USB micro cable to connect ESP32 development board to the computer.

c) 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”.

d) Software :
Download this demo from the AskSensors Github page.
The code includes the libraries for both connecting to a WiFi network and to perform the HTTP requests. You need to fill the following:

    • 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 :
Here are the main steps to run your code:

  • Connect your ESP32 to the computer and upload the code from Arduino IDE.
  • Return back to your sensor page on askSensors, click on ‘visualize’ and ‘Show Graph’ to view your sensor data in graph.
  • Open a serial terminal on Arduino IDE. You can cross-check the AskSensors graph readings with the values being printed on your Arduino IDE 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 connects to a website (https://asksensors.com) using an ESP32 Wifi module.
* Author: https://asksensors.com, 2018
* github: https://github.com/asksensors
*/
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
WiFiMulti WiFiMulti;
HTTPClient ask;
// TODO: ESP32 user config
const char* ssid = "............."; //Wifi SSID
const char* password = "............."; //Wifi Password
String apiKeyIn = "............."; // API Key
const unsigned int writeInterval = 25000; // write interval (in ms)
// ASKSENSORS host config
const char* host = "http://api.asksensors.com";
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());
}
void loop(){
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}else {
// Create a URL for updating module 1 and module2 request
String url = "http://api.asksensors.com/write/";
url += apiKeyIn;
url += "?module1=";
url += random(10, 100);
url += "&module2=";
url += random(10, 100);
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(writeInterval); // delay
}
Was this article helpful to you? Yes No 1

How can we help?