From 14e047f5a12e9401bb73154bc9c015f2dcb03d4b Mon Sep 17 00:00:00 2001 From: PeterArandis Date: Mon, 18 Nov 2019 22:37:13 +0100 Subject: [PATCH] Version 1 --- H60-OS.ino | 39 +++++++------------ HD_Globals.h | 5 +-- HD_H1comm.h | 30 +++------------ HD_Utilities.h | 102 +++---------------------------------------------- HD_Webserver.h | 66 ++++++++------------------------ README.md | 16 +++++++- 6 files changed, 55 insertions(+), 203 deletions(-) diff --git a/H60-OS.ino b/H60-OS.ino index 09e36cf..6fe7d8d 100644 --- a/H60-OS.ino +++ b/H60-OS.ino @@ -17,7 +17,6 @@ #include // Web Client #include // Serial port for H1 #include // Json parser support -#include // SPIFFS SPI-Flash Filsystem, Used for Webserver #include "HD_Globals.h" SoftwareSerial H1(13, 12, false, 1024); // Init software serial Pin 13, 12, non inv, buff for H1 H60 os_timer_t myTimer; // Init soft interrupt timer @@ -31,7 +30,6 @@ void setup() { // H60 Boot sequence pinMode(LED, OUTPUT); // Initialize the LED_BUILTIN pin as an output digitalWrite(LED, LOW); // LED Status: ON: Init | Quick-blink: WifiOK, No Online | One-sec-blink: All OK - SPIFFS.begin(); // Initialize filesystem för Config, FW och Web buffering Serial.begin(115200); // Debug serial usb setup H1.begin(19200); // H1 soft serial setup @@ -96,13 +94,13 @@ void timerCallback(void *pArg) { // Every second if (digitalRead(D2) == LOW) { // Read button press B1_Count++; // Button 1 B1_Pushed=true; - Serial.println("Button Pushed " + String(B1_Count) + " sec"); + Serial.println("Button Pushed " + String(B1_Count) + " sec"); } else { // Button release - //if (B1_Pushed && B1_Count == 1) { flash_ip=true; } // telegrafera ip sista tecken om touchar knapp - //if (B1_Count >5 ) { config("CFG WIFI_SSID=");config("CFG DHCP_ENAB=0");config("CFG WEB_ENABLED=1");config("CFG HD_ONLINE=1"); } // Default inst + + //if (B1_Count >5 ) {} // Button 5 sek B1_Count=0; B1_Pushed = false; } @@ -123,36 +121,25 @@ void loop() { FlashLed(); // ======= Every SEK ======== - - if (onesec){ - - onesec=0; + if (onesec){ onesec=false; } - - } - - - // ======= VARJE MINUT ======== - if (onemin){ // Kör varje ny minut utanför timer rutin då den krashar annars. + // ======= EVERY MINUTE ======== + if (onemin){ - if (WiFi.status() == 3) state_Wifi=true; else state_Wifi=false; // Check Wifi status 3 = ok, 0/1/6=fault - - uptime++; - mincount=0; - onemin=false; + if (WiFi.status() == 3) state_Wifi=true; else state_Wifi=false; // Check Wifi status 3 = ok, 0/1/6=fault + uptime++; + mincount=0; + onemin=false; } - // ======= VAR 5e SEKUND ======== - if (every5seconds) { // Kör var 10:e sekund - //Serial.print("+"); + // ======= EVERY 5th SECOND ======== + if (every5seconds) { every5seconds=0; secondsCount=0; H1_CommInit(0); - - - } + } } diff --git a/HD_Globals.h b/HD_Globals.h index 0321ccf..d39d9eb 100644 --- a/HD_Globals.h +++ b/HD_Globals.h @@ -1,4 +1,4 @@ -// Prototyper ------------- +// Prototypes ------------- String H1_format_value (char idx, char type); String H1_unit_value (char idx); byte H1_IDXtoID (String idx); @@ -31,7 +31,6 @@ char g_wifi_rssi[20] = ""; char g_wifi_ip[20]; char g_wifi_mac[20]; -String restart_reason = "unknown"; // H1 comm related String H1_Ver=""; @@ -52,5 +51,3 @@ bool state_H1Comm=0; bool state_HPComm=0; bool state_XL_ListRead = false; bool state_Indexes_Received=0; -//bool state_Startuplog_Sent=0; // Vid uppstart skickas en log efter 1 min, detta görs bara en gÃ¥ng -//bool state_SupportServer = false; diff --git a/HD_H1comm.h b/HD_H1comm.h index 74a2dbb..9b87bdc 100644 --- a/HD_H1comm.h +++ b/HD_H1comm.h @@ -84,13 +84,6 @@ void H1_HandleData() { // Handle incoming H1 data } -// DETECT AND CORRECT MISSING FIRMWARE - else if (incomingH1.substring(0,10)=="Bootloader") {//and g_startseq == START_INIT: - trace("H1 Booted","LOG LF SER"); - // H1_Booted = true; - - } - else { @@ -106,7 +99,6 @@ void H1_HandleData() { // Handle incoming H1 data String H1_PrintVpModel() { String vpmod; -//String t; if (H1_Type == "00") vpmod = "Rego 600"; else if (H1_Type == "05") vpmod = "Rego 400"; @@ -119,33 +111,25 @@ String vpmod; else if (H1_Type == "70") vpmod = "Thermia Villa"; else return "Not initialized"; - //t = vpmod + ", Firmware: " + H1_Type+H1_Ver; //trace (t,"SER LOG LF"); return vpmod + ", Firmware: " + H1_Type+H1_Ver; //trace (t,"SER LOG LF"); //return t; } //------------------------------------------------------------------------------------------------- -void H1_Recv() { // Data mottages frÃ¥n H1 Interface +void H1_Recv() { // Data from H1 controller static byte ndx = 0; char endMarker = '\r'; char rc; - #ifdef simulator - return; - #endif while (H1.available() > 0 && new_H1_data == false) { rc = H1.read(); - - //if (H1_FlashMode) { if (receivedCharsH1.endsWith("Bootloader..")) {H1_Booted = true; Serial.print ("+");} } -// if (H1_FlashMode) { if (receivedCharsH1.indexOf("Bootloader..")) {Serial.print (receivedCharsH1);receivedCharsH1="";H1_Booted = true;} } - - + if (rc != endMarker) { - + receivedCharsH1 += rc; ndx++; @@ -168,8 +152,7 @@ void H1_Recv() { // Data mottages frÃ¥n H1 Interface receivedCharsH1=""; } - - + yield(); } //} @@ -177,7 +160,7 @@ void H1_Recv() { // Data mottages frÃ¥n H1 Interface //-------------------------------------------------------------------------------------------------------- -void H1_hp_set(String idx, int value) +void H1_hp_set(String idx, int value) // Set parameter in Heat pump { char v_hex[10]; String s; @@ -194,7 +177,6 @@ void H1_hp_set(String idx, int value) } //------------------------------------------------------------------------------------------------- - String H1_unit_value (char idx) { @@ -229,9 +211,7 @@ String H1_format_value (char idx, char type) { char z[2]; char *endptr; - //String valprint; String val; - float v; int i; int unit; diff --git a/HD_Utilities.h b/HD_Utilities.h index 25bd8a0..feae6fd 100644 --- a/HD_Utilities.h +++ b/HD_Utilities.h @@ -1,8 +1,6 @@ - void trace (String text, String func) ; - // Time / NTP #define TZ 0 // (utc+) TZ in hours #define DST_MN 60 // use 60mn for summer time in some countries @@ -12,10 +10,6 @@ void trace (String text, String func) ; timeval tv; time_t now; - - - - void FlashLed() { @@ -26,10 +20,10 @@ void FlashLed() const int pattern[][15]= { {1,5,5000,0}, // 0: Dark {1,70,3000,0}, // 1: . Wifi Err {1,70,300,70,3000,0}, // 2: . . H1 Err - {1,70,300,70,300,70,3000,0}, // 3: . . . HD Online Err - {1,70,300,70,70,300,300,70,3000,0}, // 4: . . . . Ledig för ytterligare err + {1,70,300,70,300,70,3000,0}, // 3: . . . Not used + {1,70,300,70,70,300,300,70,3000,0}, // 4: . . . . Not used {1,1000,1000,0}, // 5: - - Slow flash, all OK - {1,50,50,0}, // 6: ....... Super fast flash, AP mode + {1,50,50,0}, // 6: ....... Super fast flash, AP mode not used {1,70,300,70,300,700,2000,0}, // 7: . . - Test not used }; @@ -43,7 +37,7 @@ if(!flash_ip) } - if (!pattern[pattern_no][pattern_pos]) pattern_pos=0; // Nollställ och starta om + if (!pattern[pattern_no][pattern_pos]) pattern_pos=0; if (pattern_pos == 0) {m = millis(); digitalWrite(LED, HIGH);pattern_pos++;} // INIT else if ( pattern_pos % 2 == 0) {if (millis() > m + pattern[pattern_no][pattern_pos]) {m = millis();digitalWrite(LED, HIGH);pattern_pos++;} } // EVEN 2,4,6 @@ -56,9 +50,6 @@ if(!flash_ip) //----------------------------------------------------------------------------------------------- - - - void rssi_update() { int i = WiFi.RSSI(); @@ -70,18 +61,6 @@ void rssi_update() sprintf(g_wifi_rssi,"%s %ddBm", s, i); // Uppdatera signalstyrka } -//----------------------------------------------------------------------------------------------- -void DispOled(char *text) { - /* - oled.clear(); - //oled.drawString(0, 0, text); - //oled.drawString(0, 0, receivedChars); - oled.drawString(0, 15, g_wifi_ip); - oled.drawString(0, 30, g_wifi_rssi); - oled.drawString(0, 45, g_wifi_mac); - oled.display(); - */ -} //----------------------------------------------------------------------------------------------- void trace (String text, String func) // SER LOG LF @@ -89,10 +68,8 @@ void trace (String text, String func) // SER LOG LF Serial.println(text); } +//----------------------------------------------------------------------------------------------- - -//=============================================================================== -// behöver inte returnera nÃ¥tt, modifierar originalvariabeln char *tolow (char *s) { char *p; @@ -100,78 +77,9 @@ char *tolow (char *s) return s; } -//=============================================================================== -//------------------------------------------------------------------------------------------------------------------ -String get_restart_reason () -{ - File f = SPIFFS.open("/restart_reason.txt", "r"); - if (f) { - if (f.available()) return(" " + f.readStringUntil(',')); - } - return " "; -} -void restart_h60 (bool restart) -{ - - File f = SPIFFS.open("/restart_reason.txt", "w"); - if (f) {f.println(restart_reason); f.close(); } - - if (restart) ESP.restart(); - -} - -//--------------------------------------------------------------------------------------------------------------- - -String chkmem() -{ - // Check memory avalible, Critical under 9000 bytes - long fh = ESP.getFreeHeap(); - char fhc[20]; - ltoa(fh, fhc, 10); - return String(fhc); -} - - -//------------------------------------------------------------------------------------------------------------------ - - - -String urlencode(String str) // URL encode string for web posting -{ - String encodedString=""; - char c; - char code0; - char code1; - char code2; - for (int i =0; i < str.length(); i++){ - c=str.charAt(i); - if (c == ' '){ - encodedString+= '+'; - } else if (isalnum(c)){ - encodedString+=c; - } else{ - code1=(c & 0xf)+'0'; - if ((c & 0xf) >9){ - code1=(c & 0xf) - 10 + 'A'; - } - c=(c>>4)&0xf; - code0=c+'0'; - if (c > 9){ - code0=c - 10 + 'A'; - } - code2='\0'; - encodedString+='%'; - encodedString+=code0; - encodedString+=code1; - //encodedString+=code2; - } - yield(); - } - return encodedString; -} diff --git a/HD_Webserver.h b/HD_Webserver.h index 18fa3d6..632b0e0 100644 --- a/HD_Webserver.h +++ b/HD_Webserver.h @@ -1,10 +1,8 @@ ESP8266WebServer *server; void H1_hp_set(String idx, int value); -void spif_web(); void handleNotFound(); void ApiAllData(); -void AllData(bool homey); void ApiSet(); const String title = ""; @@ -30,8 +28,6 @@ const String footer = "<br><br>(C) 2019 Arandis AB Husdata.se\ //--------------------------------------------------------------------------------------------------------------- - - void pageRoot() { String buff,typ; @@ -40,22 +36,17 @@ void pageRoot() { byte i; int ID=99; float v; - - File f = SPIFFS.open("/webpage.html", "w"); - f.print(title + "H60" + header); - buff = "<br><form action='/' method='post' autocomplete='off'>\ + + buff = title + "H60" + header; + buff += "<br><form action='/' method='post' autocomplete='off'>\ <table width=100% border=0 cellspacing=0 cellpadding=4>\ <tr bgcolor=#133670><td width=40><font color='white'><b> Status</td><td width=40><font color='white'></td></tr>\ <tr><td> IP Address</td><td>" + String(g_wifi_ip) + "</td></tr>\ <tr><td> Mac Address</td><td>" + String(g_wifi_mac) + "</td></tr>\ <tr><td> Signal strength</td><td>" + String(g_wifi_rssi) + "</td></tr>\ <tr><td> Interface cable</td><td>" + H1_PrintVpModel() + "</td></tr>"; - - + buff+="</table>"; - f.print(buff); - buff=""; - if(server->args()>0) // Any arguments { @@ -88,13 +79,13 @@ void pageRoot() { else buff += "<br>Setting value has too few characters!<br>"; - f.print(buff); + // f.print(buff); } } - buff="<br>\ + buff+="<br>\ <table width=100% cellspacing=0 cellpadding=4 border = 0>\ <tr bgcolor=#133670>\ <td width=20%><font color='white'><b> Idx</td>"; @@ -102,7 +93,7 @@ void pageRoot() { buff += "<td width=40%><font color='white'><b>Name</td>\ <td width=30%><font color='white'><b>Value</td>\ </tr>"; - f.print(buff); + for (i=0;i<indexNo;i++) @@ -115,12 +106,12 @@ void pageRoot() { { if (col) {bgcolor="b3b3b3";col=false;} else {bgcolor="c5c5c5";col=true;} - buff = "<tr bgcolor='#" + bgcolor + "'><td> <a href='?id=" + String(i) + "'><span class='setting'>" + String(H1_ID[i]) + "</span></a></td>"; //SETTABLE + buff += "<tr bgcolor='#" + bgcolor + "'><td> <a href='?id=" + String(i) + "'><span class='setting'>" + String(H1_ID[i]) + "</span></a></td>"; //SETTABLE } else { // if NOT a setting if (col) {bgcolor="eeeeee";col=false;} else {bgcolor="FFFFFF";col=true;} - buff = "<tr bgcolor='#" + bgcolor + "'><td> " + String(H1_ID[i]) + "</td>"; // NON Settable + buff += "<tr bgcolor='#" + bgcolor + "'><td> " + String(H1_ID[i]) + "</td>"; // NON Settable } @@ -133,33 +124,17 @@ void pageRoot() { buff += "</tr>"; - f.print(buff); + } } - buff = "</table></form>"; - - f.print(buff + footer); - f.close(); - spif_web(); + buff += "</table></form>"; - //server->send(200, "text/html", title + setting[NAME] + header+menu+buff+footer); + server->send(200, "text/html", buff+footer); rssi_update(); } -//--------------------------------------------------------------------------------------------------------------- - -//---------------------------------------------------------------------------------------------- - - -//---------------------------------------------------------------------------------------------- -void spif_web(){ // Stream SPIFFS html file - - File dataFile = SPIFFS.open("/webpage.html", "r"); - server->streamFile(dataFile, "text/html"); - dataFile.close(); - } //---------------------------------------------------------------------------------------------- @@ -181,15 +156,12 @@ void handleNotFound() { //---------------------------------------------------------------------------------------------- void setup_webserver(void) { - /* if (MDNS.begin("esp8266")) { - Serial.println("MDNS responder started"); - } */ server = new ESP8266WebServer(80); server->on("/", pageRoot); - server->on("/api/alldata", ApiAllData); // API Setting to 1 for read enable - server->on("/api/set", ApiSet); // API Setting to 2 for write enable + server->on("/api/alldata", ApiAllData); + server->on("/api/set", ApiSet); server->onNotFound(handleNotFound); server->begin(); trace("HTTP server started ", "SER LOG LF"); @@ -198,24 +170,18 @@ void setup_webserver(void) { } - - //---------------------------------------------------------------------------------------------- - -void ApiAllData() {AllData(false);} // Read all registers in JSON - -void AllData(bool homey) { // Read all registers in JSON format +void ApiAllData() { // Read all registers in JSON format String buff; byte i; //{"0001":238,"0002":459,"0003":167,"0004":426,"0005":26,"0006":43,"0007":8,"0008":133,"0009":356,"000A":388,"000B":310,"3104":472,"0107":364,"0111":278,"0203":204,"2204":36,"2205":230,"0207":441,"0208":229,"7209":214,"1A01":1,"1A02":0,"1A03":0,"1A04":1,"1A05":0,"1A06":1,"1A07":1,"1A20":0} - // API Setting has to be 1 or larger + buff = "{"; for (i=0;i<indexNo;i++) { if (i!=0) buff += ","; buff += String(char(34)); - if (homey) buff += "X"; buff += String(H1_ID[i]) + String(char(34)); buff += ":"; buff += String(H1_ValueNow[i]); diff --git a/README.md b/README.md index 9e82d96..3a04ef9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,16 @@ # H60-OS - Husdata H60 Arduino get started code +Husdata H60 Arduino get started code +HUSDATA.SE, Peter Hansson. h60@husdata.se + +Open H60 Arduino code as a foundation for own development projects on the H60 platform +Provided as-is with no support. Can be freely used in personal och commercial projects. + +Functions: + - Wifi connection with DHCP and fixed SSID/PASS + - Basic code for communication with heat pump + - Web page showing status and heat pump data + - restAPI at /api/alldata + - LED status H60 red/green + - Routine for button pushed + - Debug printout via serial/usb port +