-
Notifications
You must be signed in to change notification settings - Fork 0
/
NODEMCUTEMP
335 lines (273 loc) · 7.76 KB
/
NODEMCUTEMP
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Includes
#include <ArduinoJson.h>
#include "DHT.h"
#include <fs.h>
#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
#include <Wire.h>
#define DHTPIN 12 // IMPORTANT D2 on NodeMCU is GPIO 4
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFiManager.h>
// Defintions
#define JSON_CONFIG_FILE "tempsettings.json"
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Global variables
bool shouldSaveConfig = true;
char apiKey[50] = "API KEY";
int chanNum = 1234;
int localHum = 0;
int localTemp = 0;
int rssi = 0;
const char *server = "api.thingspeak.com";
// Temp Sensor Initialize
DHT dht(DHTPIN, DHTTYPE);
// Display Initialize
SSD1306 display(0x3c, 4, 5); // Initialise the OLED display using Wire library
// WiFi Client Initialize
WiFiClient client;
// WiFi Manager Initialize
WiFiManager wm;
// Setup Function
void setup()
{
int x=0;
int y=0;
dht.begin();
ThingSpeak.begin(client);
// display
display.init();
display.flipScreenVertically();
display.clear();
display.drawString(0 + x, 0 + y, "Connect to");
display.drawString(0 + x, 15 + y, "'TempSensorSetup' for");
display.drawString(0 + x, 30 + y, "configuration");
display.drawString(0 + x, 45 + y, "at '192.168.4.1'");
display.display();
// Change to true when testing to force configuration every time we run
bool forceConfig = false;
bool spiffsSetup = loadConfigFile();
if (!spiffsSetup)
{
Serial.println(F("Forcing config mode as there is no saved config"));
forceConfig = true;
}
// Explicitly set WiFi mode
WiFi.mode(WIFI_STA);
// Setup Serial monitor
Serial.begin(115200);
delay(10);
// Reset settings (only for development)
//wm.resetSettings();
// Set config save notify callback
wm.setSaveConfigCallback(saveConfigCallback);
// Set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
wm.setAPCallback(configModeCallback);
// Custom elements
// Text box (String) - 50 characters maximum
WiFiManagerParameter api_key_text_box("key_text", "Enter your API Key", apiKey, 50);
// Need to convert numerical input to string to display the default value.
char convertedValue[30];
sprintf(convertedValue, "%d", chanNum);
// Text box (Number) - 30 characters maximum
WiFiManagerParameter chan_num_text_box("key_num", "Enter your channel number", convertedValue, 30);
// Add all defined parameters
wm.addParameter(&api_key_text_box);
wm.addParameter(&chan_num_text_box);
if (forceConfig)
// Run if we need a configuration
{
if (!wm.startConfigPortal("TempSensorSetup", "password"))
{
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.restart();
delay(5000);
}
}
else
{
if (!wm.autoConnect("TempSensorSetup", "password"))
{
Serial.println("failed to connect and hit timeout");
delay(3000);
// if we still have not connected restart and try all over again
ESP.restart();
delay(5000);
}
}
// If we get here, we are connected to the WiFi
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Lets deal with the user config values
// Copy the string value
strncpy(apiKey, api_key_text_box.getValue(), sizeof(apiKey));
Serial.print("API KEY: ");
Serial.println(apiKey);
//Convert the number value
chanNum = atoi(chan_num_text_box.getValue());
Serial.print("Channel Number: ");
Serial.println(chanNum);
// Save the custom parameters to FS
if (shouldSaveConfig)
{
saveConfigFile();
}
}
// Functions
// JSON SAVE
void saveConfigFile()
{
Serial.println("Saving Configuration...");
StaticJsonDocument<512> json;
json["apiKey"] = apiKey;
json["chanNum"] = chanNum;
File configFile = SPIFFS.open(JSON_CONFIG_FILE, "w");
if (!configFile)
{
Serial.println("Failed to open config file!");
}
serializeJsonPretty(json, Serial);
if (serializeJson(json, configFile) == 0)
{
Serial.println(F("Failed to write to file"));
}
configFile.close();
}
// JSON LOAD
bool loadConfigFile()
{
Serial.println("Mounting the FS");
if (SPIFFS.begin())
{
Serial.println("Mounted the FS");
if (SPIFFS.exists(JSON_CONFIG_FILE))
{
Serial.println("Reading Config");
File configFile = SPIFFS.open(JSON_CONFIG_FILE, "r");
if (configFile)
{
Serial.println("Openend!");
StaticJsonDocument<512> json;
DeserializationError error = deserializeJson(json, configFile);
serializeJsonPretty(json, Serial);
if (!error)
{
Serial.println("Parsing the JSON");
strcpy(apiKey, json["apiKey"]);
chanNum = json["chanNum"].as<int>();
return true;
}
else
{
Serial.println("Failed to load json Config");
}
}
}
}
else
{
Serial.println("Failed to mount FS");
}
return false;
}
void saveConfigCallback()
{
Serial.println("Should save config");
shouldSaveConfig = true;
}
void configModeCallback(WiFiManager *myWiFiManager)
{
Serial.println("Entered Configuration Mode");
Serial.print("Config SSID: ");
Serial.println(myWiFiManager->getConfigPortalSSID());
Serial.print("Config IP Address: ");
Serial.println(WiFi.softAPIP());
}
void updateThingSpeak(float t, float h)
{
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, h);
ThingSpeak.writeFields(chanNum, apiKey);
delay(20 * 1000);
}
void loop()
{
getDHT();
display.clear();
drawDHT();
getRSSI();
display.display();
delay(2000);
}
void getDHT()
{
float tempIni = localTemp;
float humIni = localHum;
localTemp = dht.readTemperature(true);
localHum = dht.readHumidity();
if (isnan(localHum) || isnan(localTemp)) // Check if any reads failed and exit early (to try again).
{
Serial.println("Failed to read from DHT sensor!");
localTemp = tempIni;
localHum = humIni;
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(localTemp, localHum);
Serial.print("Humidity: ");
Serial.print(localHum);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(localTemp);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hif);
Serial.println(" *F");
if (client.connect(server, 80))
{
updateThingSpeak(localTemp, localHum);
}
}
void drawDHT()
{
int x = 0;
int y = 0;
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0 + x, 40 + y, "WiFi Signal Strength: ");
display.setFont(ArialMT_Plain_10);
String myrssi = String(rssi) + "%";
display.drawString(102 + x, 40 + y, myrssi);
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0 + x, 0 + y, "Hum");
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(40 + x, y, "SERVERS");
display.setFont(ArialMT_Plain_24);
String hum = String(localHum) + "%";
display.drawString(0 + x, 15 + y, hum);
int humWidth = display.getStringWidth(hum);
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(100 + x, 0 + y, "Temp");
display.setFont(ArialMT_Plain_24);
String temp = String(localTemp) + "°F";
display.drawString(75 + x, 15 + y, temp);
int tempWidth = display.getStringWidth(temp);
display.setFont(ArialMT_Plain_10);
display.drawString(20 + x, 52 + y, "595");
}
void getRSSI()
{
float rssiIni = rssi;
rssi = WiFi.RSSI();
}