• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

How to program sensor and how to program use with one node red (mqtt) with esp 32 many more

Q&A Forum › Category: ESP32 › How to program sensor and how to program use with one node red (mqtt) with esp 32 many more
0 Vote Up Vote Down
Fadhli Azinnuddin asked 2 years ago

Sara, can u help me ?
how to place program with a new sensor example : light sensor, uv sensor, pressure gauge sensor  and etc on your mqtt program on module 5.3 publish sensor reading?
and how to program with one mqtt broker and one dashboard node red with many more esp 32. example every esp 32 have one sensor ( pressure sensor ) and another esp 32 have sensor (light sensor ).. and i want every esp 32 have a integrated connection with one mqtt broker and one dashboard node red

12 Answers
0 Vote Up Vote Down
Best Answer
Sara Santos Staff answered 2 years ago

Hi.
do you still have the issue with the mqttreconnecttimer? Can you share the code using pastebin or github?
 
What is exactly the issue with the LUX sensors? I’m not sure that I understood your issue.
Regards,
Sara

0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Hi.
First, you need to be familiar with the sensors you’re using.
Start with a basic example. Which sensors exactly are you using? can you provide the model?
Then, you just need to replace the sections of code that refer to the BME280 sensors to the required lines for your sensor.
If you want multiple ESP32 boards publishing to Node-RED dashboard, each board can publish on a different topic. Here are some suggestions:
board1/UV
board1/light
board2/UV
board3/UV 
and so on.
Then, on Node-RED dshboard, you just need to create MQTT nodes that are subscribed to those topics.
Let me know if you have more specific questions.
 
Regards,
Sara

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

I use Lux sensor GY 302 BH1750 and Uv sensor GY ML 8511 for now.. and after i done program use that sensor with every esp32 ( esp32 connect to uv sensor ) and another esp 32 ( esp 32 connect to lux sensor ) with one mqtt broker or one dashboard.. and then on the next i will use pressure sensor and temperature/thermocouple sensor on another esp 32

0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Unfortunately, we don’t have any tutorials using those specific sensors.
I hope my previous answer helps you line up your project.
 
Regards,
Sara

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

sara i have problem with my coding on module 5.3 Publish sensor readings, i am use sensor max 6675 for thermocouple..
/*********
Fadhli.
******/
#include <Arduino.h>
#include <WiFi.h>
extern “C” {
#include “freertos/FreeRTOS.h”
#include “freertos/timers.h”
}
#include <AsyncMqttClient.h>
#include “max6675.h”

#define WIFI_SSID “FDL”
#define WIFI_PASSWORD “Jiancok717273”
#define MQTT_HOST IPAddress(192, 168, 1, 101)
#define MQTT_PORT 1883
#define BROKER_USER “fadhli”
#define BROKER_PASS “1234”

#define MQTT_PUB_Temp1 “esp/max6675/temperature1”
int thermoDO = 19;
int thermoCS = 23;
int thermoCLK = 5;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
 
// Variables to hold sensor readings
float Temp1;
 
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
unsigned long previousMillis = 0; // Stores last time a message was published
const long interval = 10000; // Interval at which to publish values
void connectToWifi() {
Serial.println(“Connecting to Wi-Fi…”);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void connectToMqtt() {
Serial.println(“Connecting to MQTT…”);
mqttClient.connect();
}
void WiFiEvent(WiFiEvent_t event) {
Serial.printf(“[WiFi-event] event: %d\n”, event);
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println(“WiFi connected”);
Serial.println(“IP address: “);
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println(“WiFi lost connection”);
xTimerStop(mqttReconnectTimer, 0); // ensure we don’t reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
}
}
void onMqttConnect(bool sessionPresent) {
Serial.println(“Connected to MQTT.”);
Serial.print(“Session present: “);
Serial.println(sessionPresent);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println(“Disconnected from MQTT.”);
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
Serial.println(“Subscribe acknowledged.”);
Serial.print(” packetId: “);
Serial.println(packetId);
Serial.print(” qos: “);
Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
Serial.println(“Unsubscribe acknowledged.”);
Serial.print(” packetId: “);
Serial.println(packetId);
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
// Do whatever you want when you receive a message
Serial.println(“Publish received.”);
Serial.print(” topic: “);
Serial.println(topic);
Serial.print(” qos: “);
Serial.println(properties.qos);
Serial.print(” dup: “);
Serial.println(properties.dup);
Serial.print(” retain: “);
Serial.println(properties.retain);
Serial.print(” len: “);
Serial.println(len);
Serial.print(” index: “);
Serial.println(index);
Serial.print(” total: “);
Serial.println(total);
}
void onMqttPublish(uint16_t packetId) {
Serial.println(“Publish acknowledged.”);
Serial.print(” packetId: “);
Serial.println(packetId);
}
void setup() {
Serial.begin(230400);
Serial.println(“MAX6675 test”);
// wait for MAX chip to stabilize
delay(500);
}
mqttReconnectTimer = xTimerCreate(“mqttTimer”, pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate(“wifiTimer”, pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
mqttClient.setCredentials(BROKER_USER, BROKER_PASS);

connectToWifi();
}
void loop() {
unsigned long currentMillis = millis();

if (currentMillis – previousMillis >= interval) {

previousMillis = currentMillis;

Temp1 = thermocouple.readCelsius();
;

uint16_t packetIdPub3 = mqttClient.publish(MQTT_PUB_Temp1, 1, true, String(Temp1).c_str());
Serial.printf(“Publishing on topic %s at QoS 1, packetId: %i”, MQTT_PUB_Temp1, packetIdPub3);
Serial.printf(“Message: %.2f \n”, Temp1);

}
}
 
that coding error on ‘mqttReconnectTimer’ does not name a type 
you can see on here for details : https://drive.google.com/file/d/12Uh1ug9IaIz-kEvBzXvFI0eRDtyN5FHg/view?usp=sharing

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

but i am success for program to coding lux sensor BH 1750 :
 
/*********
Fadhli.
******/
#include <Arduino.h>
#include <WiFi.h>
extern “C” {
#include “freertos/FreeRTOS.h”
#include “freertos/timers.h”
}
#include <AsyncMqttClient.h>
#include <BH1750.h>
#include <Wire.h>

#define WIFI_SSID “FDL”
#define WIFI_PASSWORD “Jiancok717273”
#define MQTT_HOST IPAddress(192, 168, 1, 101) //MQTT BROKER IP ADDRESS
#define MQTT_PORT 1883
#define BROKER_USER “fadhli”
#define BROKER_PASS “1234”

#define MQTT_PUB_LUX “esp/bh1750/intensitas”

BH1750 lightMeter;

// Variables to hold sensor readings
float lux;
 
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
unsigned long previousMillis = 0; // Stores last time a message was published
const long interval = 10000; // Interval at which to publish values
void connectToWifi() {
Serial.println(“Connecting to Wi-Fi…”);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void connectToMqtt() {
Serial.println(“Connecting to MQTT…”);
mqttClient.connect();
}
void WiFiEvent(WiFiEvent_t event) {
Serial.printf(“[WiFi-event] event: %d\n”, event);
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println(“WiFi connected”);
Serial.println(“IP address: “);
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println(“WiFi lost connection”);
xTimerStop(mqttReconnectTimer, 0); // ensure we don’t reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
}
}
void onMqttConnect(bool sessionPresent) {
Serial.println(“Connected to MQTT.”);
Serial.print(“Session present: “);
Serial.println(sessionPresent);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println(“Disconnected from MQTT.”);
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
Serial.println(“Subscribe acknowledged.”);
Serial.print(” packetId: “);
Serial.println(packetId);
Serial.print(” qos: “);
Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
Serial.println(“Unsubscribe acknowledged.”);
Serial.print(” packetId: “);
Serial.println(packetId);
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
// Do whatever you want when you receive a message
Serial.println(“Publish received.”);
Serial.print(” topic: “);
Serial.println(topic);
Serial.print(” qos: “);
Serial.println(properties.qos);
Serial.print(” dup: “);
Serial.println(properties.dup);
Serial.print(” retain: “);
Serial.println(properties.retain);
Serial.print(” len: “);
Serial.println(len);
Serial.print(” index: “);
Serial.println(index);
Serial.print(” total: “);
Serial.println(total);
}
void onMqttPublish(uint16_t packetId) {
Serial.println(“Publish acknowledged.”);
Serial.print(” packetId: “);
Serial.println(packetId);
}
void setup() {
Serial.begin(230400);
Wire.begin();
// Initialize BME280 sensor
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println(F(“BH1750 Advanced begin”));
} else {
Serial.println(F(“Error initialising BH1750”));
}

mqttReconnectTimer = xTimerCreate(“mqttTimer”, pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate(“wifiTimer”, pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
mqttClient.setCredentials(BROKER_USER, BROKER_PASS);

connectToWifi();
}
void loop() {
unsigned long currentMillis = millis();

if (currentMillis – previousMillis >= interval) {

previousMillis = currentMillis;

lux = lightMeter.readLightLevel();
;

uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_LUX, 1, true, String(lux).c_str());
Serial.printf(“Publishing on topic %s at QoS 1, packetId: %i”, MQTT_PUB_LUX, packetIdPub1);
Serial.printf(“Message: %.2f \n”, lux);

}
}
 
but i have problem sensitivy for sensor when i use lamp on my phone and i am close to that sensor BH1750.. response on user interface node red have a delay..
maybe you can solving my problem
 
 
 

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

Yess sara.. i still have the issue.. oke i will share on pastebin :
https://pastebin.com/CCTrk2JT
 
 
 
that coding error on ‘mqttReconnectTimer’ does not name a type 
you can see on here for details : https://drive.google.com/file/d/12Uh1ug9IaIz-kEvBzXvFI0eRDtyN5FHg/view?usp=sharing
 

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

But for lux meter BH 1750 i am success use this coding but.. why sensitivy of sensor have a delay on user interface node-red.. maybe source code can modify.. source code details on pastebin : https://pastebin.com/MzMTdPcC
 

0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Hi.
You need to remove that bracket  } and move it to the end of the setup.
 

 
How long is the delay on the interface?
 
Regards,
Sara

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

The delay on the user interface node-red about 1 second until 5 seconds sara 

0 Vote Up Vote Down
Rui Santos Staff answered 2 years ago

Hi.
I think that might be “normal” depending on the strength of your internet connection and if the ESP32 has a good connection with your router as well as the broker.
Regards,
Sara

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

Oke thanks sara and riu 🙏

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.