Guest User

Untitled

a guest
Jul 26th, 2019
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <HTTPClient.h>
  2. #include <ArduinoJson.h>
  3.  
  4. #include <WiFi.h>
  5. #include <NTPClient.h>
  6. #include <WiFiUdp.h>
  7.  
  8. #include <TFT_eSPI.h>
  9. #include <SPI.h>
  10.  
  11. #include <TimeLib.h>
  12.  
  13. #define TFT_BL          4  // Display backlight control pin
  14.  
  15. // Replace with your network credentials
  16. const char* ssid     = "";
  17. const char* password = "";
  18. // openweather.org key
  19. String WEATHERKEY="";
  20.  
  21. // temp in celcius
  22. double temperature = 0.0;
  23. // offset in seconds
  24. int gmtOffset_sec = 0;
  25.  
  26.  
  27. WiFiUDP ntpUDP;
  28. NTPClient timeClient(ntpUDP);
  29.  
  30. TFT_eSPI tft = TFT_eSPI();
  31. String payload, TZone;//, lastUpdate;
  32. String coordinateTZ; //lat=xx.xxx&lon=xxx.xxx
  33.  
  34. SemaphoreHandle_t serialMutex = NULL;
  35. void updateNTP (void *pvParams);
  36. void updateScreen (void *pvParams);
  37.  
  38. void setup() {
  39.   // put your setup code here, to run once:
  40.   Serial.begin (115200);
  41.  
  42.   serialMutex = xSemaphoreCreateMutex ();
  43.  
  44.   if (TFT_BL > 0) {
  45.     pinMode(TFT_BL, OUTPUT);
  46.     digitalWrite(TFT_BL, HIGH);
  47.   }
  48.  
  49.   tft.init();
  50.   tft.setRotation(1);
  51.   tft.fillScreen(TFT_BLACK);
  52.  
  53.   tft.setTextColor(TFT_YELLOW, TFT_BLACK); // Note: the new fonts do not draw the background colour
  54.  
  55.   WiFi.begin(ssid, password);
  56.   while (WiFi.status() != WL_CONNECTED) {
  57.     delay(500);
  58.     Serial.println (".");
  59.   }
  60.   Serial.println(WiFi.localIP());
  61.  
  62.  
  63.   timeClient.begin();
  64.   geolocation (); // guess where I am
  65.   timeClient.setTimeOffset(gmtOffset_sec);
  66.  
  67.   xTaskCreate (updateNTP, "NTP Client", 4096, NULL, 2, NULL);
  68.   xTaskCreate (updateScreen, "Screen", 4096, NULL, 1, NULL);
  69. }
  70.  
  71. void loop() {
  72. }
  73.  
  74. void getJson(String url) {  
  75.    if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
  76.      HTTPClient http;  //Declare an object of class HTTPClient
  77.      http.begin(url);  //Specify request destination
  78.     int httpCode = http.GET();                                                                  //Send the request
  79.      if (httpCode > 0) { //Check the returning code
  80.        payload = http.getString();   //Get the request response payload
  81.      
  82.     }
  83.  
  84.     http.end();   //Close connection
  85.  
  86.   }
  87. }
  88.  
  89. /*
  90.  * This function will use the external IP to get a Latitude and Longitude
  91.  * which will be used for many different things for ISS tracker.
  92.  * The website http://ip-api.com is used for this.
  93.  * https://github.com/kd8bxp/M5Stack-ISS-Tracker-Updated/tree/master/M5Stack_ISS_Tracker_Updated
  94.  */
  95.  
  96. int geolocation(){
  97.   String url;
  98.   url = "http://ip-api.com/json";
  99.   getJson(url);
  100.  
  101.   const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
  102.   DynamicJsonDocument jsonBuffer(1024);
  103.   DeserializationError error = deserializeJson (jsonBuffer, payload);
  104.   if (error){
  105.       Serial.println ("deserializeJson () failed");
  106.   }
  107.   double lat = jsonBuffer["lat"];
  108.   double lon = jsonBuffer["lon"];
  109.   url = "http://api.openweathermap.org/data/2.5/weather?lat=" + String(lat) + "&lon=" + String(lon) + "&appid=" + WEATHERKEY;
  110.   getJson(url);
  111.  
  112.   error = deserializeJson (jsonBuffer, payload);
  113.   if (error){
  114.       Serial.println ("deserializeJson () failed");
  115.   }
  116.   gmtOffset_sec = jsonBuffer["timezone"];
  117.   temperature = jsonBuffer["main"]["temp"];
  118.   temperature = temperature - 273.0;
  119. }
  120.  
  121. void updateNTP (void *pvParameters) {
  122.   (void) pvParameters;
  123.  
  124.   for (;;) {
  125.     while(!timeClient.update()) {
  126.       timeClient.forceUpdate();
  127.     }
  128.     if (xSemaphoreTake (serialMutex, (TickType_t)10) == pdTRUE) {
  129.       setTime (timeClient.getEpochTime ());
  130.       xSemaphoreGive (serialMutex);
  131.     }
  132.     vTaskDelay ((1000/portTICK_PERIOD_MS) * 60 * 15);  // update every 15 minutes.
  133.     geolocation (); // update based on location
  134.   }
  135. }
  136.  
  137. void updateScreen (void *pvParameters) {
  138.   (void) pvParameters;
  139.  
  140.   for (;;) {
  141.     if (xSemaphoreTake (serialMutex, (TickType_t)10) == pdTRUE) {
  142.       char timeString[25];
  143.       time_t t = now ();
  144.       sprintf (timeString, "%02i:%02i:%02i", hour (t), minute (t), second (t));
  145.       xSemaphoreGive (serialMutex);
  146.  
  147.       tft.setTextColor(0x39C4, TFT_BLACK);
  148.       tft.drawString("88:88:88",10,10,7);
  149.       tft.setTextColor(0xFBE0, TFT_BLACK);
  150.       tft.drawString (timeString, 10, 10, 7);
  151.  
  152.  
  153.       tft.setCursor(190, 60 ,2);
  154.       tft.setTextColor(TFT_WHITE);
  155.       char out[25];
  156.       sprintf (out, "%2.0f`C", temperature);
  157.       tft.println(out);
  158.     }
  159.     vTaskDelay (1000/portTICK_PERIOD_MS);
  160.   }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment