In this guide, we are going to publish data to the AskSensors IoT platform using an ESP8266 board and MQTT protocol.
a) Prerequisites :
- Create an AskSensors account.
- Setup a new sensor.
- Get started using this step-by-step user guide.
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 :
- Install the MQTT PubSubClient Library for the Arduino IDE.
- 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 to AskSensors using MQTT.
You need to fill the following:
- Your WIFI SSID and password.
- Your Asksensors username and Api Key In to set the MQTT Topic:publish/username/apiKeyIn
const char* ssid = "....."; // Wifi SSID
const char* password = ".....";// Wifi Password
const char* username = "................."; // my AskSensors username
const char* pubTopic = "publish/..../....."; // publish/username/apiKeyIn
const char* mqtt_server = "mqtt.asksensors.com";
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.
/*
* MQTT and AskSensors IoT Platform
* Description: ESP8266 sends data to AskSensors using MQTT
* Author: https://asksensors.com, 2020
* github: https://github.com/asksensors/AskSensors-ESP8266-MQTT
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// TODO: AskSensors MQTT user config
const char* ssid = ".................."; // Wifi SSID
const char* password = ".................."; // Wifi Password
const char* username = "................."; // my AskSensors username
const char* pubTopic = "publish/..../....."; // publish/username/apiKeyIn
const unsigned int writeInterval = 25000; // write interval (in ms)
// MQTT host config
const char* mqtt_server = "mqtt.asksensors.com";
unsigned int mqtt_port = 1883;
WiFiClient askClient;
PubSubClient client(askClient);
void setup() {
Serial.begin(115200);
Serial.println("*****************************************************");
Serial.println("********** Program Start : ESP8266 publishes data to AskSensors over MQTT");
Serial.print("********** connecting to WIFI : ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("->WiFi connected");
Serial.println("->IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}
void loop() {
if (!client.connected())
reconnect();
client.loop();
Serial.println("********** Publish MQTT data to ASKSENSORS");
char mqtt_payload[30] = "";
snprintf (mqtt_payload, 100, "m1=%ld&m2=%ld", random(10,100), random(10,100));
Serial.print("Publish message: ");
Serial.println(mqtt_payload);
client.publish(pubTopic, mqtt_payload);
Serial.println("> MQTT data published");
Serial.println("********** End ");
Serial.println("*****************************************************");
delay(writeInterval);// delay
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("********** Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client",username, "")) {
Serial.println("-> MQTT client connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println("-> try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}