-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp8266-azure-event-hub.ino
123 lines (91 loc) · 3 KB
/
esp8266-azure-event-hub.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
#include <ESP8266WiFi.h>
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
const char* ssid = "<YOURSSID>";
const char* password = "<YOURPASSWORD>";
String KEY = "<YOUTTHINGSPEAKKEY>";
const char* THINGSPEAK_HOST = "api.thingspeak.com";
const char* GATEWAY_HOST= "esp8266gateway.local"; //address of the gateway
const String DEVICE_ID = "esp8266_huzzah_001";
DHT dht(DHTPIN, DHTTYPE, 11);
void setup() {
pinMode(A0, INPUT);
Serial.begin(115200);
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(10000);
++value;
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);
float hi = dht.computeHeatIndex(f, h);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hi);
Serial.println(" *F");
int light = analogRead(A0);
Serial.println("Light: "+ String(light));
Serial.print("sending the data ");
// Use WiFiClient class to create TCP connections
WiFiClient client;
// We now create a URI for the request
String thingspeakUrl = "/update?key="+ KEY + "&field1="+String((int)f) + "&field2="+String((int)t) + "&field3="+String((int)h) + "&field4="+String(light);
SendHttpGet(client, thingspeakUrl, THINGSPEAK_HOST);
String azureUrl = "/sensorData?temp="+String((int)f) + "&humidity=" + String((int)h) +"&light="+ String(light) +"&deviceId=" + DEVICE_ID;
SendHttpGet(client, azureUrl, GATEWAY_HOST);
}
void SendHttpGet(WiFiClient client, String url, const char* host)
{
Serial.println("attempting connection to host: "+ String(host));
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
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");
}