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
305
306
307
308
309
310
311
312
313
314
315
316
317
318 | #include <ESP8266WiFi.h>
#include <SPI.h>
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#define TFT_CS 15
#define TFT_RST 12
#define TFT_DC 16
#define TFT_SCLK 14
#define TFT_MOSI 13
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
//Colors
#define rgb tft.Color565
#define BACK rgb(0,0,255)
#define TOWER rgb(63,63,63)
#define LAMP_ON 0xffff//ST7735_YELLOW
#define LAMP_OFF rgb(127,127,127)
#define LAMP_POS rgb(255,0,0)
#define DIGITS LAMP_ON
#define BACK_TXT rgb(128,128,128)
void towerart(boolean);
bool tower;
#include <TimeLib.h>
#include <WiFiUdp.h>
time_t getNtpTime();
uint16_t read16(WiFiClient f)
{byte b[2];f.readBytes(b,2); // LSB
return 256*b[1]+b[0];
}
#define ROT2 0
#define ROT0 2
uint32_t read32(WiFiClient f)
{byte b[4];f.readBytes(b,4); // LSB
return 16777216*b[3]+65536*b[2]+256*b[1]+b[0];
}
bool Internet(char *ssid, char *pass)
{int i;
WiFi.begin(ssid, pass);
Serial.print("\nConnecting to ");
Serial.print(ssid);
while (WiFi.status() != WL_CONNECTED && i++ < 20)
{delay(500);Serial.print(" .");
}
if(i == 21)
{Serial.print("\nNo internet.");
WiFi.disconnect();
return false;
}
Serial.print("\nInternet ok.");
Serial.println();
return true;
}
bool getMYbmp(char *url,char *command)
{uint8_t r, g, b; WiFiClient client;
if (!client.connect(url, 80))
{Serial.println("connection failed");return false;
}
client.print(command);
if(read16(client) == 0x4D42)// BMP signature
{Serial.print("File size: ");
Serial.println(read32(client));
(void)read32(client); // Read & ignore creator bytes
Serial.print("Image Offset: ");
Serial.println(read32(client), DEC);
Serial.print("Header size: ");
Serial.println(read32(client));
Serial.print("Image size: ");
Serial.print(read32(client));
Serial.print('x');
Serial.println(read32(client));
if(read16(client) == 1)
// # planes -- must be '1'
{Serial.print("Bit Depth: ");
Serial.println(read16(client));
if(read32(client) == 0) // 0 = uncompressed
{// Set TFT address window to clipped image bounds
read32(client); read32(client); read32(client);
read32(client); read16(client); //trial&error
tft.setRotation(ROT2);
tft.setAddrWindow(0,0, 128-1, 160-1);
for (long i=0;i<(128*160);i++)
{client.readBytes(&g,1);
client.readBytes(&r,1);
client.readBytes(&b,1);
tft.pushColor(tft.Color565(r,g,b));
}
}else return false;
}else return false;
}
tft.setRotation(ROT0);
return true;
}
int book(char *id)
{WiFiClient client;
if (!client.connect("amazon.de", 80))
Serial.println("connection to amazon.de failed");
client.print("GET http://www.amazon.de/gp/bestsellers/
books/189259/ref=pd_zg_hrsr_b_3_4_last\r\n");
if(client.find(id)){Serial.print(id);
Serial.println(" found.");}
else return 0;
String tmp=client.readStringUntil('"');
//Serial.println(tmp);
int e=tmp.lastIndexOf('/');
int a=tmp.lastIndexOf('_');
if((e-a)<1)return 0;
tmp=tmp.substring(a+1,e);
return tmp.toInt();
}
void setup()
{long startTime;
char ssid[] = "FRITZ!Box"; // your network SSID (name)
char pass[] = "0123456789"; // your network password
Serial.begin(9600);
tft.initR(INITR_BLACKTAB);
tft.setRotation(ROT0);
tft.setTextWrap(true);
tft.setTextColor(DIGITS,BACK_TXT);
towerart(false);
if(Internet(ssid,pass))
{startTime=millis();
if(getMYbmp("hjberndt.de",
"GET http://www.hjberndt.de/soft/rtbmp\r\n"))
{tower=true;
//#define Serial tft
Serial.print("Loaded in ");
Serial.print(millis()-startTime);
Serial.println(" ms");
}
else Serial.print("\nError loading bitmap.");
}
setSyncProvider(getNtpTime);
setSyncInterval(900);
}
void processMessage()
{String s=Serial.readStringUntil('\n');
if(s.startsWith("S"))
{s.remove(0,1);
tft.print(s);
}
if(s.startsWith("P"))
{s.remove(0,1);
tft.print(s);
}
if(s.startsWith("S"))
{s.remove(0,1);
tft.print(s);
}
if(s.startsWith("B"))
{getMYbmp("hjberndt.de",
"GET http://www.hjberndt.de/soft/rtbmp\r\n");
tower=true;
}
if(s.startsWith("1"))
{getMYbmp("hjberndt.de",
"GET http://www.hjberndt.de/soft/b1bmp\r\n");
tower=false;
}
if(s.startsWith("2"))
{getMYbmp("hjberndt.de",
"GET http://www.hjberndt.de/soft/b2bmp\r\n");
tower=false;
}
}
void books()
{if(second()==15)
{tft.setCursor(tft.width()/2+8*5, tft.height()-10);
int pos=book("B00SM1UMQG");
if(pos==1)
{getMYbmp("hjberndt.de",
"GET http://www.hjberndt.de/soft/b2bmp\r\n");
tower=false;
}
else if(!tower){getMYbmp("hjberndt.de",
"GET http://www.hjberndt.de/soft/rtbmp\r\n");
tower=true;}
tft.print("#");tft.print(pos);tft.print(" ");
}
if(second()==45)
{tft.setCursor(tft.width()/2+8, tft.height()-10);
int pos=book("B00CO5TGEK");
if(pos==1)
{getMYbmp("hjberndt.de",
"GET http://www.hjberndt.de/soft/b1bmp\r\n");
tower=false;
}
else if(!tower){getMYbmp("hjberndt.de",
"GET http://www.hjberndt.de/soft/rtbmp\r\n");
tower=true;}
tft.print("#");tft.print(book("B00CO5TGEK"));
tft.print(" ");
}
}
void loop()
{time_t prev=0;
if(Serial.available())processMessage();
if(now()!=prev)
{prev=now();
if(tower)towerart(true);
books();
}
}
//THE TOWER ART -- RHINETOWER -- BY HJBERNDT.DE
#define H_OFF (-1)
void towerart(boolean lamps)
{if(!lamps)
{int w2=tft.width()/2+H_OFF;
int h=tft.height();
tft.fillScreen(BACK);
tft.fillTriangle(w2-15,50,w2+15,50,w2+1,75,TOWER);//V
tft.fillTriangle(w2+1,5,w2-6,h,w2+8,h,TOWER);// ^
tft.fillRect(w2-14,43,30,3,TOWER);//=
tft.fillRect(w2-11,36,24,3,TOWER);//-
tft.setTextSize(1);
}
else
{boolean is;char s[20];
int x=tft.width()>>1;x+=H_OFF;
int y=tft.height()-8;
int i=1; //Lamp 1-39
int s10=0,s01=0;
int m10=0,m01=0;
int h10=2,h01=3;
s01=second() % 10; s10=second() / 10;
m01=minute() % 10; m10=minute() / 10;
h01=hour() % 10; h10=hour() / 10;
do //The Clock
{switch (i) //Decode Time To Lamp
{case 1: case 2: case 3: case 4: case 5: case 6: case 7:
case 8: case 9: is=s01>(i-1); break;
case 10:case 11:case 12:case 13:case 14: is=s10>(i-10);
break; //Seconds
case 15:case 16:case 17:case 18:case 19:case 20:case 21:
case 22:case 23: is=m01>(i-15);break;
case 24:case 25:case 26:case 27:case 28: is=m10>(i-24);
break; //Minutes
case 29:case 30:case 31:case 32:case 33:case 34:case 35:
case 36:case 37: is=h01>(i-29);break;
case 38:case 39: is=h10>(i-38);
break; //Hours
}
tft.drawFastHLine(x, y, 2, is ? LAMP_ON : LAMP_OFF);
y-=2;
if(i==9 || i==9+5 || i==9+5+9 || i==9+5+9+5 || i==9+5+9+5+9)
{tft.fillCircle(x-2, y-1, 1, LAMP_POS);
tft.fillCircle(x+2, y-1, 1, LAMP_POS);
y-=4;
}
}while(++i<=39); //all done
//Text Time
tft.setCursor(3, tft.height()-10);
sprintf(s,"%0d%0d:%0d%0d:%0d%0d",h10,h01,m10,m01,s10,s01);
tft.print(s);
}
}
/*-------- NTP code ----------*/
time_t getNtpTime()
{IPAddress timeServer(132, 163, 4, 101);
// time-a.timefreq.bldrdoc.gov
const int timeZone = 1; // Central European Time
const int NTP_PACKET_SIZE = 48;
// NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE];
//buffer to hold incoming & outgoing packets
WiFiUDP Udp;
unsigned int localPort = 8888;
// local port to listen for UDP packets
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(Udp.localPort());
Serial.println("waiting for sync");
while (Udp.parsePacket() > 0) ;
// discard any previously received packets
ifquot;char Serial.println("Transmit NTP Request");
memset(packetBuffer, 0, NTP_PACKET_SIZE);
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
Udp.beginPacket(timeServer, 123);
//NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
uint32_t beginWait = millis();
while (millis() - beginWait < 1500)
{int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE)
{Serial.println("Receive NTP Response");
Udp.read(packetBuffer, NTP_PACKET_SIZE);
// read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return 1+secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
return 0;
}
|