Skip to content

Instantly share code, notes, and snippets.

@vladakru
Last active April 23, 2020 17:49
Show Gist options
  • Save vladakru/f272ea616d9501bfe22116e632bed091 to your computer and use it in GitHub Desktop.
Save vladakru/f272ea616d9501bfe22116e632bed091 to your computer and use it in GitHub Desktop.
Sketch za NodeMCU ESP8266 - Home Energy Monitor - Thingsboard work!
// Code works on Thingsboard, sends telemetry data. //
// Thanks ItKindaWorks on the source code. //
#include <PubSubClient.h>
#include "ESPHelper.h"
#include <Metro.h>
#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <SoftwareSerial.h>
SoftwareSerial mySerial1(D3,D2); /* RX:D3, TX:D2 */
#define AVG_COUNT 50
const char* voltTopic = "/v1/devices/me/telemetry/volt";
const char* ampTopic = "/v1/devices/me/telemetry/amp";
const char* wattTopic = "/v1/devices/me/telemetry/watt";
const char* hostnameStr = "NodeMCU";
const char* otaPass = "vladaall";
netInfo homeNet = {
.mqttHost = "demo.thingsboard.io", //can be blank if not using MQTT
.mqttUser = "ThingsBoard Access Token", //can be blank(Here enter ThingsBoard Access Token)
.mqttPass = "", //can be blank
.mqttPort = 1883, //default port for MQTT is 1883 - only chance if needed.
.ssid = "WiFI SSID", //wifi
.pass = "PASSWORD"}; //password
ESPHelper myESP(&homeNet);
Metro powerMetro = Metro(5000);
void setup() {
Serial.begin(115200);
mySerial1.begin(9600);
myESP.OTA_enable();
myESP.OTA_setPassword(otaPass);
myESP.OTA_setHostnameWithVersion(hostnameStr);
myESP.setHopping(false);
myESP.begin();
WiFiManager wifiManager;
wifiManager.autoConnect(":-)", "123456789");
}
void loop(){
int count = 0;
//where to store the data to be averaged
double watts[AVG_COUNT];
double volts[AVG_COUNT];
double amps[AVG_COUNT];
//vars to maintain averages for all data points
double wattAvg = 0;
double voltAvg = 0;
double ampAvg = 0;
//the serial buffer of 64 bytes
char serialBuf[64];
while(1){
//reset the count when we hit the max. The average acts and a rolling average
if(count >= AVG_COUNT){
count = 0;
}
// listen for communication from the ESP8266 and then write it to the serial monitor
while(mySerial1.available())
{
// '*' marks the beginning of a transmission
bool start = mySerial1.find('*');
//parse out the floats
if(start){
volts[count] = mySerial1.parseFloat();
amps[count] = mySerial1.parseFloat();
watts[count++] = mySerial1.parseFloat();
break;
}
delay(1);
}
//calculate averages
wattAvg = 0;
ampAvg = 0;
voltAvg = 0;
for(int i = 0; i < AVG_COUNT; i++){
wattAvg += watts[i];
voltAvg += volts[i];
ampAvg += amps[i];
}
wattAvg /= AVG_COUNT;
ampAvg /= AVG_COUNT;
voltAvg /= AVG_COUNT;
if (wattAvg < 3.50){
wattAvg = 0;
}
//only send the data every so often (set by the metro timer) and only when connected to WiFi and MQTT
if(myESP.loop() == FULL_CONNECTION && powerMetro.check()){
if (ampAvg <= 0.03){
ampAvg = 0;
}
String payload = "{"; //Starting payload //
//post just watts
char wattStr[10];
dtostrf(wattAvg,4,1,wattStr);
payload += "\"Watts\":"; payload += wattStr; payload += ",";
delay(5);
//post just volts
char voltStr[10];
dtostrf(voltAvg,4,1,voltStr);
payload += "\"Volts\":"; payload += voltStr; payload += ",";
delay(5);
//post just amps
char ampStr[10];
dtostrf(ampAvg,4,1,ampStr);
payload += "\"Amps\":"; payload += ampStr; payload += "}";
delay(5);
mySerial1.println(payload); //End payload //
char attributes[100];
payload.toCharArray( attributes, 100 );
myESP.publish("v1/devices/me/telemetry", attributes, true );
mySerial1.println( attributes );
Serial.println( attributes );
}
yield();
}
}
void callback(char* topic, uint8_t* payload, unsigned int length) {
}
@BullsEye34
Copy link

Is it connecting to WiFi?

@vladakru
Copy link
Author

Yes. Connect yo wifi and connect to thingsboard and send data to telemetry on thingsboard.

@BullsEye34
Copy link

So it's working?

@vladakru
Copy link
Author

Yes, but now its problem with corect reading data from arduino....

@BullsEye34
Copy link

Same problem I'm facing

@vladakru
Copy link
Author

I tried the code that loads what comes to the serial port on the nodemcu and then I get the correct data from the arduino. I think the problem is in this powermonitoresp code in the part where it calculates the average ...

@BullsEye34
Copy link

Or it might also be the board wiring, where the data is sent and received, and between, there's a voltage divider.....

And I think it should be powered with 3.3V and not 5V

@vladakru
Copy link
Author

Nodemcu is powered by 5V. only if a voltage divider needs to be placed on the rx pin. I'll try these days, and we'll talk about it. Tell me if you can do something. Sensors are waiting to be installed, only codes make the problem ... Greeting...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment