-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcanvasjs.ino
123 lines (99 loc) · 2.73 KB
/
canvasjs.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
/*
* Concebut i recuinat per Jordi Orts (http://jorts.net)
* A la carpeta data s'utilitza llibreria canvas.js amb llicencia CC, https://canvasjs.com/
*
* Si us plau, respecteu aquestes llicencies
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <FS.h>
#include <WEMOS_DHT12.h>
DHT12 dht12;
const char *ssid = "Aquí el nom de la teva xarxa";
const char *password = "Aquí la contrasenya de la xarxa";
ESP8266WebServer server ( 80 );
const int led = BUILTIN_LED;
int t;
void handleRoot() {
digitalWrite ( led, LOW );
char temp[400];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf ( temp, 400,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>ESP8266 Demo</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>Sonda de temperatura amb ESP8266</h1>\
<p><a href='canvasjs/index.html'>canvas</a><p/>\
<p>Uptime: %02d:%02d:%02d</p>\
</body>\
</html>",
hr, min % 60, sec % 60
);
server.send ( 200, "text/html", temp );
digitalWrite ( led, HIGH );
}
void handleNotFound() {
digitalWrite ( led, LOW );
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for ( uint8_t i = 0; i < server.args(); i++ ) {
message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
}
server.send ( 404, "text/plain", message );
digitalWrite ( led, HIGH );
}
void handleAjax(){
digitalWrite ( led, LOW );
dht12.get();
t=dht12.cTemp;
Serial.print("Temperature: ");
Serial.println(t);
String out = "";
char temp[100];
snprintf(temp, 100, "%d", t);
out += temp;
server.send ( 200, "text/txt", out);
digitalWrite ( led, HIGH );
}
void setup ( void ) {
pinMode ( led, OUTPUT );
digitalWrite ( led, HIGH );
Serial.begin ( 115200 );
WiFi.begin ( ssid, password );
SPIFFS.begin();
Serial.println ( "" );
// Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
server.on ( "/", handleRoot );
server.serveStatic("/canvasjs", SPIFFS, "/canvasjs"); // canvas files dir
server.on ("/ajax_info.txt", handleAjax );
server.onNotFound ( handleNotFound );
server.begin();
Serial.println ( "HTTP server started" );
}
void loop ( void ) {
server.handleClient();
}