Advertisement
Guest User

Untitled

a guest
Oct 26th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. #include <LiquidCrystal_I2C.h>
  2. #include <Wire.h>
  3. #include <ESP8266WiFi.h>
  4.  
  5. #include <NTPClient.h>
  6. #include <Time.h>
  7. #include <TimeLib.h>
  8. #include <Timezone.h>
  9. #include <WiFiUdp.h>
  10. #include <String.h>
  11.  
  12. #include <WiFiClientSecure.h>
  13. #include <ArduinoJson.h>
  14. WiFiClient client;
  15. LiquidCrystal_I2C lcd(0x20, 16, 2);
  16. const char* ssid = "NAZWA WIFI";
  17. const char* password = "HASŁO WIFI";
  18. String APIKEY = "API Z STRONY OPENWEATHERMAP.ORG";
  19. String CityID = "ID MIASTA Z STRONY OPENWEATHERMAP.ORG"; //NALEZY ZMIENIĆ JESZCZE LINIJKE String location = "TWOJA MIEJSCOWOŚĆ";
  20.  
  21.  
  22.  
  23.  
  24. #define NTP_OFFSET 60 * 60 // In seconds
  25. #define NTP_INTERVAL 60 * 1000 // In miliseconds
  26. #define NTP_ADDRESS "uk.pool.ntp.org" // change this to whatever pool is closest (see ntp.org)
  27.  
  28. // Set up the NTP UDP client
  29. WiFiUDP ntpUDP;
  30. NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
  31.  
  32.  
  33. String date; //create the string for the date which will be printed on the lcd screen below
  34. String t; // create the string for the time
  35.  
  36. const char * days[] = {"Niedziela", "Pon", "Wtorek", "Sroda", "Czwartek", "Piatek", "Sobota"} ;//my screen is 20 across to can't fit in Wednesday
  37. const char * months[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"} ;
  38.  
  39.  
  40.  
  41. char servername[]="api.openweathermap.org";
  42. String result;
  43.  
  44. int counter = 10;
  45.  
  46. String weatherDescription ="";
  47. String weatherLocation = "";
  48. String Country;
  49. float Temperature;
  50. float Humidity;
  51. float Pressure;
  52.  
  53.  
  54. int show = -1;
  55.  
  56. void setup()
  57. {
  58. int error;
  59.  
  60. Serial.begin(115200);
  61. timeClient.begin();
  62. int cursorPosition=0;
  63. Serial.println("LCD...");
  64.  
  65.  
  66. while (!Serial)
  67. ;
  68.  
  69. Serial.println("Dose: check for LCD");
  70.  
  71.  
  72. Wire.begin();
  73. Wire.beginTransmission(0x20);
  74. error = Wire.endTransmission();
  75. Serial.print("Error: ");
  76. Serial.print(error);
  77.  
  78. if (error == 0) {
  79. Serial.println(": LCD found.");
  80. show = 0;
  81. lcd.begin(16, 2);
  82. Serial.println("POLACZONO");
  83. WiFi.begin(ssid, password);
  84.  
  85. while (WiFi.status() != WL_CONNECTED) {
  86. delay(500);
  87. lcd.setCursor(cursorPosition,2);
  88. lcd.print(".");
  89. cursorPosition++;
  90. }
  91. lcd.clear();
  92. lcd.setCursor(0, 0);
  93. lcd.print(" DZIEN DOBRY");
  94. lcd.setCursor(0, 1);
  95. lcd.print("LACZENIE Z BAZA");
  96.  
  97. Serial.println("Connected");
  98. delay(1000);
  99. } else {
  100. Serial.println(": LCD not found.");
  101. }
  102.  
  103. }
  104.  
  105.  
  106. void loop()
  107. {
  108. if(counter == 10)
  109. {
  110. counter = 0;
  111. displayGettingData();
  112. delay(1000);
  113. getWeatherData();
  114. api.getChannelStatistics(CHANNEL_ID);
  115. }
  116. else
  117. {
  118. counter++;
  119.  
  120. date = "";
  121. t = "";
  122. timeClient.update();
  123. unsigned long epochTime = timeClient.getEpochTime();
  124. time_t local, utc;
  125. utc = epochTime;
  126. TimeChangeRule BST = {"BST", Last, Sun, Mar, 1, 60};
  127. TimeChangeRule GMT = {"GMT", Last, Sun, Oct, 2, 60};
  128. Timezone UK(BST, GMT);
  129. local = UK.toLocal(utc);
  130.  
  131. if (day(local) < 10)
  132. date += "0";
  133. date += day(local);
  134. date += ".";
  135. if (month(local) - 1 < 10)
  136. date += "0";
  137. date += months[month(local) - 1];
  138. date += ".";
  139. date += year(local);
  140.  
  141. t += days[weekday(local) - 1];
  142. t += " ";
  143. if (hour(local) < 10)
  144. t += "0";
  145. t += hour(local);
  146. t += ":";
  147. if (minute(local) < 10) // add a zero if minute is under 10
  148. t += "0";
  149. t += minute(local);
  150.  
  151. lcd.clear();
  152. lcd.setCursor(0, 0);
  153. lcd.print(t);
  154. lcd.setCursor(0, 1);
  155. lcd.print(date);
  156. delay(10000);
  157.  
  158. displayWeather(weatherLocation,weatherDescription);
  159. delay(3000);
  160.  
  161. displayConditions(Temperature,Humidity,Pressure);
  162. delay(10000);
  163.  
  164.  
  165. }
  166. }
  167.  
  168. void getWeatherData()
  169. {
  170. if (client.connect(servername, 80)) {
  171. client.println("GET /data/2.5/weather?id="+CityID+"&units=metric&APPID="+APIKEY);
  172. client.println("Host: api.openweathermap.org");
  173. client.println("User-Agent: ArduinoWiFi/1.1");
  174. client.println("Connection: close");
  175. client.println();
  176. }
  177. else {
  178. Serial.println("connection failed");
  179. Serial.println();
  180. }
  181.  
  182. while(client.connected() && !client.available()) delay(1);
  183. while (client.connected() || client.available()) {
  184. char c = client.read();
  185. result = result+c;
  186. }
  187.  
  188. client.stop(); //stop client
  189. result.replace('[', ' ');
  190. result.replace(']', ' ');
  191. Serial.println(result);
  192.  
  193. char jsonArray [result.length()+1];
  194. result.toCharArray(jsonArray,sizeof(jsonArray));
  195. jsonArray[result.length() + 1] = '\0';
  196.  
  197. StaticJsonBuffer<1024> json_buf;
  198. JsonObject &root = json_buf.parseObject(jsonArray);
  199. if (!root.success())
  200. {
  201. Serial.println("parseObject() failed");
  202. }
  203.  
  204. String location = "TWOJA MIEJSCOWOŚĆ";
  205. String country = root["sys"]["country"];
  206. float temperature = root["main"]["temp"];
  207. float humidity = root["main"]["humidity"];
  208. String weather = root["weather"]["main"];
  209. String description = root["weather"]["description"];
  210. float pressure = root["main"]["pressure"];
  211.  
  212. weatherDescription = description;
  213. weatherLocation = location;
  214. Country = country;
  215. Temperature = temperature;
  216. Humidity = humidity;
  217. Pressure = pressure;
  218.  
  219. }
  220.  
  221. void displayWeather(String location,String description)
  222. {
  223. lcd.clear();
  224. lcd.setCursor(0,0);
  225. lcd.print(location);
  226. lcd.print(", ");
  227. lcd.print(Country);
  228. lcd.setCursor(0,1);
  229. lcd.print(description);
  230. }
  231.  
  232. void displayConditions(float Temperature,float Humidity, float Pressure)
  233. {
  234. lcd.clear();
  235. lcd.print("T:");
  236. lcd.print(Temperature,1);
  237. lcd.print((char)223);
  238. lcd.print("C ");
  239.  
  240. //Printing Humidity
  241. lcd.print(" W:");
  242. lcd.print(Humidity,0);
  243. lcd.print(" %");
  244.  
  245. //Printing Pressure
  246. lcd.setCursor(0,1);
  247. lcd.print("C: ");
  248. lcd.print(Pressure,1);
  249. lcd.print(" hPa");
  250.  
  251. }
  252.  
  253. void displayGettingData()
  254. {
  255. lcd.clear();
  256. lcd.print(" Pobieram dane");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement