-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemperatureServer.ino
134 lines (100 loc) · 2.63 KB
/
TemperatureServer.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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ArduinoJson.h>
#include <FS.h>
#define ONE_WIRE_BUS 14
const char* ssid;
const char* password;
const char* hostname;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
ESP8266WebServer server(80);
void setup() {
Serial.begin(9600);
SPIFFS.begin();
loadConfig();
setupWiFi();
setupWebServer();
sensors.begin();
}
void loop() {
server.handleClient();
}
boolean loadConfig() {
File configFile = SPIFFS.open("/config.json", "r");
if(!configFile) {
Serial.println("Failed to open config file");
}
size_t size = configFile.size();
if(size > 1024) {
Serial.println("Config file is too large");
return false;
}
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
if(!json.success()) {
Serial.println("Failed to parse config file");
return false;
}
ssid = json["ssid"];
password = json["password"];
hostname = json["hostname"];
return true;
}
void setupWiFi() {
Serial.println("Connecting to wifi...");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Connected to: ");
Serial.println(ssid);
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
if(MDNS.begin(hostname)) {
Serial.println("mDNS responder started");
}
}
void setupWebServer() {
server.on("/", handleRoot);
server.on("/fahrenheit", handleFahrenheit);
server.on("/celsius", handleCelsius);
server.on("/app.js", handleScript);
server.onNotFound([] {
});
server.begin();
}
void handleRoot() {
File file = SPIFFS.open("/index.html", "r");
server.streamFile(file, "text/html");
file.close();
}
void handleScript(){
File file = SPIFFS.open("/app.js", "r");
server.streamFile(file, "application/javascript");
file.close();
}
void handleFahrenheit() {
char buffer[23];
sensors.requestTemperatures();
float temp = sensors.getTempFByIndex(0);
sprintf(buffer, "{\"temperature\": %3.2f}", temp);
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "application/json", buffer);
}
void handleCelsius(){
char buffer[23];
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
sprintf(buffer, "{\"temperature\": %3.2f}", temp);
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "application/json", buffer);
}