Emonlib with MQTT

Emonlib with MQTT

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.
  1. #include <stdio.h>
  2. #include <WiFi.h>
  3. #include <PubSubClient.h>
  4. #include <ArduinoJson.h>
  5. #include <DHT.h>
  6. #include <EmonLib.h>

  7. #define VOLT_CAL    85             // voltage calib


  8. // MQTT definitions
  9. #define MQTT_PUB_TOPIC "tago/data/post"
  10. #define MQTT_USERNAME  "test"                                     // any value
  11. #define MQTT_PASSWORD  "66534b39-c363-41f8-b665-fd644a2423c9"     // Device Token TagoIO

  12. // WIFI
  13. const char* ssid_wifi = "AndroidAP";            // WI-FI network SSID
  14. const char* password_wifi = "cett0940";         // WI-FI network password
  15. WiFiClient espClient;     

  16. // MQTT
  17. const char* broker_mqtt = "mqtt.tago.io";       // MQTT broker URL
  18. int broker_port = 1883;                         // MQTT broker port
  19. PubSubClient MQTT(espClient); 


  20. DHT dht(25, DHT11);                             // (pin, type DHT)
  21. EnergyMonitor emon5;                            // instance emon

  22. //Prototypes
  23. void init_wifi(void);
  24. void init_MQTT(void);
  25. void connect_MQTT(void);
  26. void connect_wifi(void);
  27. void send_data_iot_platform(void);

  28. unsigned long millis_Time = millis();
  29. float temperature_read;
  30. float humidity_read;
  31. double voltage_read;

  32. void setup() {
  33.     
  34.     Serial.begin(115200);            
  35.     dht.begin();                          // init DHT
  36.     init_wifi();                          // func init wifi
  37.     init_MQTT();                          // func init mqtt
  38.     connect_MQTT();                       // func connect mqtt
  39.     emon5.voltage(26, VOLT_CAL, 1.7);     // voltage (pin analóg, calib, fase change)
  40.     
  41.     delay(500);
  42. }

  43. void loop() {
  44.     connect_wifi();                   // verify wi-fi
  45.     connect_MQTT();                   // verify MQTT
  46.     send_data_iot_platform();         // send data to TagoIO)
  47.    
  48. }


  49. // Func init wi-fi
  50. void init_wifi(void)  {
  51.     delay(10);
  52.     Serial.println();
  53.     Serial.println("------ WI-FI -----");
  54.     Serial.print("Connecting to Wifi: ");
  55.     Serial.println(ssid_wifi);
  56.     connect_wifi();
  57. }

  58. // Func connect to wifi
  59. void connect_wifi(void){
  60.     if (WiFi.status() == WL_CONNECTED)
  61.         return;
  62.     WiFi.begin(ssid_wifi, password_wifi);
  63.     while (WiFi.status() != WL_CONNECTED){
  64.         WiFi.begin(ssid_wifi, password_wifi);
  65.         delay(3000);
  66.         Serial.print(".");
  67.     }
  68.   
  69.     Serial.println();
  70.     Serial.print("Successful connection to Wifi network: ");
  71.     Serial.println(ssid_wifi);
  72.     Serial.print("IP: ");
  73.     Serial.println(WiFi.localIP());
  74.     Serial.println("---------------");
  75. }

  76. // Func init MQTT
  77. void init_MQTT(void)  {
  78.     MQTT.setServer(broker_mqtt, broker_port);
  79. }

  80. // Func conect to MQTT
  81. void connect_MQTT(void){
  82.     char mqtt_id_randomico[5] = {0};
  83.     while (!MQTT.connected()){
  84.         Serial.print("Connecting to MQTT: ");
  85.         Serial.println(broker_mqtt);

  86.         /* id mqtt random */
  87.         randomSeed(random(9999));
  88.         sprintf(mqtt_id_randomico, "%ld", random(9999));
  89.         
  90.         if (MQTT.connect(mqtt_id_randomico, MQTT_USERNAME, MQTT_PASSWORD)){
  91.             Serial.println("Successful connection to MQTT");
  92.             Serial.println("----------");
  93.         } 
  94.         else{
  95.             Serial.println("Failed to connect to MQTT...");
  96.             Serial.println("Retry in 5s...");
  97.             delay(5000);
  98.         }
  99.     }
  100. }


  101. // Func send data to TagoIO (MQTT)
  102. void send_data_iot_platform(void){


  103.     temperature_read = dht.readTemperature();
  104.     humidity_read = dht.readHumidity();
  105.     emon5.calcVI(10,100);
  106.     voltage_read = emon5.Vrms;
  107.     //voltage_read = 127;

  108. if((millis() - millis_Time) > 3000) {
  109.   
  110.     Serial.print("Temp: ");
  111.     Serial.print(temperature_read,1);
  112.     Serial.print("°C;\t");
  113.     Serial.print("Humidity: ");
  114.     Serial.print(humidity_read,0);
  115.     Serial.print("%\t");
  116.     Serial.print("Voltage: ");
  117.     Serial.print(voltage_read,1);
  118.     Serial.println("V");
  119.  
  120.  

  121.     // send temperature 
  122.     char temperature[250] = {0};
  123.     StaticJsonDocument <250> tago_json_temperature;
  124.     tago_json_temperature["variable"] = "temperature";
  125.     tago_json_temperature["unit"] = "C";
  126.     tago_json_temperature["value"] = temperature_read;
  127.     memset(temperature, 0, sizeof(temperature));
  128.     serializeJson(tago_json_temperature, temperature);
  129.     MQTT.publish(MQTT_PUB_TOPIC, temperature);

  130.     // send humidity
  131.     char humidity[250] = {0};
  132.     StaticJsonDocument <250> tago_json_humidity;
  133.     tago_json_humidity["variable"] = "humidity";
  134.     tago_json_humidity["unit"] = "%";
  135.     tago_json_humidity["value"] = humidity_read;
  136.     memset(humidity, 0, sizeof(humidity));
  137.     serializeJson(tago_json_humidity, humidity);
  138.     MQTT.publish(MQTT_PUB_TOPIC, humidity);


  139.     // send voltage
  140.     char voltage[250] = {0};
  141.     StaticJsonDocument <250> tago_json_voltage;
  142.     tago_json_voltage["variable"] = "voltage";
  143.     tago_json_voltage["unit"] = "V";
  144.     tago_json_voltage["value"] = voltage_read;
  145.     memset(voltage, 0, sizeof(voltage));
  146.     serializeJson(tago_json_voltage, voltage);
  147.     MQTT.publish(MQTT_PUB_TOPIC, voltage);
  148.    
  149.     millis_Time = millis();
  150.   }   
  151. }