-
Notifications
You must be signed in to change notification settings - Fork 12
/
arduinoESP8266wifishieldstream.ino
187 lines (151 loc) · 4.85 KB
/
arduinoESP8266wifishieldstream.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Should work with most ESP8266 boards.
// Special thanks to Kurt Lanes for providing the example
#include <Arduino.h>
#include <ESP8266WiFi.h>
////////////////////////////
// Initial State Streamer //
////////////////////////////
// Data destination
#define ISDestURL "insecure-groker.initialstate.com" // https can't be handled by the ESP8266, thus "insecure"
#define bucketKey "arduino" // Bucket key (hidden reference to your bucket that allows appending):
#define bucketName "Arduino Stream" // Bucket name (name your data will be associated with in Initial State):
#define accessKey "Your_IS_Access_Key" // Access key (the one you find in your account settings):
const int NUM_SIGNALS = 2; // How many signals are in your stream? You can have as few or as many as you want
String signalName[NUM_SIGNALS] = {"Signal 1", "Signal 2"}; // What are the names of your signals (i.e. "Temperature", "Humidity", etc.)
String signalData[NUM_SIGNALS]; // This array is to store our signal data later
/////////////
// Signals //
////////////
//Signal number 1
int i = 0;
//Signal number 2
bool increase = true;
//////////////////////
// Network Settings //
/////////////////////
char ssid[] = "WiFi_SSID"; // your network SSID (name)
char password[] = "WiFi_PW"; // your network password
WiFiClient client; // initialize the library instance:
void setup() {
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());
// The postBucket() function creates a bucket
// (unnecessary if the bucket already exists)
while (!postBucket()) {};
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.write(c);
}
// these lines generate a random signal that can be easily understood on the dashboard
if (increase)
{
i = i + 1;
if (i == 10)
{
increase = false;
}
}
else
{
i = i - 1;
if (i == 0)
{
increase = true;
}
}
// Gather Data
// Read from a port for input or output or generate your own values/messages
signalData[0] = String(i);
signalData[1] = String(increase);
// The postData() function streams our events
while(!postData());
// Wait for 1 seconds before collecting and sending the next batch
delay(1000);
}
// this method makes a HTTP connection to the server and creates a bucket is it does not exist:
bool postBucket() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if (client.connect(ISDestURL, 80) > 0) {
Serial.println("connecting...");
// send the HTTP PUT request:
// Build HTTP request.
String toSend = "POST /api/buckets HTTP/1.1\r\n";
toSend += "Host:";
toSend += ISDestURL;
toSend += "\r\n" ;
toSend += "User-Agent:Arduino\r\n";
toSend += "Accept-Version: ~0\r\n";
toSend += "X-IS-AccessKey: " accessKey "\r\n";
toSend += "Content-Type: application/json\r\n";
String payload = "{\"bucketKey\": \"" bucketKey "\",";
payload += "\"bucketName\": \"" bucketName "\"}";
payload += "\r\n";
toSend += "Content-Length: "+String(payload.length())+"\r\n";
toSend += "\r\n";
toSend += payload;
client.println(toSend);
Serial.println(toSend);
return true;
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
return false;
}
}
// this method makes a HTTP connection to the server and sends the signals measured:
bool postData() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if (client.connect(ISDestURL, 80) > 0) {
Serial.println("connecting...");
// send the HTTP PUT request:
// Build HTTP request.
for (int i=0; i<NUM_SIGNALS; i++){
String toSend = "POST /api/events HTTP/1.1\r\n";
toSend += "Host:";
toSend += ISDestURL;
toSend += "\r\n" ;
toSend += "Content-Type: application/json\r\n";
toSend += "User-Agent: Arduino\r\n";
toSend += "Accept-Version: ~0\r\n";
toSend += "X-IS-AccessKey: " accessKey "\r\n";
toSend += "X-IS-BucketKey: " bucketKey "\r\n";
String payload = "[{\"key\": \"" + signalName[i] + "\", ";
payload +="\"value\": \"" + signalData[i] + "\"}]\r\n";
toSend += "Content-Length: "+String(payload.length())+"\r\n";
toSend += "\r\n";
toSend += payload;
Serial.println(toSend);
client.println(toSend);
}
return true;
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
return false;
}
}