-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_temp_read.ino
86 lines (73 loc) · 2.1 KB
/
basic_temp_read.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
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <OneWire.h>
#include <DallasTemperature.h>
const char* ssid = "********";
const char* password = "********";
char domain[] = "emoncms.org";
char apikey[] = "********";
#define SLEEP_DELAY_IN_SECONDS 600
#define ONE_WIRE_BUS D4 // DS18B20 pin
// D0 is connected to RST to wake from deep sleep
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
WiFiClient client;
unsigned long old;
char temperatureString[6];
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// setup OneWire bus
DS18B20.begin();
}
float getTemperature() {
Serial.println("Requesting DS18B20 temperature...");
float temp;
do {
DS18B20.requestTemperatures();
temp = DS18B20.getTempCByIndex(0);
delay(100);
} while (temp == 85.0 || temp == (-127.0));
return temp;
}
void loop() {
char c;
float temperature = getTemperature();
// convert temperature to a string with two digits before the comma and 2 digits for precision
dtostrf(temperature, 2, 2, temperatureString);
// send temperature to the serial console
Serial.print("Sending temperature: ");
Serial.println(temperatureString);
delay(100);
Serial.println("connect to Server...");
if( client.connect(domain, 80) )
{
Serial.println("connected");
client.print("GET /input/post.json?json={temp:");
client.print(temperatureString);
client.print("}&apikey=");
client.println(apikey);
old = millis();
while( (millis()-old)<500 ) // 500ms timeout for 'ok' answer from server
{
while( client.available() )
{
c = client.read();
Serial.write(c);
}
}
client.stop();
Serial.println("\nclosed");
}
Serial.println("Entering deep sleep mode");
ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
delay(500); // wait for deep sleep to happen
}