Controlling LED’s from Mendix App using ESP32 Microcontroller 

The Internet of Things (IoT) is revolutionizing how we interact with our environment. By connecting physical devices to the digital world, IoT enables seamless control and automation. In this blog, we’ll dive into a practical application of IoT by demonstrating how to effortlessly control LED lights using a Mendix app and the versatile ESP32 microcontroller. Discover how to build a smart lighting solution that combines the power of low-code development with the flexibility of IoT hardware.

Steps to Achieve: 

There are 15 steps to subscribe your Mendix app to the IBM Watson IoT Platform and configure the ESP32 to enable communication. Let’s get started. 

Steps 1: Download the MQTT client from the Mendix marketplace: Link to download

Step 2: To create IBM Watson IoT Platform instances, please refer to my previous blog: Link to blog

Step 3: You need to subscribe to the Device via IBM Watson, so create a microflow and add the MQTT subscribe activity. Connect this microflow to a button from the home page. 

Configure the MQTT Subscribe activity as follows: 

  • Broker Host: ‘messaging.internetofthings.ibmcloud.com’ 
  • Broker port: 1883 
  • Broker Organization: You’re Organization ID 
  • Time Out: 6000 
  • Username: a-xxxxx-xxxxx 
  • Password: xxxxxxxxxxxx 
  • Topic Name: ‘iot-2/type/” DeviceType”/id/”DeviceID”/evt/status1/fmt/json’ 
  • Qos : At_Least_Once_1 
Step 4: Create on message microflow: Create a log message and display. 
Step 5: Create a domain model with an entity called Light and add the attributes. 
Step 6: Create a Light Object with the respective light name and call a page from navigation to determine which light we want to control. Call a page from navigation -> Control_Light -> Add data view on that page with a DS microflow to retrieve specific area light and return the light object. 
Step 7: Add two Switch widgets and configure the on change microflow to control the respective light..
Create OCH_PublishData_Red microflow and call this microflow from one switch and publish the payload value using MQTT Publish activity.

Create OCH_PublishData_White microflow and call this microflow from one switch and publish the payload value using MQTT Publish activity.

Step 8: To configure the ESP32, use Arduino Software. To install Arduino, you can refer to this link.

Step 9: Once your software is ready, now we can start programming ESP32. First, Include libraries Wifi and PubSubClient. As we are using the MQTT protocol, we need PubSubClient to subscribe and publish data.

#include <WiFi.h>

#include <WiFiClient.h>

#include <PubSubClient.h>

Step 10: Define all the credential details which we have copied from the IBM Watson IOT platform.

#define ORG “XXXXXX”

#define DEVICE_TYPE “Light”

#define DEVICE_ID “XXXXX”

#define TOKEN “XXXXXXXXX”

Step 11: Add Wifi credentials as a char variable as SSID and Password.

const char* ssid = “XXXXXX”;

const char* password = “XXXXXX”;

Step 12: Add the details of the server, topics, auth method, and client ID to publish data.

char server[] = ORG “.messaging.internetofthings.ibmcloud.com”;

char subtopic[] = “iot-2/cmd/status1/fmt/String”;

char authMethod[] = “use-token-auth”;

char token[] = TOKEN;

char clientId[] = “d:” ORG “:” DEVICE_TYPE “:” DEVICE_ID;

Step 13: In the ‘void setup’ method, initialize the .begin() function to establish the connection with Wifi and the IBM Watson IoT platform.

void setup() {

  // put your setup code here, to run once:

Serial.begin (115200);

pinMode(5, OUTPUT);

pinMode(18, OUTPUT);

//pinMode(19, OUTPUT);

 wifiConnect(); //function call to connect to wifi

 mqttConnect(); //function call to connect to MQTT

}

//function to connect to the wifi

void wifiConnect() {

  Serial.print(“Connecting to “);

  Serial.print(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(“.”);

  }

  Serial.print(“WiFi connected, IP address: “);

  Serial.println(WiFi.localIP());

  Serial.println(WiFi.SSID());

  Serial.println();

}

//connecting to MQTT CLIENT

void mqttConnect() {

  if (!client.connected()) {

    Serial.print(“Reconnecting MQTT client to “);

    Serial.println(server);

    while (!client.connect(clientId, authMethod, token)) {

      Serial.print(“.”);

      delay(500);

    }

    initManagedDevice();

    Serial.println();

  }

}

Step 14: In the ‘void loop’ function, receive the data from IBM Watson as a payload and process the logic to turn on and off the lights.

WiFiClient wifiClient;

PubSubClient client(server, 1883, callback, wifiClient);

void loop() {

  // put your main code here, to run repeatedly:

client.loop();

mqttConnect();

delay(500);

}

//function to subscribe

void callback(char* subtopic, byte* payload, unsigned int payloadLength) {

  Serial.print(“callback invoked for topic: “);

  Serial.println(subtopic);

  for (int i = 0; i < payloadLength; i++) {

    command += (char)payload[i];

  }

Serial.println(command);

if(command == “red”){

  re=digitalRead(18);

  if(re == 1){

    digitalWrite(18,LOW);

    }

    else{

      digitalWrite(18,HIGH);

      }

  }

  else if(command == “white”){

    re=digitalRead(5);

    if(re == 1){

    digitalWrite(5,LOW);

    }

    else{

      digitalWrite(5,HIGH);

      }

 command=””;

}

Step 15: Run the ESP to make communication.

These are simple steps to control LED’s from Mendix App.

Mendix simplifies IoT device management by integrating seamlessly with popular platforms like Amazon IoT, IBM Watson, and ThingSpeak. This, in turn, enables control over a vast array of internet-connected devices, from smart home appliances to industrial sensors. By streamlining the process, Mendix empowers developers to build IoT applications more efficiently.



Author: Ajitha Deborah
Ajitha Deborah is a skilled Mendix developer with a year of experience in crafting innovative solutions. Her technical expertise extends to Java, API integration, HTML, CSS, and JavaScript, enabling her to build robust and user-friendly applications. Ajitha's ability to integrate IoT devices into Mendix platforms showcases her versatility and deep understanding of emerging technologies.