AskSensors IoT Platform Documentation


Connect Hardware to AskSensors over MQTT


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



5-Connect Arduino WiFi over HTTPS

In this section we will configure a setup composed from an Arduino and an ESP8266 WiFi module as an IoT Thing.

AskSensors : Arduino + WiFi


a) Prerequisites :

b) Required Hardwares :
  • Computer running Arduino software
  • An Arduino board such as the Arduino Uno.
  • ESP8266 WiFi module. I'm using the ESP-01S
  • USB micro cable to connect ESP32 development board to the computer.
  • Wires and breadboard.

The connection between Arduino and ESP8266 is as follows:
  • ESP TX to Arduino pin 10
  • ESP RX to Arduino pin 11
  • ESP VCC to Arduino 3V3
  • ESP CH_PD to Arduino 3V3
  • ESP GND to Arduino GND

c) Software :
Now let’s write the code to send a simple data from the Arduino to the AskSensors cloud through WiFi. The Arduino code communicates with the ESP8266 WiFi module using AT commands. Data will be sent to AskSensors over HTTP connection. A ready to use code is provided in the AskSensors Github page.
Download the code and set the following variables to your setup (WiFi SSID, password and the 'Api Key In'):
  String ssid = "............."; //Wifi SSID
  String password = "............."; //Wifi Password
  String apiKeyIn = "............."; // API Key
  const unsigned int writeInterval = 25000; // write interval (in ms)

d) Run the code :
  • Connect the Arduino to your computer through USB cable.
  • Open Arduino IDE and flash the code.
  • 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. You should see you Arduino handles AT commands with the ESP8266 which performs the connection to WiFi networks and sending data to the AskSensors cloud over HTTP requests.

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

/*
Connect Arduino WiFi to AskSensors
* Description: This sketch connects the Arduino to AskSensors IoT Platform (https://asksensors.com) using an ESP8266 WiFi.
* Author: https://asksensors.com, 2018-2019
* github: https://github.com/asksensors/AskSensors-Arduino-WiFi
*/
#include <SoftwareSerial.h>
// serial config
#define RX 10
#define TX 11
SoftwareSerial AT(RX,TX);
// TODO: change user config
String ssid = "............."; //Wifi SSID
String password = "............."; //Wifi Password
String apiKeyIn = "............."; // API Key
String host = "api.asksensors.com"; // API host name
String port = "80"; // port
int AT_cmd_time;
boolean AT_cmd_result = false;
void setup() {
Serial.begin(9600);
// open serial
Serial.println("*****************************************************");
Serial.println("********** Program Start : Connect Arduino WiFi to AskSensors");
AT.begin(115200);
Serial.println("Initiate AT commands with ESP8266 ");
sendATcmd("AT",5,"OK");
sendATcmd("AT+CWMODE=1",5,"OK");
Serial.print("Connecting to WiFi:");
Serial.println(ssid);
sendATcmd("AT+CWJAP=\""+ ssid +"\",\""+ password +"\"",20,"OK");
}
void loop() {
// Create the URL for the request
String url = "GET /write/";
url += apiKeyIn;
url += "?module1=";
url += random(10, 100);
Serial.println("*****************************************************");
Serial.println("********** Open TCP connection ");
sendATcmd("AT+CIPMUX=1", 10, "OK");
sendATcmd("AT+CIPSTART=0, \"TCP\",\"" + host +"\"," + port, 20, "OK");
sendATcmd("AT+CIPSEND=0," + String(url.length() + 4), 10, ">");
Serial.print("********** requesting URL: ");
Serial.println(url);
AT.println(url);
delay(2000);
sendATcmd("AT+CIPCLOSE=0", 10, "OK");
Serial.println("********** Close TCP Connection ");
Serial.println("*****************************************************");
delay(20000); // delay
}
// sendATcmd
void sendATcmd(String AT_cmd, int AT_cmd_maxTime, char readReplay[]) {
Serial.print("AT command:");
Serial.println(AT_cmd);
while(AT_cmd_time < (AT_cmd_maxTime)) {
  AT.println(AT_cmd);
  if(AT.find(readReplay)) {
    AT_cmd_result = true;
    break;
  }
  AT_cmd_time++;
}
Serial.print("...Result:");
if(AT_cmd_result == true) {
  Serial.println("DONE");
  AT_cmd_time = 0;
}
if(AT_cmd_result == false) {
  Serial.println("FAILED");
  AT_cmd_time = 0;
}
AT_cmd_result = false;
}