/
ESP8266_DHT__Pushingbox_GoogleSheets
126 lines (106 loc) · 3.74 KB
/
ESP8266_DHT__Pushingbox_GoogleSheets
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
//-----------------------------------------------
//This sketch is combined from Adafruit DHT sensor and tdicola
//for dht.h library
//Along with some esp8266 NodeMCU specifics from teos009
// https://learn.adafruit.com/dht/overview
// https://gist.github.com/teos0009/acad7d1e54b97f4b2a88
//other Authors Arduino and associated Google Script:
//Aditya Riska Putra
//Ahmed Reza Rafsanzani
//Ryan Eko Saputro
//See Also
//http://jarkomdityaz.appspot.com/
//
//ELINS UGM
//
//Modified for Hackster.io project to the ESP8266 from
//a Ethernet Sheild project
//by Stephen Borsay
//Since Arduino can't handle https, we need to use Pushingbox API
//(uses http)to run
//the Google Script (uses https).
#include "ESP8266WiFi.h"
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
//pin2 to D4 on esp board
// Uncomment whatever DHT sensor type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21
//#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN,DHTTYPE);
const char WEBSITE[] = "api.pushingbox.com"; //pushingbox API server
const String devid = "<YOUR_Devid_HERE>"; //device ID from Pushingbox
const char* MY_SSID = "<YOUR_WIFI_NAME_HERE";
const char* MY_PWD = "<YOUR_WIFI_PASSWORD_HERE>";
void setup()
{
Serial.begin(115200);
dht.begin();
Serial.print("Connecting to "+*MY_SSID);
WiFi.begin(MY_SSID, MY_PWD);
Serial.println("going into wl connect");
while (WiFi.status() != WL_CONNECTED) //not connected,..waiting to connect
{
delay(1000);
Serial.print(".");
}
Serial.println("wl connected");
Serial.println("");
Serial.println("Credentials accepted! Connected to wifi\n ");
Serial.println("");
}
void loop()
{
//Wait between measurements longer then normal to slow donwn
//google sheet populate, as We dont want to exceed free service quota
delay(10000); //10 seconds, (sampling rate vs. service call quota)
float humidityData = dht.readHumidity();
// Read temperature as Celsius (the default)
float celData = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float fehrData = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(humidityData) || isnan(celData) || isnan(fehrData))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Celsius (isFahreheit = false)
float hicData = dht.computeHeatIndex(celData, humidityData, false);
// Compute heat index in Fahrenheit (the default)
float hifData = dht.computeHeatIndex(fehrData, humidityData);
//Print to Serial monitor or Terminal of your chocice at 115200 Baud
Serial.print("Humidity: ");
Serial.print(humidityData);
Serial.print(" %\t");
Serial.print("Temperature in Cel: ");
Serial.print(celData);
Serial.print(" *C ");
Serial.print("Temperature in Fehr: ");
Serial.print(fehrData);
Serial.print(" *F\t");
Serial.print("Heat index in Cel: ");
Serial.print(hicData);
Serial.print(" *C ");
Serial.print("Heat index in Fehr: ");
Serial.print(hifData);
Serial.print(" *F\n");
WiFiClient client; //Instantiate WiFi object
//Start or API service using our WiFi Client through PushingBox
if (client.connect(WEBSITE, 80))
{
client.print("GET /pushingbox?devid=" + devid
+ "&humidityData=" + (String) humidityData
+ "&celData=" + (String) celData
+ "&fehrData=" + (String) fehrData
+ "&hicData=" + (String) hicData
+ "&hifData=" + (String) hifData
);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(WEBSITE);
client.println("User-Agent: ESP8266/1.0");
client.println("Connection: close");
client.println();
}
}