Advertisement
pleasedontcode

**Weather Fetching** rev_01

Jan 16th, 2025
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Weather Fetching**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-01-17 02:06:24
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* test my tft */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <SPI.h>
  27. #include <ILI9488.h>    //https://github.com/jaretburkett/ILI9488
  28. #include <U8g2_for_Adafruit_GFX.h>  //https://github.com/olikraus/U8g2_for_Adafruit_GFX
  29. #include <TFT_eSPI.h>      // Added for TFT_eSPI library
  30. #include <WiFi.h>         // Added for WiFi functionality
  31. #include <HTTPClient.h>   // Added for HTTP requests
  32. #include <Arduino_JSON.h> // Added for JSON parsing
  33. #include "Free_Fonts.h"   // Added for free fonts
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  40. const uint8_t mytft_ILI9488_TFT_RST_PIN_D4      = 4;
  41. const uint8_t mytft_ILI9488_TFT_DC_PIN_D13      = 13;
  42.  
  43. /***** DEFINITION OF SPI PINS *****/
  44. const uint8_t mytft_ILI9488_TFT_SPI_PIN_MOSI_D23        = 23;
  45. const uint8_t mytft_ILI9488_TFT_SPI_PIN_MISO_D19        = 19;
  46. const uint8_t mytft_ILI9488_TFT_SPI_PIN_SCLK_D18        = 18;
  47. const uint8_t mytft_ILI9488_TFT_SPI_PIN_CS_D5       = 5;
  48.  
  49. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  50. bool    mytft_ILI9488_TFT_RST_PIN_D4_rawData        = 0;
  51. bool    mytft_ILI9488_TFT_DC_PIN_D13_rawData        = 0;
  52.  
  53. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  54. float   mytft_ILI9488_TFT_RST_PIN_D4_phyData        = 0.0;
  55. float   mytft_ILI9488_TFT_DC_PIN_D13_phyData        = 0.0;
  56.  
  57. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  58. TFT_eSPI tft = TFT_eSPI(); // Added instance for TFT_eSPI
  59.  
  60. // Color definitions
  61. #define WHITE       0xFFFF
  62. #define BLACK       0x0000
  63. #define BLUE        0x001F
  64. #define RED         0xF800
  65. #define GREEN       0x07E0
  66. #define CYAN        0x07FF
  67. #define MAGENTA     0xF81F
  68. #define YELLOW      0xFFE0
  69. #define GREY        0x2108
  70. #define SCALE0      0xC655
  71. #define SCALE1      0x5DEE
  72. #define SCALE2      0x10CE
  73. #define TEXT_COLOR  0xFFFF
  74.  
  75. // WiFi and API variables
  76. const char* ssid = "xxxxxxxxxxxxxxxx"; // your network wifi credentials
  77. const char* password = "yyyyyyyyyyyyyyyy"; // your wifi network key
  78. String openWeatherMapApiKey = "zzzzzzzzzzzzzzzz"; // your API Openweather account
  79. String city = "oooooooooooooooo"; // your city
  80. String countryCode = "11"; // your country code
  81. String jsonDocument(1024); // memory allocation for json string
  82. unsigned long lastTime = 0;
  83. unsigned long timerDelay; // controls timing of Json string call
  84. float actualTemperature = 10;
  85. float actualHumidity;
  86. int actualPressure = 960;
  87.  
  88. // Function prototypes for user code
  89. void startWifi();
  90. void doTheHardWork();
  91. void updateOutputs(); // Refresh output data
  92.  
  93. void setup(void)
  94. {
  95.     Serial.begin(9600);
  96.     tft.init();
  97.     tft.setRotation(1);
  98.     tft.fillScreen(TFT_BLACK);
  99.  
  100.     pinMode(mytft_ILI9488_TFT_RST_PIN_D4, OUTPUT);
  101.     pinMode(mytft_ILI9488_TFT_DC_PIN_D13, OUTPUT);
  102.     pinMode(mytft_ILI9488_TFT_SPI_PIN_CS_D5, OUTPUT);
  103.  
  104.     // start the SPI library:
  105.     SPI.begin();
  106.  
  107.     // Initialize WiFi connection
  108.     startWifi();
  109.     timerDelay = 600000; // Set timer delay to 10 minutes
  110. }
  111.  
  112. void loop(void)
  113. {
  114.     // Refresh output data
  115.     updateOutputs();
  116.  
  117.     // Perform HTTP request and update data
  118.     doTheHardWork();
  119.     delay(5000); // Delay to prevent flooding the server
  120. }
  121.  
  122. void updateOutputs()
  123. {
  124.     digitalWrite(mytft_ILI9488_TFT_RST_PIN_D4, mytft_ILI9488_TFT_RST_PIN_D4_rawData);
  125.     digitalWrite(mytft_ILI9488_TFT_DC_PIN_D13, mytft_ILI9488_TFT_DC_PIN_D13_rawData);
  126. }
  127.  
  128. // Function to connect to WiFi
  129. void startWifi() {
  130.     WiFi.begin(ssid, password);
  131.     Serial.println("connecting");
  132.     while (WiFi.status() != WL_CONNECTED) {
  133.         delay(500);
  134.         Serial.print(".");
  135.     }
  136.     Serial.println();
  137.     Serial.print("Connected to WiFi network ");
  138.     Serial.print(ssid);
  139.     delay(1000);
  140.     tft.fillCircle(230, 28, 3, GREEN); // set wifi indicator
  141.     Serial.print(" - IP address: ");
  142.     Serial.println(WiFi.localIP());
  143. }
  144.  
  145. // Function to handle HTTP requests and JSON parsing
  146. void doTheHardWork() {
  147.     if ((millis() - lastTime) > timerDelay) {
  148.         if (WiFi.status() == WL_CONNECTED) {
  149.             tft.fillCircle(230, 28, 3, YELLOW); // set wifi indicator to indicate JSON reception
  150.             String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + countryCode + "&APPID=" + openWeatherMapApiKey;
  151.             jsonDocument = httpGETRequest(serverPath.c_str());
  152.             JSONVar myObject = JSON.parse(jsonDocument);
  153.  
  154.             if (JSON.typeof(myObject) == "undefined") {
  155.                 Serial.println("parsing input failed!");
  156.                 return;
  157.             }
  158.  
  159.             double temp_01 = (myObject["main"]["temp"]);
  160.             double hum_01 = (myObject["main"]["humidity"]);
  161.             double press_01 = (myObject["main"]["pressure"]);
  162.             double w_speed_01 = (myObject["wind"]["speed"]);
  163.             double windDir_01 = (myObject["wind"]["deg"]);
  164.             double min_temp_01 = (myObject["main"]["temp_min"]);
  165.             double max_temp_01 = (myObject["main"]["temp_max"]);
  166.  
  167.             actualTemperature = temp_01 - 273; // Convert Kelvin to Celsius
  168.             actualHumidity = hum_01;
  169.             actualPressure = press_01;
  170.  
  171.             Serial.println("*******************************************");
  172.             Serial.print("JSON object = ");
  173.             Serial.println(myObject);
  174.             Serial.println("*******************************************");
  175.             Serial.println("extracted from JSON object:");
  176.             Serial.print("Temperature: ");
  177.             Serial.print(myObject["main"]["temp"]);
  178.             Serial.println(" *Kelvin");
  179.             Serial.print("Pressure: ");
  180.             Serial.print(myObject["main"]["pressure"]);
  181.             Serial.println(" mB");
  182.             Serial.print("Humidity: ");
  183.             Serial.print(myObject["main"]["humidity"]);
  184.             Serial.println(" %");
  185.             Serial.print("Wind Speed: ");
  186.             Serial.print(myObject["wind"]["speed"]);
  187.             Serial.println(" m/s");
  188.             Serial.print("Wind Direction: ");
  189.             Serial.print(myObject["wind"]["deg"]);
  190.             Serial.println(" degrees");
  191.  
  192.             tft.fillCircle(230, 28, 3, GREEN); // set wifi indicator back to green
  193.         } else {
  194.             Serial.println("WiFi Disconnected");
  195.         }
  196.         lastTime = millis();
  197.     }
  198. }
  199.  
  200. String httpGETRequest(const char* serverName) {
  201.     HTTPClient http;
  202.     http.begin(serverName); // your IP address with path or Domain name with URL path
  203.     int httpResponseCode = http.GET(); // send HTTP GET request
  204.     String payload = "{}";
  205.     if (httpResponseCode > 0) {
  206.         Serial.print("HTTP Response code: ");
  207.         Serial.println(httpResponseCode);
  208.         payload = http.getString();
  209.     } else {
  210.         Serial.print("Error code: ");
  211.         Serial.println(httpResponseCode);
  212.     }
  213.     http.end(); // free microcontroller resources
  214.     return payload;
  215. }
  216.  
  217. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement