Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Weather Fetching**
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-01-17 02:06:24
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* test my tft */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <SPI.h>
- #include <ILI9488.h> //https://github.com/jaretburkett/ILI9488
- #include <U8g2_for_Adafruit_GFX.h> //https://github.com/olikraus/U8g2_for_Adafruit_GFX
- #include <TFT_eSPI.h> // Added for TFT_eSPI library
- #include <WiFi.h> // Added for WiFi functionality
- #include <HTTPClient.h> // Added for HTTP requests
- #include <Arduino_JSON.h> // Added for JSON parsing
- #include "Free_Fonts.h" // Added for free fonts
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t mytft_ILI9488_TFT_RST_PIN_D4 = 4;
- const uint8_t mytft_ILI9488_TFT_DC_PIN_D13 = 13;
- /***** DEFINITION OF SPI PINS *****/
- const uint8_t mytft_ILI9488_TFT_SPI_PIN_MOSI_D23 = 23;
- const uint8_t mytft_ILI9488_TFT_SPI_PIN_MISO_D19 = 19;
- const uint8_t mytft_ILI9488_TFT_SPI_PIN_SCLK_D18 = 18;
- const uint8_t mytft_ILI9488_TFT_SPI_PIN_CS_D5 = 5;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- bool mytft_ILI9488_TFT_RST_PIN_D4_rawData = 0;
- bool mytft_ILI9488_TFT_DC_PIN_D13_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float mytft_ILI9488_TFT_RST_PIN_D4_phyData = 0.0;
- float mytft_ILI9488_TFT_DC_PIN_D13_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- TFT_eSPI tft = TFT_eSPI(); // Added instance for TFT_eSPI
- // Color definitions
- #define WHITE 0xFFFF
- #define BLACK 0x0000
- #define BLUE 0x001F
- #define RED 0xF800
- #define GREEN 0x07E0
- #define CYAN 0x07FF
- #define MAGENTA 0xF81F
- #define YELLOW 0xFFE0
- #define GREY 0x2108
- #define SCALE0 0xC655
- #define SCALE1 0x5DEE
- #define SCALE2 0x10CE
- #define TEXT_COLOR 0xFFFF
- // WiFi and API variables
- const char* ssid = "xxxxxxxxxxxxxxxx"; // your network wifi credentials
- const char* password = "yyyyyyyyyyyyyyyy"; // your wifi network key
- String openWeatherMapApiKey = "zzzzzzzzzzzzzzzz"; // your API Openweather account
- String city = "oooooooooooooooo"; // your city
- String countryCode = "11"; // your country code
- String jsonDocument(1024); // memory allocation for json string
- unsigned long lastTime = 0;
- unsigned long timerDelay; // controls timing of Json string call
- float actualTemperature = 10;
- float actualHumidity;
- int actualPressure = 960;
- // Function prototypes for user code
- void startWifi();
- void doTheHardWork();
- void updateOutputs(); // Refresh output data
- void setup(void)
- {
- Serial.begin(9600);
- tft.init();
- tft.setRotation(1);
- tft.fillScreen(TFT_BLACK);
- pinMode(mytft_ILI9488_TFT_RST_PIN_D4, OUTPUT);
- pinMode(mytft_ILI9488_TFT_DC_PIN_D13, OUTPUT);
- pinMode(mytft_ILI9488_TFT_SPI_PIN_CS_D5, OUTPUT);
- // start the SPI library:
- SPI.begin();
- // Initialize WiFi connection
- startWifi();
- timerDelay = 600000; // Set timer delay to 10 minutes
- }
- void loop(void)
- {
- // Refresh output data
- updateOutputs();
- // Perform HTTP request and update data
- doTheHardWork();
- delay(5000); // Delay to prevent flooding the server
- }
- void updateOutputs()
- {
- digitalWrite(mytft_ILI9488_TFT_RST_PIN_D4, mytft_ILI9488_TFT_RST_PIN_D4_rawData);
- digitalWrite(mytft_ILI9488_TFT_DC_PIN_D13, mytft_ILI9488_TFT_DC_PIN_D13_rawData);
- }
- // Function to connect to WiFi
- void startWifi() {
- WiFi.begin(ssid, password);
- Serial.println("connecting");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println();
- Serial.print("Connected to WiFi network ");
- Serial.print(ssid);
- delay(1000);
- tft.fillCircle(230, 28, 3, GREEN); // set wifi indicator
- Serial.print(" - IP address: ");
- Serial.println(WiFi.localIP());
- }
- // Function to handle HTTP requests and JSON parsing
- void doTheHardWork() {
- if ((millis() - lastTime) > timerDelay) {
- if (WiFi.status() == WL_CONNECTED) {
- tft.fillCircle(230, 28, 3, YELLOW); // set wifi indicator to indicate JSON reception
- String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + countryCode + "&APPID=" + openWeatherMapApiKey;
- jsonDocument = httpGETRequest(serverPath.c_str());
- JSONVar myObject = JSON.parse(jsonDocument);
- if (JSON.typeof(myObject) == "undefined") {
- Serial.println("parsing input failed!");
- return;
- }
- double temp_01 = (myObject["main"]["temp"]);
- double hum_01 = (myObject["main"]["humidity"]);
- double press_01 = (myObject["main"]["pressure"]);
- double w_speed_01 = (myObject["wind"]["speed"]);
- double windDir_01 = (myObject["wind"]["deg"]);
- double min_temp_01 = (myObject["main"]["temp_min"]);
- double max_temp_01 = (myObject["main"]["temp_max"]);
- actualTemperature = temp_01 - 273; // Convert Kelvin to Celsius
- actualHumidity = hum_01;
- actualPressure = press_01;
- Serial.println("*******************************************");
- Serial.print("JSON object = ");
- Serial.println(myObject);
- Serial.println("*******************************************");
- Serial.println("extracted from JSON object:");
- Serial.print("Temperature: ");
- Serial.print(myObject["main"]["temp"]);
- Serial.println(" *Kelvin");
- Serial.print("Pressure: ");
- Serial.print(myObject["main"]["pressure"]);
- Serial.println(" mB");
- Serial.print("Humidity: ");
- Serial.print(myObject["main"]["humidity"]);
- Serial.println(" %");
- Serial.print("Wind Speed: ");
- Serial.print(myObject["wind"]["speed"]);
- Serial.println(" m/s");
- Serial.print("Wind Direction: ");
- Serial.print(myObject["wind"]["deg"]);
- Serial.println(" degrees");
- tft.fillCircle(230, 28, 3, GREEN); // set wifi indicator back to green
- } else {
- Serial.println("WiFi Disconnected");
- }
- lastTime = millis();
- }
- }
- String httpGETRequest(const char* serverName) {
- HTTPClient http;
- http.begin(serverName); // your IP address with path or Domain name with URL path
- int httpResponseCode = http.GET(); // send HTTP GET request
- String payload = "{}";
- if (httpResponseCode > 0) {
- Serial.print("HTTP Response code: ");
- Serial.println(httpResponseCode);
- payload = http.getString();
- } else {
- Serial.print("Error code: ");
- Serial.println(httpResponseCode);
- }
- http.end(); // free microcontroller resources
- return payload;
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement