Using current sensor trough ESP8266

Hello,

I’m trying to use cayenne firmware to receive information from this device:

https://robotdyn.com/current-sensor-acs712-5a.html

I’m trying to use a ESP8266 - CP2102 NodeMCU V3 Lua, to receive the data on my cayenne online dashboard, from a widget that would tell me the current that the sensor is analysing.

Is it possible to do so with cayenne firmware?

are you able to read data from the sensor on to your nodemcu. Once you are done then you can send that data to cayenne using Cayenne.virtualWrite(1, data, "current", "a");

I was not able to send data from the sensor to ESP8266 because I did not find code for it.
What you suggest?

a simple google search gave this https://basicelectronics.in/how-to-interface-acs712-current-sensor-with-nodemcu/

I managed to read data on arduinoIDE serial monitor, connecting the sensor to ESP8266 using this code:

const int sensorIn = A0;
int mVperAmp = 185; // use 185 for 5A, 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
void setup(){
pinMode(A0, INPUT);
Serial.begin(115200);
delay(10);
Serial.println(F("Init...."));
}
void loop(){
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707; // sq root
AmpsRMS = (VRMS * 1000)/mVperAmp;
float Wattage = (230*AmpsRMS)-13; //Observed 18-20 Watt when no load was connected, so substracting offset value to get real consumption.
Serial.print(AmpsRMS);
Serial.println(" Amps RMS ");
Serial.print(Wattage);
Serial.println(" Watt ");
}
float getVPP()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the maximum sensor value*/
minValue = readValue;
}
/* Serial.print(readValue);
Serial.println(" readValue ");
Serial.print(maxValue);
Serial.println(" maxValue ");
Serial.print(minValue);
Serial.println(" minValue ");
delay(1000); */
}
// Subtract min from max
result = ((maxValue - minValue) * 5)/1024.0;
return result;
}

I want to have a graph as a cayenne widget on the online dashboard of the Wattage (sensor data). How and where should I inserted the line you sent?

Cayenne.virtualWrite(1, data, “current”, “a”);

you need to use the cayenne code for esp8266 Cayenne-MQTT-Arduino/examples/Connections/ESP8266 at master · myDevicesIoT/Cayenne-MQTT-Arduino · GitHub and add the sensor code along with the line i shared above. if you need to change it watt data type then have a look at this Data types for Cayenne MQTT API

Hello,

I think i’m very close to the objective. But the error: " ‘Wattage’ was not declared in scope " appears when uploading the code to ESP8266:

#include <CayenneMQTTESP8266.h>

// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.

#define CAYENNE_PRINT Serial
#define CAYENNE_DEBUG
#define SENSOR_PIN 0 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL 1

// WiFi network info.
char ssid = “–”;
char wifiPassword = “–”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “–”;
char password = “–”;
char clientID = “–”;

const int sensorIn = A0;
int mVperAmp = 185; // use 185 for 5A, 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
void setup(){
pinMode(A0, INPUT);
Serial.begin(115200);
delay(10);
Serial.println(F(“Init…”));
pinMode(SENSOR_PIN, INPUT);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}
void loop(){
Voltage = getVPP();
VRMS = (Voltage/2.0) 0.707; // sq root
AmpsRMS = (VRMS * 1000)/mVperAmp;
float Wattage = (230
AmpsRMS)-13; //Observed 18-20 Watt when no load was connected, so substracting offset value to get real consumption.
Serial.print(AmpsRMS);
Serial.println(" Amps RMS “);
Serial.print(Wattage);
Serial.println(” Watt “);
Cayenne.loop();
checkSensor();
}
float getVPP()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/record the maximum sensor value/
maxValue = readValue;
}
if (readValue < minValue)
{
/record the maximum sensor value/
minValue = readValue;
}
/* Serial.print(readValue);
Serial.println(” readValue “);
Serial.print(maxValue);
Serial.println(” maxValue “);
Serial.print(minValue);
Serial.println(” minValue ");
delay(1000); */
}
// Subtract min from max
result = ((maxValue - minValue) * 5)/1024.0;
return result;
}

int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;

void checkSensor()
{
unsigned long currentMillis = millis();
// Check sensor data every 250 milliseconds
if (currentMillis - previousMillis >= 100) {
// Check the sensor state and send data when it changes.
currentState = digitalRead(SENSOR_PIN);
if (currentState != previousState) {
Cayenne.virtualWrite(1 , Wattage, “Potência”, “W”);
previousState = currentState;
}
previousMillis = currentMillis;
}
}

what does this code do?

It’s a code that is working in other project. I used it for having a widget on cayenne dashboard, that gave me information if a movement sensor was activated or not (movement sensor connected to ESP8266, ESP8266 connect via mqtt to cayenne):

#include <CayenneMQTTESP8266.h>

//Resistência de 100ohms entre ESP8266 e PIR
// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling. 

#define CAYENNE_PRINT Serial
#define CAYENNE_DEBUG

// WiFi network info.
char ssid[] = "--";
char wifiPassword[] = "--";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "--";
char password[] = "--";
char clientID[] = "--";

#define SENSOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL 1

void setup()
{
  pinMode(SENSOR_PIN, INPUT);
  Serial.begin(74880);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  pinMode(5, OUTPUT);
  digitalWrite(5,HIGH);
}

void loop()
{
  Cayenne.loop();
  checkSensor();
}

int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;

void checkSensor()
{
  unsigned long currentMillis = millis();
  // Check sensor data every 250 milliseconds
  if (currentMillis - previousMillis >= 100) {
    // Check the sensor state and send data when it changes.
    currentState = digitalRead(SENSOR_PIN);
    if (currentState != previousState) {
      Cayenne.virtualWrite(1  , currentState, "digital_sensor", "d");
      previousState = currentState;
    }
    previousMillis = currentMillis;
  }
}

CAYENNE_IN(0)
{

  digitalWrite(5, !getValue.asInt()); 

}

i am not sure what your code is doing and why you are using it. the error you are getting is because you have not declared Wattage globally.

I removed the code you said didn’t understand.
Now the following error appears (while uploading the code): “stray ‘\stray342’ in program”

#include <CayenneMQTTESP8266.h>

// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.

#define CAYENNE_PRINT Serial
#define CAYENNE_DEBUG
#define VIRTUAL_CHANNEL 1

// WiFi network info.
char ssid = “”;
char wifiPassword = “”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “”;
char password = “”;
char clientID = “”;

const int sensorIn = A0;
int mVperAmp = 185; // use 185 for 5A, 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

float Wattage = (230*AmpsRMS)-13; //Observed 18-20 Watt when no load was connected, so substracting offset value to get real consumption.

void setup(){
pinMode(A0, INPUT);
Serial.begin(115200);
delay(10);
Serial.println(F(“Init…”));
pinMode(SENSOR_PIN, INPUT);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}
void loop(){
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707; // sq root
AmpsRMS = (VRMS * 1000)/mVperAmp;

Serial.print(AmpsRMS);
Serial.println(" Amps RMS “);
Serial.print(Wattage);
Serial.println(” Watt “);
Cayenne.loop();
Cayenne.virtualWrite(1, Wattage, “Potência”, “W”);
}
float getVPP()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/record the maximum sensor value/
maxValue = readValue;
}
if (readValue < minValue)
{
/record the maximum sensor value/
minValue = readValue;
}
/* Serial.print(readValue);
Serial.println(” readValue “);
Serial.print(maxValue);
Serial.println(” maxValue “);
Serial.print(minValue);
Serial.println(” minValue ");
delay(1000); */
}
// Subtract min from max
result = ((maxValue - minValue) * 5)/1024.0;
return result;
}
int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;

you need to brush your programming knowledge, it is quite bad at the moment. You can start with this https://www.arduino.cc/en/Tutorial/HomePage?from=Main.Tutorials

here is the code that should work:

#include <CayenneMQTTESP8266.h>

// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.

#define CAYENNE_PRINT Serial
#define CAYENNE_DEBUG
#define VIRTUAL_CHANNEL 1

// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

const int sensorIn = A0;
int mVperAmp = 185; // use 185 for 5A, 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

float Wattage = (230 * AmpsRMS) - 13; //Observed 18-20 Watt when no load was connected, so substracting offset value to get real consumption.

void setup() {
  pinMode(A0, INPUT);
  Serial.begin(115200);
  delay(10);
  Serial.println(F("Init…"));
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}
void loop()
{
  Voltage = getVPP();
  VRMS = (Voltage / 2.0) * 0.707; // sq root
  AmpsRMS = (VRMS * 1000) / mVperAmp;

  Serial.print(AmpsRMS);
  Serial.println(" Amps RMS ");
  Serial.print(Wattage);
  Serial.println(" Watt ");
  Cayenne.loop();
}
float getVPP()
{
  float result;
  int readValue; //value read from the sensor
  int maxValue = 0; // store max value here
  int minValue = 1024; // store min value here
  uint32_t start_time = millis();
  while ((millis() - start_time) < 1000) //sample for 1 Sec
  {
    readValue = analogRead(sensorIn);
    // see if you have a new maxValue
    if (readValue > maxValue)
    {
      maxValue = readValue;
    }
    if (readValue < minValue)
    {
      minValue = readValue;
    }
  }
  // Subtract min from max
  result = ((maxValue - minValue) * 5) / 1024.0;
  return result;
}

CAYENNE_OUT_DEFAULT()
{
  Cayenne.virtualWrite(1, Wattage, "Potência", "W");
}

I’m sorry, I’m not very good at this as I am a begginer in arduino systems.
Thank you so much for the help and my apologies.

The code works but not correctly. When moving this line:

float Wattage = (230 * AmpsRMS) - 13;

From:

void loop ()
{
}

To before that, the Wattage variable does not “refresh”, it only sends one value to the cayenne widget on the dashboard. As I refered, the code that gives the Wattage values trough the ArduinoIDE trough the serial monitor, has the line of code I wrote above in void loop, because it is constantly looping to read the current values.

How can I solve this problem?

#include <CayenneMQTTESP8266.h>

// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.

#define CAYENNE_PRINT Serial
#define CAYENNE_DEBUG
#define VIRTUAL_CHANNEL 1

// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

const int sensorIn = A0;
int mVperAmp = 185; // use 185 for 5A, 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

float Wattage;

void setup() {
  pinMode(A0, INPUT);
  Serial.begin(115200);
  delay(10);
  Serial.println(F("Init…"));
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}
void loop()
{
  Voltage = getVPP();
  VRMS = (Voltage / 2.0) * 0.707; // sq root
  AmpsRMS = (VRMS * 1000) / mVperAmp;
  Wattage = (230 * AmpsRMS) - 13; //Observed 18-20 Watt when no load was connected, so 
 substracting offset value to get real consumption.
  Serial.print(AmpsRMS);
  Serial.println(" Amps RMS ");
  Serial.print(Wattage);
  Serial.println(" Watt ");
  Cayenne.loop();
}
float getVPP()
{
  float result;
  int readValue; //value read from the sensor
  int maxValue = 0; // store max value here
  int minValue = 1024; // store min value here
  uint32_t start_time = millis();
  while ((millis() - start_time) < 1000) //sample for 1 Sec
  {
    readValue = analogRead(sensorIn);
    // see if you have a new maxValue
    if (readValue > maxValue)
    {
      maxValue = readValue;
    }
    if (readValue < minValue)
    {
      minValue = readValue;
    }
  }
  // Subtract min from max
  result = ((maxValue - minValue) * 5) / 1024.0;
  return result;
}

CAYENNE_OUT_DEFAULT()
{
  Cayenne.virtualWrite(1, Wattage, "Potência", "W");
}

Working perfectly!
Thank you so much for your patience and help