Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <HTTPClient.h>
- #include <ArduinoJson.h>
- #include <WiFi.h>
- #include <NTPClient.h>
- #include <WiFiUdp.h>
- #include <TFT_eSPI.h>
- #include <SPI.h>
- #include <TimeLib.h>
- #define TFT_BL 4 // Display backlight control pin
- // Replace with your network credentials
- const char* ssid = "";
- const char* password = "";
- // openweather.org key
- String WEATHERKEY="";
- // temp in celcius
- double temperature = 0.0;
- // offset in seconds
- int gmtOffset_sec = 0;
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP);
- TFT_eSPI tft = TFT_eSPI();
- String payload, TZone;//, lastUpdate;
- String coordinateTZ; //lat=xx.xxx&lon=xxx.xxx
- SemaphoreHandle_t serialMutex = NULL;
- void updateNTP (void *pvParams);
- void updateScreen (void *pvParams);
- void setup() {
- // put your setup code here, to run once:
- Serial.begin (115200);
- serialMutex = xSemaphoreCreateMutex ();
- if (TFT_BL > 0) {
- pinMode(TFT_BL, OUTPUT);
- digitalWrite(TFT_BL, HIGH);
- }
- tft.init();
- tft.setRotation(1);
- tft.fillScreen(TFT_BLACK);
- tft.setTextColor(TFT_YELLOW, TFT_BLACK); // Note: the new fonts do not draw the background colour
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.println (".");
- }
- Serial.println(WiFi.localIP());
- timeClient.begin();
- geolocation (); // guess where I am
- timeClient.setTimeOffset(gmtOffset_sec);
- xTaskCreate (updateNTP, "NTP Client", 4096, NULL, 2, NULL);
- xTaskCreate (updateScreen, "Screen", 4096, NULL, 1, NULL);
- }
- void loop() {
- }
- void getJson(String url) {
- if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
- HTTPClient http; //Declare an object of class HTTPClient
- http.begin(url); //Specify request destination
- int httpCode = http.GET(); //Send the request
- if (httpCode > 0) { //Check the returning code
- payload = http.getString(); //Get the request response payload
- }
- http.end(); //Close connection
- }
- }
- /*
- * This function will use the external IP to get a Latitude and Longitude
- * which will be used for many different things for ISS tracker.
- * The website http://ip-api.com is used for this.
- * https://github.com/kd8bxp/M5Stack-ISS-Tracker-Updated/tree/master/M5Stack_ISS_Tracker_Updated
- */
- int geolocation(){
- String url;
- url = "http://ip-api.com/json";
- getJson(url);
- const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
- DynamicJsonDocument jsonBuffer(1024);
- DeserializationError error = deserializeJson (jsonBuffer, payload);
- if (error){
- Serial.println ("deserializeJson () failed");
- }
- double lat = jsonBuffer["lat"];
- double lon = jsonBuffer["lon"];
- url = "http://api.openweathermap.org/data/2.5/weather?lat=" + String(lat) + "&lon=" + String(lon) + "&appid=" + WEATHERKEY;
- getJson(url);
- error = deserializeJson (jsonBuffer, payload);
- if (error){
- Serial.println ("deserializeJson () failed");
- }
- gmtOffset_sec = jsonBuffer["timezone"];
- temperature = jsonBuffer["main"]["temp"];
- temperature = temperature - 273.0;
- }
- void updateNTP (void *pvParameters) {
- (void) pvParameters;
- for (;;) {
- while(!timeClient.update()) {
- timeClient.forceUpdate();
- }
- if (xSemaphoreTake (serialMutex, (TickType_t)10) == pdTRUE) {
- setTime (timeClient.getEpochTime ());
- xSemaphoreGive (serialMutex);
- }
- vTaskDelay ((1000/portTICK_PERIOD_MS) * 60 * 15); // update every 15 minutes.
- geolocation (); // update based on location
- }
- }
- void updateScreen (void *pvParameters) {
- (void) pvParameters;
- for (;;) {
- if (xSemaphoreTake (serialMutex, (TickType_t)10) == pdTRUE) {
- char timeString[25];
- time_t t = now ();
- sprintf (timeString, "%02i:%02i:%02i", hour (t), minute (t), second (t));
- xSemaphoreGive (serialMutex);
- tft.setTextColor(0x39C4, TFT_BLACK);
- tft.drawString("88:88:88",10,10,7);
- tft.setTextColor(0xFBE0, TFT_BLACK);
- tft.drawString (timeString, 10, 10, 7);
- tft.setCursor(190, 60 ,2);
- tft.setTextColor(TFT_WHITE);
- char out[25];
- sprintf (out, "%2.0f`C", temperature);
- tft.println(out);
- }
- vTaskDelay (1000/portTICK_PERIOD_MS);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment