Components and supplies
1
DHT22 Temperature Sensor
2
Resistor 10k ohm
1
Breadboard (generic)
1
NodeMCU ESP8266 Breakout Board
1
LDR, 5 Mohm
1
Jumper wires (generic)
6
MAXREFDES99# MAX7219 Display Driver Shield
Project description
Code
WiFi NodeMCU ESP8266 Google Clock - v.11 Oct2020
arduino
1 2//V.3 - Data converted and daylight savings time 3//V.4 - localized data, days... (use yours....) 4//V.6 - thermometer and hygrometer 5//V.7 - animated clock corrected + seconds +1,5 6//V.8 - tuning thermometer and hygrometer 7//v.9 - DST corrected / compiler error corrected (long)round 8//v.10 - Date change at 00:00 corrected for GMT>0 9//v.11 - automitc panel brightness (+3.3V ---|==10k==|---A0---|GND 10// review: anthias64@gmail.com 11// kudos original maker code can't find its name... 12 13#include "Arduino.h" 14#include <ArduinoJson.h> 15#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library (you most likely already have this in your sketch) 16 17WiFiClient client; 18 19#define NUM_MAX 6 20 21// for NodeMCU 1.0 22#define DIN_PIN 13 // D7 23#define CS_PIN 0 // D3 24#define CLK_PIN 14 // D5 25 26#include "max7219.h" 27#include "fonts.h" 28 29#include "DHT.h" // sensor DHT 30 31// DHT sensor 32#define DHTPIN 12 // what pin we're connected to // GPIO 12 = D6 33// Uncomment whatever type you're using! 34//#define DHTTYPE DHT11 // DHT 11 35#define DHTTYPE DHT22 // DHT 22 (AM2302) 36//#define DHTTYPE DHT21 // DHT 21 (AM2301) 37 38DHT dht(DHTPIN, DHTTYPE, 11); // for ESP8266 39 40//DHT 41float temperature = 0; 42int temp = 0; 43byte minus = 0; 44int umidity = 0; 45int poz, poz2; 46int mult = 0; 47 48float temp_offset = -1.0; // taratura termometro 49float rh_offset = +3; // taratura igrometro 50 51char TempLabel[] = " Temperatura: "; // Fixxed text 52char UmiLabel[] = " Umidita': "; // Fixxed text 53 54//modifica test ip 55 56const char ssid[] = "xxxxxxxxx"; // your network SSID (name) <-------------- 57const char password[] = "xxxxxxxxxxxxxxxxxx"; // your network password <-------------- 58 59// Localized Months and DoWeek 60 61String M_arr[13] = {" ", "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"}; 62String Dow_arr[8] = {" ", "Lunedi", "Martedi", "Mercoledi", "Giovedi", "Venerdi", "Sabato", "Domenica"}; 63 64void setup() 65{ 66 Serial.begin(115200); 67 68 initMAX7219(); // 69 sendCmdAll(CMD_SHUTDOWN,1); // 70 sendCmdAll(CMD_INTENSITY,1); // brightness from 1 to 5 71 72 Serial.print("Connecting to "); 73 Serial.println(ssid); 74 75 WiFi.begin(ssid, password); 76 77 printStringWithShift("Connecting",30); // 78 delay (1000); 79 80 while (WiFi.status() != WL_CONNECTED) { 81 delay(500); 82 Serial.print("."); 83 } 84 85 86 Serial.print("IP number assigned by DHCP is "); 87 Serial.println(WiFi.localIP()); 88 89 printStringWithShift((String(" IP:")+WiFi.localIP().toString()).c_str(), 30);// 90 delay(1000);// 91 92 // DHT 93 94 95dht.begin(); 96temperature = dht.readTemperature() + temp_offset; 97umidity = dht.readHumidity() + rh_offset; 98 99Serial.print(temperature); 100Serial.println(" gr.Celsius"); 101 102Serial.print(umidity); 103Serial.println(" %RH"); 104 105 if (temperature < 0) 106 { 107 temp = -10*temperature; 108 minus = 1; 109 } 110 else 111 { 112 minus = 0; 113 temp = 10*temperature; 114 } 115 116} 117 118 119// =============================DEFINE VARS============================== 120#define MAX_DIGITS 20 121byte dig[MAX_DIGITS]={0}; 122byte digold[MAX_DIGITS]={0}; 123byte digtrans[MAX_DIGITS]={0}; 124int updCnt = 0; 125int dots = 0; 126long dotTime = 0; 127long clkTime = 0; 128int dx=0; 129int dy=0; 130byte del=0; 131int h,m,s; 132float utcOffset = +1; // ==> your timezone 133long localEpoc = 0; 134long localMillisAtUpdate = 0; 135int day, month, year, dayOfWeek; 136int monthnum = 0; 137int downum = 0; 138int summerTime = 0; 139String date; 140String dayloc; 141String monthloc; 142 143 144// ======================================================================= 145 146void loop() 147{ 148 if(updCnt<=0) { // every 10 scrolls, ~450s=7.5m 149 updCnt = 60; 150 Serial.println("Getting data ..."); 151 // clr(); 152 printStringWithShift(" Get Time...",30); 153 getTime(); 154 Serial.println("Data loaded"); 155 clkTime = millis(); 156 157 } 158 159 contrast(); // //autobrightness 160 161//print data 162 163 if(millis()-clkTime > 30000 && !del && dots) { // clock for 30s, then scrolls for about 30s 164 printStringWithShift(" ",40); //Space before 165 printStringWithShift(date.c_str(),40); //data1 166 167// Reading temperature or humidity 168 int umidity = dht.readHumidity()+ rh_offset; 169 float temperature = dht.readTemperature()+ temp_offset; 170 171 //umidity = umidity + rh_offset; 172 //temperature = temperature + temp_offset; 173 174 Serial.println("Temp e Umid corretti"); 175 Serial.println(temperature); 176 Serial.println(umidity); 177 178 int t2 = 10*temperature; 179 180// print temperature 181 182// http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/ 183char c[3], d[2]; 184String str1, str2; 185int t2z = t2/10; 186int t2u = t2 - t2z*10; 187str1=String(t2z); 188str1.toCharArray(c,3); 189str2=String(t2u); 190str2.toCharArray(d,2); 191printStringWithShift(TempLabel ,40); // Send scrolling Text 192printStringWithShift("+" ,40); 193printStringWithShift(c ,40); 194printStringWithShift("," ,40); 195printStringWithShift(d ,40); 196printStringWithShift(" C^" ,40); 197//delay(2000); 198 199// print umidity 200 201// http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/ 202char b[3]; 203String str; 204str=String(umidity); 205str.toCharArray(b,3); 206printStringWithShift(UmiLabel ,40); // Send scrolling Text 207printStringWithShift(b ,40); 208printStringWithShift(" %" ,40); 209//delay(2000); 210 211 printStringWithShift(" ",40); //Space after 212 delay(200); 213 214 updCnt--; 215 clkTime = millis(); 216 } 217 if(millis()-dotTime > 500) { 218 dotTime = millis(); 219 dots = !dots; 220 } 221 updateTime(); // get time 222 223// print clock 224 showAnimClock(); 225 //showSimpleClock(); 226 227} 228 229// ======================================================================= 230 231void showSimpleClock() 232{ 233 dx=dy=0; 234 clr(); 235 showDigit(h/10, 0, dig6x8); 236 showDigit(h%10, 8, dig6x8); 237 showDigit(m/10, 17, dig6x8); 238 showDigit(m%10, 25, dig6x8); 239 showDigit(s/10, 34, dig6x8); 240 showDigit(s%10, 42, dig6x8); 241 setCol(15,dots ? B00100100 : 0); 242 setCol(32,dots ? B00100100 : 0); 243 refreshAll(); 244} 245 246// ======================================================================= 247 248void showAnimClock() 249{ 250 byte digPos[6]={0,8,17,25,34,42}; 251 int digHt = 12; 252 int num = 6; 253 int i; 254 if(del==0) { 255 del = digHt; 256 for(i=0; i<num; i++) digold[i] = dig[i]; 257 dig[0] = h/10 ? h/10 : 10; 258 dig[1] = h%10; 259 dig[2] = m/10; 260 dig[3] = m%10; 261 dig[4] = s/10; 262 dig[5] = s%10; 263 for(i=0; i<num; i++) digtrans[i] = (dig[i]==digold[i]) ? 0 : digHt; 264 } else 265 del--; 266 267 clr(); 268 for(i=0; i<num; i++) { 269 if(digtrans[i]==0) { 270 dy=0; 271 showDigit(dig[i], digPos[i], dig6x8); 272 } else { 273 dy = digHt-digtrans[i]; 274 showDigit(digold[i], digPos[i], dig6x8); 275 dy = -digtrans[i]; 276 showDigit(dig[i], digPos[i], dig6x8); 277 digtrans[i]--; 278 } 279 } 280 dy=0; 281 setCol(15,dots ? B00100100 : 0); 282 setCol(32,dots ? B00100100 : 0); 283 refreshAll(); 284 delay(10); //default 30 285} 286 287// ======================================================================= 288 289void showDigit(char ch, int col, const uint8_t *data) 290{ 291 if(dy<-8 | dy>8) return; 292 int len = pgm_read_byte(data); 293 int w = pgm_read_byte(data + 1 + ch * len); 294 col += dx; 295 for (int i = 0; i < w; i++) 296 if(col+i>=0 && col+i<8*NUM_MAX) { 297 byte v = pgm_read_byte(data + 1 + ch * len + 1 + i); 298 if(!dy) scr[col + i] = v; else scr[col + i] |= dy>0 ? v>>dy : v<<-dy; 299 } 300} 301 302// ======================================================================= 303 304void setCol(int col, byte v) 305{ 306 if(dy<-8 | dy>8) return; 307 col += dx; 308 if(col>=0 && col<8*NUM_MAX) 309 if(!dy) scr[col] = v; else scr[col] |= dy>0 ? v>>dy : v<<-dy; 310} 311 312// ======================================================================= 313 314int showChar(char ch, const uint8_t *data) 315{ 316 int len = pgm_read_byte(data); 317 int i,w = pgm_read_byte(data + 1 + ch * len); 318 for (i = 0; i < w; i++) 319 scr[NUM_MAX*8 + i] = pgm_read_byte(data + 1 + ch * len + 1 + i); 320 scr[NUM_MAX*8 + i] = 0; 321 return w; 322} 323 324// ======================================================================= 325 326 327int dualChar = 0; 328 329unsigned char convertPolish(unsigned char _c) 330{ 331 unsigned char c = _c; 332 if(c==196 || c==197 || c==195) { 333 dualChar = c; 334 return 0; 335 } 336 if(dualChar) { 337 switch(_c) { 338 case 133: c = 1+'~'; break; // '' 339 case 135: c = 2+'~'; break; // '' 340 case 153: c = 3+'~'; break; // '' 341 case 130: c = 4+'~'; break; // '' 342 case 132: c = dualChar==197 ? 5+'~' : 10+'~'; break; // '' and '' 343 case 179: c = 6+'~'; break; // '' 344 case 155: c = 7+'~'; break; // '' 345 case 186: c = 8+'~'; break; // '' 346 case 188: c = 9+'~'; break; // '' 347 //case 132: c = 10+'~'; break; // '' 348 case 134: c = 11+'~'; break; // '' 349 case 152: c = 12+'~'; break; // '' 350 case 129: c = 13+'~'; break; // '' 351 case 131: c = 14+'~'; break; // '' 352 case 147: c = 15+'~'; break; // '' 353 case 154: c = 16+'~'; break; // '' 354 case 185: c = 17+'~'; break; // '' 355 case 187: c = 18+'~'; break; // '' 356 default: break; 357 } 358 dualChar = 0; 359 return c; 360 } 361 switch(_c) { 362 case 185: c = 1+'~'; break; 363 case 230: c = 2+'~'; break; 364 case 234: c = 3+'~'; break; 365 case 179: c = 4+'~'; break; 366 case 241: c = 5+'~'; break; 367 case 243: c = 6+'~'; break; 368 case 156: c = 7+'~'; break; 369 case 159: c = 8+'~'; break; 370 case 191: c = 9+'~'; break; 371 case 165: c = 10+'~'; break; 372 case 198: c = 11+'~'; break; 373 case 202: c = 12+'~'; break; 374 case 163: c = 13+'~'; break; 375 case 209: c = 14+'~'; break; 376 case 211: c = 15+'~'; break; 377 case 140: c = 16+'~'; break; 378 case 143: c = 17+'~'; break; 379 case 175: c = 18+'~'; break; 380 default: break; 381 } 382 return c; 383} 384 385 386// ======================================================================= 387 388void printCharWithShift(unsigned char c, int shiftDelay) { 389 c = convertPolish(c); 390 if (c < ' ' || c > '~'+25) return; 391 c -= 32; 392 int w = showChar(c, font); 393 for (int i=0; i<w+1; i++) { 394 delay(shiftDelay); 395 scrollLeft(); 396 refreshAll(); 397 } 398} 399 400// ======================================================================= 401 402void printStringWithShift(const char* s, int shiftDelay){ 403 while (*s) { 404 printCharWithShift(*s, shiftDelay); 405 s++; 406 } 407} 408// ======================================================================= 409 410void getTime() 411{ 412 WiFiClient client; 413 if (!client.connect("www.google.com", 80)) { 414 Serial.println("connection to google failed"); 415 return; 416 } 417 418 client.print(String("GET / HTTP/1.1\ \ 419") + 420 String("Host: www.google.com\ \ 421") + 422 String("Connection: close\ \ 423\ \ 424")); 425 int repeatCounter = 0; 426 while (!client.available() && repeatCounter < 10) { 427 delay(500); 428 //Serial.println("."); 429 repeatCounter++; 430 } 431 432 String line; 433 client.setNoDelay(false); 434 while(client.connected() && client.available()) { 435 line = client.readStringUntil('\ 436'); 437 line.toUpperCase(); 438 // Serial.println(line); //test stringa 439 if (line.startsWith("DATE: ")) { 440 //date = " "+line.substring(6, 22); 441 date =line.substring(6, 22); 442 date.toUpperCase(); 443 444 Serial.println(date); //check 445 446 int day = atoi (date.substring(5,7).c_str()); //day number 447 int year = 2000 + atoi (date.substring(13,16).c_str()); //year number 448 449 monthnum = month2index(date.substring(8,11)); // convert month in mumber 450 month = monthnum; //check 451 downum = dow2index(date.substring(0,3)); // convert dow in mumber 452 dayOfWeek = downum; //check 453 454 monthloc = M_arr[month]; // localize month 455 dayloc = Dow_arr[dayOfWeek]; // localize DayOfWeek 456 457 date = dayloc + " " + day + " " + monthloc + " " + year; 458 459 460 461 // decodeDate(date); //data3 462 h = line.substring(23, 25).toInt(); 463 m = line.substring(26, 28).toInt(); 464 s = line.substring(29, 31).toInt()+1.5; //correzione ora 465 466 // summerTime = checkSummerTime(); 467 468// calcolo ora legale 469 470 Serial.print("Ora GMT: " ); 471 Serial.print(h); //check 472 Serial.print(" : " ); 473 Serial.print(m); //check 474 Serial.print(" : " ); 475 Serial.print(s); //check 476 Serial.println(""); //check 477 Serial.print("Data: " ); 478 Serial.println(date); //check 479 480 //day = 31; 481 //year = 2021; 482 //month = 10; 483 484 if(month>3 && month<=10) summerTime=1; 485 if(month==3 && day>=31-(((5*year/4)+4)%7) ) summerTime=1; 486 if(month==10 && day>=31-(((5*year/4)+1)%7) ) summerTime=0; 487 488 489 Serial.print("Summertime: "); 490 Serial.println(summerTime); 491 492 493 unsigned short MoZiff[12] = { 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; //Monatsziffer 494 unsigned short Tage_Monat[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 495 if(h+utcOffset+summerTime>23) { 496 // if(++day>31) { day=1; month++; }; // needs better patch 497 // if(++dayOfWeek>7) dayOfWeek=1; 498 h = h - 24; 499 dayOfWeek = dayOfWeek +1; 500 if(dayOfWeek>7) dayOfWeek=1; 501 day = day + 1; 502 if (day > Tage_Monat[month - 1]) { 503 day = 1; 504 month = month + 1; 505 if (month > 12) { 506 month = 1; 507 year = year + 1; 508 } 509 } 510 } 511 512 monthloc = M_arr[month]; // localize month 513 dayloc = Dow_arr[dayOfWeek]; // localize DayOfWeek 514 515 date = dayloc + ", " + day + " " + monthloc + " " + year; 516 517 localMillisAtUpdate = millis(); 518 localEpoc = (h * 60 * 60 + m * 60 + s); 519 } 520 } 521 client.stop(); 522} 523 524// ======================================================================= 525 526//convert month string in month number 527 528int month2index (String month) 529{ 530 String months[12] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; 531 for (int i = 0; i < 12; i++) { 532 if (months[i] == month) 533 return i + 1; 534 535 } 536 return 0; 537} 538 539// ======================================================================= 540 541//convert dow string in dow number 542 543int dow2index (String dayOfWeek) 544{ 545 String dows[7] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"}; 546 for (int i = 0; i < 12; i++) { 547 if (dows[i] == dayOfWeek) 548 return i + 1; 549 550 } 551 return 0; 552} 553 554// ======================================================================= 555 556//check if it is summer time 557 558// int checkSummerTime() 559 560//{ 561 562 //if(month>3 && month<10) return 1; 563 //if(month==3 && day>=31-(((5*year/4)+4)%7) ) return 1; 564 //if(month==10 && day<31-(((5*year/4)+1)%7) ) return 0; 565 //return 0; 566 567 // return 1; 568 569//} 570 571 572 573// ======================================================================= 574 575 576 577void updateTime() 578{ 579 long curEpoch = localEpoc + ((millis() - localMillisAtUpdate) / 1000); 580 //long epoch = round(curEpoch + 3600 * (utcOffset+summerTime) + 86400L) % 86400L; 581 long epoch = (long)round(curEpoch + 3600 * (utcOffset+summerTime) + 86400L) % 86400L; // fix compiler error 582 //long epoch = round(curEpoch + 3600 * (utcOffset+summerTime) + 86400L); 583 h = ((epoch % 86400L) / 3600) % 24; 584 m = (epoch % 3600) / 60; 585 s = epoch % 60; 586 587 588} 589 590// ======================================================================= 591 592void contrast() 593{ 594 int lumina = analogRead(A0); //autobrightness (+3.3V ---|==10k==|---A0---|GND (niq_ro) 595 lumina = map(lumina, 0, 1023, 0, 4); //broghtness 0-4 596 //Serial.print("brightness = "); 597 //Serial.print(lumina); 598 //Serial.println (" / 4"); 599 sendCmdAll(CMD_INTENSITY, lumina); 600} 601
WiFi NodeMCU ESP8266 Google Clock - v.11 Oct2020
arduino
1 2//V.3 - Data converted and daylight savings time 3//V.4 - localized data, days... (use yours....) 4//V.6 - thermometer and hygrometer 5//V.7 - animated clock corrected + seconds +1,5 6//V.8 - tuning thermometer and hygrometer 7//v.9 - DST corrected / compiler error corrected (long)round 8//v.10 - Date change at 00:00 corrected for GMT>0 9//v.11 - automitc panel brightness (+3.3V ---|==10k==|---A0---|GND 10// review: anthias64@gmail.com 11// kudos original maker code can't find its name... 12 13#include "Arduino.h" 14#include <ArduinoJson.h> 15#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library (you most likely already have this in your sketch) 16 17WiFiClient client; 18 19#define NUM_MAX 6 20 21// for NodeMCU 1.0 22#define DIN_PIN 13 // D7 23#define CS_PIN 0 // D3 24#define CLK_PIN 14 // D5 25 26#include "max7219.h" 27#include "fonts.h" 28 29#include "DHT.h" // sensor DHT 30 31// DHT sensor 32#define DHTPIN 12 // what pin we're connected to // GPIO 12 = D6 33// Uncomment whatever type you're using! 34//#define DHTTYPE DHT11 // DHT 11 35#define DHTTYPE DHT22 // DHT 22 (AM2302) 36//#define DHTTYPE DHT21 // DHT 21 (AM2301) 37 38DHT dht(DHTPIN, DHTTYPE, 11); // for ESP8266 39 40//DHT 41float temperature = 0; 42int temp = 0; 43byte minus = 0; 44int umidity = 0; 45int poz, poz2; 46int mult = 0; 47 48float temp_offset = -1.0; // taratura termometro 49float rh_offset = +3; // taratura igrometro 50 51char TempLabel[] = " Temperatura: "; // Fixxed text 52char UmiLabel[] = " Umidita': "; // Fixxed text 53 54//modifica test ip 55 56const char ssid[] = "xxxxxxxxx"; // your network SSID (name) <-------------- 57const char password[] = "xxxxxxxxxxxxxxxxxx"; // your network password <-------------- 58 59// Localized Months and DoWeek 60 61String M_arr[13] = {" ", "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"}; 62String Dow_arr[8] = {" ", "Lunedi", "Martedi", "Mercoledi", "Giovedi", "Venerdi", "Sabato", "Domenica"}; 63 64void setup() 65{ 66 Serial.begin(115200); 67 68 initMAX7219(); // 69 sendCmdAll(CMD_SHUTDOWN,1); // 70 sendCmdAll(CMD_INTENSITY,1); // brightness from 1 to 5 71 72 Serial.print("Connecting to "); 73 Serial.println(ssid); 74 75 WiFi.begin(ssid, password); 76 77 printStringWithShift("Connecting",30); // 78 delay (1000); 79 80 while (WiFi.status() != WL_CONNECTED) { 81 delay(500); 82 Serial.print("."); 83 } 84 85 86 Serial.print("IP number assigned by DHCP is "); 87 Serial.println(WiFi.localIP()); 88 89 printStringWithShift((String(" IP:")+WiFi.localIP().toString()).c_str(), 30);// 90 delay(1000);// 91 92 // DHT 93 94 95dht.begin(); 96temperature = dht.readTemperature() + temp_offset; 97umidity = dht.readHumidity() + rh_offset; 98 99Serial.print(temperature); 100Serial.println(" gr.Celsius"); 101 102Serial.print(umidity); 103Serial.println(" %RH"); 104 105 if (temperature < 0) 106 { 107 temp = -10*temperature; 108 minus = 1; 109 } 110 else 111 { 112 minus = 0; 113 temp = 10*temperature; 114 } 115 116} 117 118 119// =============================DEFINE VARS============================== 120#define MAX_DIGITS 20 121byte dig[MAX_DIGITS]={0}; 122byte digold[MAX_DIGITS]={0}; 123byte digtrans[MAX_DIGITS]={0}; 124int updCnt = 0; 125int dots = 0; 126long dotTime = 0; 127long clkTime = 0; 128int dx=0; 129int dy=0; 130byte del=0; 131int h,m,s; 132float utcOffset = +1; // ==> your timezone 133long localEpoc = 0; 134long localMillisAtUpdate = 0; 135int day, month, year, dayOfWeek; 136int monthnum = 0; 137int downum = 0; 138int summerTime = 0; 139String date; 140String dayloc; 141String monthloc; 142 143 144// ======================================================================= 145 146void loop() 147{ 148 if(updCnt<=0) { // every 10 scrolls, ~450s=7.5m 149 updCnt = 60; 150 Serial.println("Getting data ..."); 151 // clr(); 152 printStringWithShift(" Get Time...",30); 153 getTime(); 154 Serial.println("Data loaded"); 155 clkTime = millis(); 156 157 } 158 159 contrast(); // //autobrightness 160 161//print data 162 163 if(millis()-clkTime > 30000 && !del && dots) { // clock for 30s, then scrolls for about 30s 164 printStringWithShift(" ",40); //Space before 165 printStringWithShift(date.c_str(),40); //data1 166 167// Reading temperature or humidity 168 int umidity = dht.readHumidity()+ rh_offset; 169 float temperature = dht.readTemperature()+ temp_offset; 170 171 //umidity = umidity + rh_offset; 172 //temperature = temperature + temp_offset; 173 174 Serial.println("Temp e Umid corretti"); 175 Serial.println(temperature); 176 Serial.println(umidity); 177 178 int t2 = 10*temperature; 179 180// print temperature 181 182// http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/ 183char c[3], d[2]; 184String str1, str2; 185int t2z = t2/10; 186int t2u = t2 - t2z*10; 187str1=String(t2z); 188str1.toCharArray(c,3); 189str2=String(t2u); 190str2.toCharArray(d,2); 191printStringWithShift(TempLabel ,40); // Send scrolling Text 192printStringWithShift("+" ,40); 193printStringWithShift(c ,40); 194printStringWithShift("," ,40); 195printStringWithShift(d ,40); 196printStringWithShift(" C^" ,40); 197//delay(2000); 198 199// print umidity 200 201// http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/ 202char b[3]; 203String str; 204str=String(umidity); 205str.toCharArray(b,3); 206printStringWithShift(UmiLabel ,40); // Send scrolling Text 207printStringWithShift(b ,40); 208printStringWithShift(" %" ,40); 209//delay(2000); 210 211 printStringWithShift(" ",40); //Space after 212 delay(200); 213 214 updCnt--; 215 clkTime = millis(); 216 } 217 if(millis()-dotTime > 500) { 218 dotTime = millis(); 219 dots = !dots; 220 } 221 updateTime(); // get time 222 223// print clock 224 showAnimClock(); 225 //showSimpleClock(); 226 227} 228 229// ======================================================================= 230 231void showSimpleClock() 232{ 233 dx=dy=0; 234 clr(); 235 showDigit(h/10, 0, dig6x8); 236 showDigit(h%10, 8, dig6x8); 237 showDigit(m/10, 17, dig6x8); 238 showDigit(m%10, 25, dig6x8); 239 showDigit(s/10, 34, dig6x8); 240 showDigit(s%10, 42, dig6x8); 241 setCol(15,dots ? B00100100 : 0); 242 setCol(32,dots ? B00100100 : 0); 243 refreshAll(); 244} 245 246// ======================================================================= 247 248void showAnimClock() 249{ 250 byte digPos[6]={0,8,17,25,34,42}; 251 int digHt = 12; 252 int num = 6; 253 int i; 254 if(del==0) { 255 del = digHt; 256 for(i=0; i<num; i++) digold[i] = dig[i]; 257 dig[0] = h/10 ? h/10 : 10; 258 dig[1] = h%10; 259 dig[2] = m/10; 260 dig[3] = m%10; 261 dig[4] = s/10; 262 dig[5] = s%10; 263 for(i=0; i<num; i++) digtrans[i] = (dig[i]==digold[i]) ? 0 : digHt; 264 } else 265 del--; 266 267 clr(); 268 for(i=0; i<num; i++) { 269 if(digtrans[i]==0) { 270 dy=0; 271 showDigit(dig[i], digPos[i], dig6x8); 272 } else { 273 dy = digHt-digtrans[i]; 274 showDigit(digold[i], digPos[i], dig6x8); 275 dy = -digtrans[i]; 276 showDigit(dig[i], digPos[i], dig6x8); 277 digtrans[i]--; 278 } 279 } 280 dy=0; 281 setCol(15,dots ? B00100100 : 0); 282 setCol(32,dots ? B00100100 : 0); 283 refreshAll(); 284 delay(10); //default 30 285} 286 287// ======================================================================= 288 289void showDigit(char ch, int col, const uint8_t *data) 290{ 291 if(dy<-8 | dy>8) return; 292 int len = pgm_read_byte(data); 293 int w = pgm_read_byte(data + 1 + ch * len); 294 col += dx; 295 for (int i = 0; i < w; i++) 296 if(col+i>=0 && col+i<8*NUM_MAX) { 297 byte v = pgm_read_byte(data + 1 + ch * len + 1 + i); 298 if(!dy) scr[col + i] = v; else scr[col + i] |= dy>0 ? v>>dy : v<<-dy; 299 } 300} 301 302// ======================================================================= 303 304void setCol(int col, byte v) 305{ 306 if(dy<-8 | dy>8) return; 307 col += dx; 308 if(col>=0 && col<8*NUM_MAX) 309 if(!dy) scr[col] = v; else scr[col] |= dy>0 ? v>>dy : v<<-dy; 310} 311 312// ======================================================================= 313 314int showChar(char ch, const uint8_t *data) 315{ 316 int len = pgm_read_byte(data); 317 int i,w = pgm_read_byte(data + 1 + ch * len); 318 for (i = 0; i < w; i++) 319 scr[NUM_MAX*8 + i] = pgm_read_byte(data + 1 + ch * len + 1 + i); 320 scr[NUM_MAX*8 + i] = 0; 321 return w; 322} 323 324// ======================================================================= 325 326 327int dualChar = 0; 328 329unsigned char convertPolish(unsigned char _c) 330{ 331 unsigned char c = _c; 332 if(c==196 || c==197 || c==195) { 333 dualChar = c; 334 return 0; 335 } 336 if(dualChar) { 337 switch(_c) { 338 case 133: c = 1+'~'; break; // '' 339 case 135: c = 2+'~'; break; // '' 340 case 153: c = 3+'~'; break; // '' 341 case 130: c = 4+'~'; break; // '' 342 case 132: c = dualChar==197 ? 5+'~' : 10+'~'; break; // '' and '' 343 case 179: c = 6+'~'; break; // '' 344 case 155: c = 7+'~'; break; // '' 345 case 186: c = 8+'~'; break; // '' 346 case 188: c = 9+'~'; break; // '' 347 //case 132: c = 10+'~'; break; // '' 348 case 134: c = 11+'~'; break; // '' 349 case 152: c = 12+'~'; break; // '' 350 case 129: c = 13+'~'; break; // '' 351 case 131: c = 14+'~'; break; // '' 352 case 147: c = 15+'~'; break; // '' 353 case 154: c = 16+'~'; break; // '' 354 case 185: c = 17+'~'; break; // '' 355 case 187: c = 18+'~'; break; // '' 356 default: break; 357 } 358 dualChar = 0; 359 return c; 360 } 361 switch(_c) { 362 case 185: c = 1+'~'; break; 363 case 230: c = 2+'~'; break; 364 case 234: c = 3+'~'; break; 365 case 179: c = 4+'~'; break; 366 case 241: c = 5+'~'; break; 367 case 243: c = 6+'~'; break; 368 case 156: c = 7+'~'; break; 369 case 159: c = 8+'~'; break; 370 case 191: c = 9+'~'; break; 371 case 165: c = 10+'~'; break; 372 case 198: c = 11+'~'; break; 373 case 202: c = 12+'~'; break; 374 case 163: c = 13+'~'; break; 375 case 209: c = 14+'~'; break; 376 case 211: c = 15+'~'; break; 377 case 140: c = 16+'~'; break; 378 case 143: c = 17+'~'; break; 379 case 175: c = 18+'~'; break; 380 default: break; 381 } 382 return c; 383} 384 385 386// ======================================================================= 387 388void printCharWithShift(unsigned char c, int shiftDelay) { 389 c = convertPolish(c); 390 if (c < ' ' || c > '~'+25) return; 391 c -= 32; 392 int w = showChar(c, font); 393 for (int i=0; i<w+1; i++) { 394 delay(shiftDelay); 395 scrollLeft(); 396 refreshAll(); 397 } 398} 399 400// ======================================================================= 401 402void printStringWithShift(const char* s, int shiftDelay){ 403 while (*s) { 404 printCharWithShift(*s, shiftDelay); 405 s++; 406 } 407} 408// ======================================================================= 409 410void getTime() 411{ 412 WiFiClient client; 413 if (!client.connect("www.google.com", 80)) { 414 Serial.println("connection to google failed"); 415 return; 416 } 417 418 client.print(String("GET / HTTP/1.1\ \ 419") + 420 String("Host: www.google.com\ \ 421") + 422 String("Connection: close\ \ 423\ \ 424")); 425 int repeatCounter = 0; 426 while (!client.available() && repeatCounter < 10) { 427 delay(500); 428 //Serial.println("."); 429 repeatCounter++; 430 } 431 432 String line; 433 client.setNoDelay(false); 434 while(client.connected() && client.available()) { 435 line = client.readStringUntil('\n'); 436 line.toUpperCase(); 437 // Serial.println(line); //test stringa 438 if (line.startsWith("DATE: ")) { 439 //date = " "+line.substring(6, 22); 440 date =line.substring(6, 22); 441 date.toUpperCase(); 442 443 Serial.println(date); //check 444 445 int day = atoi (date.substring(5,7).c_str()); //day number 446 int year = 2000 + atoi (date.substring(13,16).c_str()); //year number 447 448 monthnum = month2index(date.substring(8,11)); // convert month in mumber 449 month = monthnum; //check 450 downum = dow2index(date.substring(0,3)); // convert dow in mumber 451 dayOfWeek = downum; //check 452 453 monthloc = M_arr[month]; // localize month 454 dayloc = Dow_arr[dayOfWeek]; // localize DayOfWeek 455 456 date = dayloc + " " + day + " " + monthloc + " " + year; 457 458 459 460 // decodeDate(date); //data3 461 h = line.substring(23, 25).toInt(); 462 m = line.substring(26, 28).toInt(); 463 s = line.substring(29, 31).toInt()+1.5; //correzione ora 464 465 // summerTime = checkSummerTime(); 466 467// calcolo ora legale 468 469 Serial.print("Ora GMT: " ); 470 Serial.print(h); //check 471 Serial.print(" : " ); 472 Serial.print(m); //check 473 Serial.print(" : " ); 474 Serial.print(s); //check 475 Serial.println(""); //check 476 Serial.print("Data: " ); 477 Serial.println(date); //check 478 479 //day = 31; 480 //year = 2021; 481 //month = 10; 482 483 if(month>3 && month<=10) summerTime=1; 484 if(month==3 && day>=31-(((5*year/4)+4)%7) ) summerTime=1; 485 if(month==10 && day>=31-(((5*year/4)+1)%7) ) summerTime=0; 486 487 488 Serial.print("Summertime: "); 489 Serial.println(summerTime); 490 491 492 unsigned short MoZiff[12] = { 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; //Monatsziffer 493 unsigned short Tage_Monat[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 494 if(h+utcOffset+summerTime>23) { 495 // if(++day>31) { day=1; month++; }; // needs better patch 496 // if(++dayOfWeek>7) dayOfWeek=1; 497 h = h - 24; 498 dayOfWeek = dayOfWeek +1; 499 if(dayOfWeek>7) dayOfWeek=1; 500 day = day + 1; 501 if (day > Tage_Monat[month - 1]) { 502 day = 1; 503 month = month + 1; 504 if (month > 12) { 505 month = 1; 506 year = year + 1; 507 } 508 } 509 } 510 511 monthloc = M_arr[month]; // localize month 512 dayloc = Dow_arr[dayOfWeek]; // localize DayOfWeek 513 514 date = dayloc + ", " + day + " " + monthloc + " " + year; 515 516 localMillisAtUpdate = millis(); 517 localEpoc = (h * 60 * 60 + m * 60 + s); 518 } 519 } 520 client.stop(); 521} 522 523// ======================================================================= 524 525//convert month string in month number 526 527int month2index (String month) 528{ 529 String months[12] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; 530 for (int i = 0; i < 12; i++) { 531 if (months[i] == month) 532 return i + 1; 533 534 } 535 return 0; 536} 537 538// ======================================================================= 539 540//convert dow string in dow number 541 542int dow2index (String dayOfWeek) 543{ 544 String dows[7] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"}; 545 for (int i = 0; i < 12; i++) { 546 if (dows[i] == dayOfWeek) 547 return i + 1; 548 549 } 550 return 0; 551} 552 553// ======================================================================= 554 555//check if it is summer time 556 557// int checkSummerTime() 558 559//{ 560 561 //if(month>3 && month<10) return 1; 562 //if(month==3 && day>=31-(((5*year/4)+4)%7) ) return 1; 563 //if(month==10 && day<31-(((5*year/4)+1)%7) ) return 0; 564 //return 0; 565 566 // return 1; 567 568//} 569 570 571 572// ======================================================================= 573 574 575 576void updateTime() 577{ 578 long curEpoch = localEpoc + ((millis() - localMillisAtUpdate) / 1000); 579 //long epoch = round(curEpoch + 3600 * (utcOffset+summerTime) + 86400L) % 86400L; 580 long epoch = (long)round(curEpoch + 3600 * (utcOffset+summerTime) + 86400L) % 86400L; // fix compiler error 581 //long epoch = round(curEpoch + 3600 * (utcOffset+summerTime) + 86400L); 582 h = ((epoch % 86400L) / 3600) % 24; 583 m = (epoch % 3600) / 60; 584 s = epoch % 60; 585 586 587} 588 589// ======================================================================= 590 591void contrast() 592{ 593 int lumina = analogRead(A0); //autobrightness (+3.3V ---|==10k==|---A0---|GND (niq_ro) 594 lumina = map(lumina, 0, 1023, 0, 4); //broghtness 0-4 595 //Serial.print("brightness = "); 596 //Serial.print(lumina); 597 //Serial.println (" / 4"); 598 sendCmdAll(CMD_INTENSITY, lumina); 599} 600
Downloadable files
WiFi NodeMCU ESP8266 Google Clock - v.11 Oct2020
WiFi NodeMCU ESP8266 Google Clock - v.11 Oct2020

Comments
Only logged in users can leave comments