ESP32 model for MQTT Energy Meter

Hi,
I'm tryninig to setup an mqtt energy meter with CT clamps by using emonlib on esp32 Node-MCU (datasheet in attachment).
Boards seems not reading properly analog pins while wifi is activie even though I'm using ADC1 pins (GPIO 32,33,34,35).
Emonlib works fine without wifi (basic example sketch).
Could issue be associated with the esp32 Node-MCU board model?
Are there more suitable models for my scope?
thank you for your support.
regards

ESP-32 NodeMCU.pdf (1017,3 KB)

Please post your full sketch, using code tags when you do. This prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It follows my code:

#include <ArduinoJson.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include "EmonLib.h"  //EmonLib-esp32-master


//setup ct clamps
EnergyMonitor emon0;  
EnergyMonitor emon1; 
EnergyMonitor emon2; 
EnergyMonitor emon3; 

//Device configuration
const char* device_id = "esp32TestBoard";

//WLAN configuration
const char* ssid = "****";
const char* password = "****";
const char* device_ip = "***";
IPAddress local_IP(192, 168, 1, 60);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

// Setup of MQTT Broker IP:
const char* mqtt_server = "****";
const char* mqttUser = "****";
const char* mqttPassword = "****";


WiFiClient espClient;
PubSubClient client(espClient);

StaticJsonDocument<256> current_dict;

void setup() {
  
  Serial.begin(115200);
  
  // Configures static IP address
  if (!WiFi.config(local_IP, gateway, subnet)) {
    Serial.println("STA Failed to configure");
  }



  //startup wifi and Mqtt connection
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);


  //sturtup emon sensors
  emon0.current(34, 26);
  adcAttachPin(34);
  emon1.current(35, 26);
  adcAttachPin(35);
  emon2.current(32, 29);
  adcAttachPin(32);
  emon3.current(33, 29);
  adcAttachPin(33);
   
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* message, unsigned int length) {

  StaticJsonDocument<256> payload;
  deserializeJson(payload, message, length);

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(device_id, mqttUser, mqttPassword)) {
      Serial.println("connected");
      // Subscribe


    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}


void loop() {  

      if (!client.connected()) {
        reconnect();
      }
      client.loop();

      float Irms0 = emon0.calcIrms(1480);
      float Irms1 = emon1.calcIrms(1480);
      float Irms2 = emon2.calcIrms(1480);
      float Irms3 = emon3.calcIrms(1480);

      current_dict["ch1"] = Irms0;
      current_dict["ch2"] = Irms1;
      current_dict["ch3"] = Irms2;
      current_dict["ch4"] = Irms3;

      Serial.println(Irms0);

      char current_Buffer[256];
      serializeJson(current_dict, current_Buffer);     

      client.publish("esp32TestBoard/sensors/current", current_Buffer);
      
      delay(1000);
    
}

No one can help me about this topic?

thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.