Guest User

Untitled

a guest
Jan 30th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.32 KB | None | 0 0
  1. // Sketch
  2. /*
  3.  * Copyright (c) 2015, Majenko Technologies
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without modification,
  7.  * are permitted provided that the following conditions are met:
  8.  *
  9.  * * Redistributions of source code must retain the above copyright notice, this
  10.  *   list of conditions and the following disclaimer.
  11.  *
  12.  * * Redistributions in binary form must reproduce the above copyright notice, this
  13.  *   list of conditions and the following disclaimer in the documentation and/or
  14.  *   other materials provided with the distribution.
  15.  *
  16.  * * Neither the name of Majenko Technologies nor the names of its
  17.  *   contributors may be used to endorse or promote products derived from
  18.  *   this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  24.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  27.  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. // ESP-12-E - Wi-Fi & HTTP Server
  33. #include <ESP8266WiFi.h>
  34. #include <WiFiClient.h>
  35. #include <ESP8266WebServer.h>
  36.  
  37. // TeleInfo
  38. #include <SoftwareSerial.h>
  39. #include <TeleInfo.h>
  40.  
  41. // Settings
  42. const char *ssid = "...";
  43. const char *password = "...";
  44. const int port = 80;
  45.  
  46. // Serveur Web
  47. ESP8266WebServer server(port);
  48.  
  49. // Compteurs electriques
  50. TeleInfo compteur_1(D1);
  51. //TeleInfo compteur_2(D2);
  52. TeleInfo compteurs[] = { compteur_1/*, compteur_2*/ };
  53.  
  54. void setup() {
  55.   Serial.begin(115200);
  56.   WiFi.begin (ssid, password);
  57.   Serial.println();
  58.  
  59.   // Wait for connection
  60.   while (WiFi.status() != WL_CONNECTED) {
  61.     delay(500);
  62.     Serial.print(".");
  63.   }
  64.  
  65.   Serial.println();
  66.  
  67.   Serial.print("Connected to ");
  68.   Serial.println(ssid);
  69.  
  70.   Serial.print("IP address: ");
  71.   Serial.println(WiFi.localIP());
  72.  
  73.   // Page not found
  74.   server.onNotFound(handleNotFound);
  75.  
  76.   // URLs
  77.   server.on("/", handleRoot); // Home page
  78.   server.on("/data.json", handleJson); // Informations de tous les compteurs
  79.  
  80.   // Démarrage du serveur
  81.   server.begin();
  82.  
  83.   Serial.print("HTTP server started: ");
  84.   Serial.print("http://");
  85.   Serial.print(WiFi.localIP());
  86.   Serial.print(":");
  87.   Serial.print(port);
  88.   Serial.println("/");
  89. }
  90.  
  91. void loop() {
  92.   // Mise à jour des compteurs
  93.   for(int i = 0; i < sizeof(compteurs) / sizeof(TeleInfo); ++i) {
  94.     compteurs[i].update();
  95.   }
  96.  
  97.   // Prise en charge des connexions client
  98.   server.handleClient();
  99. }
  100.  
  101. void handleRoot() {
  102.     int sec = millis() / 1000;
  103.     int min = sec / 60;
  104.     int hr = min / 60;
  105.  
  106.   char uptime[9];
  107.   snprintf(uptime, 9, "%02d:%02d:%02d", hr, min % 60, sec % 60);
  108.  
  109.   String body = "<!DOCTYPE html>";
  110.   body += "<html>";
  111.   body += " <head>";
  112.   body += "  <meta http-equiv=\"refresh\" content=\"5\" />";
  113.   body += "  <title>Tele-Info</title>";
  114.   body += " </head>";
  115.   body += " <body>";
  116.   body += "  <h1>Tele-Info</h1>";
  117.   body += "  <p>Uptime: " + String(uptime) + "</p>";
  118.   body += "  <table>";
  119.   body += "   <tr>";
  120.   body += "    <th>ACDO</th>";
  121.   body += "    <th>Option</th>";
  122.   body += "    <th>Idx BASE</th>";
  123.   body += "    <th>Idx HCHP</th>";
  124.   body += "    <th>Idx HCHC</th>";
  125.   body += "    <th>Last check</th>";
  126.   body += "   </tr>";
  127.  
  128.   for(int i = 0; i < sizeof(compteurs) / sizeof(TeleInfo); ++i) {
  129.     body += "   <tr>";
  130.     body += "    <td>" + String(compteurs[i].getId()) + "</td>";
  131.     body += "    <td>" + String(compteurs[i].getOption()) + "</td>";
  132.     body += "    <td>" + String(compteurs[i].getIndexBase()) + "</td>";
  133.     body += "    <td>" + String(compteurs[i].getIndexHcHp()) + "</td>";
  134.     body += "    <td>" + String(compteurs[i].getIndexHcHc()) + "</td>";
  135.     body += "    <td>0</td>";
  136.     body += "   </tr>";
  137.   }
  138.  
  139.   body += "  </table>";
  140.   body += " </body>";
  141.   body += "</html>";
  142.  
  143.     server.send(200, "text/html", body);
  144. }
  145.  
  146. void handleNotFound() {
  147.     String message = "File Not Found\n\n";
  148.     message += "URI: ";
  149.     message += server.uri();
  150.     message += "\nMethod: ";
  151.     message += (server.method() == HTTP_GET) ? "GET" : "POST";
  152.     message += "\nArguments: ";
  153.     message += server.args();
  154.     message += "\n";
  155.  
  156.     for(uint8_t i = 0; i < server.args(); i++) {
  157.         message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  158.     }
  159.  
  160.     server.send(404, "text/plain", message);
  161. }
  162.  
  163. void handleJson() {
  164.   const int n = sizeof(compteurs) / sizeof(TeleInfo);
  165.   String json = "[\n";
  166.  
  167.   for(int i = 0; i < n; ++i) {
  168.     json += "  {\n";
  169.     json += "    \"adco\": \"" + String(compteurs[i].getId()) + "\",\n";
  170.     json += "    \"option\": " + String(compteurs[i].getOption()) + ",\n";
  171.     json += "    \"indexes\": {\n";
  172.    
  173.     // Option tarifaire de base
  174.     json += "      \"base\": " + String(compteurs[i].getIndexBase()) + ",\n";
  175.  
  176.     // Option heures creuses
  177.     json += "      \"hc_hp\": " + String(compteurs[i].getIndexHcHp()) + ",\n";
  178.     json += "      \"hc_hc\": " + String(compteurs[i].getIndexHcHc()) + "\n";
  179.    
  180.     json += "    }\n";
  181.     json += "  }";
  182.  
  183.     if(i + 1 < n) {
  184.       json += ",";
  185.     }
  186.  
  187.     json += "\n";
  188.   }
  189.  
  190.   json += "]\n";
  191.  
  192.   server.send(200, "application/json", json);
  193. }
  194.  
  195. // ARDUINO_WORKSPACE/libraries/TeleInfo/TeleInfo.h
  196.  
  197. #ifndef _TELEINFO_H_
  198. #define _TELEINFO_H_
  199.  
  200. #include <SoftwareSerial.h>
  201.  
  202. class TeleInfo {
  203.     public:
  204.         // Options disponibles
  205.         enum Option {
  206.             // Option tarifaire inconnue
  207.             Unknown = 0,
  208.  
  209.             // Option tarifaire de base (OPTARIF = BASE)
  210.             Base = 1,
  211.  
  212.             // Option tarifaire heures creuses (OPTARIF = HC..)
  213.             HeuresCreuses = 2
  214.         };
  215.  
  216.         // Constructeur
  217.         TeleInfo(short rx_pin);
  218.  
  219.         // Retourne true si des données ont été récupérées une fois
  220.         bool isReady();
  221.  
  222.         // Met à jour les données depuis le port série
  223.         bool update();
  224.  
  225.         // Retourne l'identifiant du compteur (ADCO)
  226.         const char* getId();
  227.  
  228.         // Retourne l'option tarifaire
  229.         Option getOption();
  230.  
  231.         // Retourne l'index si option = base (BASE)
  232.         long getIndexBase();
  233.  
  234.         // Retourne l'index heures pleines si option = hc (HCHP)
  235.         long getIndexHcHp();
  236.  
  237.         // Retourne l'index heures creuses si option = hc (HCHC)
  238.         long getIndexHcHc();
  239.  
  240.     private:
  241.         // PIN pour RX
  242.         short rx_pin;
  243.  
  244.         // Identifiant du compteur
  245.         char adco[13];
  246.  
  247.         // Dernière vérification
  248.         long last_check;
  249.  
  250.         // Option tarifaire
  251.         Option option;
  252.  
  253.         // Index (si option = Base)
  254.         int idx_base;
  255.  
  256.         // Index Heures Pleines (si option = HeuresCreuses)
  257.         int idx_hchp;
  258.  
  259.         // Index Heures Creuses (si option = HeuresCreuses)
  260.         int idx_hchc;
  261.  
  262.         // Port série
  263.         SoftwareSerial *serial;
  264. };
  265.  
  266. #endif /* _TELEINFO_H_ */
  267.  
  268. // .../libraries/TeleInfo/TeleInfo.cpp
  269.  
  270. #include "TeleInfo.h"
  271.  
  272. #if defined(ARDUINO) && ARDUINO >= 100
  273.   #include "Arduino.h"
  274. #else
  275.   #include "WProgram.h"
  276. #endif
  277.  
  278. #define READ(s) ((s)->read())
  279. #define START_FRAME 0x02
  280. #define END_FRAME 0x03
  281.  
  282. TeleInfo::TeleInfo(short rx_pin) {
  283.     this->rx_pin = rx_pin;
  284.  
  285.     this->adco[0] = 0;
  286.     this->last_check = 0;
  287.     this->option = Unknown;
  288.     this->idx_base = 0;
  289.     this->idx_hchp = 0;
  290.     this->idx_hchc = 0;
  291.    
  292.     this->serial = NULL;
  293. }
  294.  
  295. bool TeleInfo::isReady() {
  296.     return this->adco[0] > 0;
  297. }
  298.  
  299. bool TeleInfo::update() {
  300.     char charIn = 0;
  301.    
  302.     if(!this->last_check || millis() - this->last_check > 60000) {
  303.         this->last_check = millis();
  304.        
  305.         if(!this->serial) {
  306.             this->serial = new SoftwareSerial(this->rx_pin, SW_SERIAL_UNUSED_PIN);
  307.             this->serial->begin(1200);
  308.             delay(100);
  309.         } else if(this->serial->available() > 0) {
  310.             Serial.print("[Donnees sur PIN ");
  311.             Serial.print(this->rx_pin);
  312.             Serial.println("]");
  313.            
  314.             for(charIn = 0; charIn != START_FRAME; charIn = READ(this->serial)) {
  315.                 yield();
  316.             }
  317.            
  318.             Serial.println("On commence à lire...");
  319.            
  320.             for(charIn = READ(this->serial); charIn != END_FRAME; charIn = READ(this->serial)) {
  321.                 yield();
  322.                 Serial.print(charIn);
  323.             }
  324.  
  325.             Serial.println("\nFin de trame");
  326.  
  327.             // TODO: Traiter l'interruption de trame
  328.         }
  329.     }
  330.    
  331.     return true;
  332. }
  333.  
  334. TeleInfo::Option TeleInfo::getOption() {
  335.     return this->option;
  336. }
  337.  
  338. const char* TeleInfo::getId() {
  339.     return this->adco;
  340. }
  341.  
  342. long TeleInfo::getIndexBase() {
  343.     return this->idx_base;
  344. }
  345.  
  346. long TeleInfo::getIndexHcHp() {
  347.     return this->idx_hchp;
  348. }
  349.  
  350. long TeleInfo::getIndexHcHc() {
  351.     return this->idx_hchc;
  352. }
Advertisement
Add Comment
Please, Sign In to add comment