Affichage Heure/température

Réalisation d’un affichage de l’heure et de la température de piscine.

Mon horloge DCF77 ayant rendu l’âme et la piscine étant mise à profit cet été, il est temps d’affiche l’heure et la température de l’eau au utilisatrices/eurs.

Composants:

Lolin Wemos D1 mini ESP8266 (1,82€)

Affichage 7 segments TM1637 (2€)

Alimentation et câble USB A-Micro (Action !) (4€)

Donc pour 7,82 € (sans la boite).

Connexions assez simples:

|D1 Mini|Modul TM1637| |3,3 V|VCC| |GND|GND| |D5|DIO| |D6|CLK|

Une boite à imprimer en 3D

Composantterminé

Librairies (Arduino IDE)

TM1637Display

esp-knx-ip

NTPClient

ESP8266WiFi

WiFiUdp

ESP8266TimerInterrupt

Code:

#include <TM1637Display.h>
#include <esp-knx-ip.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "ESP8266TimerInterrupt.h"

#define TIMER_INTERVAL_MS       5000 // display swap timer interval in microseconds

// set your own SSID/pword crednetials in here

const char *ssid     = "XXXX";
const char *password = "YYYY";

config_id_t my_GA;
config_id_t param_id;
config_id_t enable_sending_id;

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);



const int CLK = D6; //Set the CLK pin connection to the display
const int DIO = D5; //Set the DIO pin connection to the display
config_id_t temp_ga;
float last_temp=0.0;
int letemps;
int value;
float Ptemp = 0;
uint8_t colonOn = 0x80;
uint8_t colonOff = 0x00;
bool colon = true;

TM1637Display display(CLK, DIO); //set up the 4-Digit Display.


volatile bool status = false;
volatile uint32_t lastMillis = 0;


// Init ESP8266 timer 1
ESP8266Timer ITimer;

//=======================================================================
// routine de traitement des interruptions du timer
// set variable to the value to be displayed, 
// alternate between the Time and the temperature

void IRAM_ATTR TimerHandler()
{
static bool started = false;

if (!started)
{
    started = true;
    pinMode(BUILTIN_LED, OUTPUT);    //signal with LED
}

if (status) {
    value=int(Ptemp*100);
} else {
    value=letemps;
}
status = !status; //toggle status flag
}
//=========================================================================
void pooltemp(message_t const &msg, void *arg)
// routine that handle he reception of a pool temperature value

{
    switch (msg.ct)
    {
    case KNX_CT_WRITE:
        // Save received data
    Serial.print("received Pool temp on KNX: ");
        Ptemp = knx.data_to_2byte_float(msg.data);
    Serial.println(Ptemp);
        break;
    }
}

bool show_periodic_options()
{
return knx.config_get_bool(enable_sending_id);
}

void setup()
{
Serial.begin(115200);
display.setBrightness(0x0a); //set the diplay to maximum brightness


temp_ga = knx.config_register_ga("Pool Temperature", show_periodic_options);

// set the knx callback routine
knx.callback_register("Set/Get callback", pooltemp);
knx.load();

// setup IP connectivity

WiFi.begin(ssid, password);

while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
}
Serial.println();
Serial.println("Connected to wifi");
Serial.println(WiFi.localIP());


// start knx instance

knx.start();
// start the NTP client to get the time from Internet
timeClient.begin();
timeClient.setTimeOffset(7200);  // receiving GMT time -> local
// instanciate a timer to exchange displayed valuee.
// Interval in microsecs
if (ITimer.attachInterruptInterval(TIMER_INTERVAL_MS * 1000, TimerHandler))
{
    lastMillis = millis();
    Serial.print(F("Starting ITimer OK, millis() = "));
    Serial.println(lastMillis);
}
else
    Serial.println(F("Can't set ITimer correctly. Select another freq. or interval"));

display.clear();  // clear the 7 segments display.

}


void ploop()
{
    timeClient.update();
    Serial.println("starting knx loop");
    Serial.println(timeClient.getFormattedTime());
    int hours = timeClient.getHours();
    int min = timeClient.getMinutes();
    int sec = timeClient.getSeconds();
    // store in global
    letemps = hours *100 + min;
    Serial.println(letemps);
    // switch on/off the coon every seconds
    // value hold the current data to be displayed
    // either temperature
    // or time in hours/minutes
    if (sec%2 > 0) {
    display.showNumberDecEx(value, 0x40,false);
    } else
    display.showNumberDecEx(value, 0x80,false);
    colon = !colon;  
}

void loop()

{
ploop();  //get the upfated time and flash the colon
knx.loop();  //process knx events
delay(500);  //0.5 second delay no need to go faster
}