-
Notifications
You must be signed in to change notification settings - Fork 0
/
mikedice-weatherama.ino
295 lines (248 loc) · 8.43 KB
/
mikedice-weatherama.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
#include <ccspi.h>
#include <Adafruit_CC3000_Server.h>
#include <Adafruit_CC3000.h>
#include <SFE_BMP180.h>
#include <Wire\Wire.h>
#include <LiquidCrystal.h>
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include "Clock.h"
#include "TempSensor.h"
#include "Display.h"
#include <IPAddress.h>
#define LOOP_MS 250
// Little hardware thingies used by the project
Clock clock;
TempSensor tempSensor;
Display display;
DateTime currentTime;
// CC3000 interrupt and control pins
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
#define ADAFRUIT_CC3000_VBAT 5 // These can be
#define ADAFRUIT_CC3000_CS 10 // any two pins
// Hardware SPI required for remaining pins.
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(
ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIVIDER);
const char* wifiSSID = "proton";
const char* wifiPass = "kona1234";
#define WEBUPDATE_DELAY_LOOPCOUNT 4 * 5
uint8_t webpage_delay = 0;
IPAddress testServer(68, 1, 168, 192);
#define WEBSITE "www.weatherama.com"
#define WEBPAGE "/ArduinoHello"
#define IDLE_TIMEOUT_MS 2000
bool networkReady = false;
void initializeWiFi()
{
cc3000.begin();
uint8_t majorVersion = 0;
uint8_t minorVersion = 0;
bool dhcpComplete = false;
uint8_t rssi = 0;
uint8_t secMode = 0;
char networkName[64] = { 0 };
bool networkFound = false;
uint32_t numNetworks = 0;
bool connected = false;
cc3000.getFirmwareVersion(&majorVersion, &minorVersion);
Serial.print("firmware: ");
Serial.print(majorVersion);
Serial.print(".");
Serial.println(minorVersion);
Serial.println("Scanning networks...");
cc3000.startSSIDscan(&numNetworks);
Serial.print("found ");
Serial.print(numNetworks);
Serial.println(" networks");
for (uint32_t i = 0; i < numNetworks; i++)
{
rssi = 0;
secMode = 0;
memset(networkName, 0, sizeof(networkName)*sizeof(networkName[0]));
cc3000.getNextSSID(&rssi, &secMode, networkName);
Serial.println(networkName);
if (strcmp(networkName, wifiSSID) == 0)
{
networkFound = true;
}
}
if (networkFound)
{
Serial.println("Network found, connecting to AP");
connected = cc3000.connectToAP(wifiSSID, wifiPass, secMode, 5);
if (connected)
{
Serial.print("Connected to AP ");
Serial.println(wifiSSID);
Serial.println("Checking for DHCP");
unsigned long start = millis();
do
{
if (cc3000.checkDHCP())
{
Serial.print("DHCP completed in ");
Serial.print(millis() - start);
Serial.println(" milliseconds");
dhcpComplete = true;
break;
}
if (millis() - start > 120000)
{
Serial.println("DHCP timed out after 120 seconds");
break;
}
delay(100);
} while (!dhcpComplete);
if (dhcpComplete)
{
uint32_t ipAddress = 0;
uint32_t netMask = 0;
uint32_t gateway = 0;
uint32_t dhcpSrv = 0;
uint32_t dnsSrv = 0;
if (cc3000.getIPAddress(&ipAddress, &netMask, &gateway, &dhcpSrv, &dnsSrv))
{
Serial.print("IPAddress: ");
cc3000.printIPdotsRev(ipAddress);
Serial.println();
Serial.print("DNS Server: ");
cc3000.printIPdotsRev(dnsSrv);
Serial.println();
Serial.print("Net Mask: ");
cc3000.printIPdotsRev(netMask);
Serial.println();
Serial.print("Gateway: ");
cc3000.printIPdotsRev(gateway);
Serial.println();
Serial.print("DHCP Server: ");
cc3000.printIPdotsRev(dhcpSrv);
Serial.println();
networkReady = true;
}
else
{
Serial.println("Did not get IP Address");
}
}
}
else
{
Serial.println("Could not connect to AP");
}
}
}
void retrieveWebPage()
{
if (networkReady)
{
//uint32_t ip = testServer;
uint32_t 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);
}
Serial.print("Connecting to server at ip address: ");
cc3000.printIPdotsRev(ip);
Serial.println();
// 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, 8001);
if (www.connected()) {
www.fastrprint(F("GET "));
www.fastrprint(WEBPAGE);
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("-------------------------------------"));
Serial.print("last web page access at ");
Serial.print(currentTime.hours); Serial.print(":"); Serial.print(currentTime.minutes); Serial.print(":"); Serial.println(currentTime.seconds);
delay(5000);
/*
if (connected)
{
cc3000.disconnect();
Serial.print("Disconnected from AP ");
Serial.println(wifiSSID);
}
*/
}
else
{
Serial.println("Could not retrieve web page because network is not ready");
}
}
void setup()
{
currentTime.hours = currentTime.minutes = currentTime.seconds = 0;
Serial.begin(115200);
initializeWiFi();
tempSensor.init();
clock.init();
display.init();
tempSensor.updateSensorImmediate();
Serial.println("weatherama setup completed.");
// TODO: come up with a way to set the date/time other than using code
DateTime current;
current.seconds = 00;
current.minutes = 39;
current.hours = 17;
current.wday = 7;
current.month = 11;
current.day = 22;
current.year = 2015;
clock.setDateTime(current);
}
void loop()
{
// update readings from sensors & clock
clock.readDateTime(currentTime);
tempSensor.updateSensor();
// Update the TFT screen with new values.
int16_t tftStartY = 5;
int16_t tftStartX = 5;
display.printTimeOnLcd(currentTime, tftStartX, tftStartY);
display.printDateOnLcd(currentTime, tftStartX, tftStartY);
display.printTempOnLcd(tempSensor.getCurrentTempF(), tftStartX, tftStartY);
display.printPressureOnLcd(tempSensor.getCurrentPressure(), tftStartX, tftStartY);
if (webpage_delay > WEBUPDATE_DELAY_LOOPCOUNT)
{
retrieveWebPage();
webpage_delay = 0;
}
else
{
webpage_delay++;
}
delay(LOOP_MS);
}