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.
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 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?
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
// 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 = (230AmpsRMS)-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;
}
}
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 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
// 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.
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;
#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");
}
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.
#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");
}