forked from shroomeater/energymonitor_Wemos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenergymonitor.ino
166 lines (134 loc) · 4.57 KB
/
energymonitor.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
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>
//#include <bitBangedSPI.h>
//#include <MAX7219_Dot_Matrix.h>
#define OTA_UPDATES
#define OTA_HOSTNAME "POWER-MONITOR"
#define emonTxV3 // Wemos D1 mini tolerates up to 3.3V
#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
// 4 chips (display module), hardware SPI with load on D2
//MAX7219_Dot_Matrix display (4, 2); // Chips / LOAD
char message [] = "Booting...";
#define BLYNK_PRINT Serial
char auth[] = "c40e28d8880a4483a34c0d4e3672e715";
const char* ssid = "HUAWEI-E5172-E9FC";
const char* password = "0M65JFMA5BA";
unsigned long lastMoved = 0;
unsigned long MOVE_INTERVAL = 20; // mS
int messageOffset;
int adc = 0;
const long price = 0.6*24*30; // price kwh * 24 hours * 30 days
/*void updateDisplay ()
{
// display.sendSmooth (message, messageOffset);
// next time show one pixel onwards
if (messageOffset++ >= (int) (strlen (message) * 8))
messageOffset = - chips * 8;
} // end of updateDisplay
*/
int calcPower() {
double Irms = emon1.calcIrms(1480); // Calculate Irms only
Serial.print(analogRead(A0));
Serial.print(" ");
Serial.print(Irms*236.0); // Apparent power
Serial.print(" ");
Serial.println(Irms); // Irms
Serial.println();
char buf [20];
sprintf (buf, "%.0fWh", Irms*236 ); // I measured 236V on my mains
Serial.println(buf);
// display.sendString (buf);
return Irms*236;
}
void readPower()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V1, calcPower());
}
BLYNK_READ(V2)
{
// This command writes only when we ask data from it
Blynk.virtualWrite(V2, calcPower());
}
BLYNK_WRITE(V0)
{
double pinValue = param.asDouble(); // assigning incoming value from pin V0 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("Calibrate value is: ");
Serial.println(pinValue);
emon1.current(A0, pinValue);
}
void setup() {
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
emon1.current(A0, 23.4); // Current: input pin, calibration (2000/burden resistance)
// display.begin ();
// display.setIntensity (1);
// display.sendString("..");
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
// ArduinoOTA.setHostname("myesp8266");
// No authentication by default
// ArduinoOTA.setPassword("admin");
// Password can be set with it's md5 value as well
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//display.sendString("OK");
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
Blynk.begin(auth, ssid, password);
// Setup a function to be called every second
timer.setInterval(1000L, readPower);
}
void loop() {
ArduinoOTA.handle();
Blynk.run();
timer.run(); // Initiates BlynkTimer
//calcPower();
}