Some updates

This commit is contained in:
2021-01-30 10:50:07 +01:00
parent dd88541135
commit eb912b8e74
13 changed files with 2765 additions and 299 deletions

View File

@ -0,0 +1,37 @@
// OTA Stuff to push the Code using Arduino OTA
#include "Fil_Pilot.h"
// Setup OTA
void setup_ota() {
//Port defaults to 8266
//ArduinoOTA.setPort(8266);
// Hostname defaults to projet name
ArduinoOTA.setHostname(Mqtt_clientid);
// No auth per default
ArduinoOTA.setPassword((const char *)OTAPASSWORD);
ArduinoOTA.onStart([]() {
Serial.println("OTA Update is Starting !");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nOTA is done. Rebooting...");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("OTA Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
}

View File

@ -0,0 +1,147 @@
// Wifi Manager
// Thanks to https://github.com/tzapu/WiFiManager/blob/b22ba24283ad3f22f8c8dfb2a52ed0583c0222d7/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino
//callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
void setup_WMN() {
//read configuration from FS json
Serial.println("mounting FS...");
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
DeserializationError deserializeError = deserializeJson(json, buf.get());
serializeJsonPretty(json, Serial);
if (!deserializeError) {
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
#endif
Serial.println("\nparsed json");
strcpy(mqtt_server, json["mqtt_server"]);
strcpy(mqtt_port, json["mqtt_port"]);
strcpy(blynk_token, json["blynk_token"]);
} else {
Serial.println("failed to load json config");
}
configFile.close();
}
}
} else {
Serial.println("failed to mount FS");
}
// The extra parameters to be configured (can be either global or just in the setup)
// After connecting, parameter.getValue() will get you the configured value
// id/name placeholder/prompt default length
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
//set static ip
wifiManager.setSTAStaticIPConfig(IPAddress(10, 0, 1, 99), IPAddress(10, 0, 1, 1), IPAddress(255, 255, 255, 0));
//add all your parameters here
wifiManager.addParameter(&custom_mqtt_server);
wifiManager.addParameter(&custom_mqtt_port);
wifiManager.addParameter(&custom_blynk_token);
//reset settings - for testing
//wifiManager.resetSettings();
//set minimu quality of signal so it ignores AP's under that quality
//defaults to 8%
//wifiManager.setMinimumSignalQuality();
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
//wifiManager.setTimeout(120);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
//read updated parameters
strcpy(mqtt_server, custom_mqtt_server.getValue());
strcpy(mqtt_port, custom_mqtt_port.getValue());
strcpy(blynk_token, custom_blynk_token.getValue());
Serial.println("The values in the file are: ");
Serial.println("\tmqtt_server : " + String(mqtt_server));
Serial.println("\tmqtt_port : " + String(mqtt_port));
Serial.println("\tblynk_token : " + String(blynk_token));
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
DynamicJsonDocument json(1024);
#else
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
#endif
json["mqtt_server"] = mqtt_server;
json["mqtt_port"] = mqtt_port;
json["blynk_token"] = blynk_token;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
serializeJsonPretty(json, Serial);
serializeJson(json, configFile);
#else
json.printTo(Serial);
json.printTo(configFile);
#endif
configFile.close();
//end save
}
Serial.println("local ip");
Serial.println(WiFi.localIP());

View File

@ -0,0 +1,8 @@
// Some constants and variables for this project.
const char* Mqtt_clientid = "ESP-FP";
const char* dom_in = "domoticz/in";
const char* dom_out = "domoticz/out";
// Flag for saving data
bool shouldSaveConfig = false;

View File

@ -0,0 +1,5 @@
// Personnal Settings
// OTA Password for Arduino
#define OTAPASSWORD "123"

View File

@ -0,0 +1,28 @@
// Fil Pilot code
//
// Hande low level code for Xavier Beaudouin's Fil Pilot Board
//
#include <FS.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ArduinoJson.h>
#include <ArduinoOTA.h>
#include "Fil_Pilot.h" // Defines and some configurations
#include "FP_const.h" // Constants variables.
void setup() {
Serial.begin (115200);
while (!Serial); // Wait for Arduino
Serial.println("\n\n");
// Launch OTA stuff
setup_ota();
}
void loop() {
// End
ArduinoOTA.handle();
}

248
Fil_Pilot/Fp_2/Fp_2.ino Normal file
View File

@ -0,0 +1,248 @@
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <LittleFS.h>
#ifndef STASSID
#define STASSID "Kiwi"
#define STAPSK "ZeKiwi127"
#endif
// FP status info LittleFS
#define FP_STATUS "/fp_status.txt"
const char* ssid = STASSID;
const char* password = STAPSK;
File Status_File;
// Configuration des IO
const uint16_t ALT_POS = 13; // Broche pilotant l'alternance positive
const uint16_t ALT_NEG = 14; // Broche pilotant l'alternance négative
const uint8_t DEFAULT_PILOTE_STATUS = 1; // Mode par défaut Eco de manière à ce que la LED passe en orange après entrée d'une configuration valide
unsigned int fp = 0;
// Cette fonction pilote le changement d'état des sorties, de la LED bicolore d'état et le mémorise dans le fichier d'état
void Pilote (int Status) {
switch (Status) {
case 0 : // aucune alternance en sortie, LED allumée en rouge : Confort
Serial.println("Confort");
digitalWrite(ALT_NEG,LOW);
digitalWrite(ALT_POS,LOW);
break;
case 1 : // pleine alternance en sortie, LED allumée en orange (rouge+vert) : Eco
Serial.println("Eco");
digitalWrite(ALT_NEG,HIGH);
digitalWrite(ALT_POS,HIGH);
break;
case 2 : // demie alternance négative en sortie, LED allumée en vert : Hors Gel
Serial.println("Hors Gel");
digitalWrite(ALT_NEG,HIGH);
digitalWrite(ALT_POS,LOW);
break;
case 3 : // demie alternance positive en sortie, LED éteinte : Arrêt
Serial.println("Arrêt");
digitalWrite(ALT_NEG,LOW);
digitalWrite(ALT_POS,HIGH);
break;
}
// Mémorisation dans le fichier
Serial.print(F("Status écrit : "));
Serial.println(Status);
Status_File = LittleFS.open(FP_STATUS, "w+");
Status_File.write(Status);
Status_File.seek(0, SeekSet);
Serial.print(F("Status relu : "));
Serial.println(Status_File.read());
Status_File.close();
}
// Setup OTA Stuff
void setupOTA() {
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
//ArduinoOTA.setHostname("myesp8266");
// No authentication by default
ArduinoOTA.setPassword("12345");
// Password can be set with it's md5 value as well
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
// NOTE: if updating FS this would be the place to unmount FS using FS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
}
// Setup LittleFS
void setupLFS() {
Serial.println(F("Initializing FS..."));
if (LittleFS.begin()) {
Serial.println(F("LittleFS system mounted with success"));
} else {
Serial.println(F("An Error has occurred while mounting LittleFS"));
}
// Get all information about LittleFS
FSInfo fsInfo;
LittleFS.info(fsInfo);
Serial.println("------------------------------");
Serial.println("File system info");
Serial.println("------------------------------");
// Taille de la zone de fichier
Serial.print("Total space: ");
Serial.print(fsInfo.totalBytes);
Serial.println(" byte");
// Espace total utilise
Serial.print("Total space used: ");
Serial.print(fsInfo.usedBytes);
Serial.println(" byte");
// Taille d un bloc et page
Serial.print("Block size: ");
Serial.print(fsInfo.blockSize);
Serial.println(" byte");
Serial.print("Page size: ");
Serial.print(fsInfo.totalBytes);
Serial.println(" byte");
Serial.print("Max open files: ");
Serial.println(fsInfo.maxOpenFiles);
// Taille max. d un chemin
Serial.print("Max path lenght: ");
Serial.println(fsInfo.maxPathLength);
Serial.println();
Serial.println("------------------------------");
Serial.println("List files");
Serial.println("------------------------------");
// Ouvre le dossier racine | Open folder
Dir dir = LittleFS.openDir("/");
// Affiche le contenu du dossier racine | Print dir the content
while (dir.next()) {
// recupere le nom du fichier | get filename
Serial.print(dir.fileName());
Serial.print(" - ");
// et sa taille | and the size
if (dir.fileSize()) {
File file = dir.openFile("r");
Serial.print(file.size());
Serial.println(" byte");
file.close();
} else {
File file = dir.openFile("r");
if ( file.isDirectory() ) {
Serial.println("this is a folder");
} else {
Serial.println("file is empty");
}
file.close();
}
}
}
void fp_from_fs() {
unsigned int Num_Cde;
if (!LittleFS.exists(FP_STATUS)) {
Serial.println("Fil Pilot status file does not exists. Create it");
Status_File = LittleFS.open(FP_STATUS, "w");
Status_File.write(DEFAULT_PILOTE_STATUS);
Status_File.close();
Pilote(DEFAULT_PILOTE_STATUS);
return;
}
// File should exist so read it
Serial.println(F("Status FP from FS : "));
Status_File = LittleFS.open(FP_STATUS, "r");
Num_Cde = Status_File.read();
Status_File.close();
Serial.print(F("Status is : "));
Serial.println(Num_Cde);
Pilote(Num_Cde);
}
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// Setup OTA
setupOTA();
// Setup LittleFS
setupLFS();
// Alternance -
pinMode(ALT_NEG, OUTPUT);
// Alternance +
pinMode(ALT_POS, OUTPUT);
// Now retreive FP status from FS
fp_from_fs();
// We are ready
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
Pilote(fp);
fp = fp + 1;
if (fp >= 4) {
fp = 0;
}
delay(30000);
ArduinoOTA.handle();
}

File diff suppressed because it is too large Load Diff