3- Control ESP32 Actuators over MQTT

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 send commands to your ESP32 device with AskSensors over MQTT.

AskSensors : Control ESP32 MQTT

a) Prerequisites :

b) Required Material :

The hardwares needed in this section are:

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

c) Install ESP32 in Arduino IDE:

If not done yet, 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) Required Software :

The provided code includes the libraries for both connecting to a WiFi network and to read actuator commands from AskSensors over MQTT.
You need to to modify the following variables:

    • Your WIFI SSID and password.
    • The Actuator Api Key Out and username to set the MQTT Topic:actuator/username/apiKeyOut

const char* ssid = "....."; // Wifi SSID
const char* password = ".....";// Wifi Password
const char* username = "................."; // my AskSensors username
const char* pubTopic = "actuator/..../....."; // actuator/username/apiKeyOut

e) Testing the code :

  • Connect your ESP32 board to the computer via serial/USB and upload the code using the Arduino IDE. The code will be running automatically after Reset.
  • Return to the AskSensors web appplication, open your actuator devices workspace and send commands to the actuator module that your ESP32 reads data from.
  • Open a serial terminal in Arduino IDE. You can cross-check the ESP32 terminal reading with the values being sent from the AskSensors Application.

f) Source Code :

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


/*
* Description: Read actuator command from AskSensors over MQTT
* Author: https://asksensors.com, 2020
* github: https://github.com/asksensors
*/
#include <WiFi.h>
#include <PubSubClient.h>

//TODO: ESP32 MQTT user config
const char* ssid = ".............."; // Wifi SSID
const char* password = ".............."; // Wifi Password
const char* username = ".............."; // my AskSensors username
const char* subTopic = "actuator/............../.............."; // actuator/username/apiKeyOut
//AskSensors MQTT 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 : Read actuator command from 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);
if (!client.connected())
reconnect();
Serial.println("********** Subscribe to AskSensors MQTT topic");
// susbscribe
client.subscribe(subTopic);
}

void loop() {
client.loop();
}

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Command received from AskSensors[");
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("ESP32Client", 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);
}
}
}

Was this article helpful to you? Yes 1 No

How can we help?