good evening
again a new sketch for the "void receiveTime" function
(without RTC clock)
start synchronization with the controller and regularly for the drift of the arduino.
display time and date on an OLED screen
error message when a loss of communication with the controller occurs
a video "https://www.casimages.com/v/RSWTNb-vokoscreen-2022-05-24-19-33-49.mp4.html"
/*
* LIBRAIRIE:
* MySensors v2 : https://github.com/mysensors/MySensors
* TimeLib.h : https://github.com/PaulStoffregen/Time
* SSD1306Ascii.h : https://github.com/greiman/SSD1306Ascii
*
* MATERIEL:
* Arduino Uno
* -Afficheur OLED 0,96" I2C TF052 / circuit SSD1306.
* bus TTLModule RS485
*
* REVISION HISTORY
* base de Henrik Ekblad 2013-2015 et autres :)
* ajout bus RS485 et OLED
*
* DESCRIPTION
* demander Date et Heure au contrôleur et l'affiche sur ecran OLED
* Request date and time from the controller and display it on OLED screen
*
* Connectique i2c:
* Arduino : A4 A5
* OLED : SDA SCL
*
*/
//-- The sketch uses 11932 bytes (36%) ----global variables use 839 bytes (40%)-----------
//-
//------------ 2022 ----------------------- Fonctionnelle avec MyC et MyS-------------------
//#define MY_DEBUG /*Enable debug prints to serial monitor*/
#define MY_TRANSPORT_WAIT_READY_MS 3000 /*Timeout (ms) pour mis en Com.(0 pour aucun)*/
#define MY_NODE_ID 22 /*Node en ID static*/
/* ----- Module RS485 ----*/
#define MY_RS485 /*Apl du transport RS485 (protocol?)*/
#define MY_RS485_DE_PIN 2 /*Cmd DE pin*/
#define MY_RS485_BAUD_RATE 9600 /*Set RS485 baud rate to use*/
//#define MY_RS485_HWSERIAL Serial1 /*pour port Serial autre*/
#include "SSD1306Ascii.h"
#include "SSD1306AsciiAvrI2c.h"
#include <MySensors.h>
#include <TimeLib.h>
#define CHILD_ID 22 /* MyS numero du Node*/
#define I2C_ADDRESS 0x3C /*i2c adresse*/
#define RST_PIN -1 // Define proper RST_PIN if required.(oled)
SSD1306AsciiAvrI2c oled; //type de com oled
//----- MyS ------
bool timeReceived = false;
bool stateCom = false;
unsigned long lastUpdate=0, lastRequest=0 , lastmillis = 0;
unsigned long newTime = 0, oldTime = 0, ComTime = 6000;
//---------------- SETUP ----------------------------
void setup() {
//--- OLED ----
#if RST_PIN >= 0
oled.begin(&Adafruit128x32, I2C_ADDRESS, RST_PIN);
#else // RST_PIN >= 0
oled.begin(&Adafruit128x32, I2C_ADDRESS);
#endif // RST_PIN >= 0
oled.setFont(Adafruit5x7); /*font Oled*/
// oled.setFont(font8x8); //bien
// oled.setFont(Verdana12); //grand
// oled.setFont(fixed_bold10x15); //tres grand
// oled.setFont(Arial_bold_14); //grand sympa
oled.clear();
}
//--------------MySensors-------------
void presentation() {
sendSketchInfo("Clock", "2.0");
}
//------------------ LOOP -----------------------
void loop() {
// ---- MyS time --------
unsigned long now = millis();
if (now-lastRequest > (ComTime)) {
timeReceived = false;
requestTime(receiveTime); // Request time from controller.
lastRequest = now;
}
//----- Tempo Display Oled ----------
if (now-lastUpdate > 10000) { //10secondes
lastUpdate = now;
// Serial.print(" test lastmillis------------ display : ");
// Serial.println(timeReceived);
updateDisplay();
lastmillis = millis();
//----- Stat Com ---
if (timeReceived == 1) { //En Com Controller stateCom
ComTime = 480UL*1000UL; // 480=8 minutes cycles for Control presence controller
}
else { // Erreur Com Controller
stateCom = false;
ComTime = 60UL*1000UL; // secondes for attempted reconnection to the controller
// Serial.println(" ----*** Erreur Com Controller *** ---- ");
}
}
} //---Loop
// -------------------- MyS --------------------------------
void receiveTime(unsigned long controllerTime) {
if (receiveTime) stateCom = true ; else stateCom = false;
newTime = controllerTime;
timeReceived = true;
setTime(controllerTime); // apl pour TimeLib.h
}
//------------Gestion de l'affichage OLED---------------
void updateDisplay() {
oled.set1X(); //ligne Message
oled.setCursor(2, 0);
printStatDay () ; // affiche jour de la semaine/poster day of the week
//oled.print(day());
oled.set2X(); //ligne Heures
oled.setCursor(18,1);
oled.print(hour());
printDigits(minute());
printDigits(second());
oled.set1X(); // ligne jour mois Année / day month year
oled.setCursor(35,3);
oled.print(day());
printDigitsDay(month());
printDigitsDay(year());
}
//-------- Day displaying-------
void printDigitsDay(int digitday) { //function for Day displaying "0" and separator "/" for day/month/year values
oled.print("/");
if(digitday < 10)
oled.print('0');
oled.print(digitday);
}
//-------- Time displaying-------
void printDigits(int digits) { //function for Time displaying "0" and separator ":" for hour:minute:sec values
oled.print(":");
if(digits < 10)
oled.print('0');
oled.print(digits);
}
//------ conversion date en texte (fonction weekday) et statu Com Controller -----------
void printStatDay () { //function for display Day texte
oled.invertDisplay(!(stateCom)); // inverse N/B Oled sur erreur Com
if(stateCom == true) { oled.print("com "); } //com okai
if(stateCom == false){ oled.print("Error"); } //com Error
// oled.print(stateCom);
oled.print(" ");
if(weekday() == 1) { oled.print("dimanche"); } //Sunday
if(weekday() == 2) { oled.print("lundi"); } //Monday
if(weekday() == 3) { oled.print("mardi"); } //Tuesday
if(weekday() == 4) { oled.print("mercredi"); } //Wednesday
if(weekday() == 5) { oled.print("jeudi"); } //Thursday
if(weekday() == 6) { oled.print("vendredi"); } //Friday
if(weekday() == 7) { oled.print("samedi"); } //Saturday
oled.print(" ");
oled.print(day());
}
//-------------------FIN PGM --------------------------
//------------------- INFO -------------------
why open a new post, there is still some space here