Code For Arduino
#include "ACS712.h"
char watt[5];
ACS712 sensor(ACS712_30A, A0);
unsigned long last_time =0;
unsigned long current_time =0;
float Wh =0 ;
void setup() {
[Link](115200);
[Link]();
}
void loop() {
float V = 230;
float I = [Link]();
// [Link](I);
float P = V * I;
last_time = current_time;
current_time = millis();
Wh = Wh+ P *(( current_time -last_time) /3600000.0) ;
dtostrf(Wh, 4, 2, watt);
[Link](watt);
delay(10000);
}
Code For NodeMCU
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define WLAN_SSID "a*************"
#define WLAN_PASS "*******************"
char watt[5];
#define AIO_SERVER "[Link]"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "rjrishabh"
#define AIO_KEY "***********************"
WiFiClient client;
int bill_amount = 0;
unsigned int energyTariff = 8.0;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME,
AIO_KEY);
Adafruit_MQTT_Publish Power = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Power");
Adafruit_MQTT_Publish bill = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/bill");
void MQTT_connect();
void setup() {
[Link](115200);
delay(10);
[Link](F("Adafruit MQTT demo"));
// Connect to WiFi access point.
[Link](); [Link]();
[Link]("Connecting to ");
[Link](WLAN_SSID);
[Link](WLAN_SSID, WLAN_PASS);
while ([Link]() != WL_CONNECTED) {
delay(500);
[Link](".");
}
[Link]();
[Link]("WiFi connected");
[Link]("IP address: "); [Link]([Link]());
}
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
int i=0;
float watt1;
if([Link]() > 0 ){
delay(100); //allows all serial sent to be received together
while([Link]() && i<5) {
watt[i++] = [Link]();
}
watt[i++]='\0';
}
watt1 = atof(watt);
bill_amount = watt1 * (energyTariff/1000); // 1unit = 1kwH
[Link](F("\nSending Power val "));
[Link](watt1);
[Link]("...");
if (! [Link](watt1)) {
[Link](F("Failed"));
} else {
[Link](F("OK!"));
}
if (! [Link](bill_amount)) {
[Link](F("Failed"));
} else {
[Link](F("OK!"));
}
if (bill_amount==4){
for (int i =0; i<=2; i++)
{
[Link](bill_amount);
delay(5000);
}
bill_amount =6;
}
delay(5000);
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if ([Link]()) {
return;
}
[Link]("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = [Link]()) != 0) { // connect will return 0 for connected
[Link]([Link](ret));
[Link]("Retrying MQTT connection in 5 seconds...");
[Link]();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
[Link]("MQTT Connected!");
}