Previsioni
Previsioni
Previsioni
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>
#include <ArduinoJson.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "bitmaps.h"
// WIFI INFORMATION
#define WIFI_SSID "YOUR WIFI SSIDE"
#define WIFI_PASSWORD "YOUR WIFI PASSWORD"
#define JSON_MEMORY_BUFFER 1024*2
// DISPLAY PINS
#define TFT_CS 15
#define TFT_DC 4
#define TFT_RST 2
#define TFT_BL 5
// You can get API KEY and HOST KEY from RapidAPI, Search weatherapi.com and
subscribe.
const char* API_KEY = "YOUR API KEY";
const char* API_HOST = "YOUR HOST KEY";
// API endpoint.
String weather_url = "https://weatherapi-com.p.rapidapi.com/current.json?q=" +
String(lat) + "%2C" + String(lon);
// Global variables
String current_time;
String hour;
String minute;
String alternative;
String weekDay;
String month;
int day;
int year;
int temp;
// Array for days and months
String weekDays[7]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
String months[12]={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"};
void setup(void)
{
// Initialization
Serial.begin(9600);
tft.init(240, 240);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
timeClient.begin();
// Clear display
tft.fillScreen(0);
// Clear display
tft.fillScreen(0);
// Show connected
tft.setCursor(60, 110);
tft.println("Connected!");
delay(3000);
// Displaying items.
display();
}
void display()
{
// default font size = 6x8px
int font_w = 6;
int font_h = 8;
// UI size
int time_size = 6;
int alt_size = 2;
int day_size = 3;
// Display WxH
int display_w = 240;
int display_h = 240;
tft.setCursor(time_x, time_y);
tft.println(current_time);
tft.setTextSize(alt_size);
tft.setCursor((time_x + (font_w*time_size)*5), time_y);
tft.println(alternative);
tft.drawBitmap((time_x + (font_w*time_size)*4 + 14), (time_y + (font_h*time_size)
+ padding), wifi, 31, 24, ST77XX_WHITE);
tft.setTextSize(day_size);
tft.setCursor(20, time_y+(font_h*time_size) + padding + 10);
tft.println(weekDay);
tft.setCursor(20, time_y+(font_h*time_size) + (font_h*day_size) + padding * 2 +
10);
tft.println(day);
tft.setCursor(20 + (font_w * day_size)*2 + padding, time_y+(font_h*time_size) +
(font_h*day_size) + padding * 2 + 10);
tft.println(month);
tft.setTextSize(4);
tft.setCursor(20, time_y+(font_h*time_size) + (font_h*day_size) * 2 + padding *
3 + 10);
tft.println(year);
int temp_x = display_w - (font_w * 4)*2 - padding - (font_w * alt_size);
tft.setCursor(temp_x, time_y+(font_h*time_size) + (font_h*day_size) + padding *
2 + 10);
tft.println(temp);
tft.setTextSize(alt_size);
tft.setCursor(temp_x +(font_w * 4) *2 , time_y+(font_h*time_size) +
(font_h*day_size) + padding * 2 + 10);
tft.println("o");
tft.setTextSize(4);
tft.setCursor(temp_x + 10 ,time_y+(font_h*time_size) + (font_h*day_size) * 2 +
padding * 3 + 10);
tft.println("C");
}
// ==============================================================================
curl --request GET --url 'https://weatherapi-com.p.rapidapi.com/current.json?
q=TERRACINA%2CIT' --header 'X-RapidAPI-Host: weatherapi-com.p.rapidapi.com' --
header 'X-RapidAPI-Key:b2536c47femshf06cb50511bb415p1227fbjsndd36d384894d'
[root@portatile2009 ~] #
[root@portatile2009 ~] # [root@portatile2009 ~] # curl --request GET \
--url 'https://weatherapi-com.p.rapidapi.com/current.json?q=TERRACINA%2CIT'
\
--header 'X-RapidAPI-Host: weatherapi-com.p.rapidapi.com' \
--header 'X-RapidAPI-Key:
b2536c47femshf06cb50511bb415p1227fbjsndd36d384894d'
bash: [root@portatile2009: comando non trovato
bash: --url: comando non trovato
[root@portatile2009 ~] # curl --request GET \
--url 'https://weatherapi-com.p.rapidapi.com/current.json?q=TERRACINA%2CIT'
\
--header 'X-RapidAPI-Host: weatherapi-com.p.rapidapi.com' \
--header 'X-RapidAPI-Key:
b2536c47femshf06cb50511bb415p1227fbjsndd36d384894d'
[root@portatile2009 ~] # b2536c47femshf06cb50511bb415p1227fbjsndd36d384894d
https://api.openweathermap.org/data/2.5/weather?
appid=ed681b02a54b389f681cb7261e53f703&q=terracina,it&units=metric
================================================================================
WHEATHER,SH
================================================================================
#!/bin/bash
# Questo script preleva il tempo da openweathermap.com nella forma di un file json
in modo da effetuare il display dei dati anche se posto offline
# https://api.openweathermap.org/data/2.5/weather?
appid=ed681b02a54b389f681cb7261e53f703&q=terracina,it&units=metric&lang=it
# Replace 'CITY_NAME' with the name of the city you want to get the ID for
CITY_NAME="terracina,it"
API_KEY="ed681b02a54b389f681cb7261e53f703"
CITY_ID=6691831
LOCATION="Vatican City,VA"
#// url="api.openweathermap.org/data/2.5/weather?q=Città del
Vaticano,VA&appid=ed681b02a54b389f681cb7261e53f703&lang=it&units=metric"
# City and country code for the location (e.g., London,uk)
# cercare la citta-id in https://openweathermap.org/find
#API_ENDPOINT="api.openweathermap.org/data/2.5/weather?q=${LOCATION}&appid=$
{API_KEY}&cnt=5&units=metric&lang=it"
API_ENDPOINT="api.openweathermap.org/data/2.5/weather?q=${CITY_NAME}&appid=$
{API_KEY}&cnt=5&units=metric&lang=it"
# Construct the API URL with the city name and API key
API_URL="https://${API_ENDPOINT}"
# url="api.openweathermap.org/data/2.5/weather?id=${CITY_ID}&appid=$
{API_KEY}&cnt=5&units=metric&lang=it"
# curl ${url} -s -o weather.json
# exit
# Extract the city ID from the API response (assuming the API response is in JSON
format)
CITY_ID=$(echo "$API_RESPONSE" | jq -r '.id')
# weather.sh
Previsioni del tempo per [terracina,it] di oggi [21/luglio/2023 Week venerdì ]
"cielo sereno"
Icona Weather [01d] salvata come [weather_icon.png]
{
"coord":
{
"lon": 13.2217,
"lat": 41.2863
},
"weather":
[
{
"id": 800,
"main": "Clear",
"description": "cielo sereno",
"icon": "01d"
}
],
"base": "stations",
"main":
{
"temp": 31.85,
"feels_like": 38.85,
"temp_min": 30.51,
"temp_max": 35.16,
"pressure": 1007,
"humidity": 83
},
"visibility": 10000,
"wind":
{
"speed": 1.34,
"deg": 135,
"gust": 2.68
},
"clouds":
{
"all": 3
},
"dt": 1689954895,
"sys":
{
"type": 2,
"id": 2041426,
"country": "IT",
"sunrise": 1689911488,
"sunset": 1689964509
},
"timezone": 7200,
"id": 6541893,
"name": "Comune di Terracina",
"cod": 200
}
================================================================================
RISPOSTA DELLA CALL
================================================================================
{"coord":{"lon":12.4533,"lat":41.9024},"weather":
[{"id":800,"main":"Clear","description":"cielo
sereno","icon":"01n"}],"base":"stations","main":
{"temp":20.01,"feels_like":19.71,"temp_min":17.16,"temp_max":23.58,"pressure":1017,
"humidity":63},"visibility":10000,"wind":{"speed":1.03,"deg":0},"clouds":
{"all":0},"dt":1694922025,"sys":
{"type":2,"id":2000926,"country":"VA","sunrise":1694926314,"sunset":1694971081},"ti
mezone":7200,"id":6691831,"name":"Città del Vaticano","cod":200}
================================================================================
-----------------------------------------------------------------------------------
------------------------------------------------------------
ESP8266 Weather Forecaster
In this post you’re going to build a weather forecaster with an ESP8266.
-----------------------------------------------------------------------------------
-------------------------------------------------------------
https://randomnerdtutorials.com/esp8266-weather-forecaster/
This project was written by Emmanuel Odunlade and edited by Rui Santos.
I hate being out on a rainy day without an umbrella and I’m pretty sure it’s the
same for everyone else.
Often, the weather widget on our smartphones displays the weather forecast but when
you’re in
a hurry you forget to pay attention to it.
So, it would be nice to have a device hanging at the back of the door which reminds
you to leave
your house with an umbrella on a day where it might rain. This device tells you the
weather forecast
with a change in the LED color. This device wouldn’t have an alarm neither a
screen, just a few LEDs
that would fit naturally into your environment.
Parts required
Here’s a complete list of the components you need for this project (click the links
below to find
the best price at Maker Advisor):
This project is based on the Open Weather Map API, so it’s necessary to sign up on
their platform
and obtain an API key before we get down to putting together the schematics and
writing the code.
OpenWeatherMap’s free plan provides everything you need for thins example.
To use the API you need an API key, known as the APIID. To get an APIID:
This is a unique key you need to pull information from the site.
Copy and paste this key somewhere, you’ll need it in a moment.
To pull information on weather in your chosen location, enter the following URL
with the sections
in curly brackets replaced with your chosen location information and your unique
API key:
Replace {your city} with the city you want data for, {your country code} with the
country code
for that city, and {your API key} with your unique API key we found previously.
For example, our API URL for the town of Porto in Portugal, after replacing with
the details, would be:
http://api.openweathermap.org/data/2.5/forecast?
q=Porto,PT&APPID=801d2603e9f2e1c70e042e4------&mode=json&units=metric&cnt=2
https://api.openweathermap.org/data/2.5/weather?q=Città%20del
%20Vaticano,VA&appid=ed681b02a54b389f681cb7261e53f703&lang=it&units=metric
Note: more information on using the API to get weather information is available
here.
https://openweathermap.org/current
Copy your URL into your browser and it should give you a bunch of information that
corresponds
to your local weather forecast information.
Keep your unique API key safe and we can then move to the code section.
Click here to download the ArduinoJson. You should have a .zip folder in your
Downloads folder
Unzip the .zip folder and you should get ArduinoJson-master folder
Rename your folder from ArduinoJson-master to ArduinoJson
Move the ArduinoJson folder to your Arduino IDE installation libraries folder
Finally, re-open your Arduino IDE
Uploading code
Having the ESP8266 add-on for the Arduino IDE installed (how to Install the ESP8266
Board in Arduino IDE),
go to Tools and select “NodeMCU (ESP-12E Module)”.
Copy the following code to your Arduino IDE and upload it to your ESP8266 board.
Go through the code to add your SSID, password, city, country code and Open Weather
Map API key.
/*
* Author: Emmanuel Odunlade
* Complete Project Details https://randomnerdtutorials.com
*/
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
WiFiClient client;
// Replace the next line to match your city and 2 letter country code
String nameOfCity = "REPLACE_WITH_YOUR_CITY,REPLACE_WITH_YOUR_COUNTRY_CODE";
// How your nameOfCity variable would look like for Lagos on Nigeria
//String nameOfCity = "Lagos,NG";
String text;
int jsonend = 0;
boolean startJson = false;
int status = WL_IDLE_STATUS;
void setup() {
pinMode(clearLed, OUTPUT);
pinMode(rainLed, OUTPUT);
pinMode(snowLed, OUTPUT);
pinMode(hailLed, OUTPUT);
Serial.begin(9600);
text.reserve(JSON_BUFF_DIMENSION);
WiFi.begin(ssid,pass);
Serial.println("connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected");
printWiFiStatus();
}
void loop() {
//OWM requires 10mins between request intervals
//check if 10mins has passed then conect again and pull
if (millis() - lastConnectionTime > postInterval) {
// note the time that the connection was made:
lastConnectionTime = millis();
makehttpRequest();
}
}
char c = 0;
while (client.available()) {
c = client.read();
// since json contains equal number of open and close curly brackets, this
means we can determine when a json is completely received by counting
// the open and close occurences,
//Serial.print(c);
if (c == '{') {
startJson = true; // set startJson true to indicate json message
has started
jsonend++;
}
if (c == '}') {
jsonend--;
}
if (startJson == true) {
text += c;
}
// if jsonend = 0 then we have have received equal number of curly braces
if (jsonend == 0 && startJson == true) {
parseJson(text.c_str()); // parse c string text in parseJson function
text = ""; // clear text string for the next time
startJson = false; // set startJson to false to indicate that a new
message has not yet started
}
}
}
else {
// if no connction was made:
Serial.println("connection failed");
return;
}
}
// including temperature and humidity for those who may wish to hack it in
Serial.println();
}
}
// can't remember last time I saw hail anywhere but just in case
else if (weatherType == "hail") {
if (indexNow == -1 && indexLater != -1) {
digitalWrite(hailLed,HIGH);
digitalWrite(clearLed,LOW);
digitalWrite(rainLed,LOW);
digitalWrite(snowLed,LOW);
Serial.println("Oh no! It is going to " + weatherType + " later! Predicted "
+ later);
}
}
// for clear sky, if the current weather does not contain the word clear and the
later message does, send notification that it will be sunny later
else {
if (indexNow == -1 && indexLater != -1) {
Serial.println("It is going to be sunny later! Predicted " + later);
digitalWrite(clearLed,HIGH);
digitalWrite(rainLed,LOW);
digitalWrite(snowLed,LOW);
digitalWrite(hailLed,LOW);
}
}
}
Schematics
Wire your circuit accordingly to the schematic below:
Demonstration
The next figure describes what each LED indicates: (D2) Rain, (D3) Clear sky, (D4)
Snow and (D5) Hail:
Wrapping up
Nowadays, the goal of computing is to create a natural user interface between man
and computers. We are looking to things like AR (Augmented Reality) to make that
reality, but the truth is we are all getting tired of constantly looking at our
phones and PCs for the tiniest piece of information.
I think having this project hanged somewhere around the house where you can easily
know the weather on your way out is extremely useful.
To take this project further, I may throw in a 3D printed enclosure with a battery
back up to make this look more neat. I will certainly share with you when that is
done.
You might also like reading our Home Automation using ESP8266 eBook.
Build-Web-Servers-with-ESP32-and-ESP8266-eBook-2nd-Edition-500px-h
[eBook] Build Web Servers with ESP32 and ESP8266 (2nd Edition)
Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and
monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server
communication protocols DOWNLOAD »
Recommended Resources