1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
#include <SPI.h>
#include <PubSubClient.h>
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
#include <WiFi.h>
#include "config.h"
#include <TFT_eSPI.h> // by Bodmer ver 2.5.34
#include <NTPClient.h> // by F.Weinberg ver 3.2.1
#include <HTTPClient.h> // by A.McEwen ver 2.2.0
void WIFISetUp(void);
void mqttCallback(char *topic, byte *payload, unsigned int length);
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = WLAN_SSID; // your network SSID (name)
char password[] = WLAN_PASSWD; // your network password (use for WPA, or use as key for WEP)
const char* metar = "https://aviationweather.gov/api/data/metar?ids=LFLL&format=json"; // KDEN = ICAO code for Dever
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
int wifiTimeOutCounter = 0;
#define TIMEOFFSET 3600 * 2 // no daylight saving time but 5h offset to UTC
// definition for the screen
#define MAX_Y 240
#define MAX_X 320
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite spr = TFT_eSprite(&tft); // Déclarer l'objet Sprite "spr" avec un pointeur sur l'objet "tft".
uint16_t palette[16] = { TFT_GREENYELLOW, TFT_NAVY, TFT_ORANGE, TFT_DARKCYAN, TFT_MAROON,
TFT_PURPLE, TFT_PINK, TFT_LIGHTGREY, TFT_YELLOW, TFT_BLUE,
TFT_GREEN, TFT_CYAN, TFT_RED, TFT_MAGENTA, TFT_BLUE, TFT_WHITE };
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
// update parameter for the weather data
unsigned long previousMillis = 0;
#define INTERVAL 60000 * 5 // 5 min
// init parameter for iniversum simulation
int const n = 5, m = 200;
float const r = 0.1;
float x = 0, v = 0, t = 0;
const char mqttBroker[] = "mqtt3.thingspeak.com";
int mqttPort = 1883;
const char mqttTopic[] = "channels/2499901/subscribe";
const char mqttClientID[] = MQTT_CLIENT;
const char mqttUsername[] = MQTT_USER;
const char mqttPassword[] = MQTT_PASSWORD;
byte omm = 99, oss = 99;
byte xcolon = 0, xsecs = 0;
unsigned int colour = 0;
// set global variables for the weather informations
int temperature = 0; // °C
int dew_point = 0; // °C
int wind_speed_knots = 0;
int pressure = 0; // hPa
int relative_humidity = 0; // %
int wind_speed_kmh = 0;
int data_age_min = 0;
unsigned long epochTime = 0;
unsigned long obsTime = 0;
/*
const char* piscine;
const char* TempExt;
const char* HumExt;
const char* Bat;
*/
float piscine = 0; //<=================
int TempExt = 0; //<=================
int HumExt = 0; //<=================
int Bat = 0; //<=================
//*********************************************
// SETUP
//*********************************************
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("Booting...");
// print debugg infos an serial
uint32_t chipId = 0;
for (int i = 0; i < 17; i = i + 8) chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
Serial.printf("ESP32 Chip model = %s Rev %d\n", ESP.getChipModel(), ESP.getChipRevision());
Serial.printf("This chip has %d cores\n", ESP.getChipCores());
Serial.print("Chip ID: ");
Serial.println(chipId);
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.print(" ");
// connect to router
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
wifiTimeOutCounter++;
if (wifiTimeOutCounter >= 60) ESP.restart();
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
uint8_t mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
for (int i = 0; i < 6; i++) {
Serial.print(mac[i], HEX);
if (i < 5) Serial.print(":");
}
Serial.println("");
// Convert MAC address to unique identifier (UID)
uint64_t uid = ((uint64_t)mac[0] << 40) | ((uint64_t)mac[1] << 32) | ((uint64_t)mac[2] << 24) | ((uint64_t)mac[3] << 16) | ((uint64_t)mac[4] << 8) | ((uint64_t)mac[5]);
Serial.print("MAC unique identifier: ");
Serial.println(uid);
// init time system
timeClient.begin();
timeClient.setTimeOffset(TIMEOFFSET);
// Init ttf
tft.init();
spr.setColorDepth(4);
spr.createSprite(MAX_X, MAX_Y);
tft.setRotation(1);
// load first data to start with
timeClient.update();
weatherData();
mqttClient.setServer(mqttBroker, mqttPort);
mqttClient.setCallback(mqttCallback);
mqttClient.setBufferSize(512);
while (!mqttClient.connected()) {
Serial.print("Tentative de connexion au MQTT broker: ");
Serial.println(mqttBroker);
if (mqttClient.connect(mqttClientID, mqttUsername, mqttPassword)) {
Serial.println("Vous êtes connecté au MQTT broker!");
if (mqttClient.subscribe(mqttTopic)) {
Serial.print("S'abonner au topic ");
Serial.print(mqttTopic);
Serial.println(": succès");
Serial.println("En attente de messages ...");
} else {
Serial.println("L'abonnement au Topic a échoué");
}
} else {
Serial.print("Failed, state=");
Serial.print(mqttClient.state());
Serial.println(" Nouvel essai en 5s...");
delay(5000);
}
}
}
//*********************************************
// LOOP
//*********************************************
void loop() {
spr.fillSprite(0); // init sprite
// dessiner une simulation d'univers dans un sprite vide
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++) {
float u = sin(i + v) + sin(r * i + x);
v = cos(i + v) + cos(r * i + x);
x = u + t;
int px = u * MAX_X / 4 + MAX_X / 2;
int py = v * MAX_Y / 4 + MAX_Y / 2;
uint16_t color = (i * 255) % 16;
for (int dx = 0; dx <= 1; ++dx)
for (int dy = 0; dy <= 1; ++dy)
spr.drawPixel(px + dx, py + dy, color);
}
t += 0.01;
// update weather information
/*
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > INTERVAL) {
previousMillis = currentMillis;
weatherData();
}
*/
// Print everything
spr.setTextColor(3);
timeClient.update();
String strtmp = timeClient.getFormattedTime() + " " + getFormattedDate();
spr.drawLine(0, 25, 319, 25,TFT_LIGHTGREY);
if (timeClient.isTimeSet()) {
spr.drawString(timeClient.getFormattedTime(), 5, 0, 4);
spr.drawString(getFormattedDate(), 185, 0, 4);
spr.setTextColor(5); // 3 =
spr.drawString("C",280,70,4);
spr.drawString(String(piscine,2), 35, 80, 8);
spr.setTextColor(6);
String outputString ="";
outputString = "T Ext.: "+String(TempExt) + "c " + "H Ext.: "+String(HumExt) + "% " +"Batterie : "+ String(Bat) + "%";
if (data_age_min < 70) spr.drawString(outputString, 0, 200, 2);
// push sprite (update screen)
spr.pushSprite(0, 0);
}
mqttClient.loop();
}
//****se connecter au serveur météorologique et obtenir des données ****************
void weatherData() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(metar);
Serial.print("Sending HTTP request to: ");
Serial.println(metar);
int httpCode = http.GET(); // Send HTTP GET request
if (httpCode > 0) { // Check if the request was successful
DynamicJsonDocument metardoc(2048); // Create JSON document (adjust the size accordingly)
String payload = http.getString(); // Retrieve response from the website
Serial.print("Response received: ");
Serial.println(payload); // Output METAR data to the Serial Console
DeserializationError error = deserializeJson(metardoc, payload); // Parse JSON data
if (!error) {
// start extractun weather data and update global variables
temperature =metardoc[0]["temp"];
dew_point = metardoc[0]["dewp"];
wind_speed_knots =metardoc[0]["wspd"];
pressure = metardoc[0]["altim"];
obsTime = metardoc[0]["obsTime"];
relative_humidity = 100 * expf(17.625f * dew_point / (243.04f + dew_point)) / expf(17.625f * temperature / (243.04f + temperature));
wind_speed_kmh = wind_speed_knots * 1.852f;
epochTime = timeClient.getEpochTime() - TIMEOFFSET;
data_age_min = (epochTime - obsTime) / 60;
// show most importand data on serial
/*
Serial.print("Temperature: ");
Serial.print(temperature, 0);
Serial.println();
Serial.print("Relative Humidity: ");
Serial.print(relative_humidity, 0);
Serial.println("%");
Serial.print("Wind Speed: ");
Serial.print(wind_speed_kmh, 0);
Serial.println("km/h");
Serial.print("Pressure: ");
Serial.print(pressure, 0);
Serial.println("hPa");
Serial.print("Data age: ");
Serial.print((epochTime - obsTime) / 60);
Serial.println("min");
*/
} else Serial.println("Erreur d'analyse des données JSON");
} else Serial.println("Erreur dans la récupération des données METAR");
http.end();
}
}
//********this is missing in the library *******************************************************
String getFormattedDate() {
#define LEAP_YEAR(Y) ((Y > 0) && !(Y % 4) && ((Y % 100) || !(Y % 400)))
unsigned long rawTime = timeClient.getEpochTime() / 86400L;
unsigned long days = 0, year = 1970;
uint8_t month;
static const uint8_t monthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
while ((days += (LEAP_YEAR(year) ? 366 : 365)) <= rawTime) year++;
rawTime -= days - (LEAP_YEAR(year) ? 366 : 365);
for (month = 0; month < 12; month++) {
uint8_t monthLength;
if (month == 1) monthLength = LEAP_YEAR(year) ? 29 : 28;
else monthLength = monthDays[month];
if (rawTime < monthLength) break;
rawTime -= monthLength;
}
String monthStr = ++month < 10 ? "0" + String(month) : String(month);
String dayStr = ++rawTime < 10 ? "0" + String(rawTime) : String(rawTime);
return String(dayStr) + "-" + monthStr + "-" + year;
}
//***************************************************************
void mqttCallback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrivé du topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
JsonDocument doc;
deserializeJson(doc, (unsigned char*)payload);
piscine = doc["field4"];
Serial.print("T° piscine = ");
Serial.println(piscine);
TempExt = doc["field1"];
Serial.print("T° Exterieur = ");
Serial.println(TempExt,0);
HumExt = doc["field2"];
Serial.print("Humidité Exterieure = ");
Serial.println(HumExt,0);
Bat = doc["field3"];
Serial.print("Batterie = ");
Serial.println(Bat,0);
} |
Partager