Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.05 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <TimeLib.h>
  3. #include <WiFiUdp.h>
  4. #include <string>
  5. #include <SPI.h>
  6. #include <Wire.h>
  7. #include <Adafruit_GFX.h>
  8. #include <Adafruit_SSD1306.h>
  9. #include "icons.h"
  10.  
  11. #define OLED_RESET LED_BUILTIN//4
  12. Adafruit_SSD1306 display(OLED_RESET);
  13.  
  14. #define NUMFLAKES 10
  15. #define XPOS 0
  16. #define YPOS 1
  17. #define DELTAY 2
  18.  
  19. #define LOGO16_GLCD_HEIGHT 16
  20. #define LOGO16_GLCD_WIDTH 16
  21.  
  22. const char *ssid = "Redmi";
  23. const char *pass = "666dupa666";
  24. const char *servername = "api.openweathermap.org";
  25. String city = "Zabrze"; //Enter your City Name
  26.  
  27. // NTP Servers:
  28. static const char ntpServerName[] = "us.pool.ntp.org";
  29. const int timeZone = +530; // Central European Time
  30. WiFiUDP Udp;
  31. unsigned int localPort = 8888; // local port to listen for UDP packets
  32.  
  33. time_t getNtpTime();
  34. void digitalClockDisplay();
  35. void printDigits(int digits);
  36. void sendNTPpacket(IPAddress &address);
  37.  
  38.  
  39. unsigned long oldMilli;
  40. unsigned long newMillis;
  41. unsigned long interval = 1000 * 60 * 5;
  42.  
  43. String old_time = "00";
  44. //String temperature;
  45. //String humidity;
  46. //String icon;
  47. String result;
  48.  
  49. WiFiClient client;
  50.  
  51. void setWeather();
  52.  
  53. void setup() {
  54. Serial.begin(115200);
  55. delay(10);
  56. Serial.println("");
  57. Serial.print("Connecting to ");
  58. Serial.println(ssid);
  59.  
  60. WiFi.begin(ssid, pass);
  61. while (WiFi.status() != WL_CONNECTED)
  62. {
  63. delay(500);
  64. Serial.print(".");
  65. }
  66. Serial.println("");
  67. Serial.println("Wifi Connected!");
  68. Serial.print("IP = ");
  69. Serial.println(WiFi.localIP());
  70.  
  71. display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  72. display.setTextSize(2);
  73. display.setTextColor(WHITE);
  74. display.setCursor(0, 0);
  75. //display.clearDisplay();
  76. //display.println(WiFi.localIP());
  77. //display.clearDisplay();
  78. //display.drawBitmap(0, 0, myBitmap, 24, 24, WHITE);
  79. //display.display();
  80. //delay(2000);
  81.  
  82. display.clearDisplay();
  83. display.drawBitmap(48, 16, draw01d, 32, 32, WHITE);
  84. display.display();
  85. delay(500);
  86.  
  87. display.clearDisplay();
  88. display.drawBitmap(48, 16, draw01n, 32, 32, WHITE);
  89. display.display();
  90. delay(500);
  91.  
  92. display.clearDisplay();
  93. display.drawBitmap(48, 16, draw02d, 32, 32, WHITE);
  94. display.display();
  95. delay(500);
  96.  
  97.  
  98. display.clearDisplay();
  99. display.drawBitmap(48, 16, draw02n, 32, 32, WHITE);
  100. display.display();
  101. delay(500);
  102.  
  103.  
  104. display.clearDisplay();
  105. display.drawBitmap(48, 16, draw03d, 32, 32, WHITE);
  106. display.display();
  107. delay(500);
  108.  
  109. display.clearDisplay();
  110. display.drawBitmap(48, 16, draw09d, 32, 32, WHITE);
  111. display.display();
  112. delay(500);
  113.  
  114. display.clearDisplay();
  115. display.drawBitmap(48, 16, draw11d, 32, 32, WHITE);
  116. display.display();
  117. delay(500);
  118.  
  119. display.clearDisplay();
  120. display.drawBitmap(48, 16, draw11d, 32, 32, WHITE);
  121. display.display();
  122. delay(500);
  123.  
  124. display.clearDisplay();
  125. display.drawBitmap(48, 16, draw11n, 32, 32, WHITE);
  126. display.display();
  127. delay(500);
  128.  
  129. //delay(2000)
  130. //display.display();
  131.  
  132. setWeather();
  133. oldMilli = millis();
  134.  
  135. Udp.begin(localPort);
  136. setSyncProvider(getNtpTime);
  137. setSyncInterval(300);
  138.  
  139.  
  140. }
  141.  
  142. String getValue(char *key)
  143. {
  144. String token;
  145. volatile int i = result.indexOf(key);
  146. while ((result[i] <= 47) || (result[i] >= 58 ))
  147. {
  148. i++;
  149. }
  150.  
  151. while (result[i] != ',') {
  152. token += result[i];
  153. i++;
  154. }
  155. return token;
  156. }
  157.  
  158. time_t prevDisplay = 0; // when the digital clock was displayed
  159. String prevTime = "";
  160. int prevDay = 0;
  161. String prevWeekDay = "";
  162.  
  163. void loop() {
  164. newMillis = millis();
  165.  
  166. if (timeStatus() != timeNotSet) {
  167. if (minute() != prevDisplay) { //update the display only if time has changed
  168. prevDisplay = minute();
  169. digitalClockDisplay();
  170. }
  171. }
  172.  
  173. if (newMillis - oldMilli > interval)
  174. {
  175. setWeather();
  176. digitalClockDisplay();
  177. }
  178. }
  179.  
  180. void digitalClockDisplay()
  181. {
  182. String currTime;
  183. String currDate;
  184.  
  185. if(hour() < 10)
  186. currTime += "0";
  187. currTime += String(hour()) + ":";
  188.  
  189. if(minute() < 10)
  190. currTime += "0";
  191. currTime += String(minute());
  192.  
  193. Serial.print("Time = ");
  194. Serial.println(currTime);
  195. display.setTextColor(BLACK);
  196. display.setCursor(0, 0);
  197. display.print(prevTime);
  198. display.display();
  199. display.setTextColor(WHITE);
  200. display.setCursor(0, 0);
  201. display.print(currTime);
  202.  
  203. if (day() != prevDay)
  204. {
  205. //prevDay = day();
  206. display.setCursor(95, 0);
  207. display.setTextSize(1);
  208. display.setTextColor(BLACK);
  209. display.println(prevDay);
  210.  
  211. display.setCursor(95, 0);
  212. display.setTextColor(WHITE);
  213.  
  214. if(day() < 10)
  215. currDate += "0";
  216. currDate += String(day()) + "/";
  217.  
  218. if(month() < 10)
  219. currDate += "0";
  220. currDate += String(month()) /*+ "/" + String(year())*/;
  221. display.print(currDate);
  222.  
  223. display.setCursor(95, 8);
  224. display.setTextColor(BLACK);
  225. display.println(prevWeekDay);
  226. display.setCursor(95, 8);
  227. display.setTextColor(WHITE);
  228.  
  229. switch (weekday()) {
  230.  
  231. case 1:
  232. display.print("Sun");
  233. prevWeekDay = "Sun";
  234. break;
  235.  
  236. case 2:
  237. display.print("Mon");
  238. prevWeekDay = "Mon";
  239. break;
  240.  
  241. case 3:
  242. display.print("Tue");
  243. prevWeekDay = "Tue";
  244. break;
  245.  
  246. case 4:
  247. display.print("Wed");
  248. prevWeekDay = "Wed";
  249. break;
  250.  
  251. case 5:
  252. display.print("Thu");
  253. prevWeekDay = "Thu";
  254. break;
  255.  
  256. case 6:
  257. display.print("Fri");
  258. prevWeekDay = "Fri";
  259. break;
  260.  
  261. case 7:
  262. display.print("Sat");
  263. prevWeekDay = "Sat";
  264. break;
  265.  
  266. default:
  267. break;
  268.  
  269. }
  270. }
  271. else {
  272. display.setTextSize(1);
  273. display.setCursor(95, 0);
  274. display.println(currDate);
  275. display.setCursor(95, 8);
  276. display.println(prevWeekDay);
  277. display.setTextSize(2);
  278. }
  279.  
  280. display.display();
  281. display.setTextSize(2);
  282.  
  283. prevTime = currTime;
  284. }
  285.  
  286.  
  287. void setWeather()
  288. {
  289.  
  290. String temperature;
  291. String humidity;
  292. String icon;
  293.  
  294. oldMilli = millis();
  295.  
  296. client.stop();
  297. if (client.connect(servername, 80))
  298. {
  299. client.println("GET /data/2.5/weather?q="+ city + "&units=metric&APPID=8b88ec290029617ed8640c45d3a685cc&mode=json&cnt=2 HTTP/1.1");
  300. client.println("Host: api.openweathermap.org");
  301. client.println("User-Agent: ESP8266");
  302. client.println("Connection: close");
  303. client.println();
  304.  
  305. while (client.connected() || client.available()) {
  306. char c = client.read();
  307. result = result + c;
  308. }
  309.  
  310. Serial.println(result);
  311. temperature = getValue("\"temp\":");
  312. humidity = getValue("\"humidity\":");
  313. icon = getValue("\"icon\"");
  314. result = "";
  315.  
  316. Serial.print("Icon = ");
  317. Serial.println(icon);
  318. Serial.print("Temperature = ");
  319. Serial.println(temperature);
  320. Serial.print("Humidity = ");
  321. Serial.println(humidity);
  322.  
  323. client.stop();
  324.  
  325. display.clearDisplay();
  326. display.setCursor(0, 20);
  327. display.print(temperature);
  328. display.setTextSize(1);
  329. display.print(" o");
  330. display.setTextSize(2);
  331. display.println("C");
  332. display.print(humidity);
  333. display.println(" %");
  334. display.setCursor(49, 54);
  335. display.setTextSize(1);
  336. display.print(city);
  337. display.display();
  338. display.setTextSize(2);
  339.  
  340. if (icon == "01d\"}]")
  341. display.drawBitmap(75, 20, draw01d, 32, 32, WHITE);
  342.  
  343. if (icon == "01n\"}]")
  344. display.drawBitmap(75, 20, draw01n, 32, 32, WHITE);
  345.  
  346. if (icon == "02d\"}]")
  347. display.drawBitmap(75, 20, draw02d, 32, 32, WHITE);
  348.  
  349. if (icon == "02n\"}]")
  350. display.drawBitmap(75, 20, draw02n, 32, 32, WHITE);
  351.  
  352. if (icon == "03d\"}]")
  353. display.drawBitmap(75, 20, draw03d, 32, 32, WHITE);
  354.  
  355. if (icon == "03n\"}]")
  356. display.drawBitmap(75, 20, draw03n, 32, 32, WHITE);
  357.  
  358. if (icon == "04d\"}]")
  359. display.drawBitmap(75, 20, draw04d, 32, 32, WHITE);
  360.  
  361. if (icon == "04n\"}]")
  362. display.drawBitmap(75, 20, draw04n, 32, 32, WHITE);
  363.  
  364. if (icon == "09d\"}]")
  365. display.drawBitmap(75, 20, draw09d, 32, 32, WHITE);
  366.  
  367. if (icon == "09n\"}]")
  368. display.drawBitmap(75, 20, draw09n, 32, 32, WHITE);
  369.  
  370. if (icon == "10d\"}]")
  371. display.drawBitmap(75, 20, draw10d, 32, 32, WHITE);
  372.  
  373. if (icon == "10n\"}]")
  374. display.drawBitmap(75, 20, draw10n, 32, 32, WHITE);
  375.  
  376. if (icon == "11d\"}]")
  377. display.drawBitmap(75, 20, draw11d, 32, 32, WHITE);
  378.  
  379. if (icon == "11n\"}]")
  380. display.drawBitmap(75, 20, draw11n, 32, 32, WHITE);
  381.  
  382. if (icon == "50d\"}]")
  383. display.drawBitmap(75, 20, draw50d, 32, 32, WHITE);
  384.  
  385. if (icon == "50n\"}]")
  386. display.drawBitmap(75, 20, draw50n, 32, 32, WHITE);
  387.  
  388. display.display();
  389. }
  390. }
  391.  
  392. const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
  393. byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
  394.  
  395. time_t getNtpTime()
  396. {
  397. IPAddress ntpServerIP; // NTP server's ip address
  398.  
  399. while (Udp.parsePacket() > 0) ; // discard any previously received packets
  400. Serial.println("Transmit NTP Request");
  401. // get a random server from the pool
  402. WiFi.hostByName(ntpServerName, ntpServerIP);
  403. Serial.print(ntpServerName);
  404. Serial.print(": ");
  405. Serial.println(ntpServerIP);
  406. sendNTPpacket(ntpServerIP);
  407. uint32_t beginWait = millis();
  408. while (millis() - beginWait < 1500) {
  409. int size = Udp.parsePacket();
  410. if (size >= NTP_PACKET_SIZE) {
  411. Serial.println("Receive NTP Response");
  412. Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
  413. unsigned long secsSince1900;
  414. // convert four bytes starting at location 40 to a long integer
  415. secsSince1900 = (unsigned long)packetBuffer[40] << 24;
  416. secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
  417. secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
  418. secsSince1900 |= (unsigned long)packetBuffer[43];
  419. return secsSince1900 - 2208988800UL + 3600; //Indian Time zone is +5:30 or 19800 epoch. Adjust according to your timezone
  420. }
  421. }
  422. Serial.println("No NTP Response :-(");
  423. return 0; // return 0 if unable to get the time
  424. }
  425.  
  426. // send an NTP request to the time server at the given address
  427. void sendNTPpacket(IPAddress &address)
  428. {
  429. // set all bytes in the buffer to 0
  430. memset(packetBuffer, 0, NTP_PACKET_SIZE);
  431. // Initialize values needed to form NTP request
  432. // (see URL above for details on the packets)
  433. packetBuffer[0] = 0b11100011; // LI, Version, Mode
  434. packetBuffer[1] = 0; // Stratum, or type of clock
  435. packetBuffer[2] = 6; // Polling Interval
  436. packetBuffer[3] = 0xEC; // Peer Clock Precision
  437. // 8 bytes of zero for Root Delay & Root Dispersion
  438. packetBuffer[12] = 49;
  439. packetBuffer[13] = 0x4E;
  440. packetBuffer[14] = 49;
  441. packetBuffer[15] = 52;
  442. // all NTP fields have been given values, now
  443. // you can send a packet requesting a timestamp:
  444. Udp.beginPacket(address, 123); //NTP requests are to port 123
  445. Udp.write(packetBuffer, NTP_PACKET_SIZE);
  446. Udp.endPacket();
  447. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement