-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTempSensor.cpp
More file actions
199 lines (163 loc) · 4.7 KB
/
TempSensor.cpp
File metadata and controls
199 lines (163 loc) · 4.7 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
extern "C" {
#include "user_interface.h" // Required for wifi_station_connect() and RTC read/write to work
}
#include <OneWire.h>
#include <ESP8266WiFi.h>
#define FPM_SLEEP_MAX_TIME 0xFFFFFFF
OneWire ds(4); // Temp on D1
IPAddress staticIP(192,168,1,203);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
IPAddress hubIP(192,168,1,14);
const unsigned int hubPort = 39500;
//Include wifi login info here
const char* wifiSSID = "placeholder";
const char* wifipassword = "placeholder";
const int refreshTime = 60; //Check temp every X seconds
const int maxRefreshCycles = 10; //Force update every x refreshes
float minTempDelta = 1.0; //Minimum deltaTemp to update
typedef struct {
byte realFlag;
int refreshCount;
float oldtemp;
} rtcStore;
void setup(void) {
byte addr[8];
Serial.begin(115200);
Serial.setTimeout(2000);
//Wait for serial
while(!Serial) {}
Serial.println();
Serial.println("I'm awake");
rtcStore rtcMem;
bool updateTemp = false;
system_rtc_mem_read(64, &rtcMem, sizeof(rtcMem)); //Get saved memory from RTC
//Tell sensor to get temp reading
startTempRead(addr);
delay(800);
float currenttemp = readSensor(addr);
Serial.print("Current temp is ");
Serial.println(currenttemp);
Serial.print("Last sent temp is ");
Serial.println(rtcMem.oldtemp);
Serial.print("Last transmitted ");
Serial.print(rtcMem.refreshCount);
Serial.println(" refreshes ago");
//Use 126 as marker to check if this is the first boot
if (rtcMem.realFlag != 126) {
rtcMem.realFlag = 126;
rtcMem.refreshCount = 0;
Serial.println("First boot detected");
updateTemp = true;
}
if (~updateTemp && (abs(currenttemp - rtcMem.oldtemp) >= minTempDelta)) {
Serial.println("Temperature delta reached");
updateTemp = true;
}
if (~updateTemp && (rtcMem.refreshCount >= maxRefreshCycles)) {
Serial.println("Max refreshes reached");
updateTemp = true;
}
if (updateTemp) {
rtcMem.oldtemp = currenttemp;
rtcMem.refreshCount = 0;
Serial.println("Preparing to send temp to ST");
//Connect to wifi
WiFiOn();
WifiConnect();
WiFiClient st_client;
String message = String(currenttemp);
message = String(message + ',');
if (st_client.connect(hubIP, hubPort)) { //Connect to hub. Return false if connect failed
Serial.print("Sending ");
Serial.println(message);
st_client.println(F("POST / HTTP/1.1"));
st_client.print(F("HOST: "));
st_client.print(hubIP);
st_client.print(F(":"));
st_client.println(hubPort);
st_client.println(F("CONTENT-TYPE: text"));
st_client.print(F("CONTENT-LENGTH: "));
st_client.println(message.length());
st_client.println();
st_client.println(message);
}
else {
Serial.println("Failed to connect to ST");
}
}
else {
rtcMem.refreshCount += 1;
Serial.println("No reason to update. Going back to sleep");
}
WiFiOff();
system_rtc_mem_write(64, &rtcMem, sizeof(rtcMem)); //Write memory to RTC
Serial.println("Going to sleep...");
Serial.println();
ESP.deepSleep(1e6 * refreshTime, WAKE_RF_DEFAULT);
}
void loop() {}
bool startTempRead(byte addr[8]) {
ds.reset_search();
if (!ds.search(addr)) {
Serial.println("No sensors found");
return false;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1);
pinMode(4, OUTPUT);
return true;
}
float readSensor(byte addr[8]) {
byte data[12];
pinMode(4, INPUT);
ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
data[0] = ds.read();
data[1] = ds.read();
ds.reset();
int16_t raw = (data[1] << 8) | data[0];
float fahrenheit = ((float)raw / 16.0)*9.0/5.0+32.0;
//Serial.print(" Temperature = ");
//Serial.print(fahrenheit);
//Serial.println(" Fahrenheit");
return fahrenheit;
}
void WifiConnect() {
Serial.print("Connecting");
WiFi.begin(wifiSSID, wifipassword);
WiFi.config(staticIP, gateway, subnet);
while (WiFi.status() != WL_CONNECTED)
{
if (WiFi.status() == WL_CONNECT_FAILED) {
Serial.println();
Serial.println("Failed to connect to WiFi.");
return;
}
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
Serial.print("MAC Addr: ");
Serial.println(WiFi.macAddress());
}
void WiFiOn() {
wifi_fpm_do_wakeup();
wifi_fpm_close();
//Serial.println("Reconnecting");
wifi_set_opmode(STATION_MODE);
wifi_station_connect();
}
void WiFiOff() {
//Serial.println("diconnecting client and wifi");
//client.disconnect();
wifi_station_disconnect();
wifi_set_opmode(NULL_MODE);
wifi_set_sleep_type(MODEM_SLEEP_T);
wifi_fpm_open();
wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME);
}