Nurgak / Internet-of-Things-Power-Meter

Device to view household power consumption
https://hackaday.io/project/6938-internet-of-things-power-meter
36 stars 21 forks source link

Refactoring with Node-RED and MQTT #2

Closed c0zm0z closed 6 years ago

c0zm0z commented 6 years ago

Hello Nurgak,

i have testet your old IPM. I love how you write code.

Is there any updated code for "Refactoring with Node-RED and MQTT" I am very interested in in. i have some small MQTT experience, but i have never used Node-RED before. It looks interesting.

Nurgak commented 6 years ago

@c0zm0z I uploaded the MQTT version: https://github.com/Nurgak/Internet-of-Things-Power-Meter/tree/master/Software/IoTPowerMeterMQTT

Node-RED is something that can really tie everything together. Now I log the data in the Node-RED server instead of the device SD card (less parts and not risking of running out of space) or remotely to Google Spreadsheets. This ensures that even if there is no access to the internet the data is saved.

I've made another project that shows the power consumption thanks to Node-RED: https://github.com/Nurgak/IoT-RGB-LED-Matrix

dony71 commented 5 years ago

I read your article about node-red and mqtt below but don't understand how you create calculation in node-red Would you mind to share how you configure this in node-red? I couldn't find icon output "instant power", "cost", etc, so if you don't mind share node-red flow in github so that can import directly?

https://hackaday.io/project/6938-internet-of-things-power-meter/log/66881-refactoring-with-node-red-and-mqtt

Nurgak commented 5 years ago

@dony71 The instant power is not calculated, you need to subscribe to the relevant MQTT topic. In the case of instant power the MQTT topic is defined as "powerCounterNow": https://github.com/Nurgak/Internet-of-Things-Power-Meter/blob/e39c132d896e99e5e8b9e577e6842b63cb67a39d/Software/IoTPowerMeterMQTT/IoTPowerMeterMQTT.ino#L420

The "mqtt in" node configuration would look like this: screenshot 2019-02-02 at 11 15 23

As far as the cost calculator goes I cannot really help, it depends on your provider tariffs and the model you have. In my case there is a full price and low price depending on the hour of the day and if it's a week-end. That makes a long function that is only relevant to me. Basically all I do it multiply the power cost by the instant usage and I get the instant cents/hour number, nothing really fancy.

dony71 commented 5 years ago

cost calculation here in USA is about the same rate kW/h for certain hour of the day if cheaper (for ex. during working hour) and also weekend rate will be different too. My question for this is how do you get the time while power consume? does mqtt reporting the time as well? also how about function extract power for each of them? what is this function equation?

Nurgak commented 5 years ago

@dony71 I really suggest you read more about Node-RED functions. The function node in Node-RED is a blank JavaScript canvas, if you want the hour of the day you can do the following:

var d = new Date();
var n = d.getHours();

The time is that of your MQTT server.

The "extract power" function your refer to is just a parser, as the incoming data is in the format of minute,ticks: https://github.com/Nurgak/Internet-of-Things-Power-Meter/blob/e39c132d896e99e5e8b9e577e6842b63cb67a39d/Software/IoTPowerMeterMQTT/IoTPowerMeterMQTT.ino#L308

The Node-RED function contains the following code:

var data = msg.payload.split(",");
var power = data[1];
msg.payload = power

return msg;

As this is going off-topic I'm locking this conversation. I suggest you make separate issues for every question, it makes it easier to answer.