Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
140 lines (117 loc) · 3.76 KB
/
Copy pathmain.cpp
File metadata and controls
140 lines (117 loc) · 3.76 KB
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
/*
This program is written for an ESP 32 which can be installed with ease into any electricity meter to measure the number of flashes made and calculate the usage over time.
The data is stored in flashes.log on the local IP and can then be further accessed.
From the file stored on the file, which includes the length of each impulse it is very easy to track any garbage readings since all of the flashes from the meter are more or less the same time duration with very small margin of error.
You'll notice that the loop is empty and that everything is done through the ReadADC ticker. this was just a convenient choice for CPU usage optimization.
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <FS.h>
#include <time.h>
#include <LittleFS.h>
#include <ESPAsyncWebServer.h>
#include <Ticker.h>
//insert WiFi and password below
const char* ssid = "";
const char* password = "";
long LastFlash = millis();
int Pin = 4;
int A;
int lastA;
int StartTime;
int OnTime;
int ThisTime;
int LastTime;
int Watt;
int State;
AsyncWebServer server(80);
Ticker ReadADC;
void CheckADC()
{
A = analogRead(A0);
if (A > 150)
{
if ( State == 0)
{
State = 1;
ThisTime = millis() - LastTime;
OnTime = millis() - StartTime;
Serial.print(millis());
Serial.print(" ");
Serial.print(OnTime);
Serial.print(" ");
Serial.print(ThisTime);
Serial.print(" ");
Serial.print(A);
Watt = 3600000 / (ThisTime);
Serial.print(" Watt:");
Serial.println(Watt);
LastTime = StartTime;
StartTime = 0;
File file = LittleFS.open("/flashes.log", "a");
if (!file) {
Serial.println("Failed to open file for appending");
return;
}
file.print(millis());
file.print("\r\n");
file.close();
}
}
else
{
if (State == 1)
{
StartTime = millis();
State = 0;
}
}
}
void setup() {
Serial.begin(19200);
Serial.println("Flash meter");
State = 1;
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(++i); Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
Serial.println("Connected.");
///////////////////////////////////////////////////////////////////////////////////////////////////////
configTime(3600 * 2, 0, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
struct tm tmstruct;
delay(2000);
tmstruct.tm_year = 0;
getLocalTime(&tmstruct, 5000);
Serial.printf("\nNow is : %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
////////////////////////////////////////////////////////////////////////////////////////////////////////
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(LittleFS, "/index.html", "text/html"); });
server.on("/flashes.log", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(LittleFS, "/flashes.log", "text/html"); });
server.on("/clear", HTTP_GET, [](AsyncWebServerRequest *request)
{ LittleFS.remove("/flashes.log");
request->send(200, "Done", "Cleared. <a href=/flashes.log>Continue</a>"); });
/////////////////////////////////////////////////////////////////////////////////////////////////////////
server.begin();
if (!LittleFS.begin()) {
Serial.println("LittleFS mount failed");
LittleFS.end();
LittleFS.format();
}
if (!LittleFS.begin()) {
Serial.println("LittleFS mount failed again");
}
ReadADC.attach_ms(5, CheckADC);
}
void loop() {
delay(1);
}