-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightLevel.ino
129 lines (96 loc) · 2.81 KB
/
LightLevel.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
// https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <WiFiManager.h>
WiFiManager wifiManager;
String MAC = "";
#if defined ESP32
#include <HTTPClient.h>
#elif defined ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#else
#error An ESP8266/ESP32 is required to build this code
#endif
HTTPClient http;
#include <WiFiClient.h>
WiFiClient wclient;
// https://github.com/scottchiefbaker/ESP-WebOTA
#include <WebOTA.h>
//////////////////////////////////////////////////////////////////////////////////////
int pin_max = 1; // Store the max calibrated value
const int apin = 34; // Needs to not be on ADC2 https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/
//////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
webota.delay(500);
// Connect to the WiFi or go into "portal" mode to let you set it
wifiManager.autoConnect();
// Get the MAC address of the WiFi
MAC = WiFi.macAddress();
// Set the analog pin to input
pinMode(apin, INPUT);
// Calculate the highest light level
calibrate();
char msg[200];
snprintf(msg, 200, "%s boot up... Calibrated max at %d", MAC.c_str(), pin_max);
log_message(msg);
Serial.print("Starting monitor\r\n");
}
void loop() {
static bool last_report = true;
static bool first = true;
int level = analogRead(apin);
int i_percent = ((level * 100) / (pin_max * 1));
if (i_percent > 100) {
i_percent = 100;
}
Serial.printf("Level: %d (%d%%)\r\n", level, i_percent);
// It's "on" if it's higher than 50%
bool is_on = (level > (pin_max / 2));
// If the on/off status has changed we phone home
if (!first && (is_on != last_report)) {
phone_home(is_on);
last_report = is_on;
}
webota.handle();
webota.delay(200);
first = false;
}
int phone_home(bool is_on) {
Serial.printf("Phoning home\r\n");
char url[100] = "";
if (is_on) {
snprintf(url, 100, "http://scott.web-ster.com/api/lights.php?status=on&mac=%s", MAC.c_str());
} else {
snprintf(url, 100,"http://scott.web-ster.com/api/lights.php?status=off&mac=%s", MAC.c_str());
}
http.begin(wclient, url);
int httpCode = http.GET();
//String payload = http.getString();
http.end();
return 1;
}
// Read five light levels and use the max as the ceiling
void calibrate() {
for (int i = 0; i < 5; i++) {
int level = analogRead(apin);
if (level > pin_max) {
Serial.printf("Calibrating max to: %d\r\n", level);
pin_max = level;
}
webota.delay(1000);
}
}
int log_message(const char* msg) {
char url[500] = "";
snprintf(url, 500, "http://scott.web-ster.com/api/lights.php?msg=%s", msg);
for (int i = 0; i < 500; i++) {
if (url[i] == ' ') {
url[i] = '+';
}
}
http.begin(wclient, url);
int httpCode = http.GET();
//String payload = http.getString();
http.end();
return 1;
}