Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Relevé Téléinfo -Gore-
- * Remise des infos téléinfo sur requête Ethernet (http)
- * Stockage temporaire en mémoire ou carte SD
- *
- * version: 0.1 - Lecture 1 compteur, affichage sur console série
- */
- /********************* Infos *********************/
- // TH.. => Toutes les Heures
- // HC.. => Heures Creuses
- // HP.. => Heures Pleines
- // HN.. => Heures Normales
- // PM.. => Heures de Pointe Mobile
- // HCJB => Heures Creuses Jours Bleus
- // HCJW => Heures Creuses Jours Blancs
- // HCJR => Heures Creuses Jours Rouges
- // HPJB => Heures Pleines Jours Bleus
- // HPJW => Heures Pleines Jours Blancs
- // HPJR => Heures Pleines Jours Rouges
- //#include "print.h"
- #include "string.h"
- #include "stdio.h"
- //#include <SD.h>
- #include <SPI.h>
- #include <Wire.h>
- #include "RTClib.h"
- //#include <Ethernet.h>
- //#include <WebServer.h>
- #include <MsTimer2.h>
- #include <EEPROM.h>
- //#include <Streaming.h>
- /********************* Ethernet *********************/
- /* v0.1
- //EthernetClient client;
- byte mac[] = { 0x3C, 0x3C, 0x54, 0x44, 0x3E, 0x3E };
- P(htmlHead) = "<html>" ;
- P(teleinfo_head) = "<TELEINFO>";
- P(teleinfo_foot) = "</TELEINFO>";
- P(xml) = "<?xml version='1.0' encoding='ISO-8859-1'?>";
- WebServer webserver("", 80);*/
- /********************* Abonnement *********************/
- //#define abo_BASE // abonnement de Base
- #define abo_HCHP // abonnement Heures Creuses
- //#define abo_EJP // abonnement EJP
- //#define abo_BBR // abonnement tempo (Bleu Blanc Rouge)
- #define MONOPHASE
- //#define TRIPHASE
- /********************* Choix compteur *********************/
- #define CPT1 5 // Broche sélection du compteur 1
- #define CPT2 6 // Broche sélection du compteur 2
- byte inByte = 0 ; // caractère entrant téléinfo
- char buffteleinfo[21] = "";
- byte bufflen = 0;
- // Délais lecture
- unsigned int delay_spool = 0;
- unsigned int val_delay = 0;
- // Déclarations Teleinfo
- char adco[13]= ""; // numero ADCO compteur consommation
- char optarif[5]= ""; // Option tarifaire choisie: BASE => Option Base, HC.. => Option Heures Creuses, EJP. => Option EJP, BBRx => Option Tempo [x selon contacts auxiliaires]
- byte isousc = 0; // Intensité souscrite, A
- unsigned int papp = 0; // Puissance apparente, VA
- char motdetat[10] = ""; // Mot d'état du compteur
- unsigned long base = 0; // Index option Base, Wh
- char ptec[4] = ""; // Période Tarifaire en cours, 4 alphanumériques
- // Monophasé
- unsigned int iinst = 0; // Monophasé - Intensité Instantanée, A (intensité efficace instantanée)
- unsigned int imax = 0; // Monophasé - Intensité maximale appelée, A
- // Abonnement HPHC
- unsigned long hchc = 0;
- unsigned long hchp = 0;
- const char debtrame = 0x02;
- const char deb = 0x0A;
- const char fin = 0x0D;
- int compteur_actif = 1; // Numéro du compteur en cours de lecture
- byte donnee_ok_cpt1 = 0; // Pour vérifier que les données sont bien en memoire avant écriture dans fichier
- byte donnee_ok_cpt2 = 0;
- byte donnee_ok_cpt1_ph = 0;
- // Delay spool
- void time_spool() {
- val_delay++;
- }
- /********************* Page web Teleinfo *********************/
- /* v0.1
- void pageWebTeleInfo(WebServer &server, WebServer::ConnectionType type, char *, bool)
- {
- // This line sends the standard "we're all OK" headers back to the browser
- server.httpSuccess();
- // If we're handling a GET or POST, we can output our data here.
- // For a HEAD request, we just stop after outputting headers.
- if (type == WebServer::GET)
- {
- server.printP(xml);
- server.printP(teleinfo_head);
- server << "<ADCO>" << adco << "</ADCO>";
- server << "<OPTARIF>" << optarif << "</OPTARIF>";
- server << "<ISOUSC>" << isousc << "</ISOUSC>";
- server << "<BASE>" << base << "</BASE>";
- server << "<HCHC>" << hchc << "</HCHC>";
- server << "<HCHP>" << hchp << "</HCHP>";
- server << "<PTEC>" << ptec << "</PTEC>";
- server << "<IINST>" << iinst << "</IINST>";
- server << "<PAPP>" << papp << "</PAPP>";
- server << "<IMAX>" << imax << "</IMAX>";
- server.printP(teleinfo_foot);
- }
- }*/
- /********************* Page web Admin *********************/
- /* v0.1
- void pageWebIndex(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool)
- {
- URLPARAM_RESULT rc;
- int i;
- server.httpSuccess();
- // If we're handling a GET or POST, we can output our data here.
- // For a HEAD request, we just stop after outputting headers.
- if (type == WebServer::GET) {
- server.printP(htmlHead);
- }
- }
- */
- /********************* SETUP *********************/
- void setup()
- {
- // Initialisation du port 0-1 lecture Téléinfo
- initSerieTeleinfo() ;
- // Sélection du compteur 1
- pinMode(CPT1, OUTPUT);
- pinMode(CPT2, OUTPUT);
- digitalWrite(CPT1, HIGH);
- digitalWrite(CPT2, LOW);
- // Initialisation carte TcpIp ------------
- /* v0.1
- while (Ethernet.begin(mac) == 0) {
- delay(200);
- }
- // Initialisation serveur Web -------------
- webserver.setDefaultCommand(&pageWebIndex);
- webserver.addCommand("index.html", &pageWebIndex);
- webserver.addCommand("teleinfo", &pageWebTeleInfo);
- webserver.begin();
- */
- // Initalise tempo spool à 1s ------------
- MsTimer2::set(1000, time_spool); // 1s par period
- if (delay_spool != 0) MsTimer2::start();
- }
- /********************* LOOP *********************/
- void loop()
- {
- // webserver.processConnection();
- //#ifdef compteur2_actif
- // #ifdef abo_BASE
- // if (donnee_ok_cpt1 == B10000001) {
- // #endif
- // #ifdef abo_HCHP or abo_EJP
- // if (donnee_ok_cpt1 == B10000011) {
- // #endif
- // #ifdef abo_EJP
- // if (donnee_ok_cpt1 == B10000011) {
- // #endif
- // #ifdef abo_BBR
- // if (donnee_ok_cpt1 == B10001111) {
- // #endif
- // #ifdef MONOPHASE
- // if (donnee_ok_cpt1_ph == B00000001) bascule_compteur();
- // #endif
- // #ifdef TRIPHASE
- // if (donnee_ok_cpt1_ph == B00000111) bascule_compteur();
- // #endif
- // }
- // if (donnee_ok_cpt2 == B00000111) bascule_compteur();
- //#endif
- // Lecture teleinfo ------------------------
- read_teleinfo();
- // Appel page maj sur serveur web ----------
- if (val_delay > delay_spool) {
- val_delay = 0;
- }
- /* v0.1
- // TEST DE DE LA GENERATION DES DONNEES DANS LE LOOP
- pageWebTeleInfo("", WebServer::GET, "A", true);
- webserver.addCommand("teleinfo", &pageWebTeleInfo);
- // FIN DU TEST
- Ethernet.maintain();
- */
- }
- /********************* Calcul Checksum *********************/
- char cksum(char *buff, int len)
- {
- int i;
- char sum = 0;
- for (i=1; i<(len-2); i++) sum = sum + buff[i];
- sum = (sum & 0x3F) + 0x20;
- return(sum);
- }
- /********************* Analyse de la ligne de Teleinfo *********************/
- void traitbuf_cpt(char *buff, int len)
- {
- if (compteur_actif == 1){
- #ifdef abo_BASE
- if (strncmp("BASE ", &buff[1] , 5)==0){
- base = atol(&buff[6]);
- donnee_ok_cpt1 = donnee_ok_cpt1 | B00000001;
- }
- #endif
- #ifdef abo_HCHP
- if (strncmp("HCHP ", &buff[1] , 5)==0){
- hchp = atol(&buff[6]);
- donnee_ok_cpt1 = donnee_ok_cpt1 | B00000001;
- }
- else if (strncmp("HCHC ", &buff[1] , 5)==0){
- hchc = atol(&buff[6]);
- donnee_ok_cpt1 = donnee_ok_cpt1 | B00000010;
- }
- #endif
- #ifdef abo_EJP
- if (strncmp("EJPHN ", &buff[1] , 6)==0){
- EJPHN = atol(&buff[7]);
- donnee_ok_cpt1 = donnee_ok_cpt1 | B00000001;
- }
- else if (strncmp("EJPHPM ", &buff[1] , 7)==0){
- EJPHPM = atol(&buff[8]);
- donnee_ok_cpt1 = donnee_ok_cpt1 | B00000010;
- }
- #endif
- #ifdef abo_BBR
- if (strncmp("BBRHCJB ", &buff[1] , 8)==0){
- BBRHCJB = atol(&buff[9]);
- donnee_ok_cpt1 = donnee_ok_cpt1 | B00000001;
- }
- else if (strncmp("BBRHPJB ", &buff[1] , 8)==0){
- BBRHPJB = atol(&buff[9]);
- donnee_ok_cpt1 = donnee_ok_cpt1 | B00000010;
- }
- else if (strncmp("BBRHCJW ", &buff[1] , 8)==0){
- BBRHCJW = atol(&buff[9]);
- donnee_ok_cpt1 = donnee_ok_cpt1 | B00000100;
- }
- else if (strncmp("BBRHPJW ", &buff[1] , 8)==0){
- BBRHPJW = atol(&buff[9]);
- donnee_ok_cpt1 = donnee_ok_cpt1 | B00001000;
- }
- else if (strncmp("BBRHCJR ", &buff[1] , 8)==0){
- BBRHCJR = atol(&buff[9]);
- donnee_ok_cpt1 = donnee_ok_cpt1 | B00010000;
- }
- else if (strncmp("BBRHPJR ", &buff[1] , 8)==0){
- BBRHPJR = atol(&buff[9]);
- donnee_ok_cpt1 = donnee_ok_cpt1 | B00100000;
- }
- #endif
- #ifdef MONOPHASE
- else if (strncmp("IINST ", &buff[1] , 6)==0){
- iinst = atol(&buff[7]);
- donnee_ok_cpt1_ph = donnee_ok_cpt1_ph | B00000001;
- }
- #endif
- else if (strncmp("PAPP ", &buff[1] , 5)==0){
- papp = atol(&buff[6]);
- donnee_ok_cpt1 = donnee_ok_cpt1 | B10000000;
- }
- else if (strncmp("ADCO ", &buff[1] , 5)==0){
- strncpy(adco, &buff[6], 12);
- adco[12]='\0';
- }
- else if (strncmp("IMAX ", &buff[1] , 5)==0){
- imax = atol(&buff[6]);
- }
- else if (strncmp("OPTARIF ", &buff[1] , 8)==0){
- strncpy(optarif, &buff[9], 4);
- optarif[4]='\0';
- }
- else if (strncmp("ISOUSC ", &buff[1] , 7)==0){
- isousc = atol(&buff[8]);
- }
- else if (strncmp("PTEC ", &buff[1] , 5)==0){
- strncpy(ptec, &buff[6], 4);
- ptec[4]='\0';
- }
- else if (strncmp("BASE ", &buff[1] , 5)==0){
- base = atol(&buff[6]);
- }
- else if (strncmp("MOTDETAT ", &buff[1] , 9)==0){
- strncpy(motdetat, &buff[10], 6);
- motdetat[6]='\0';
- }
- }
- }
- /********************* Lecture trame teleinfo (ligne par ligne) *********************/
- void read_teleinfo()
- {
- // si une donnée est dispo sur le port série
- if (Serial.available() > 0)
- {
- // recupère le caractère dispo
- inByte = Serial.read();
- if (inByte == debtrame) bufflen = 0; // test le début de trame
- if (inByte == deb) // test si c'est le caractère de début de ligne
- {
- bufflen = 0;
- }
- buffteleinfo[bufflen] = inByte;
- bufflen++;
- if (bufflen > 21)bufflen=0; // longueur max du buffer (21 pour lire trame ADCO)
- if (inByte == fin && bufflen > 5) // si Fin de ligne trouvée
- {
- if (cksum(buffteleinfo,bufflen-1)== buffteleinfo[bufflen-2]) // Test du Checksum
- {
- traitbuf_cpt(buffteleinfo,bufflen-1); // ChekSum OK => Analyse de la Trame
- }
- }
- }
- }
- /********************* Initialisation communication série pour Téléinfo *********************/
- void initSerieTeleinfo() {
- // initialisation du port série sur broches 0-1 : 1200,7,E,1 pour connexion téléinfo
- Serial.begin(1200);
- //~ UCSR0C = B00100100; // configuration USART hardware
- // parité paire E
- // 7 bits data
- // NB : à 1200 bauds, un caractère sur 10 bits mets théoriquement 1/120 = 8.33ms pour arriver...
- } // fin initSerieTeleinfo
- /********************* Initialisation communication série pour Arduino *********************/
- void initSerieArduino() {
- Serial.begin(115200); // réinitialise communication série
- //~ UCSR0C = B00000110; // configuration USART hardware
- // 8 bits data, Sans parité, Sans bit de stop
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement