-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP8266LivingSensor.ino
144 lines (115 loc) · 3.67 KB
/
ESP8266LivingSensor.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
#include <Arduino.h>
#include <user_interface.h>
#include "MySensors.h"
#include "NetworkManager.h"
#include "DebugMacros.h"
#define SENSORS_WAIT_MS 500 // a half minute
#define LOOP_INTERVAL_MS (3 * 60 * 1000UL) // 3 minutes
static void displayChipInformation(void);
static MySensors sensors;
static NetworkManager netMan;
static bool isBooted;
/*-----------------------------------------------------------------------------------------------*/
void setup()
{
isBooted = !(ESP.getResetInfoPtr()->reason == rst_reason::REASON_DEEP_SLEEP_AWAKE);
#ifdef DEBUG || DEBUG_HTTPS_REDIRECT
Serial.begin(SERIAL_BAUD_RATE);
if (isBooted) {
displayChipInformation();
MySensors::scanI2C();
}
#endif
}
void loop()
{
sensors.activate();
delay(SENSORS_WAIT_MS - millis());
RECORD_T record;
bool isSuccess = sensors.doSensing(record);
sensors.inactivate();
if (!isSuccess) {
dprintln(F("Failed to sense..."));
goto bail;
}
isSuccess = netMan.setupWiFi();
if (isSuccess) {
String recordsPayload = "[" + MySensors::convert2JSON(record) + "]";
isSuccess = netMan.uploadRecords(recordsPayload);
if (!isSuccess) {
dprintln(F("Failed to upload..."));
}
} else {
dprintln(F("Failed to setup Wi-Fi..."));
}
bail:
long spentTime = millis();
dprint("Spent Time (ms)=");
dprintln(spentTime);
if (isSuccess) {
dprintln(F("Deep sleep."));
ESP.deepSleep((LOOP_INTERVAL_MS - spentTime) * 1000UL, WAKE_RF_DEFAULT);
} else {
dprintln(F("Restart!"));
ESP.restart();
}
delay(1000);
dprintln(F("Why can you came here?"));
}
static void displayChipInformation(void)
{
dprintln(F("----- ESP-WROOM-02 ( ESP8266 ) Chip Infomation -----"));
dprintln();
dprint(F("Core Version = "));
dprintln(ESP.getCoreVersion());
dprint(F("CPU Frequency = "));
dprint(ESP.getCpuFreqMHz());
dprintln(F(" MHz"));
dprint(F("ChipID = "));
dprintln(ESP.getChipId(), HEX);
dprint(F("Flash ID = "));
dprintln(ESP.getFlashChipId(), HEX);
dprint(F("SDK version = "));
dprintln(ESP.getSdkVersion());
dprint(F("Boot version = "));
dprintln(ESP.getBootVersion());
dprint(F("Boot Mode = "));
dprintln(ESP.getBootMode());
dprint(F("Flash Chip IDE Size = "));
dprint(ESP.getFlashChipSize());
dprintln(F(" byte"));
dprint(F("Flash Chip Real Size = "));
dprint(ESP.getFlashChipRealSize());
dprintln(F(" byte"));
dprint(F("Flash Frequency = "));
dprint(ESP.getFlashChipSpeed());
dprintln(F(" Hz"));
const __FlashStringHelper *mode_str;
switch (ESP.getFlashChipMode()) {
case 0: mode_str = F("QIO"); break;
case 1: mode_str = F("QOUT"); break;
case 2: mode_str = F("DIO"); break;
case 3: mode_str = F("DOUT"); break;
case 4: mode_str = F("Unknown");break;
default: break;
}
dprint(F("Flash Chip Mode = "));
dprintln(mode_str);
dprint(F("Free Heap Size = "));
dprintln(ESP.getFreeHeap());
dprint(F("Free Sketch Size = "));
dprintln(ESP.getFreeSketchSpace());
dprint(F("Sketch Size = "));
dprintln(ESP.getSketchSize());
uint8_t mac[6];
WiFi.macAddress(mac);
dprint(F("WiFi StationAP Mac Address = "));
dprintf("%02X:%02X:%02X:%02X:%02X:%02X\r\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
WiFi.softAPmacAddress(mac);
dprint(F("WiFi SoftAP Mac Address = "));
dprintf("%02X:%02X:%02X:%02X:%02X:%02X\r\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
dprintln();
dprintln();
}