Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sketch
- /*
- * Copyright (c) 2015, Majenko Technologies
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above copyright notice, this
- * list of conditions and the following disclaimer in the documentation and/or
- * other materials provided with the distribution.
- *
- * * Neither the name of Majenko Technologies nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- // ESP-12-E - Wi-Fi & HTTP Server
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- // TeleInfo
- #include <SoftwareSerial.h>
- #include <TeleInfo.h>
- // Settings
- const char *ssid = "...";
- const char *password = "...";
- const int port = 80;
- // Serveur Web
- ESP8266WebServer server(port);
- // Compteurs electriques
- TeleInfo compteur_1(D1);
- //TeleInfo compteur_2(D2);
- TeleInfo compteurs[] = { compteur_1/*, compteur_2*/ };
- void setup() {
- Serial.begin(115200);
- WiFi.begin (ssid, password);
- Serial.println();
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println();
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- // Page not found
- server.onNotFound(handleNotFound);
- // URLs
- server.on("/", handleRoot); // Home page
- server.on("/data.json", handleJson); // Informations de tous les compteurs
- // Démarrage du serveur
- server.begin();
- Serial.print("HTTP server started: ");
- Serial.print("http://");
- Serial.print(WiFi.localIP());
- Serial.print(":");
- Serial.print(port);
- Serial.println("/");
- }
- void loop() {
- // Mise à jour des compteurs
- for(int i = 0; i < sizeof(compteurs) / sizeof(TeleInfo); ++i) {
- compteurs[i].update();
- }
- // Prise en charge des connexions client
- server.handleClient();
- }
- void handleRoot() {
- int sec = millis() / 1000;
- int min = sec / 60;
- int hr = min / 60;
- char uptime[9];
- snprintf(uptime, 9, "%02d:%02d:%02d", hr, min % 60, sec % 60);
- String body = "<!DOCTYPE html>";
- body += "<html>";
- body += " <head>";
- body += " <meta http-equiv=\"refresh\" content=\"5\" />";
- body += " <title>Tele-Info</title>";
- body += " </head>";
- body += " <body>";
- body += " <h1>Tele-Info</h1>";
- body += " <p>Uptime: " + String(uptime) + "</p>";
- body += " <table>";
- body += " <tr>";
- body += " <th>ACDO</th>";
- body += " <th>Option</th>";
- body += " <th>Idx BASE</th>";
- body += " <th>Idx HCHP</th>";
- body += " <th>Idx HCHC</th>";
- body += " <th>Last check</th>";
- body += " </tr>";
- for(int i = 0; i < sizeof(compteurs) / sizeof(TeleInfo); ++i) {
- body += " <tr>";
- body += " <td>" + String(compteurs[i].getId()) + "</td>";
- body += " <td>" + String(compteurs[i].getOption()) + "</td>";
- body += " <td>" + String(compteurs[i].getIndexBase()) + "</td>";
- body += " <td>" + String(compteurs[i].getIndexHcHp()) + "</td>";
- body += " <td>" + String(compteurs[i].getIndexHcHc()) + "</td>";
- body += " <td>0</td>";
- body += " </tr>";
- }
- body += " </table>";
- body += " </body>";
- body += "</html>";
- server.send(200, "text/html", body);
- }
- void handleNotFound() {
- String message = "File Not Found\n\n";
- message += "URI: ";
- message += server.uri();
- message += "\nMethod: ";
- message += (server.method() == HTTP_GET) ? "GET" : "POST";
- message += "\nArguments: ";
- message += server.args();
- message += "\n";
- for(uint8_t i = 0; i < server.args(); i++) {
- message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
- }
- server.send(404, "text/plain", message);
- }
- void handleJson() {
- const int n = sizeof(compteurs) / sizeof(TeleInfo);
- String json = "[\n";
- for(int i = 0; i < n; ++i) {
- json += " {\n";
- json += " \"adco\": \"" + String(compteurs[i].getId()) + "\",\n";
- json += " \"option\": " + String(compteurs[i].getOption()) + ",\n";
- json += " \"indexes\": {\n";
- // Option tarifaire de base
- json += " \"base\": " + String(compteurs[i].getIndexBase()) + ",\n";
- // Option heures creuses
- json += " \"hc_hp\": " + String(compteurs[i].getIndexHcHp()) + ",\n";
- json += " \"hc_hc\": " + String(compteurs[i].getIndexHcHc()) + "\n";
- json += " }\n";
- json += " }";
- if(i + 1 < n) {
- json += ",";
- }
- json += "\n";
- }
- json += "]\n";
- server.send(200, "application/json", json);
- }
- // ARDUINO_WORKSPACE/libraries/TeleInfo/TeleInfo.h
- #ifndef _TELEINFO_H_
- #define _TELEINFO_H_
- #include <SoftwareSerial.h>
- class TeleInfo {
- public:
- // Options disponibles
- enum Option {
- // Option tarifaire inconnue
- Unknown = 0,
- // Option tarifaire de base (OPTARIF = BASE)
- Base = 1,
- // Option tarifaire heures creuses (OPTARIF = HC..)
- HeuresCreuses = 2
- };
- // Constructeur
- TeleInfo(short rx_pin);
- // Retourne true si des données ont été récupérées une fois
- bool isReady();
- // Met à jour les données depuis le port série
- bool update();
- // Retourne l'identifiant du compteur (ADCO)
- const char* getId();
- // Retourne l'option tarifaire
- Option getOption();
- // Retourne l'index si option = base (BASE)
- long getIndexBase();
- // Retourne l'index heures pleines si option = hc (HCHP)
- long getIndexHcHp();
- // Retourne l'index heures creuses si option = hc (HCHC)
- long getIndexHcHc();
- private:
- // PIN pour RX
- short rx_pin;
- // Identifiant du compteur
- char adco[13];
- // Dernière vérification
- long last_check;
- // Option tarifaire
- Option option;
- // Index (si option = Base)
- int idx_base;
- // Index Heures Pleines (si option = HeuresCreuses)
- int idx_hchp;
- // Index Heures Creuses (si option = HeuresCreuses)
- int idx_hchc;
- // Port série
- SoftwareSerial *serial;
- };
- #endif /* _TELEINFO_H_ */
- // .../libraries/TeleInfo/TeleInfo.cpp
- #include "TeleInfo.h"
- #if defined(ARDUINO) && ARDUINO >= 100
- #include "Arduino.h"
- #else
- #include "WProgram.h"
- #endif
- #define READ(s) ((s)->read())
- #define START_FRAME 0x02
- #define END_FRAME 0x03
- TeleInfo::TeleInfo(short rx_pin) {
- this->rx_pin = rx_pin;
- this->adco[0] = 0;
- this->last_check = 0;
- this->option = Unknown;
- this->idx_base = 0;
- this->idx_hchp = 0;
- this->idx_hchc = 0;
- this->serial = NULL;
- }
- bool TeleInfo::isReady() {
- return this->adco[0] > 0;
- }
- bool TeleInfo::update() {
- char charIn = 0;
- if(!this->last_check || millis() - this->last_check > 60000) {
- this->last_check = millis();
- if(!this->serial) {
- this->serial = new SoftwareSerial(this->rx_pin, SW_SERIAL_UNUSED_PIN);
- this->serial->begin(1200);
- delay(100);
- } else if(this->serial->available() > 0) {
- Serial.print("[Donnees sur PIN ");
- Serial.print(this->rx_pin);
- Serial.println("]");
- for(charIn = 0; charIn != START_FRAME; charIn = READ(this->serial)) {
- yield();
- }
- Serial.println("On commence à lire...");
- for(charIn = READ(this->serial); charIn != END_FRAME; charIn = READ(this->serial)) {
- yield();
- Serial.print(charIn);
- }
- Serial.println("\nFin de trame");
- // TODO: Traiter l'interruption de trame
- }
- }
- return true;
- }
- TeleInfo::Option TeleInfo::getOption() {
- return this->option;
- }
- const char* TeleInfo::getId() {
- return this->adco;
- }
- long TeleInfo::getIndexBase() {
- return this->idx_base;
- }
- long TeleInfo::getIndexHcHp() {
- return this->idx_hchp;
- }
- long TeleInfo::getIndexHcHc() {
- return this->idx_hchc;
- }
Advertisement
Add Comment
Please, Sign In to add comment