-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInternet_clock.ino
201 lines (164 loc) · 6.93 KB
/
Internet_clock.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
/**************************************************************
* DESCRIPTION
* Internet_clock is a program to get time via NTP and tempature
* from Weather Underground
*
* Adafruit Huzzah ESP8266 with:
* Adafruit OLED Monochrome 1.3" 128x64 display (Product ID: 938)
*
* Created by Mike Pate <[email protected]>
* Copyright (C) 2016 by A Third Opinion, LLC
* Repository: https://github.com/k5map/PiPower_HAT/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* REVISION HISTORY
* Version 1.0 - initial release
*/
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SPI.h> //Serial Peripheral Interface (SPI) library for synchronous serial data protocol
#include <Wire.h> //Wire library used for I2C communication: Arduino Pro Mini pins used = A4 (SDA) and A5 (SCL)
#include <Adafruit_GFX.h> //Adafruit graphic display library used for the OLED display
#include <Adafruit_SSD1306.h> //Adafruit driver for OLED
#define OLED_RESET 14 // GPIO pin
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
WiFiUDP Udp;
unsigned int localPort = 8888; // local port to listen for UDP packets
time_t getNtpTime();
int prevSecond = 0;
String colonString = " ";
String minuteString = " ";
int CurrentTemp = 0;
//*** Weather Underground parms
const char server[] = "api.wunderground.com"; // name address for Weather Underground (using DNS)
const String myKey = "xxxxxxxxxxxxxxxx"; //See: http://www.wunderground.com/weather/api/d/docs (change here with your KEY)
const String myFeatures = "conditions"; //See: http://www.wunderground.com/weather/api/d/docs?d=data/index&MR=1
const String myZipcode = "77379";
const int WU_INTERVAL = 900; // number of secs to refresh weather data
String html_cmd1 = "GET /api/" + myKey + "/" + myFeatures + "/q/" + myZipcode + ".json HTTP/1.1";
String html_cmd2 = "Host: " + (String)server;
String html_cmd3 = "Connection: close";
String responseString;
int prevCheck = 0;
boolean startCapture;
void digitalClockDisplay();
WiFiClient client;
/**************** Setup **********************************************/
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(); Serial.println();
Serial.println("Feather booting up...\n");
Serial.println("ESP8266:");
Serial.printf("- Chip-ID = %08X\n", ESP.getChipId());
Serial.printf("- Heap Size = %i\n", ESP.getFreeHeap());
Serial.printf("- Flash Chip-ID = %i\n", ESP.getFlashChipId());
Serial.printf("- Flash Chip Size = %i\n", ESP.getFlashChipSize());
Serial.printf("- Flash Chip Speed = %i\n", ESP.getFlashChipSpeed());
display.begin(SSD1306_SWITCHCAPVCC, 0x3D); // initialize the OLED and set the I2C address to 0x3C (for the 128x64 OLED)
display.clearDisplay(); //Clear display buffer.
display.setTextSize(2); //set text size to 2 (large)
display.setTextColor(WHITE);
display.setCursor(0,0); //set text start position to column=0 and row=0
display.print("K5MAP");
display.setTextSize(1); //set text size to 1 (small)
display.setCursor(0,18); //set text start position to column=0 and row=18
display.print("INTERNET CLOCK");
display.setCursor(0,30); //set text start position to column=0 and row=30
display.setTextColor(BLACK, WHITE); // 'inverted' text
display.print("INITIALISING ....."); //print "INITIALISING ....." to display
display.setCursor(0,40); //set text start position to column=0 and row=40
display.setTextColor(WHITE);
display.print("Attempting to connect to WiFi network");
display.setCursor(0,57); //set text start position to column=0 and row=57
display.print("Please Wait..."); //print "Please Wait" to display
display.display(); //update OLED with new display data
delay(1000); //short delay
display.clearDisplay(); //clear display
// Connect to a WiFi network
connectWiFi();
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(Udp.localPort());
Serial.println("waiting for sync");
setSyncProvider(getNtpTime);
setSyncInterval(600);
setDST();
setSyncProvider(getNtpTime);
prevSecond = now();
}
/**************** Main Loop **********************************************/
void loop() {
// check for change in seconds
if ((now() - prevSecond) >= 1) {
prevSecond = now();
colonString = (colonString == " ") ? ":" : " ";
minuteString = (minute()<10) ? "0" + String(minute()) : String(minute());
digitalClockDisplay();
}
if (hour() == 2) setDST();
// check to see if weather data needs to be refreshed
if ((now() - prevCheck) >= WU_INTERVAL) {
sendWeatherGetData();
prevCheck = now();
}
if (client.available()) {
char c = client.read();
if (c == '{')
startCapture=true;
if (startCapture)
responseString += c;
}
if (!client.connected()) {
client.stop();
client.flush();
String TempString = getValuesFromKey(responseString, "temp_f");
Serial.println("TempString = " + TempString);
CurrentTemp = TempString.toInt();
Serial.println(CurrentTemp);
}
}
/**************************************************/
void digitalClockDisplay() {
display.clearDisplay();
display.setTextSize(1); //set text size to 1
display.setTextColor(WHITE);
display.setCursor(50,0); //set text start position for date and time (row = 0, column =0)
display.print(String(monthShortStr(month())) + " " + String(day())); // prints date on top line of OLED
display.setTextSize(2); //set text size to 2
if (hourFormat12() < 10) {
display.setCursor(40,25); //set text start position to column=0 and row=20
}
else {
display.setCursor(32,25); //set text start position to column=0 and row=20
}
display.print(hourFormat12() + colonString + minuteString); //prints time to OLED
display.setTextSize(1);
display.print(isAM() ? " AM" : " PM");
display.setTextSize(1); //set text size to 1
display.setCursor(20,55); //set text start position to column=0 and row=20
display.print("Outside Temp: " + String(CurrentTemp) + (char)247); //prints temp to OLED
display.display(); //update OLED display
}
/**************************************************/
void sendWeatherGetData() {
responseString = "";
if (!client.connect(server, 80)) {
Serial.println("Connection to Weather Underground failed...");
return;
}
// Make a HTTP request:
client.println(html_cmd1);
client.println(html_cmd2);
client.println(html_cmd3);
client.println();
startCapture = false;
}