7 changed files with 183 additions and 457 deletions
-
54README.md
-
5platformio.ini
-
81src/main.cpp
-
61src/my_EEPROM.h
-
100src/my_MQTT.h
-
273src/my_OTA.h
-
66src/my_leds.h
@ -0,0 +1,54 @@ |
|||
# MQTT Lampe ADN # |
|||
Programme ESP8266 permettant de faire réagire une double colonne de leds (leds strip) en fonction d'un message MQTT envoyé par un brocker. |
|||
|
|||
Le nombre de leds est paramétrable. |
|||
|
|||
La partie brocker n'est pas inclus dans le présent programme. |
|||
|
|||
|
|||
# Fonctionnement # |
|||
## Connection Wifi ## |
|||
La connection wifi est gérée par l'extension **WIFI manager**. |
|||
|
|||
Si la connexion échoue, on lance un Access Point (AP) qui est visible dans les réseaux WIFI |
|||
Il faut alors se connecter avec un smarthpone sur l'AP pour configurer le Wifi en lui saisissant son SSID et le Mot de passe, |
|||
le NodeMCU reboot et se connecte avec le SSID et mot de passe saisie. Une fois, ces identifiants saisies, l'ESP8266 est déclaré sur le réseau et il ne sera plus nécessaire de les saisir. |
|||
|
|||
Peux importe l'adresse IP de l'ESP8266, elle n'est pas utile pour recevoir des messages MQTT (sauf si un filtrage est mis en place au niveau du brocker, mais cette documentation ne couvre pas cette partie). |
|||
|
|||
## MQTT ## |
|||
L'adresse IP du brocker MQTT doit être définie dans le fichier MQTT.h |
|||
|
|||
Les messages MQTT interprétés doivent être dans les topics: |
|||
|
|||
- lumiere/lampeADN [ON|OFF] : Allumage de la barre de LEDS. |
|||
- lumiere/color/lampeADN (R,V,B) : Changement de couleur des LEDS. |
|||
- lumiere/brightness/lampeADN value : luminosité [0-255] |
|||
- lumiere/animation/lampeADN value : ID de l'animation [0-6] |
|||
- lumiere/allume/lampeADN value : Allummage des leds du bas vers le haut (en %) 0 = aucune, 100 = toutes |
|||
|
|||
|
|||
|
|||
# Mise à jour OTA # |
|||
TODO |
|||
|
|||
Bien que cela soit possible il n'est pas nécessaire de brancher l'ESP8266 sur un port USB pour le mettre à jour, une simple connexion wifi suffi. |
|||
La mise à jour se fait un uploadant le firmware compilé dans l'ESP via son URL OTA. |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
--- |
|||
title: MQTT Lampe ADN |
|||
subtitle: Changeur de couleur LEDS dans la lampe ADN en fonction de messages MQTT |
|||
lang: fre |
|||
author: Alexandre PERETJATKO |
|||
language: C (Arduino) |
|||
hardware: Node MCU ESP8266 |
|||
website: https://git.alex-design.fr/Alex/MQTT-leds-color.git |
|||
... |
|||
@ -1,61 +0,0 @@ |
|||
/* |
|||
Bibliothèque permettant de stocker 2 valeurs en EEPROM |
|||
@see : https://projetsdiy.fr/esp8266-comment-lire-ecrire-effacer-eeprom/ |
|||
*/ |
|||
#include <EEPROM.h> |
|||
|
|||
|
|||
/*---------------------------------------------------------------------------- |
|||
Permet d'écrire une chaine de caractère en EEPROM à une adresse donnée. |
|||
*/ |
|||
void EEPROM_writeString(char add, String data) { |
|||
int _size = data.length(); |
|||
int i; |
|||
for (i = 0; i < _size; i++) |
|||
{ |
|||
EEPROM.write(add + i, data[i]); |
|||
} |
|||
EEPROM.write(add + _size, '\0'); //Add termination null character for String Data |
|||
EEPROM.commit(); |
|||
} |
|||
|
|||
|
|||
/*---------------------------------------------------------------------------- |
|||
Permet de lire une chaine de caractère en EEPROM à une adresse donnée. |
|||
*/ |
|||
String EEPROM_read_String(char add){ |
|||
char data[100]; //Max 100 Bytes |
|||
int len=0; |
|||
unsigned char k; |
|||
k=EEPROM.read(add); |
|||
while(k != '\0' && len<500) //Read until null character |
|||
{ |
|||
k=EEPROM.read(add+len); |
|||
data[len]=k; |
|||
len++; |
|||
} |
|||
data[len]='\0'; |
|||
return String(data); |
|||
} |
|||
|
|||
/*---------------------------------------------------------------------------- |
|||
Permet de mettre en place la gestion du nombre de leds en EEPROM. |
|||
*/ |
|||
void EEPROM_Start() { |
|||
EEPROM.begin(EEPROM_LEDS_SIZE + EEPROM_DEVICEID_SIZE + 2); |
|||
if ( writeToEEPROM == true ) { |
|||
Serial.println("* * * * * * * * * * * * * Stockage des valeurs en EEPROM * * * * * * * * * * * * * * "); |
|||
|
|||
// Ecriture des valeurs dans l'EEPROM |
|||
EEPROM.put(0, LED_COUNT); // on a un entier, on peut utiliser put |
|||
EEPROM_writeString(sizeof(LED_COUNT), DeviceID); // on a une chaine de caractère, on doit utiliser une fonction custom |
|||
} |
|||
|
|||
// Relecture de la valeur stockée dans l'EEPROM |
|||
EEPROM.get(0, LED_COUNT); |
|||
Serial.print("Nb leds (from EEPROM) LED_COUNT: "); Serial.println(LED_COUNT); |
|||
|
|||
String data = EEPROM_read_String(sizeof(LED_COUNT)); // Lecture dans une string |
|||
data.toCharArray(DeviceID, EEPROM_DEVICEID_SIZE); // Convertion de String en char |
|||
Serial.print("DeviceID (from EEPROM) : "); Serial.println(DeviceID); |
|||
} |
|||
@ -1,273 +0,0 @@ |
|||
/** |
|||
Bibliothèque pour avoir une mise à jour Over The Air d'un code source. |
|||
Utilisation : |
|||
Dans le setup, rajouter OTA_setup(); |
|||
Dans la loop, rajouter OTA_doUpdate(); |
|||
Fonctionnement : |
|||
Toutes les OTA_TimerInSecond secondes, le programme va vérifier qu'il y a une mise à jour sur le serveur. |
|||
Si une mise à jour existe, elle est téléchargée et installé et l'ESP reboot. |
|||
S'il n'y a pas de mise à jour, le serveur le dit et on ne fait rien. |
|||
|
|||
Avant toutes upload de ce script dans un Arduino, il faut executer un script Python de récupération de certificats qui |
|||
se trouve sur le repot https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/BearSSL_CertStore/certs-from-mozilla.py |
|||
*/ |
|||
|
|||
#include <ESP8266WiFi.h> |
|||
#include <ESP8266WiFiMulti.h> |
|||
|
|||
#include <ESP8266HTTPClient.h> |
|||
#include <ESP8266httpUpdate.h> |
|||
char outputBuffer[100]; // Pour les messages à afficher |
|||
char macAdresse[12]; |
|||
|
|||
// Fait un serveur de mise à jour local dans l'ESP |
|||
#include <ESP8266HTTPUpdateServer.h> |
|||
#include <ESP8266WebServer.h> |
|||
ESP8266WebServer OTA_HttpServer(80); |
|||
ESP8266HTTPUpdateServer OTA_httpUpdater; |
|||
const char* OTA_update_username = "ESPadmin"; |
|||
const char* OTA_update_password = "admin"; |
|||
const char* OTA_update_path = "/firmware"; |
|||
boolean g_BOO_UpdateFirmware = true; |
|||
|
|||
|
|||
const String firmwareUrlMiseAJour = "https://update.alex-design.fr/MQTT-leds-color/update.php"; |
|||
const String fileSystemUrlMiseAJour = "https://update.alex-design.fr/MQTT-leds-color/updateFS.php"; |
|||
//const String firmwareUrlMiseAJour = "http://192.168.0.32:9090/MQTT-leds-color/update.php"; <- Ne peut pas fonctionner car il est en http, et on veut du https |
|||
// Define global variable to know if upate is available |
|||
long OTA_UpdateTimer; |
|||
const int OTA_TimerInSecond = 60 * 10; // every 10 minute |
|||
|
|||
|
|||
// Utilisation d'un certificat ------------------------------------------ |
|||
// Pour mettre à jour l'heure (obligatoire avec un certificat https) |
|||
#include <time.h> |
|||
// A single, global CertStore which can be used by all |
|||
// connections. Needs to stay live the entire time any of |
|||
// the WiFiClientBearSSLs are present. |
|||
#include <CertStoreBearSSL.h> |
|||
BearSSL::CertStore certStore; |
|||
#include <FS.h> |
|||
#include <LittleFS.h> |
|||
char versionLitteFS[10] = "0.0.0"; |
|||
|
|||
|
|||
// Define a wifi client |
|||
ESP8266WiFiMulti WiFiMulti; |
|||
|
|||
/** |
|||
---------------------------------------------------------------------------------------------------------------- |
|||
Callback lorsque la maj OTA démarre |
|||
*/ |
|||
void _update_started() { |
|||
DEBUG("CALLBACK: HTTPS update process started"); |
|||
} |
|||
|
|||
/** |
|||
---------------------------------------------------------------------------------------------------------------- |
|||
Callback lorsque la maj OTA est terminée |
|||
*/ |
|||
void _update_finished() { |
|||
DEBUG("CALLBACK: HTTPS update process finished. Reboot"); |
|||
} |
|||
|
|||
/** |
|||
---------------------------------------------------------------------------------------------------------------- |
|||
Callback lorsque la maj OTA est en cours |
|||
*/ |
|||
void _update_progress(int cur, int total) { |
|||
sprintf(outputBuffer, "CALLBACK: HTTPS update process at %d of %d bytes...", cur, total); |
|||
DEBUG( outputBuffer ); |
|||
} |
|||
|
|||
|
|||
/** |
|||
---------------------------------------------------------------------------------------------------------------- |
|||
Callback lorsque la maj OTA a plantée |
|||
*/ |
|||
void _update_error(int err) { |
|||
sprintf(outputBuffer, "CALLBACK: HTTPS update fatal error code %d\n", err); |
|||
DEBUG( outputBuffer ); |
|||
|
|||
if (err == -103) { |
|||
DEBUG(" Please allow me, I am "); |
|||
DEBUG(WiFi.macAddress()); |
|||
DEBUG(WiFi.localIP()); |
|||
} |
|||
|
|||
if ( err == 0 ) { |
|||
DEBUG("La mise à jour du firmware via OTA n'a pas marché, on stop !!!!!!!!!!!!"); |
|||
g_BOO_UpdateFirmware = false; |
|||
} |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
/** |
|||
---------------------------------------------------------------------------------------------------------------- |
|||
Set time via NTP, as required for x.509 validation |
|||
---------------------------------------------------------------------------------------------------------------- |
|||
*/ |
|||
void OTA_setClock() { |
|||
configTime(0, 0, "pool.ntp.org", "time.nist.gov"); // UTC |
|||
DEBUG("OTA Waiting for NTP time sync: "); |
|||
time_t now = time(nullptr); |
|||
while (now < 8 * 3600 * 2) { |
|||
yield(); |
|||
delay(500); |
|||
Serial.print(F(".")); |
|||
now = time(nullptr); |
|||
} |
|||
struct tm timeinfo; |
|||
gmtime_r(&now, &timeinfo); |
|||
|
|||
DEBUG("Current time: "); |
|||
DEBUG(asctime(&timeinfo)); |
|||
} |
|||
|
|||
|
|||
|
|||
/** |
|||
---------------------------------------------------------------------------------------------------------------- |
|||
Initialisation d'une connexion wifi à l'aide des settings de wifimanager en eeprom. |
|||
---------------------------------------------------------------------------------------------------------------- |
|||
*/ |
|||
void OTA_setup() { |
|||
|
|||
/* Mise en place d'un serveur pour uploader directement un binaire */ |
|||
OTA_httpUpdater.setup(&OTA_HttpServer, OTA_update_path, OTA_update_username, OTA_update_password); |
|||
OTA_HttpServer.begin(); |
|||
sprintf(outputBuffer, "OTA HTTPUpdateServer ready! Open http://%d.%d.%d.%d%s in your browser and login with username '%s' and password '%s'\n", |
|||
WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3], OTA_update_path, OTA_update_username, OTA_update_password); |
|||
DEBUG(outputBuffer); |
|||
MQTT_publishDebug(String(outputBuffer)); |
|||
|
|||
|
|||
|
|||
|
|||
// Test la version du file system littleFS en place |
|||
if ( !LittleFS.begin()) { |
|||
DEBUG("Il n'y a pas de file system little FS installé, lecture impossible !"); |
|||
|
|||
} else { |
|||
// Get version of the certificat in LittleFS (frome file version.txt |
|||
File file = LittleFS.open("/version.txt", "r"); |
|||
if ( file ) { |
|||
while (file.available()) { |
|||
strcpy(versionLitteFS, file.readString().c_str() ); |
|||
} |
|||
file.close(); |
|||
} |
|||
|
|||
// Récupération des CA stockés dans LittleFS pour les certificats SSL |
|||
int numCerts = certStore.initCertStore(LittleFS, PSTR("/certs.idx"), PSTR("/certs.ar")); |
|||
DEBUG("Number of CA certs read: "); |
|||
DEBUG(numCerts); |
|||
if (numCerts == 0) { |
|||
DEBUG(F("No certs found. Did you run certs-from-mozill.py and upload the LittleFS directory before running?")); |
|||
} |
|||
} |
|||
DEBUG("Numéro de version de littleFS : " + String(versionLitteFS) ); |
|||
|
|||
|
|||
// Création de la connection Wifi à partir du SSID et PWD sauvé par wifimanager |
|||
/* |
|||
WiFi.mode(WIFI_STA); |
|||
WiFiMulti.addAP(wifiManager.getWiFiSSID(true).c_str(), wifiManager.getWiFiPass(true).c_str()); |
|||
*/ |
|||
|
|||
// We will request a firmware update in OTA_TimerInSecond secondes |
|||
OTA_UpdateTimer = (OTA_TimerInSecond * 1000); |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
/** |
|||
---------------------------------------------------------------------------------------------------------------- |
|||
Va voir sur l'URL si une mise à jour du firmware est disponible. Si c'est le cas, |
|||
la télécharge et met à jour le firmeware. |
|||
---------------------------------------------------------------------------------------------------------------- |
|||
*/ |
|||
boolean OTA_doUpdate() { |
|||
// Si on a pas besoin de faire de mise à jour (ou ça c'est planté la dernière fois) |
|||
if ( !g_BOO_UpdateFirmware ) { |
|||
return false; |
|||
} |
|||
|
|||
// Fait tourner le serveur http sur l'ESP |
|||
// Lorsqu'on se connect à ce serveur, il est possible d'uploader un firmware ou un filesystem |
|||
OTA_HttpServer.handleClient(); |
|||
|
|||
// Check is this is the time to check a new update |
|||
delay( 1 ); // Wait 1 milliseconde |
|||
if (OTA_UpdateTimer > 0 ) { |
|||
OTA_UpdateTimer--; |
|||
return false; |
|||
} |
|||
|
|||
if (WiFiMulti.run() == WL_CONNECTED) { |
|||
// WiFiClient client; // Client simple (incompatioble en https ) |
|||
|
|||
// Mise à jour de l'heure via un serveur NTP |
|||
OTA_setClock(); |
|||
|
|||
// Récupération du certificat SSL pour la connexion https |
|||
BearSSL::WiFiClientSecure client; // Client securise |
|||
bool mfln = client.probeMaxFragmentLength(fileSystemUrlMiseAJour, 443, 1024); // server must be the same as in ESPhttpUpdate.update() |
|||
if (mfln) { |
|||
client.setBufferSizes(1024, 1024); |
|||
} |
|||
client.setCertStore(&certStore); |
|||
|
|||
// Add optional callback notifiers |
|||
ESPhttpUpdate.onStart(_update_started); |
|||
ESPhttpUpdate.onEnd(_update_finished); |
|||
ESPhttpUpdate.onProgress(_update_progress); |
|||
ESPhttpUpdate.onError(_update_error); |
|||
ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW); |
|||
|
|||
|
|||
// Get the mac adresse in a char |
|||
snprintf(macAdresse, 12, "%06X", (uint32_t)ESP.getChipId() ); |
|||
|
|||
|
|||
// Try to update the filesystem |
|||
DEBUG(fileSystemUrlMiseAJour + "?chipID=" + String(macAdresse) ); |
|||
t_httpUpdate_return ret = ESPhttpUpdate.updateFS(client, fileSystemUrlMiseAJour + "?chipID=" + String(macAdresse), versionLitteFS); |
|||
if (ret == HTTP_UPDATE_OK) { |
|||
DEBUG("Update FileSystem Successfully"); |
|||
} |
|||
|
|||
|
|||
// Try to update the firmware |
|||
DEBUG(firmwareUrlMiseAJour + "?chipID=" + String(macAdresse) ); |
|||
DEBUG("Version firmware :" + firmwareActualVersion ); |
|||
|
|||
ret = ESPhttpUpdate.update(client, firmwareUrlMiseAJour + "?chipID=" + String(macAdresse), firmwareActualVersion); |
|||
switch (ret) { |
|||
case HTTP_UPDATE_FAILED: |
|||
|
|||
sprintf(outputBuffer, "HTTP_UPDATE_FAILED Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); |
|||
DEBUG( outputBuffer ); |
|||
|
|||
OTA_UpdateTimer = (OTA_TimerInSecond * 1000); |
|||
break; |
|||
|
|||
|
|||
case HTTP_UPDATE_NO_UPDATES: |
|||
DEBUG("No new update available"); |
|||
|
|||
OTA_UpdateTimer = (OTA_TimerInSecond * 1000); |
|||
break; |
|||
|
|||
default: |
|||
break; |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue