Advertisement
Guest User

Untitled

a guest
Jan 25th, 2014
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.23 KB | None | 0 0
  1. /*
  2.  * Relevé Téléinfo -Gore-
  3.  * Remise des infos téléinfo sur requête Ethernet (http)
  4.  * Stockage temporaire en mémoire ou carte SD
  5.  *
  6.  * version: 0.1 - Lecture 1 compteur, affichage sur console série
  7. */
  8.  
  9. /********************* Infos *********************/
  10. // TH.. => Toutes les Heures
  11. // HC.. => Heures Creuses
  12. // HP.. => Heures Pleines
  13. // HN.. => Heures Normales
  14. // PM.. => Heures de Pointe Mobile
  15. // HCJB => Heures Creuses Jours Bleus
  16. // HCJW => Heures Creuses Jours Blancs
  17. // HCJR => Heures Creuses Jours Rouges
  18. // HPJB => Heures Pleines Jours Bleus
  19. // HPJW => Heures Pleines Jours Blancs
  20. // HPJR => Heures Pleines Jours Rouges
  21.  
  22. //#include "print.h"
  23. #include "string.h"
  24. #include "stdio.h"
  25. //#include <SD.h>
  26. #include <SPI.h>
  27. #include <Wire.h>
  28. #include "RTClib.h"
  29. //#include <Ethernet.h>
  30. //#include <WebServer.h>
  31. #include <MsTimer2.h>
  32. #include <EEPROM.h>
  33. //#include <Streaming.h>
  34.  
  35. /********************* Ethernet *********************/
  36. /* v0.1
  37. //EthernetClient client;
  38. byte mac[] = { 0x3C, 0x3C, 0x54, 0x44, 0x3E, 0x3E };
  39. P(htmlHead) = "<html>" ;
  40. P(teleinfo_head) = "<TELEINFO>";
  41. P(teleinfo_foot) = "</TELEINFO>";
  42. P(xml) = "<?xml version='1.0' encoding='ISO-8859-1'?>";
  43.  
  44.  
  45. WebServer webserver("", 80);*/
  46.  
  47. /********************* Abonnement *********************/
  48. //#define abo_BASE  // abonnement de Base
  49. #define abo_HCHP  // abonnement Heures Creuses
  50. //#define abo_EJP   // abonnement EJP
  51. //#define abo_BBR   // abonnement tempo (Bleu Blanc Rouge)
  52.  
  53. #define MONOPHASE
  54. //#define TRIPHASE
  55.  
  56. /********************* Choix compteur *********************/
  57. #define CPT1 5  // Broche sélection du compteur 1
  58. #define CPT2 6  // Broche sélection du compteur 2
  59.  
  60. byte inByte = 0 ;        // caractère entrant téléinfo
  61. char buffteleinfo[21] = "";
  62. byte bufflen = 0;
  63.  
  64. // Délais lecture
  65. unsigned int delay_spool = 0;
  66. unsigned int val_delay = 0;
  67.  
  68.  
  69. // Déclarations Teleinfo
  70. char adco[13]= "";      // numero ADCO compteur consommation
  71. char optarif[5]= "";    // Option tarifaire choisie: BASE => Option Base, HC.. => Option Heures Creuses, EJP. => Option EJP, BBRx => Option Tempo [x selon contacts auxiliaires]
  72. byte isousc = 0;        // Intensité souscrite, A
  73. unsigned int papp = 0;  // Puissance apparente, VA
  74. char motdetat[10] = "";  // Mot d'état du compteur
  75. unsigned long base = 0;      // Index option Base, Wh
  76. char ptec[4] = "";      // Période Tarifaire en cours, 4 alphanumériques
  77.  
  78. // Monophasé
  79. unsigned int iinst = 0;      // Monophasé - Intensité Instantanée, A  (intensité efficace instantanée)
  80. unsigned int imax = 0;  // Monophasé - Intensité maximale appelée, A
  81.  
  82. // Abonnement HPHC
  83. unsigned long hchc = 0;
  84. unsigned long hchp = 0;
  85.  
  86. const char debtrame = 0x02;
  87. const char deb = 0x0A;
  88. const char fin = 0x0D;
  89.  
  90.  
  91. int compteur_actif = 1;  // Numéro du compteur en cours de lecture
  92. byte donnee_ok_cpt1 = 0;  // Pour vérifier que les données sont bien en memoire avant écriture dans fichier
  93. byte donnee_ok_cpt2 = 0;
  94. byte donnee_ok_cpt1_ph = 0;
  95.  
  96.    
  97. // Delay spool
  98. void time_spool() {
  99.   val_delay++;
  100. }
  101. /********************* Page web Teleinfo *********************/
  102. /* v0.1
  103. void pageWebTeleInfo(WebServer &server, WebServer::ConnectionType type, char *, bool)
  104. {
  105.   // This line sends the standard "we're all OK" headers back to the browser
  106.   server.httpSuccess();
  107.  
  108.   // If we're handling a GET or POST, we can output our data here.
  109.   //  For a HEAD request, we just stop after outputting headers.
  110.   if (type == WebServer::GET)
  111.   {      
  112.       server.printP(xml);
  113.       server.printP(teleinfo_head);
  114.       server << "<ADCO>" << adco << "</ADCO>";
  115.       server << "<OPTARIF>" << optarif << "</OPTARIF>";
  116.       server << "<ISOUSC>" << isousc << "</ISOUSC>";
  117.       server << "<BASE>" << base << "</BASE>";
  118.       server << "<HCHC>" << hchc << "</HCHC>";
  119.       server << "<HCHP>" << hchp << "</HCHP>";      
  120.       server << "<PTEC>" << ptec << "</PTEC>";
  121.       server << "<IINST>" << iinst << "</IINST>";
  122.       server << "<PAPP>" << papp << "</PAPP>";
  123.       server << "<IMAX>" << imax << "</IMAX>";
  124.       server.printP(teleinfo_foot);
  125.   }
  126. }*/
  127.  
  128. /********************* Page web Admin *********************/
  129. /* v0.1
  130. void pageWebIndex(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool)
  131. {
  132.   URLPARAM_RESULT rc;
  133.   int i;
  134.  
  135.   server.httpSuccess();
  136.  
  137.   // If we're handling a GET or POST, we can output our data here.
  138.   // For a HEAD request, we just stop after outputting headers.
  139.  
  140.   if (type == WebServer::GET) {
  141.    server.printP(htmlHead);      
  142.        
  143.   }
  144. }
  145.  */
  146.  
  147. /********************* SETUP *********************/
  148. void setup()
  149. {
  150. // Initialisation du port 0-1 lecture Téléinfo
  151.     initSerieTeleinfo() ;
  152.  
  153. // Sélection du compteur 1
  154.     pinMode(CPT1, OUTPUT);
  155.     pinMode(CPT2, OUTPUT);
  156.     digitalWrite(CPT1, HIGH);
  157.     digitalWrite(CPT2, LOW);
  158.  
  159. // Initialisation carte TcpIp ------------
  160. /* v0.1
  161.     while (Ethernet.begin(mac) == 0) {
  162.     delay(200);
  163.     }
  164.  
  165. // Initialisation serveur Web -------------
  166.  
  167.     webserver.setDefaultCommand(&pageWebIndex);
  168.     webserver.addCommand("index.html", &pageWebIndex);
  169.     webserver.addCommand("teleinfo", &pageWebTeleInfo);
  170.     webserver.begin();
  171.    
  172. */
  173.    // Initalise tempo spool à 1s ------------
  174.   MsTimer2::set(1000, time_spool); // 1s par period
  175.   if (delay_spool != 0) MsTimer2::start();
  176. }
  177.  
  178.  
  179. /********************* LOOP *********************/
  180.  
  181. void loop()
  182. {
  183.  // webserver.processConnection();
  184. //#ifdef compteur2_actif
  185. //  #ifdef abo_BASE
  186. //    if (donnee_ok_cpt1 == B10000001) {
  187. //  #endif
  188. //  #ifdef abo_HCHP or abo_EJP
  189. //    if (donnee_ok_cpt1 == B10000011) {
  190. //  #endif
  191. //  #ifdef abo_EJP
  192. //    if (donnee_ok_cpt1 == B10000011) {
  193. //  #endif
  194. //  #ifdef abo_BBR
  195. //    if (donnee_ok_cpt1 == B10001111) {
  196. //  #endif
  197. //  #ifdef MONOPHASE
  198. //      if (donnee_ok_cpt1_ph == B00000001) bascule_compteur();
  199. //  #endif
  200. //  #ifdef TRIPHASE
  201. //      if (donnee_ok_cpt1_ph == B00000111) bascule_compteur();
  202. //  #endif
  203. //    }
  204. //  if (donnee_ok_cpt2 == B00000111) bascule_compteur();
  205. //#endif
  206.  
  207. // Lecture teleinfo ------------------------
  208.   read_teleinfo();
  209.  
  210. // Appel page maj sur serveur web ----------
  211. if (val_delay > delay_spool) {
  212.       val_delay = 0;
  213.   }
  214.  
  215.   /* v0.1
  216. // TEST DE DE LA GENERATION DES DONNEES DANS LE LOOP
  217.     pageWebTeleInfo("", WebServer::GET, "A", true);
  218.     webserver.addCommand("teleinfo", &pageWebTeleInfo);
  219. // FIN DU TEST
  220.  
  221.  
  222.   Ethernet.maintain();
  223.   */
  224. }
  225.  
  226. /********************* Calcul Checksum *********************/
  227. char cksum(char *buff, int len)
  228. {
  229.   int i;
  230.   char sum = 0;
  231.     for (i=1; i<(len-2); i++) sum = sum + buff[i];
  232.     sum = (sum & 0x3F) + 0x20;
  233.     return(sum);
  234. }
  235.  
  236. /********************* Analyse de la ligne de Teleinfo *********************/
  237. void traitbuf_cpt(char *buff, int len)
  238. {
  239.  
  240. if (compteur_actif == 1){
  241.  
  242. #ifdef abo_BASE
  243.   if (strncmp("BASE ", &buff[1] , 5)==0){
  244.       base = atol(&buff[6]);
  245.       donnee_ok_cpt1 = donnee_ok_cpt1 | B00000001;      
  246.   }
  247. #endif
  248.  
  249. #ifdef abo_HCHP
  250.   if (strncmp("HCHP ", &buff[1] , 5)==0){
  251.       hchp = atol(&buff[6]);
  252.       donnee_ok_cpt1 = donnee_ok_cpt1 | B00000001;
  253.   }
  254.  
  255.   else if (strncmp("HCHC ", &buff[1] , 5)==0){
  256.       hchc = atol(&buff[6]);
  257.       donnee_ok_cpt1 = donnee_ok_cpt1 | B00000010;      
  258.   }
  259. #endif
  260.  
  261. #ifdef abo_EJP
  262.   if (strncmp("EJPHN ", &buff[1] , 6)==0){
  263.       EJPHN = atol(&buff[7]);
  264.       donnee_ok_cpt1 = donnee_ok_cpt1 | B00000001;      
  265.   }
  266.  
  267.   else if (strncmp("EJPHPM ", &buff[1] , 7)==0){
  268.       EJPHPM = atol(&buff[8]);
  269.       donnee_ok_cpt1 = donnee_ok_cpt1 | B00000010;      
  270.   }
  271. #endif
  272.  
  273. #ifdef abo_BBR
  274.   if (strncmp("BBRHCJB ", &buff[1] , 8)==0){
  275.       BBRHCJB = atol(&buff[9]);
  276.       donnee_ok_cpt1 = donnee_ok_cpt1 | B00000001;      
  277.   }
  278.   else if (strncmp("BBRHPJB ", &buff[1] , 8)==0){
  279.       BBRHPJB = atol(&buff[9]);
  280.       donnee_ok_cpt1 = donnee_ok_cpt1 | B00000010;      
  281.   }
  282.   else if (strncmp("BBRHCJW ", &buff[1] , 8)==0){
  283.       BBRHCJW = atol(&buff[9]);
  284.       donnee_ok_cpt1 = donnee_ok_cpt1 | B00000100;      
  285.   }
  286.  
  287.   else if (strncmp("BBRHPJW ", &buff[1] , 8)==0){
  288.       BBRHPJW = atol(&buff[9]);
  289.       donnee_ok_cpt1 = donnee_ok_cpt1 | B00001000;    
  290.   }
  291.   else if (strncmp("BBRHCJR ", &buff[1] , 8)==0){
  292.       BBRHCJR = atol(&buff[9]);
  293.       donnee_ok_cpt1 = donnee_ok_cpt1 | B00010000;      
  294.   }
  295.   else if (strncmp("BBRHPJR ", &buff[1] , 8)==0){
  296.       BBRHPJR = atol(&buff[9]);
  297.       donnee_ok_cpt1 = donnee_ok_cpt1 | B00100000;        
  298.   }
  299. #endif
  300.  
  301. #ifdef MONOPHASE
  302.   else if (strncmp("IINST ", &buff[1] , 6)==0){
  303.       iinst = atol(&buff[7]);
  304.       donnee_ok_cpt1_ph = donnee_ok_cpt1_ph | B00000001;    
  305.   }
  306. #endif
  307.  
  308.   else if (strncmp("PAPP ", &buff[1] , 5)==0){
  309.       papp = atol(&buff[6]);
  310.       donnee_ok_cpt1 = donnee_ok_cpt1 | B10000000;      
  311.   }
  312.  
  313.  
  314.     else if (strncmp("ADCO ", &buff[1] , 5)==0){
  315.         strncpy(adco, &buff[6], 12);
  316.         adco[12]='\0';
  317.     }
  318.  
  319.     else if (strncmp("IMAX ", &buff[1] , 5)==0){
  320.         imax = atol(&buff[6]);
  321.      
  322.       }
  323.  
  324.     else if (strncmp("OPTARIF ", &buff[1] , 8)==0){
  325.         strncpy(optarif, &buff[9], 4);
  326.         optarif[4]='\0';      
  327.       }
  328.  
  329.     else if (strncmp("ISOUSC ", &buff[1] , 7)==0){
  330.         isousc = atol(&buff[8]);      
  331.       }
  332.  
  333.     else if (strncmp("PTEC ", &buff[1] , 5)==0){
  334.         strncpy(ptec, &buff[6], 4);
  335.         ptec[4]='\0';      
  336.       }
  337.  
  338.     else if (strncmp("BASE ", &buff[1] , 5)==0){
  339.         base = atol(&buff[6]);      
  340.       }
  341.  
  342.     else if (strncmp("MOTDETAT ", &buff[1] , 9)==0){
  343.         strncpy(motdetat, &buff[10], 6);
  344.         motdetat[6]='\0';      
  345.   }  
  346.   }
  347.    
  348. }
  349.  
  350.  
  351. /********************* Lecture trame teleinfo (ligne par ligne) *********************/
  352. void read_teleinfo()
  353. {
  354.   // si une donnée est dispo sur le port série
  355.   if (Serial.available() > 0)
  356.   {
  357.   // recupère le caractère dispo
  358.     inByte = Serial.read();
  359.  
  360.  
  361.     if (inByte == debtrame) bufflen = 0; // test le début de trame
  362.     if (inByte == deb) // test si c'est le caractère de début de ligne
  363.     {
  364.       bufflen = 0;
  365.     }  
  366.     buffteleinfo[bufflen] = inByte;
  367.     bufflen++;
  368.     if (bufflen > 21)bufflen=0;       // longueur max du buffer (21 pour lire trame ADCO)
  369.     if (inByte == fin && bufflen > 5) // si Fin de ligne trouvée
  370.     {
  371.        
  372.       if (cksum(buffteleinfo,bufflen-1)== buffteleinfo[bufflen-2]) // Test du Checksum
  373.       {
  374.         traitbuf_cpt(buffteleinfo,bufflen-1); // ChekSum OK => Analyse de la Trame
  375.       }
  376.     }
  377.   }
  378. }
  379.  
  380. /********************* Initialisation communication série pour Téléinfo *********************/
  381.  
  382. void initSerieTeleinfo() {
  383.  
  384.    // initialisation du port série sur broches 0-1 : 1200,7,E,1 pour connexion téléinfo
  385.         Serial.begin(1200);
  386.  
  387.         //~ UCSR0C = B00100100; // configuration USART hardware
  388.       // parité paire E
  389.       // 7 bits data
  390.  
  391.     // NB : à 1200 bauds, un caractère sur 10 bits mets théoriquement 1/120 = 8.33ms pour arriver...    
  392.  
  393. } // fin initSerieTeleinfo
  394.  
  395. /********************* Initialisation communication série pour Arduino *********************/
  396.  
  397. void initSerieArduino() {
  398.  
  399.       Serial.begin(115200); // réinitialise communication série
  400.       //~ UCSR0C = B00000110; // configuration USART hardware
  401.       // 8 bits data, Sans parité, Sans bit de stop
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement