IoT with MQTT

Updated: 2025-10-16

The MQTT communication protocol is a standard for IoT devices.

It works through "topics", similar to social media tags. A device like a thermometer sends the obtained temperature without knowing who will request or read it.

Therefore, an "MQTT broker" is required to constantly listen to what the sensors send and be ready to transmit it to whoever needs it.

While a standard broker is Mosquitto, middleware.js incorporates a very similar functionality.

So we return to the same system architecture:

GPSS-Plus <-> (middleware.js + MQTT broker) <-> device

At this point, we move on to programming and using extra hardware.

Don't worry too much; it would be advisable to have some basic knowledge of C programming, but in general, it is enough to ask your favorite AI to build what you need.

We are going to program an ESP8266 microcontroller with WIFI to manage:

  • A BH1750 light sensor
  • A DHT22 humidity and temperature sensor
  • Turning an LED on and off (protected by a resistor).

For this, we will use these hardware elements and software to flash the ESP8266 firmware.

A simple one is "Arduino IDE", which contains all the necessary libraries.

A quick roadmap to follow is:

  • 1.- Obtain the hardware.
  • 2.- Download Arduino IDE.
  • 3.- Install the necessary driver to connect your device via USB simulating a serial port.
  • 4.- Compile the firmware's C code in Arduino IDE (available in the downloads chapter).
  • 5.- Upload it to the device through Arduino IDE.
  • 6.- Send your WIFI credentials and the broker's IP through the serial console.
  • 7.- Run GPSS-Plus and watch the real temperature and humidity data while toggling the LED.

In the example, we can see how the three standard MQTT options are performed:

  • Subscribing to a sensor (reading humidity).
  • Writing to a command (turning on an LED).
  • Checking status (Last Will and Testament - LWT).

Note: The WRITE check ("OK" response) is something reported by the BRIDGER at the network layer, not the end device itself.

Example

SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}
SYSTEM {TYPE:OPTIONS, REAL_TIME:1}


Initial options,{  }

BRIDGER {NAME:bridge1,X:733,Y:556
	,SERVER:"wss://yourdomain.com:3000"
	,CLIENT:"mqtt://localhost"
    ,OPTIONS:V$options
    ,on_error:bridge1_on_error
    }


POSITION {NAME:POS1,X:101,Y:378}

POSITION {NAME:POS2,X:143,Y:115}


Graphic {NAME:txtSubscribeL,Type:TEXT,X:394,Y:465,Text:"On SUBS"}
Graphic {NAME:txtSubscribeH,Type:TEXT,X:394,Y:435,Text:"On SUBS"}
Graphic {NAME:txtSubscribeT,Type:TEXT,X:394,Y:405,Text:"On SUBS"}
Graphic {NAME:txtSubscribeS,Type:TEXT,X:394,Y:375,Text:"On SUBS"}

Graphic {NAME:TextBrowse,Type:TEXT,X:116,Y:283,Text:"On Browse"}
Graphic {NAME:TextWrite,Type:TEXT,X:391,Y:59,Text:"On Write"}
Graphic {NAME:TextError,Type:TEXT,X:424,Y:582,Text:"Error"}


START 10

;*****************************************************

PROCEDURE PRE_RUN
    BRIDGE_SUBSCRIPTION { NAME: bridge1
	, VARIABLE: "devices/elprimero/sensor/humidity"
    , TRIGGER: on_humidity 
    }
    BRIDGE_SUBSCRIPTION { NAME: bridge1
	, VARIABLE: "devices/elprimero/sensor/lux"
    , TRIGGER: on_luz 
    }
    BRIDGE_SUBSCRIPTION { NAME: bridge1
	, VARIABLE: "devices/elprimero/sensor/temperature"
    , TRIGGER: on_temperature 
    }
    BRIDGE_SUBSCRIPTION { NAME: bridge1
	, VARIABLE: "devices/elprimero/status"
    , TRIGGER: on_status 
    }

; No output for MQTT
BRIDGE_BROWSE {name:bridge1, trigger:on_BROWSE }


 
	TERMINATE_VE
ENDPROCEDURE

;*****************************************************
GENERATE 5,0 {NAME:GEN1,X:91,Y:547}

ADVANCE 5,0 {TO:POS1,flow:1}


if (D$N% 2== 1)
ASSIGN values, {"devices/elprimero/commands/led": "on"}
BRIDGE_WRITE { name:bridge1, VALUES:V$values ,TRIGGER:on_write}
else
ASSIGN values, {"devices/elprimero/commands/led": "off"}
BRIDGE_WRITE { name:bridge1, VALUES:V$values ,TRIGGER:on_write}
endif

ADVANCE 5,0 {TO:POS2,flow:1}

ENDGENERATE 1
;----------------------------------------------------------------------------
procedure on_read
    move {name:TextRead,TEXT:"WIFI: P$(PARAM_A)"}
	TERMINATE_VE
endprocedure

procedure bridge1_on_error
    move {name:TextError,TEXT:"P$PARAM_A"}
	TERMINATE_VE
endprocedure
;-------------------------
procedure on_write
move {name:TextWrite,TEXT:"WRITE T:AC1$: P$(PARAM_A.devices/elprimero/commands/led)"}
	TERMINATE_VE
endprocedure



procedure on_luz
	move {name:txtSubscribeL,text:"Subs LUX P$(PARAM_A.VALUE)"}
	TERMINATE_VE
endprocedure

procedure on_humidity
	move {name:txtSubscribeH,text:"Subs HUM P$(PARAM_A.VALUE)"}
	TERMINATE_VE
endprocedure

procedure on_temperature
	move {name:txtSubscribeT,text:"Subs TEM P$(PARAM_A.VALUE)"}
	TERMINATE_VE
endprocedure



procedure on_status
	move {name:txtSubscribeS,text:"Subs STA P$(PARAM_A.VALUE)"}
	TERMINATE_VE
endprocedure






procedure on_BROWSE
   move {name:TextBrowse,text:"on_BROWSE P$(PARAM_A)"}
	TERMINATE_VE
endprocedure