// ********************************************************************************** // ESP8266 Teleinfo WEB Server, route web function // ********************************************************************************** // Creative Commons Attrib Share-Alike License // You are free to use/extend this library but please abide with the CC-BY-SA license: // Attribution-NonCommercial-ShareAlike 4.0 International License // http://creativecommons.org/licenses/by-nc-sa/4.0/ // // For any explanation about teleinfo ou use, see my blog // http://hallard.me/category/tinfo // // This program works with the Wifinfo board // see schematic here https://github.com/hallard/teleinfo/tree/master/Wifinfo // // Written by Charles-Henri Hallard (http://hallard.me) // // History : V1.00 2015-06-14 - First release // // All text above must be included in any redistribution. // // ********************************************************************************** // Include Arduino header #include "route.h" /* ====================================================================== Function: handleRoot Purpose : handle main page /, display information Input : - Output : - Comments: - ====================================================================== */ void handleRoot(void) { String response=""; // Just to debug where we are Debug(F("Serving / page...")); LedBluON(); // start HTML Code response += F("" "" "" "" "" // Our custom style "" "" "" "Wifinfo" ""); response += F("
" // Onglets ""); // Contenu des onglets response += F("
"); // tab teleinfo response += F( "
" "

Données de Téléinformation

" "
Charge courante : " "
" "
" "Attente des données" "
" "
" "
" "" "" "" "" "" "" "" "" "" "
EtiquetteValeurChecksumFlags
" "
"); // tab pane // tab Systeme response += F( "
" "

Données du système

" "" "" "" "" "" "" "" "
DonnéeValeur
" "
"); // tab pane // tab Configuration response += F( "
" "

Configuration du module WifInfo

" "

Cette partie reste à faire, des volontaires motivés ?

" "
"); // tab pane response += F("
"); // tab content /* response += F("")); */ response += F("
\r\n"); // Container response += F("\r\n"); response += F("\r\n"); response += F("\r\n"); response += F("" "\r\n"); response += F(""); response += F("\r\n"); // Just to debug buffer free size Debug(F("sending ")); Debug(response.length()); Debug(F(" bytes...")); // Send response to client server.send ( 200, "text/html", response ); Debug(F("OK!")); LedBluOFF(); } /* ====================================================================== Function: formatNumberJSON Purpose : check if data value is full number and send correct JSON format Input : String where to add response char * value to check Output : - Comments: 00150 => 150 ADCO => "ADCO" 1 => 1 ====================================================================== */ void formatNumberJSON( String &response, char * value) { // we have at least something ? if (value && strlen(value)) { boolean isNumber = true; uint8_t c; char * p = value; // just to be sure if (strlen(p)<=16) { // check if value is number while (*p && isNumber) { if ( *p < '0' || *p > '9' ) isNumber = false; p++; } // this will add "" on not number values if (!isNumber) { response += '\"' ; response += value ; response += F("\":") ; } else { // this will remove leading zero on numbers p = value; while (*p=='0' && *(p+1) ) p++; response += p ; } } else { Debugln(F("formatNumberJSON error!")); } } } /* ====================================================================== Function: tinfoJSONTable Purpose : dump all teleinfo values in JSON table format for browser Input : linked list pointer on the concerned data true to dump all values, false for only modified ones Output : - Comments: - ====================================================================== */ void tinfoJSONTable(void) { ValueList * me = tinfo.getList(); String response = ""; // Just to debug where we are Debug(F("Serving /tinfoJSONTable page...\r\n")); // Got at least one ? if (me) { uint8_t index=0; boolean first_item = true; // Json start response += F("[\r\n"); // Loop thru the node while (me->next) { // we're there ESP.wdtFeed(); // go to next node me = me->next; // First item do not add , separator if (first_item) first_item = false; else response += F(",\r\n"); Debug(F("(")) ; Debug(++index) ; Debug(F(") ")) ; if (me->name) Debug(me->name) ; else Debug(F("NULL")) ; Debug(F("=")) ; if (me->value) Debug(me->value) ; else Debug(F("NULL")) ; Debug(F(" '")) ; Debug(me->checksum) ; Debug(F("' ")); // Flags management if ( me->flags) { Debug(F("Flags:0x")); Debugf("%02X => ", me->flags); if ( me->flags & TINFO_FLAGS_EXIST) Debug(F("Exist ")) ; if ( me->flags & TINFO_FLAGS_UPDATED) Debug(F("Updated ")) ; if ( me->flags & TINFO_FLAGS_ADDED) Debug(F("New ")) ; } Debugln() ; response += F("{\"na\":\""); response += me->name ; response += F("\", \"va\":\"") ; response += me->value; response += F("\", \"ck\":\"") ; if (me->checksum == '"' || me->checksum == '\\' || me->checksum == '/') response += '\\'; response += (char) me->checksum; response += F("\", \"fl\":"); response += me->flags ; response += '}' ; } // Json end response += F("\r\n]"); } else { Debugln(F("sending 404...")); server.send ( 404, "text/plain", "No data" ); } Debug(F("sending...")); server.send ( 200, "text/json", response ); Debugln(F("OK!")); } /* ====================================================================== Function: sysJSONTable Purpose : dump all sysinfo values in JSON table format for browser Input : linked list pointer on the concerned data true to dump all values, false for only modified ones Output : - Comments: - ====================================================================== */ void sysJSONTable() { String response = ""; // Just to debug where we are Debug(F("Serving /sysJSONTable page...")); // Json start response += F("[\r\n"); response += "{\"na\":\"Uptime\",\"va\":\""; response += sysinfo.sys_uptime; response += "\"},\r\n"; response += "{\"na\":\"Compile le\",\"va\":\"" __DATE__ " " __TIME__ "\"},\r\n"; response += "{\"na\":\"Free Ram\",\"va\":\""; response += sysinfo.sys_free_ram; response += "\"},\r\n"; response += "{\"na\":\"Flash Real Size\",\"va\":\""; response += sysinfo.sys_flash_real_size ; response += "\"},\r\n"; response += "{\"na\":\"Firmware Size\",\"va\":\""; response += sysinfo.sys_firmware_size; response += "\"},\r\n"; response += "{\"na\":\"Free Size\",\"va\":\""; response += sysinfo.sys_firmware_free; response += "\"},\r\n"; response += "{\"na\":\"Wifi SSID\",\"va\":\""; response += config.ssid; response += "\"},\r\n"; response += "{\"na\":\"OTA Network Port\",\"va\":"; response += config.ota_port ; response += "},\r\n"; response += "{\"na\":\"Wifi RSSI\",\"va\":\""; response += WiFi.RSSI() ; response += " dB\"}\r\n"; response += "{\"na\":\"Analog\",\"va\":\""; response += sysinfo.sys_analog; response += " dB\"}\r\n"; // Json end response += F("]\r\n"); Debug(F("sending...")); server.send ( 200, "text/json", response ); Debugln(F("Ok!")); } /* ====================================================================== Function: sendJSON Purpose : dump all values in JSON Input : linked list pointer on the concerned data true to dump all values, false for only modified ones Output : - Comments: - ====================================================================== */ void sendJSON(void) { ValueList * me = tinfo.getList(); String response = ""; // Got at least one ? if (me) { // Json start response += F("{\"_UPTIME\":"); response += seconds; // Loop thru the node while (me->next) { // we're there ESP.wdtFeed(); // go to next node me = me->next; response += F(",\"") ; response += me->name ; response += F("\":") ; formatNumberJSON(response, me->value); } // Json end response += F("}\r\n") ; } else { server.send ( 404, "text/plain", "No data" ); } server.send ( 200, "text/json", response ); } /* ====================================================================== Function: handleNotFound Purpose : default WEB routing when URI is not found Input : linked list pointer on the concerned data true to dump all values, false for only modified ones Output : - Comments: We search is we have a name that match to this URI, if one we return it's pair name/value in json ====================================================================== */ void handleNotFound(void) { String response = ""; // We check for an known label ValueList * me = tinfo.getList(); const char * uri; boolean found = false; // Led on LedBluON(); // convert uri to char * for compare uri = server.uri().c_str(); Debugf("handleNotFound(%s)\r\n", uri); // Got at least one and consistent URI ? if (me && uri && *uri=='/' && *++uri ) { // Loop thru the linked list of values while (me->next && !found) { // we're there ESP.wdtFeed(); // go to next node me = me->next; //Debugf("compare to '%s' ", me->name); // Do we have this one ? if (stricmp (me->name, uri) == 0 ) { // no need to continue found = true; // Add to respone response += F("{\"") ; response += me->name ; response += F("\":") ; formatNumberJSON(response, me->value); response += F("}\r\n"); } } } // Got it, send json if (found) { server.send ( 200, "text/json", response ); } else { // send error message in plain text 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 ); } // Led off LedBluOFF(); }