forked from squix78/esp8266-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thingspeak-data-logger.ino
121 lines (94 loc) · 2.6 KB
/
thingspeak-data-logger.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
/*
* This sketch sends data via HTTP GET requests to thingspeak service every 10 minutes
* You have to set your wifi credentials and your thingspeak key.
*/
#include <ESP8266WiFi.h>
extern "C" {
#include "user_interface.h"
}
#include "DHT.h"
#define DHTPIN 12 // what pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE, 15);
const char* ssid = "ssid";
const char* password = "yourpassword";
const char* host = "api.thingspeak.com";
const char* thingspeak_key = "yourthingspeakkey";
void turnOff(int pin) {
pinMode(pin, OUTPUT);
digitalWrite(pin, 1);
}
void setup() {
Serial.begin(115200);
// disable all output to save power
turnOff(0);
turnOff(2);
turnOff(4);
turnOff(5);
turnOff(12);
turnOff(13);
turnOff(14);
turnOff(15);
dht.begin();
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String temp = String(dht.readTemperature());
String humidity = String(dht.readHumidity());
String voltage = String(system_get_free_heap_size());
String url = "/update?key=";
url += thingspeak_key;
url += "&field1=";
url += temp;
url += "&field2=";
url += humidity;
url += "&field3=";
url += voltage;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection. going to sleep...");
delay(1000);
// go to deepsleep for 10 minutes
system_deep_sleep_set_option(0);
system_deep_sleep(10 * 60 * 1000000);
}