forked from mayakraft/Weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.ino
304 lines (255 loc) · 8.92 KB
/
weather.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/***************************************************
This is an example for the Adafruit CC3000 Wifi Breakout & Shield
Designed specifically to work with the Adafruit WiFi products:
----> https://www.adafruit.com/products/1469
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried & Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
//#include "utility/debug.h"
#include <Wire.h>
#include <stdlib.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
#define WLAN_SSID "jankyrangers" // cannot be longer than 32 characters!
#define WLAN_PASS "getonjankyrangers"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
#define IDLE_TIMEOUT_MS 3000 // Amount of time to wait (in milliseconds) with no data
// received before closing the connection. If you know the server
// you're accessing is quick to respond, you can reduce this value.
#define WEBSITE "api.thingspeak.com"
#define WEBPAGE "/update"
//Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
// SPI_CLOCK_DIVIDER); // you can change this clock speed
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
uint32_t ip;
//char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
// char fmt[20];
// sprintf(fmt, "%%%d.%df", width, prec);
// sprintf(sout, fmt, val);
// return sout;
//}
void setup(void)
{
Serial.begin(115200);
delay(3000);
bmp.begin();
delay(1000);
// readBMP180(&temperature, &pressure);
// temp and pressure
// Serial.println(temperature);
// Serial.println(F("Hello, CC3000!\n"));
// Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
}
void loop(){
int pressure;
sensors_event_t event;
bmp.getEvent(&event);
float celsius;
do{
pressure = int(event.pressure);
bmp.getTemperature(&celsius);
delay(500);
}while(!event.pressure);
int fahrenheit = (celsius*1.8)+32;
Serial.println("------- BEGIN --------");
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed
/* Initialise the module */
// Serial.println(F("\nInitializing..."));
if (!cc3000.begin())
{
// Serial.println(F("E:wiring?"));
while(1);
}
// Optional SSID scan
// listSSIDResults();
// Serial.print(F("\nConnect:")); Serial.println(WLAN_SSID);
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
// Serial.println(F("Fail"));
while(1);
}
// Serial.println(F("Connected!"));
/* Wait for DHCP to complete */
// Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
}
/* Display the IP address DNS, Gateway, etc. */
// while (! displayConnectionDetails()) {
// delay(1000);
// }
ip = 0;
// Try looking up the website's IP address
// Serial.print(WEBSITE); Serial.print(F(" -> "));
while (ip == 0) {
if (! cc3000.getHostByName(WEBSITE, &ip)) {
// Serial.println(F("Couldn't resolve!"));
}
delay(500);
}
// cc3000.printIPdotsRev(ip);
// Optional: Do a ping test on the website
/*
Serial.print(F("\n\rPinging ")); cc3000.printIPdotsRev(ip); Serial.print("...");
replies = cc3000.ping(ip, 5);
Serial.print(replies); Serial.println(F(" replies"));
*/
/* Try connecting to the website.
Note: HTTP/1.1 protocol is used to keep the server from closing the connection before all data is read.
*/
Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80);
if (www.connected()) {
String webpage = "/update?key=";
String key = "EAXOWK7YC7SRDY1W";
String data1 = "&field1=";
String data2 = "&field3=";
String tempString = String(fahrenheit);
String pressString = String(pressure);
String postMessage = webpage + key + data1 + tempString + data2 + pressString;
char msg[postMessage.length() + 1]; // + 1 for the null character
postMessage.toCharArray(msg, postMessage.length() + 1);
www.fastrprint(F("POST "));
www.fastrprint(msg);
www.fastrprint(F(" HTTP/1.1\r\n"));
www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("\r\n"));
www.fastrprint(F("\r\n"));
www.println();
} else {
Serial.println(F("Connection failed"));
return;
}
// Serial.println(F("-------------------------------------"));
/* Read data until either the connection is closed, or the idle timeout is reached. */
unsigned long lastRead = millis();
while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
while (www.available()) {
char c = www.read();
Serial.print(c);
lastRead = millis();
}
}
www.close();
// Serial.println(F("-------------------------------------"));
/* You need to make sure to clean up after yourself or the CC3000 can freak out */
/* the next time your try to connect ... */
// Serial.println(F("\n\nDisconnecting"));
cc3000.disconnect();
Serial.println("------- DISCONNECT --------");
cc3000.stop();
Serial.println("------- END --------");
delay(10000);
}
//void readBMP180(int *temperature, int *pressure){
// // temp and pressure
// sensors_event_t event;
// bmp.getEvent(&event);
// int p;
// float t;
// do{
// p = int(event.pressure);
// bmp.getTemperature(&t);
// delay(500);
// }while(!event.pressure);
//// Serial.println(t);
// int temp = int(t);
// Serial.println(temp);
// temperature = &temp;
// pressure = &p;
// char tempChars[10];
// char humidChars[10];
// char pressChars[10];
// dtostrf(fahrenheit, 4, 1, tempChars);
// dtostrf(humidity, 4, 1, humidChars);
// dtostrf(pressure, 4, 1, pressChars);
// String tempStr = tempChars;
// String humidStr = humidChars;
// String pressStr = pressChars;
// String sendStr = "key=EAXOWK7YC7SRDY1W&field1=" + tempStr + "&field2=" + humidStr + "&field3=" + pressStr;
// Serial.println(temperature);
// Serial.println(pressure);
//}
//void readDHT{
//// humidity
// int chk;
// do{
// chk = DHT11.read(DHT11PIN);
// delay(500);
// }while(chk != DHTLIB_OK);
//
// float humidity = DHT11.humidity;
// float celsius = temperature;//DHT11.temperature;
// float fahrenheit = (celsius*1.8)+32;
//}
/**************************************************************************/
/*!
@brief Begins an SSID scan and prints out all the visible networks
*/
/**************************************************************************/
//void listSSIDResults(void)
//{
// uint8_t valid, rssi, sec, index;
// char ssidname[33];
//
// index = cc3000.startSSIDscan();
//
// Serial.print(F("Networks found: ")); Serial.println(index);
// Serial.println(F("================================================"));
//
// while (index) {
// index--;
//
// valid = cc3000.getNextSSID(&rssi, &sec, ssidname);
//
// Serial.print(F("SSID Name : ")); Serial.print(ssidname);
// Serial.println();
// Serial.print(F("RSSI : "));
// Serial.println(rssi);
// Serial.print(F("Security Mode: "));
// Serial.println(sec);
// Serial.println();
// }
// Serial.println(F("================================================"));
//
// cc3000.stopSSIDscan();
//}
/**************************************************************************/
/*!
@brief Tries to read the IP address and other connection details
*/
/**************************************************************************/
//bool displayConnectionDetails(void)
//{
// uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
//
// if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
// {
// Serial.println(F("Unable to retrieve the IP Address!\r\n"));
// return false;
// }
// else
// {
// Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
// Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
// Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
// Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
// Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
// Serial.println();
// return true;
// }
//}