• 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

I have problem with publish sensor reading on my coding at module 5.3

Q&A Forum › Category: ESP32 › I have problem with publish sensor reading on my coding at module 5.3
0 Vote Up Vote Down
Fadhli Azinnuddin asked 2 years ago

I have problem with my code publish subsribe use sensor ML 8511 can’t show reading sensor on serial println and node red user interface.. on serial println you can see on details serial print on arduino :
https://drive.google.com/file/d/15lebM3F2IRmByE3Zl1qkzYxVbECv0Wj6/view?usp=sharing
and then how to program you can see details on my pastebin : https://pastebin.com/K6TymHBm
 
But if i am use code on examples of arduino ML 8511 without mqtt.. that sensor can reading.. you can see on details : 
https://drive.google.com/file/d/1nj-6LVFYkZvhnardiqZsN6MaAAfQSHIY/view?usp=sharing
and then this progam.. you can see details on my pastebin : https://pastebin.com/X9ZH5MeT
 
please help me sara santos and riu santos
thank you very much
 

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

Hi.
I’m not sure what might be causing the issue?
Can you try to use different SPI pins for your sensor and see if anything changes?
Regards,
Sara

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

Okey i will try.. but i thing i have problem with my coding at void setup at my coding with mqtt program.. because if i use program use library ml 8511 before i combine in mqtt program at module 5.3.. this sensor can reading

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

Because.. if i am use code on examples of arduino ML 8511 without program/combine with mqtt.. that sensor can reading.. you can see on details : 
https://drive.google.com/file/d/1nj-6LVFYkZvhnardiqZsN6MaAAfQSHIY/view?usp=sharing
and then this progam.. you can see details on my pastebin : https://pastebin.com/X9ZH5MeT

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

after i try use different spi, the result same sara on the println sesnor can’t reading.. i thing i have problem on my coding at void loop :
void loop() {
unsigned long currentMillis = millis();

if (currentMillis – previousMillis >= interval) {

previousMillis = currentMillis;

light.enable();
float UV = light.getUV();
light.disable();

uvi = light.getUV();

Serial.print(UV, 4);
Serial.println(” mW cm^2″);
 

delay(1000);
}

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

}
 
because if i use program use examples  ml8511 on arduino sensor can reading.. like on the picture : https://drive.google.com/file/d/1nj-6LVFYkZvhnardiqZsN6MaAAfQSHIY/view?usp=sharing

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

but for use sensor BH 1750 with a program void loop :
void loop() {
unsigned long currentMillis = millis();

if (currentMillis – previousMillis >= interval) {

previousMillis = currentMillis;

lux2 = lightMeter.readLightLevel();
;

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

}
}
that’s program is work.. and then sensor can reading on serial println

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

similiar with when i use sensor temperature max6675 with program void loop :
void loop() {
unsigned long currentMillis = millis();
if (currentMillis – previousMillis >= interval) {
previousMillis = currentMillis;
}

Serial.print(“C = “);
Serial.println(thermocouple.readCelsius());
TEMP = thermocouple.readCelsius();

// For the MAX6675 to update, you must delay AT LEAST 250ms between reads!
delay(1000);

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

}
that’ program is work too.. and sensor can reading on serial println

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

void loop on sensor BH1750 i am use program : lux2 = lightMeter.readLightLevel();
and then program void loop sensor max6675 i am use program :  TEMP = thermocouple.readCelsius();
that’s sensor are work and can reading on println and node red user interface..
 
but on program sensor ml8511 i confused make program because, there are use float :
light.enable();
float UV = light.getUV();
light.disable();

uvi = light.getUV();
Serial.print(UV, 4);
Serial.println(” mW cm^2″);
 
soo i am confused

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

on the program void loop sensor ml8511 use float for void loop program.. so i am confuse :
 
void loop() {
unsigned long currentMillis = millis();

if (currentMillis – previousMillis >= interval) {

previousMillis = currentMillis;

light.enable();
float UV = light.getUV();
light.disable();

uvi = light.getUV();

Serial.print(UV, 4);
Serial.println(” mW cm^2″);
 

delay(1000);
}

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

}

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

Hi.
I think you just need to move one curly bracket to the end.
Like this:
if (currentMillis – previousMillis >= interval) {
previousMillis = currentMillis;

light.enable();
float UV = light.getUV();
light.disable();

uvi = light.getUV();
Serial.print(UV, 4);
Serial.println(” mW cm^2″);
 
delay(1000);

uint16_t packetIdPub4 = mqttClient.publish(MQTT_PUB_UVI, 1, true, String(uvi).c_str());
Serial.printf(“Publishing on topic %s at QoS 1, packetId: %i”, MQTT_PUB_UVI, packetIdPub4);
Serial.printf(“Message: %.2f \n”, uvi);
}
}
 
Let me know if this helps.
Regards,
Sara

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

still same sara.. sensor can’t reading.. details on https://drive.google.com/file/d/10x9v2GTOjEoaAdtP7V43k71zRwG-JeR7/view?usp=sharing

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

my program.. maybe you can solving :
 
/*********
Fadhli.
******/
#include <Arduino.h>
#include <WiFi.h>
#include <ML8511.h>

extern “C” {
#include “freertos/FreeRTOS.h”
#include “freertos/timers.h”
}
#include <AsyncMqttClient.h>
#include <SPI.h>
#include <Wire.h>
 

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

#define MQTT_PUB_UVI “esp/ml8511/intensitas”
#define ANALOGPIN 15
#define ENABLEPIN 4
ML8511 light(ANALOGPIN, ENABLEPIN);

float uvi;
 
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(115200);
Serial.println(__FILE__);
Serial.println(“UV UltraViolet ML8511”);
 
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;

light.enable();
float UV = light.getUV();
light.disable();

uvi = light.getUV();

Serial.print(UV, 4);
Serial.println(” mW cm^2″);
 

delay(1000);

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

}
}

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

Hi.
I’m sorry for taking so long to notice the following issue:
 
You’re using analog GPIO 15. This GPIO is an ADC2 pin. There is an issue with the ESP32 gpios that ADC2 pins cannot be used when Wi-Fi is used.
So, you must choose an ADC1 pin instead of GPIO 15.
See the ADC1 pins on the ESP321 pinout:

Let me know if this solves the problem.
Regards,
Sara
 

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

So, you can choose, for example, GPIO 33.

0 Vote Up Vote Down
Fadhli Azinnuddin answered 2 years ago

Okey solving sara
Thank you very much 🙏

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

That’s great!
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara

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.