I have an ESP32, a DHT11 and a ZMP101B. I'm trying to use the EmonLib library to read the voltage of the ZMP101B. I can do the voltage reading when I don't implement the MQTT part, but when I implement the MQTT part, it stops reading the voltage.. the voltage is at "0V".. the dht readings are still normal.
The same is true for other IoT platforms.
- #include <stdio.h>
- #include <WiFi.h>
- #include <PubSubClient.h>
- #include <ArduinoJson.h>
- #include <DHT.h>
- #include <EmonLib.h>
- #define VOLT_CAL 85 // voltage calib
- // MQTT definitions
- #define MQTT_PUB_TOPIC "tago/data/post"
- #define MQTT_USERNAME "test" // any value
- #define MQTT_PASSWORD "66534b39-c363-41f8-b665-fd644a2423c9" // Device Token TagoIO
- // WIFI
- const char* ssid_wifi = "AndroidAP"; // WI-FI network SSID
- const char* password_wifi = "cett0940"; // WI-FI network password
- WiFiClient espClient;
- // MQTT
- const char* broker_mqtt = "mqtt.tago.io"; // MQTT broker URL
- int broker_port = 1883; // MQTT broker port
- PubSubClient MQTT(espClient);
- DHT dht(25, DHT11); // (pin, type DHT)
- EnergyMonitor emon5; // instance emon
- //Prototypes
- void init_wifi(void);
- void init_MQTT(void);
- void connect_MQTT(void);
- void connect_wifi(void);
- void send_data_iot_platform(void);
- unsigned long millis_Time = millis();
- float temperature_read;
- float humidity_read;
- double voltage_read;
- void setup() {
-
- Serial.begin(115200);
- dht.begin(); // init DHT
- init_wifi(); // func init wifi
- init_MQTT(); // func init mqtt
- connect_MQTT(); // func connect mqtt
- emon5.voltage(26, VOLT_CAL, 1.7); // voltage (pin analóg, calib, fase change)
-
- delay(500);
- }
- void loop() {
- connect_wifi(); // verify wi-fi
- connect_MQTT(); // verify MQTT
- send_data_iot_platform(); // send data to TagoIO)
-
- }
- // Func init wi-fi
- void init_wifi(void) {
- delay(10);
- Serial.println();
- Serial.println("------ WI-FI -----");
- Serial.print("Connecting to Wifi: ");
- Serial.println(ssid_wifi);
- connect_wifi();
- }
- // Func connect to wifi
- void connect_wifi(void){
- if (WiFi.status() == WL_CONNECTED)
- return;
- WiFi.begin(ssid_wifi, password_wifi);
- while (WiFi.status() != WL_CONNECTED){
- WiFi.begin(ssid_wifi, password_wifi);
- delay(3000);
- Serial.print(".");
- }
-
- Serial.println();
- Serial.print("Successful connection to Wifi network: ");
- Serial.println(ssid_wifi);
- Serial.print("IP: ");
- Serial.println(WiFi.localIP());
- Serial.println("---------------");
- }
- // Func init MQTT
- void init_MQTT(void) {
- MQTT.setServer(broker_mqtt, broker_port);
- }
- // Func conect to MQTT
- void connect_MQTT(void){
- char mqtt_id_randomico[5] = {0};
- while (!MQTT.connected()){
- Serial.print("Connecting to MQTT: ");
- Serial.println(broker_mqtt);
- /* id mqtt random */
- randomSeed(random(9999));
- sprintf(mqtt_id_randomico, "%ld", random(9999));
-
- if (MQTT.connect(mqtt_id_randomico, MQTT_USERNAME, MQTT_PASSWORD)){
- Serial.println("Successful connection to MQTT");
- Serial.println("----------");
- }
- else{
- Serial.println("Failed to connect to MQTT...");
- Serial.println("Retry in 5s...");
- delay(5000);
- }
- }
- }
- // Func send data to TagoIO (MQTT)
- void send_data_iot_platform(void){
- temperature_read = dht.readTemperature();
- humidity_read = dht.readHumidity();
- emon5.calcVI(10,100);
- voltage_read = emon5.Vrms;
- //voltage_read = 127;
- if((millis() - millis_Time) > 3000) {
-
- Serial.print("Temp: ");
- Serial.print(temperature_read,1);
- Serial.print("°C;\t");
- Serial.print("Humidity: ");
- Serial.print(humidity_read,0);
- Serial.print("%\t");
- Serial.print("Voltage: ");
- Serial.print(voltage_read,1);
- Serial.println("V");
-
-
- // send temperature
- char temperature[250] = {0};
- StaticJsonDocument <250> tago_json_temperature;
- tago_json_temperature["variable"] = "temperature";
- tago_json_temperature["unit"] = "C";
- tago_json_temperature["value"] = temperature_read;
- memset(temperature, 0, sizeof(temperature));
- serializeJson(tago_json_temperature, temperature);
- MQTT.publish(MQTT_PUB_TOPIC, temperature);
- // send humidity
- char humidity[250] = {0};
- StaticJsonDocument <250> tago_json_humidity;
- tago_json_humidity["variable"] = "humidity";
- tago_json_humidity["unit"] = "%";
- tago_json_humidity["value"] = humidity_read;
- memset(humidity, 0, sizeof(humidity));
- serializeJson(tago_json_humidity, humidity);
- MQTT.publish(MQTT_PUB_TOPIC, humidity);
- // send voltage
- char voltage[250] = {0};
- StaticJsonDocument <250> tago_json_voltage;
- tago_json_voltage["variable"] = "voltage";
- tago_json_voltage["unit"] = "V";
- tago_json_voltage["value"] = voltage_read;
- memset(voltage, 0, sizeof(voltage));
- serializeJson(tago_json_voltage, voltage);
- MQTT.publish(MQTT_PUB_TOPIC, voltage);
-
- millis_Time = millis();
- }
- }