-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathH60-OS.ino
168 lines (122 loc) · 5.14 KB
/
H60-OS.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* HUSDATA.SE, Peter Hansson. [email protected]
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.
Fubnctions:
- 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
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h> // Webserver
#include <ESP8266HTTPClient.h> // Web Client
#include <SoftwareSerial.h> // Serial port for H1
#include <ArduinoJson.h> // Json parser support
#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
#define LED 14 // Define green LED
#include "HD_Utilities.h" // Helper functions
#include "HD_Webserver.h" // Internal webserver and API
#include "HD_H1comm.h" // Communications towards H1 interface and heatpump
//===============================================================================
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
Serial.begin(115200); // Debug serial usb setup
H1.begin(19200); // H1 soft serial setup
Serial.println("\n\n\r--------------------------------");
Serial.println (" Husdata H60 Demo open source");
Serial.println ("--------------------------------\n\n");
// ------------- WIFI CONNECT --------------
Serial.println("Wifi connecting...");
WiFi.mode(WIFI_STA);
WiFi.begin("Guest", ""); // Set SWSID and PASSWORD
byte i=0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if (i++>25) break; // Try to connect 25 times every 0.5 sec
}
if (WiFi.status() == WL_CONNECTED) {
trace("Wifi connected!","SER LOG LF");
state_Wifi=true;
}
else
{
trace("Giving up, no Wifi connection","SER LOG LF");
WiFi.disconnect();
}
digitalWrite(LED, HIGH); // Turn off when Wifi Int process is done
// ----------- GET IP, RSSI, MAC --------------
rssi_update();
trace("Signal strength: " + String(g_wifi_rssi),"SER LOG LF");
sprintf(g_wifi_ip,"%d.%d.%d.%d", WiFi.localIP()[0],WiFi.localIP()[1],WiFi.localIP()[2],WiFi.localIP()[3]);
trace("DHCP IP address: " + String(g_wifi_ip), "SER LOG LF");
byte mac[8]; WiFi.macAddress(mac);
sprintf(g_wifi_mac,"%02X%02X%02X%02X%02X%02X", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); tolow(g_wifi_mac); trace("MAC address: " + String(g_wifi_mac),"SER LOG LF");
// --------- INTERUPT TIMER INIT ------------------
os_timer_setfn(&myTimer, timerCallback, NULL);
os_timer_arm(&myTimer, 1000, true); // every sec
setup_webserver(); // Init Web server
H1.write("\r"); // Flush
}
//---------------------------------------------------------------------------------------
void timerCallback(void *pArg) { // Every second
static byte B1_Count;
static bool B1_Pushed;
if (secondsCount++>=5) every5seconds = true;
if (mincount++>=60) onemin=true;
onesec=true;
if (digitalRead(D2) == LOW) { // Read button press
B1_Count++; // Button 1
B1_Pushed=true;
Serial.println("Button Pushed " + String(B1_Count) + " sec");
}
else
{ // Button release
//if (B1_Count >5 ) {} // Button 5 sek
B1_Count=0;
B1_Pushed = false;
}
} // End of timerCallback
//####################################################################################################################################################################
void loop() {
int i;
// --- Fast loop --
H1_Recv(); // Receive H1 data
H1_HandleData(); // Handle new data from interface
SerialDebugRX(); // Read serial debug port
webserver_loop(); // Handle Webserver requests
FlashLed();
// ======= Every SEK ========
if (onesec){ onesec=false; }
// ======= 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;
}
// ======= EVERY 5th SECOND ========
if (every5seconds) {
every5seconds=0;
secondsCount=0;
H1_CommInit(0);
}
}
//---------------------------------------------------------------------------------------
void SerialDebugRX(){ // Data mottages på debugport
char c;
static String rxb;
if(Serial.available())
{
c = (char)Serial.read();
//Serial.print (c, DEC);
if (c!=10 && c!=13) {rxb += c; return;}
//handleCmds(rxb);
rxb="";
}
}