-
Notifications
You must be signed in to change notification settings - Fork 1
/
power_meter_v0.ino
168 lines (139 loc) · 4.4 KB
/
power_meter_v0.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <time.h>
#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
// Update these with values suitable for your network.
const char* ssid = "MEO"; //your WiFI SSID
const char* password = "*******"; //Your WiFi password
const char* mqtt_server = "192.168.1.201"; //Your MQTT broker IP
#define mqtt_port 1883
#define MQTT_USER "" //Fill in case your MQTT broker has user
#define MQTT_PASSWORD "" //Fill in case your MQTT broker has user
#define MQTT_PUBLISH1 "home/clamp/1"
#define MQTT_RECEIVER "home/powerMeter/1"
#define MQTT_PUBLISH "home/factor/1"
//float t = 0;
char* hour_and_date;
int pfPin = 5; // pin D1, GPIO5
//CLAMP in A0; //ADC pin for the clamp
int period = 10000; //delay between samples in ms
int i = 0;
int TimeOut = 1000; //timeout until the connection to the broker is done.
//This is the number of loops. If period = 10000 (10s), then the timeout will be 1000*10s = 2h46m
unsigned long time_now = 0;
WiFiClient wifiClient;
PubSubClient client(wifiClient);
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//Connect to NTP and acquire time
configTime(1 * 3600, 1, "pool.ntp.org", "time.nist.gov");
Serial.println("\nWaiting for time");
while (!time(nullptr)) {
Serial.print(".");
delay(500);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str(),MQTT_USER,MQTT_PASSWORD)) {
Serial.println("connected");
//Once connected, publish an announcement...
time_t now = time(nullptr);
hour_and_date = ctime(&now);
publishSerialData1(hour_and_date);
publishSerialData1("home/watertemp/1 is online");
// ... and resubscribe
client.subscribe(MQTT_RECEIVER);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void callback(char* topic, byte *payload, unsigned int length) {
Serial.println("-------new message from broker-----");
Serial.print("channel:");
Serial.println(topic);
Serial.print("data:");
Serial.write(payload, length);
Serial.println();
payload[length]=0;
}
void setup() {
Serial.begin(115200);
Serial.setTimeout(500);
emon1.current(A0, 97); // Current: input pin, calibration.
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
reconnect();
}
void publishSerialData1(char *serialData){
if (!client.connected()) {
reconnect();
}
client.publish(MQTT_PUBLISH1, serialData);
}
void publishSerialData2(char *serialData){
if (!client.connected()) {
reconnect();
}
client.publish(MQTT_PUBLISH, serialData);
}
void readsensors(){
String dataString;
double Irms = emon1.calcIrms(1480); // Calculate Irms only
//Serial.print(Irms*230.0); // Apparent power
//Serial.print(" ");
//Serial.println(Irms); // Irms
//int h = dht.readHumidity();
//int t2 = dht.readTemperature();
//int clamp = analogRead(clampPin);
//int pf = digitalRead(pfPin);
char copy[3];
//CLAMP
//transform the data into a string and send it
dataString = String(Irms*230.0);
//SEND DATA TO MQTT as VA
dataString.toCharArray(copy, 32);
publishSerialData1(copy);
//dataString = String(ca)+","+String(pf);
Serial.println(dataString);
}
void loop() {
client.loop();
if(millis() >= time_now + period){
time_now += period;
readsensors();
i++;
//Serial.print("i ");
//Serial.println(i);
if (i>TimeOut){
reconnect();
i=0;
}
}
}