From 13e640d0c77298ab2644a126123cd55ac5919e91 Mon Sep 17 00:00:00 2001 From: zz04303 <44981174+zz04303@users.noreply.github.com> Date: Fri, 9 Dec 2022 20:44:27 +0100 Subject: [PATCH 1/9] add modell Kazumi --- README.md | 4 +++- main/hpfuncs.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 78f5e4a..8f2c46f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Buy Me A Coffee # shorai-esp32 -This will work for Toshiba Shorai and Seiya +This will work for Toshiba Shorai, Seiya and Kazumi This works great for me, but is at your own risk! @@ -89,6 +89,8 @@ mqtt: - "fan_only" swing_modes: - "on" +# - "on-h" # uncomment only for model Kazumi +# - "on-vh" # uncomment only for model Kazumi - "off" fan_modes: - "quiet" diff --git a/main/hpfuncs.py b/main/hpfuncs.py index f6855cb..e193241 100644 --- a/main/hpfuncs.py +++ b/main/hpfuncs.py @@ -12,7 +12,7 @@ fanmodetoint = {"quiet":49, "lvl_1": 50, "lvl_2":51, "lvl_3":52, "lvl_4":53, "lvl_5":54, "auto":65} inttofanmode = dict(map(reversed, fanmodetoint.items())) -swingtoint = {"off": 49, "on":65} +swingtoint = {"off": 49, "on":65, "on-h":66, "on-vh":67} inttoswing = dict(map(reversed, swingtoint.items())) statetoint = {"ON":48, "OFF":49} From 66499fb590443c8bac33f2aebaaeea23dbdc9e89 Mon Sep 17 00:00:00 2001 From: zz04303 <44981174+zz04303@users.noreply.github.com> Date: Sat, 10 Dec 2022 11:51:37 +0100 Subject: [PATCH 2/9] print version at firstrun --- main/heatpump.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/main/heatpump.py b/main/heatpump.py index e7e29fc..28b1382 100644 --- a/main/heatpump.py +++ b/main/heatpump.py @@ -1,5 +1,7 @@ from main import hpfuncs +from main.ota_updater import OTAUpdater from machine import UART + global uart uart = UART(1, 9600) uart.init(9600,bits = 8,parity = 0,stop = 1,rx = 32,tx = 33,timeout = 10, timeout_char=50) @@ -147,8 +149,10 @@ async def firstrun(client): firstrun = False await asyncio.sleep(10) if firstrun == False: - await client.publish(config['maintopic'] + '/doinit', "firstrun") - hpfuncs.logprint("init firstrun") + ota = OTAUpdater(config['your_repo']) + current_version = ota.get_version('/main/') + await client.publish(config['maintopic'] + '/doinit', "firstrun version " + current_version ) + hpfuncs.logprint("init firstrun version " + current_version) firstrun = True while True: await asyncio.sleep(60) From 10ffb97f7c25cb53b6c3a4ccffc2addbadf6b25b Mon Sep 17 00:00:00 2001 From: zz04303 <44981174+zz04303@users.noreply.github.com> Date: Wed, 6 Dec 2023 20:01:42 +0100 Subject: [PATCH 3/9] (1)state ON OFF to lower case on off --- main/hpfuncs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/hpfuncs.py b/main/hpfuncs.py index e193241..8b80882 100644 --- a/main/hpfuncs.py +++ b/main/hpfuncs.py @@ -15,7 +15,7 @@ swingtoint = {"off": 49, "on":65, "on-h":66, "on-vh":67} inttoswing = dict(map(reversed, swingtoint.items())) -statetoint = {"ON":48, "OFF":49} +statetoint = {"on":48, "off":49} inttostate = dict(map(reversed, statetoint.items())) def checksum(msg,function): From a2a6cd09420cd4996e04b37e8fe69e3e6593109e Mon Sep 17 00:00:00 2001 From: zz04303 <44981174+zz04303@users.noreply.github.com> Date: Wed, 6 Dec 2023 20:10:47 +0100 Subject: [PATCH 4/9] (1)state ON OFF to lower case on off #2 --- main/heatpump.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/main/heatpump.py b/main/heatpump.py index 28b1382..f9ded46 100644 --- a/main/heatpump.py +++ b/main/heatpump.py @@ -11,7 +11,7 @@ import time from time import sleep import machine -power_state = 'OFF' +power_state = 'off' @@ -186,8 +186,8 @@ async def receiver(client): state = hpfuncs.inttostate[int(data[15])] power_state = state await client.publish(config['maintopic'] + '/state/state', str(state), qos=1) - if (state == "OFF"): - # when power state is OFF, sent unit mode also as "off" + if (state == "off"): + # when power state is off, sent unit mode also as "off" await client.publish(config['maintopic'] + '/mode/state', "off", qos=1) if(str(data[14]) == "160"): fanmode = hpfuncs.inttofanmode[int(data[15])] @@ -198,7 +198,7 @@ async def receiver(client): if(str(data[14]) == "176"): mode = hpfuncs.inttomode[int(data[15])] # report actual mode when unit is running or "off" when it's not - reportedState = str(mode) if (power_state == "ON") else "off" + reportedState = str(mode) if (power_state == "on") else "off" await client.publish(config['maintopic'] + '/mode/state', reportedState, qos=1) if(str(data[14]) == "190"): outdoortemp = int_to_signed(int(data[15])) @@ -214,8 +214,8 @@ async def receiver(client): state = hpfuncs.inttostate[int(data[13])] power_state = state await client.publish(config['maintopic'] + '/state/state', str(state), qos=1) - if (state == "OFF"): - # when power state is OFF, sent unit mode also as "off" + if (state == "off"): + # when power state is off, sent unit mode also as "off" await client.publish(config['maintopic'] + '/mode/state', "off", qos=1) if(str(data[12]) == "160"): fanmode = hpfuncs.inttofanmode[int(data[13])] @@ -226,7 +226,7 @@ async def receiver(client): if(str(data[12]) == "176"): mode = hpfuncs.inttomode[int(data[13])] # report actual mode when unit is running or "off" when it's not - reportedState = str(mode) if (power_state == "ON") else "off" + reportedState = str(mode) if (power_state == "on") else "off" await client.publish(config['maintopic'] + '/mode/state', reportedState, qos=1) if(str(data[12]) == "190"): outdoortemp = int_to_signed(int(data[13])) From a4c7fc37df9c590a9581913d3a78094f022e6a9b Mon Sep 17 00:00:00 2001 From: zz04303 <44981174+zz04303@users.noreply.github.com> Date: Sat, 16 Dec 2023 20:08:23 +0100 Subject: [PATCH 5/9] adjust for Deprecate power_command_topic for MQTT climate #85229 --- main/heatpump.py | 511 ++++++++++++++++++++++++----------------------- main/hpfuncs.py | 281 +++++++++++++------------- 2 files changed, 403 insertions(+), 389 deletions(-) diff --git a/main/heatpump.py b/main/heatpump.py index f9ded46..3694fa5 100644 --- a/main/heatpump.py +++ b/main/heatpump.py @@ -1,255 +1,256 @@ -from main import hpfuncs -from main.ota_updater import OTAUpdater -from machine import UART - -global uart -uart = UART(1, 9600) -uart.init(9600,bits = 8,parity = 0,stop = 1,rx = 32,tx = 33,timeout = 10, timeout_char=50) -import uasyncio as asyncio -from main.mqtt_as import MQTTClient -from config import config -import time -from time import sleep -import machine -power_state = 'off' - - - -#topic_prefix = "heatpump" -#mqtt_server = '192.168.2.30' -#client_id ='hpesp32-1' - -topic_sub_setp = b"" + config['maintopic'] + "/setpoint/set" -topic_sub_state = b"" + config['maintopic'] + "/state/set" -topic_sub_fanmode = b"" + config['maintopic'] + "/fanmode/set" -topic_sub_swingmode = b"" + config['maintopic'] + "/swingmode/set" -topic_sub_mode = b"" + config['maintopic'] + "/mode/set" -topic_sub_doinit = b"" + config['maintopic'] + "/doinit" -topic_sub_restart = b"" + config['maintopic'] + "/restart" -topic_sub_watchdog = b"" + config['maintopic'] + "/watchdog" -topics = [topic_sub_setp, topic_sub_state, topic_sub_doinit, topic_sub_fanmode, topic_sub_mode, topic_sub_swingmode, topic_sub_restart, topic_sub_watchdog] - -def int_to_signed(intval): - if intval > 127: - return (256-intval) * (-1) - else: - return intval - -#mqtt stuff -def sub_cb(topic, msg, retained): - global power_state - runwrite = True - hpfuncs.logprint(str(topic) + " -- " + str(msg)) -################################################ -#setpoint - if topic == topic_sub_setp: - try: - values = hpfuncs.setpointVal(int(float(msg))) - except Exception as e: - hpfuncs.logprint(e) - runwrite = False -################################################ -#restart - if topic == topic_sub_restart: - try: - machine.reset() - except Exception as e: - hpfuncs.logprint(e) - runwrite = False -################################################ -# state - elif topic == topic_sub_state: - try: - values = hpfuncs.stateControl(msg) - if values == False: - runwrite = False - except Exception as e: - hpfuncs.logprint(e) - runwrite = False -################################################ -# swingstate - elif topic == topic_sub_swingmode: - try: - values = hpfuncs.swingControl(msg) - if values == False: - runwrite = False - except Exception as e: - hpfuncs.logprint(e) - runwrite = False -################################################ -# mode - elif topic == topic_sub_mode: - try: - values = hpfuncs.modeControl(msg) - if values == False: - runwrite = False - except Exception as e: - hpfuncs.logprint(e) - runwrite = False -################################################ -# fanmode - elif topic == topic_sub_fanmode: - try: - values = hpfuncs.fanControl(msg) - if values == False: - runwrite = False - except Exception as e: - hpfuncs.logprint(e) - runwrite = False -################################################ -# do init - elif topic == topic_sub_doinit: - myvals = hpfuncs.queryall() - hpfuncs.logprint("initial read") - for i in myvals: - uart.write(bytearray(i)) - sleep(0.2) - hpfuncs.logprint("initial read done") - runwrite = False - -################################################ -# do watchdog - elif topic == topic_sub_watchdog: - myvals = hpfuncs.watchdog() - for i in myvals: - uart.write(bytearray(i)) - sleep(0.2) - runwrite = False -################################################ - if runwrite == True and values != False: - #print(values) - for i in values: - hpfuncs.logprint("writing: " + str(i)) - uart.write(bytearray(i)) - sleep(0.2) - - -def chunkifyarray(vals): - val_length = len(vals) - start = 0 - rest_size = val_length - myresult = [] - while rest_size > 14: - lengde= int(vals[start+6]) - chunk_size = lengde + 8 - chunk_end = start + int(vals[start+6]) + 8 - myresult.append(vals[start:chunk_end]) - start = (start + chunk_size) - rest_size = rest_size - chunk_size - return myresult - - -# subscribe to topics -async def conn_han(client): - for i in topics: - await client.subscribe(i,1) - -# first run to collect values and run watchdog -async def firstrun(client): - firstrun = False - await asyncio.sleep(10) - if firstrun == False: - ota = OTAUpdater(config['your_repo']) - current_version = ota.get_version('/main/') - await client.publish(config['maintopic'] + '/doinit', "firstrun version " + current_version ) - hpfuncs.logprint("init firstrun version " + current_version) - firstrun = True - while True: - await asyncio.sleep(60) - await client.publish(config['maintopic'] + '/watchdog', "get") - hpfuncs.logprint("running watchdog..") - -async def receiver(client): - global power_state - - sreader = asyncio.StreamReader(uart) - try: - while True: - serdata = await sreader.read(2048) - if serdata is not None: - readable = list() - for i in serdata: - readable.append(str(int(i))) - hpfuncs.logprint("length of data: " + str(len(readable))) - chunks = chunkifyarray(readable) - for data in chunks: - hpfuncs.logprint(data) - await client.publish(config['maintopic'] + '/debug/fullstring', str(data)) - if len(data) == 17: - if(str(data[14]) == "187"): - roomtemp = int_to_signed(int(data[15])) - await client.publish(config['maintopic'] + '/roomtemp', str(roomtemp), qos=1) - if(str(data[14]) == "179"): - setpoint = int(data[15]) - await client.publish(config['maintopic'] + '/setpoint/state', str(setpoint), qos=1) - if(str(data[14]) == "128"): - state = hpfuncs.inttostate[int(data[15])] - power_state = state - await client.publish(config['maintopic'] + '/state/state', str(state), qos=1) - if (state == "off"): - # when power state is off, sent unit mode also as "off" - await client.publish(config['maintopic'] + '/mode/state', "off", qos=1) - if(str(data[14]) == "160"): - fanmode = hpfuncs.inttofanmode[int(data[15])] - await client.publish(config['maintopic'] + '/fanmode/state', str(fanmode), qos=1) - if(str(data[14]) == "163"): - swingmode = hpfuncs.inttoswing[int(data[15])] - await client.publish(config['maintopic'] + '/swingmode/state', str(swingmode), qos=1) - if(str(data[14]) == "176"): - mode = hpfuncs.inttomode[int(data[15])] - # report actual mode when unit is running or "off" when it's not - reportedState = str(mode) if (power_state == "on") else "off" - await client.publish(config['maintopic'] + '/mode/state', reportedState, qos=1) - if(str(data[14]) == "190"): - outdoortemp = int_to_signed(int(data[15])) - await client.publish(config['maintopic'] + '/outdoortemp', str(outdoortemp), qos=1) - elif len(data) == 15: - if(str(data[12]) == "187"): - roomtemp = int_to_signed(int(data[13])) - await client.publish(config['maintopic'] + '/roomtemp', str(roomtemp), qos=1) - if(str(data[12]) == "179"): - setpoint = int(data[13]) - await client.publish(config['maintopic'] + '/setpoint/state', str(setpoint), qos=1) - if(str(data[12]) == "128"): - state = hpfuncs.inttostate[int(data[13])] - power_state = state - await client.publish(config['maintopic'] + '/state/state', str(state), qos=1) - if (state == "off"): - # when power state is off, sent unit mode also as "off" - await client.publish(config['maintopic'] + '/mode/state', "off", qos=1) - if(str(data[12]) == "160"): - fanmode = hpfuncs.inttofanmode[int(data[13])] - await client.publish(config['maintopic'] + '/fanmode/state', str(fanmode), qos=1) - if(str(data[12]) == "163"): - swingmode = hpfuncs.inttoswing[int(data[13])] - await client.publish(config['maintopic'] + '/swingmode/state', str(swingmode), qos=1) - if(str(data[12]) == "176"): - mode = hpfuncs.inttomode[int(data[13])] - # report actual mode when unit is running or "off" when it's not - reportedState = str(mode) if (power_state == "on") else "off" - await client.publish(config['maintopic'] + '/mode/state', reportedState, qos=1) - if(str(data[12]) == "190"): - outdoortemp = int_to_signed(int(data[13])) - await client.publish(config['maintopic'] + '/outdoortemp', str(outdoortemp), qos=1) - - except Exception as e: - hpfuncs.logprint(e) - - -async def mainloop(client): - await client.connect() - -config['subs_cb'] = sub_cb -config['connect_coro'] = conn_han -#config['server'] = SERVER -MQTTClient.DEBUG = True -client = MQTTClient(config) - - -loop = asyncio.get_event_loop() -loop.create_task(mainloop(client)) -loop.create_task(receiver(client)) -loop.create_task(firstrun(client)) -loop.run_forever() - - +from main import hpfuncs +from main.ota_updater import OTAUpdater +from machine import UART + +global uart +uart = UART(1, 9600) +uart.init(9600,bits = 8,parity = 0,stop = 1,rx = 32,tx = 33,timeout = 10, timeout_char=50) +import uasyncio as asyncio +from main.mqtt_as import MQTTClient +from config import config +import time +from time import sleep +import machine +power_state = 'OFF' + + + +#topic_prefix = "heatpump" +#mqtt_server = '192.168.2.30' +#client_id ='hpesp32-1' + +topic_sub_setp = b"" + config['maintopic'] + "/setpoint/set" +topic_sub_state = b"" + config['maintopic'] + "/state/set" +topic_sub_fanmode = b"" + config['maintopic'] + "/fanmode/set" +topic_sub_swingmode = b"" + config['maintopic'] + "/swingmode/set" +topic_sub_mode = b"" + config['maintopic'] + "/mode/set" +topic_sub_doinit = b"" + config['maintopic'] + "/doinit" +topic_sub_restart = b"" + config['maintopic'] + "/restart" +topic_sub_watchdog = b"" + config['maintopic'] + "/watchdog" +topics = [topic_sub_setp, topic_sub_state, topic_sub_doinit, topic_sub_fanmode, topic_sub_mode, topic_sub_swingmode, topic_sub_restart, topic_sub_watchdog] + +def int_to_signed(intval): + if intval > 127: + return (256-intval) * (-1) + else: + return intval + +#mqtt stuff +def sub_cb(topic, msg, retained): + global power_state + runwrite = True + hpfuncs.logprint(str(topic) + " -- " + str(msg)) +################################################ +#setpoint + if topic == topic_sub_setp: + try: + values = hpfuncs.setpointVal(int(float(msg))) + except Exception as e: + hpfuncs.logprint(e) + runwrite = False +################################################ +#restart + if topic == topic_sub_restart: + try: + machine.reset() + except Exception as e: + hpfuncs.logprint(e) + runwrite = False +################################################ +# state + elif topic == topic_sub_state: + try: + values = hpfuncs.stateControl(msg) + if values == False: + runwrite = False + except Exception as e: + hpfuncs.logprint(e) + runwrite = False +################################################ +# swingstate + elif topic == topic_sub_swingmode: + try: + values = hpfuncs.swingControl(msg) + if values == False: + runwrite = False + except Exception as e: + hpfuncs.logprint(e) + runwrite = False +################################################ +# mode + elif topic == topic_sub_mode: + try: + values = hpfuncs.modeControl(msg) + if values == False: + runwrite = False + except Exception as e: + hpfuncs.logprint(e) + runwrite = False +################################################ +# fanmode + elif topic == topic_sub_fanmode: + try: + values = hpfuncs.fanControl(msg) + if values == False: + runwrite = False + except Exception as e: + hpfuncs.logprint(e) + runwrite = False +################################################ +# do init + elif topic == topic_sub_doinit: + myvals = hpfuncs.queryall() + hpfuncs.logprint("initial read") + for i in myvals: + uart.write(bytearray(i)) + sleep(0.2) + hpfuncs.logprint("initial read done") + runwrite = False + +################################################ +# do watchdog + elif topic == topic_sub_watchdog: + myvals = hpfuncs.watchdog() + for i in myvals: + uart.write(bytearray(i)) + sleep(0.2) + runwrite = False +################################################ + if runwrite == True and values != False: + #print(values) + for i in values: + hpfuncs.logprint("writing: " + str(i)) + uart.write(bytearray(i)) + sleep(0.2) + + +def chunkifyarray(vals): + val_length = len(vals) + start = 0 + rest_size = val_length + myresult = [] + while rest_size > 14: + lengde= int(vals[start+6]) + chunk_size = lengde + 8 + chunk_end = start + int(vals[start+6]) + 8 + myresult.append(vals[start:chunk_end]) + start = (start + chunk_size) + rest_size = rest_size - chunk_size + return myresult + + +# subscribe to topics +async def conn_han(client): + for i in topics: + await client.subscribe(i,1) + +# first run to collect values and run watchdog +async def firstrun(client): + firstrun = False + await asyncio.sleep(10) + if firstrun == False: + ota = OTAUpdater(config['your_repo']) + current_version = ota.get_version('/main/') + await client.publish(config['maintopic'] + '/doinit', "firstrun version " + current_version ) + hpfuncs.logprint("init firstrun version " + current_version) + firstrun = True + while True: + await asyncio.sleep(60) + await client.publish(config['maintopic'] + '/watchdog', "get") + hpfuncs.logprint("running watchdog..") + +async def receiver(client): + global power_state + + sreader = asyncio.StreamReader(uart) + try: + while True: + serdata = await sreader.read(2048) + if serdata is not None: + readable = list() + for i in serdata: + readable.append(str(int(i))) + hpfuncs.logprint("length of data: " + str(len(readable))) + chunks = chunkifyarray(readable) + for data in chunks: + hpfuncs.logprint(data) + await client.publish(config['maintopic'] + '/debug/fullstring', str(data)) + if len(data) == 17: + if(str(data[14]) == "187"): + roomtemp = int_to_signed(int(data[15])) + await client.publish(config['maintopic'] + '/roomtemp', str(roomtemp), qos=1) + if(str(data[14]) == "179"): + setpoint = int(data[15]) + await client.publish(config['maintopic'] + '/setpoint/state', str(setpoint), qos=1) + if(str(data[14]) == "128"): + state = hpfuncs.inttostate[int(data[15])] + power_state = state + await client.publish(config['maintopic'] + '/state/state', str(state), qos=1) + if (state == "OFF"): + # when power state is OFF, sent unit mode also as "off" + await client.publish(config['maintopic'] + '/mode/state', "off", qos=1) + if(str(data[14]) == "160"): + fanmode = hpfuncs.inttofanmode[int(data[15])] + await client.publish(config['maintopic'] + '/fanmode/state', str(fanmode), qos=1) + if(str(data[14]) == "163"): + swingmode = hpfuncs.inttoswing[int(data[15])] + await client.publish(config['maintopic'] + '/swingmode/state', str(swingmode), qos=1) + if(str(data[14]) == "176"): + mode = hpfuncs.inttomode[int(data[15])] + # report actual mode when unit is running or "off" when it's not + reportedState = str(mode) if (power_state == "ON") else "off" + await client.publish(config['maintopic'] + '/mode/state', reportedState, qos=1) + if(str(data[14]) == "190"): + outdoortemp = int_to_signed(int(data[15])) + await client.publish(config['maintopic'] + '/outdoortemp', str(outdoortemp), qos=1) + elif len(data) == 15: + if(str(data[12]) == "187"): + roomtemp = int_to_signed(int(data[13])) + await client.publish(config['maintopic'] + '/roomtemp', str(roomtemp), qos=1) + if(str(data[12]) == "179"): + setpoint = int(data[13]) + await client.publish(config['maintopic'] + '/setpoint/state', str(setpoint), qos=1) + if(str(data[12]) == "128"): + state = hpfuncs.inttostate[int(data[13])] + power_state = state + await client.publish(config['maintopic'] + '/state/state', str(state), qos=1) + if (state == "OFF"): + # when power state is OFF, sent unit mode also as "off" + await client.publish(config['maintopic'] + '/mode/state', "off", qos=1) + if(str(data[12]) == "160"): + fanmode = hpfuncs.inttofanmode[int(data[13])] + await client.publish(config['maintopic'] + '/fanmode/state', str(fanmode), qos=1) + if(str(data[12]) == "163"): + swingmode = hpfuncs.inttoswing[int(data[13])] + await client.publish(config['maintopic'] + '/swingmode/state', str(swingmode), qos=1) + if(str(data[12]) == "176"): + mode = hpfuncs.inttomode[int(data[13])] + # report actual mode when unit is running or "off" when it's not + reportedState = str(mode) if (power_state == "ON") else "off" + await client.publish(config['maintopic'] + '/mode/state', reportedState, qos=1) + if(str(data[12]) == "190"): + outdoortemp = int_to_signed(int(data[13])) + await client.publish(config['maintopic'] + '/outdoortemp', str(outdoortemp), qos=1) + + except Exception as e: + hpfuncs.logprint(e) + + +async def mainloop(client): + await client.connect() + +config['subs_cb'] = sub_cb +config['connect_coro'] = conn_han +#config['server'] = SERVER +MQTTClient.DEBUG = True +client = MQTTClient(config) + + +loop = asyncio.get_event_loop() +loop.create_task(mainloop(client)) +loop.create_task(receiver(client)) +loop.create_task(firstrun(client)) +loop.run_forever() + + + diff --git a/main/hpfuncs.py b/main/hpfuncs.py index 8b80882..80174ac 100644 --- a/main/hpfuncs.py +++ b/main/hpfuncs.py @@ -1,134 +1,147 @@ -from time import sleep -from machine import RTC -import ntptime -try: - ntptime.settime() -except Exception as e: - print(e) - -modetoint = {"auto":65, "cool":66, "heat":67, "dry":68, "fan_only":69} -inttomode = dict(map(reversed, modetoint.items())) - -fanmodetoint = {"quiet":49, "lvl_1": 50, "lvl_2":51, "lvl_3":52, "lvl_4":53, "lvl_5":54, "auto":65} -inttofanmode = dict(map(reversed, fanmodetoint.items())) - -swingtoint = {"off": 49, "on":65, "on-h":66, "on-vh":67} -inttoswing = dict(map(reversed, swingtoint.items())) - -statetoint = {"on":48, "off":49} -inttostate = dict(map(reversed, statetoint.items())) - -def checksum(msg,function): - numb = 434 - msg - function - if numb > 256: - retval = numb - 256 - else: - retval = numb - return retval - - -def logprint(msg): - rtc = RTC() - t = rtc.datetime() - #(2020, 4, 22, 2, 8, 43, 38, 88387) - # yyyy, m, dd, ?, h, mm, ss, ms - timestamp = str(t[2]) + "-" + str(t[1]) + "-" + str(t[0]) + " " + str(t[4]) + ":" + str(t[5]) + ":" + str(t[6]) + "." + str(t[7]) - result = str(timestamp) + " -> " + str(msg) - print(result) - - - -def swingControl(msg): - function_code = 163 - message = msg.decode("utf-8") - - try: - function_value = swingtoint[message] - control_code = checksum(function_value,function_code) - mylist = (2,0,3,16,0,0,7,1,48,1,0,2,function_code,function_value,control_code) - getlist = (2,0,3,16,0,0,6,1,48,1,0,1,function_code,17) - myvalues = (mylist, getlist) - except Exception as e: - myvalues = False - return myvalues - -def modeControl(msg): - function_code = 176 - message = msg.decode("utf-8") - try: - function_value = modetoint[message] - control_code = checksum(function_value,function_code) - mylist = (2,0,3,16,0,0,7,1,48,1,0,2,function_code,function_value,control_code) - getlist = (2,0,3,16,0,0,6,1,48,1,0,1,function_code,4) - myvalues = (mylist, getlist) - except Exception as e: - myvalues = False - return myvalues - -def fanControl(msg): - function_code = 160 - message = msg.decode("utf-8") - try: - function_value = fanmodetoint[message] - control_code = checksum(function_value,function_code) - mylist = (2,0,3,16,0,0,7,1,48,1,0,2,function_code,function_value,control_code) - getlist = (2,0,3,16,0,0,6,1,48,1,0,1,function_code,20) - myvalues = (mylist, getlist) - except Exception as e: - myvalues = False - return myvalues - - - -def stateControl(msg): - function_code = 128 - message = msg.decode("utf-8") - try: - function_value = statetoint[message] - control_code = checksum(function_value,function_code) - mylist = (2,0,3,16,0,0,7,1,48,1,0,2,function_code,function_value,control_code) - getlist = (2,0,3,16,0,0,6,1,48,1,0,1,function_code,52) - myvalues = (mylist, getlist) - except Exception as e: - myvalues = False - return myvalues - -def setpointVal(msg): - function_code = 179 - try: - function_value = int(msg) - control_code = checksum(function_value,function_code) - mylist = (2,0,3,16,0,0,7,1,48,1,0,2,function_code,function_value,control_code) - getlist = (2,0,3,16,0,0,6,1,48,1,0,1,function_code,1) - myvalues = (mylist, getlist) - except Exception as e: - myvalues = False - return myvalues - - -def queryall(): - bootlist = [] - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,128,52)) - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,176,4)) - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,179,1)) - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,160,20)) - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,135,45)) - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,163,17)) - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,187,249)) - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,190,246)) - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,203,233)) - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,136,44)) - #bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,134,46)) - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,144,36)) - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,148,32)) - return bootlist - - -def watchdog(): - bootlist = [] - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,187,249)) - bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,190,246)) - return bootlist - - - +from time import sleep +from machine import RTC +import ntptime +try: + ntptime.settime() +except Exception as e: + print(e) + +modetoint = {"off": 49, "auto":65, "cool":66, "heat":67, "dry":68, "fan_only":69} +inttomode = dict(map(reversed, modetoint.items())) + +fanmodetoint = {"quiet":49, "lvl_1": 50, "lvl_2":51, "lvl_3":52, "lvl_4":53, "lvl_5":54, "auto":65} +inttofanmode = dict(map(reversed, fanmodetoint.items())) + +swingtoint = {"off": 49, "on":65, "on-h":66, "on-vh":67} +inttoswing = dict(map(reversed, swingtoint.items())) + +statetoint = {"ON":48, "OFF":49} +inttostate = dict(map(reversed, statetoint.items())) + +def checksum(msg,function): + numb = 434 - msg - function + if numb > 256: + retval = numb - 256 + else: + retval = numb + return retval + + +def logprint(msg): + rtc = RTC() + t = rtc.datetime() + #(2020, 4, 22, 2, 8, 43, 38, 88387) + # yyyy, m, dd, ?, h, mm, ss, ms + timestamp = str(t[2]) + "-" + str(t[1]) + "-" + str(t[0]) + " " + str(t[4]) + ":" + str(t[5]) + ":" + str(t[6]) + "." + str(t[7]) + result = str(timestamp) + " -> " + str(msg) + print(result) + + + +def swingControl(msg): + function_code = 163 + message = msg.decode("utf-8") + + try: + function_value = swingtoint[message] + control_code = checksum(function_value,function_code) + mylist = (2,0,3,16,0,0,7,1,48,1,0,2,function_code,function_value,control_code) + getlist = (2,0,3,16,0,0,6,1,48,1,0,1,function_code,17) + myvalues = (mylist, getlist) + except Exception as e: + myvalues = False + return myvalues + +def modeControl(msg): + function_code = 176 + message = msg.decode("utf-8") + try: + function_value = modetoint[message] + + control_code = checksum(48,128) + pwr_on_mylist = (2,0,3,16,0,0,7,1,48,1,0,2,128,48,control_code) + pwr_on_getlist = (2,0,3,16,0,0,6,1,48,1,0,1,128,52) + + control_code = checksum(function_value,function_code) + mylist = (2,0,3,16,0,0,7,1,48,1,0,2,function_code,function_value,control_code) + getlist = (2,0,3,16,0,0,6,1,48,1,0,1,function_code,4) + + myvalues = (pwr_on_mylist, pwr_on_getlist, mylist, getlist) + + if function_value == 49: + control_code = checksum(49,128) + mylist = (2,0,3,16,0,0,7,1,48,1,0,2,128,49,control_code) + getlist = (2,0,3,16,0,0,6,1,48,1,0,1,128,52) + myvalues = (mylist, getlist) + + except Exception as e: + myvalues = False + return myvalues + +def fanControl(msg): + function_code = 160 + message = msg.decode("utf-8") + try: + function_value = fanmodetoint[message] + control_code = checksum(function_value,function_code) + mylist = (2,0,3,16,0,0,7,1,48,1,0,2,function_code,function_value,control_code) + getlist = (2,0,3,16,0,0,6,1,48,1,0,1,function_code,20) + myvalues = (mylist, getlist) + except Exception as e: + myvalues = False + return myvalues + + + +def stateControl(msg): + function_code = 128 + message = msg.decode("utf-8") + try: + function_value = statetoint[message] + control_code = checksum(function_value,function_code) + mylist = (2,0,3,16,0,0,7,1,48,1,0,2,function_code,function_value,control_code) + getlist = (2,0,3,16,0,0,6,1,48,1,0,1,function_code,52) + myvalues = (mylist, getlist) + except Exception as e: + myvalues = False + return myvalues + +def setpointVal(msg): + function_code = 179 + try: + function_value = int(msg) + control_code = checksum(function_value,function_code) + mylist = (2,0,3,16,0,0,7,1,48,1,0,2,function_code,function_value,control_code) + getlist = (2,0,3,16,0,0,6,1,48,1,0,1,function_code,1) + myvalues = (mylist, getlist) + except Exception as e: + myvalues = False + return myvalues + + +def queryall(): + bootlist = [] + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,128,52)) + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,176,4)) + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,179,1)) + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,160,20)) + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,135,45)) + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,163,17)) + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,187,249)) + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,190,246)) + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,203,233)) + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,136,44)) + #bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,134,46)) + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,144,36)) + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,148,32)) + return bootlist + + +def watchdog(): + bootlist = [] + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,187,249)) + bootlist.append((2,0,3,16,0,0,6,1,48,1,0,1,190,246)) + return bootlist + + + From 9f669b8d99c9cb843645454c67c2bec3a70115f0 Mon Sep 17 00:00:00 2001 From: zz04303 <44981174+zz04303@users.noreply.github.com> Date: Mon, 13 May 2024 21:08:34 +0200 Subject: [PATCH 6/9] watchdog payload changed to timestamp --- main/heatpump.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/main/heatpump.py b/main/heatpump.py index 3694fa5..eee7990 100644 --- a/main/heatpump.py +++ b/main/heatpump.py @@ -1,6 +1,7 @@ from main import hpfuncs from main.ota_updater import OTAUpdater from machine import UART +from machine import RTC global uart uart = UART(1, 9600) @@ -156,7 +157,12 @@ async def firstrun(client): firstrun = True while True: await asyncio.sleep(60) - await client.publish(config['maintopic'] + '/watchdog', "get") + rtc = RTC() + t = rtc.datetime() + #(2020, 4, 22, 2, 8, 43, 38, 88387) + # yyyy, m, dd, ?, h, mm, ss, ms + timestamp = str(t[2]) + "-" + str(t[1]) + "-" + str(t[0]) + " " + str(t[4]) + ":" + str(t[5]) + ":" + str(t[6]) + "." + str(t[7]) + await client.publish(config['maintopic'] + '/watchdog', timestamp) hpfuncs.logprint("running watchdog..") async def receiver(client): @@ -254,3 +260,4 @@ async def mainloop(client): + From f776c0baf9e47977912c171998f9b0d7fce42eec Mon Sep 17 00:00:00 2001 From: zz04303 <44981174+zz04303@users.noreply.github.com> Date: Tue, 14 May 2024 21:03:15 +0200 Subject: [PATCH 7/9] force reboot publish 'reboot' at topic 'doinit' --- main/heatpump.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/main/heatpump.py b/main/heatpump.py index eee7990..7d5030b 100644 --- a/main/heatpump.py +++ b/main/heatpump.py @@ -100,6 +100,10 @@ def sub_cb(topic, msg, retained): ################################################ # do init elif topic == topic_sub_doinit: + hpfuncs.logprint(str(msg)) + if msg == b'reboot': + hpfuncs.logprint("rebooting") + machine.reset() myvals = hpfuncs.queryall() hpfuncs.logprint("initial read") for i in myvals: @@ -261,3 +265,4 @@ async def mainloop(client): + From 176ebf9456213ed68ff21ec2e094f0e41b468cb2 Mon Sep 17 00:00:00 2001 From: zz04303 <44981174+zz04303@users.noreply.github.com> Date: Tue, 14 May 2024 21:06:39 +0200 Subject: [PATCH 8/9] remove force reboot publish 'reboot' at topic 'doinit' --- main/heatpump.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/main/heatpump.py b/main/heatpump.py index 7d5030b..061599e 100644 --- a/main/heatpump.py +++ b/main/heatpump.py @@ -100,10 +100,6 @@ def sub_cb(topic, msg, retained): ################################################ # do init elif topic == topic_sub_doinit: - hpfuncs.logprint(str(msg)) - if msg == b'reboot': - hpfuncs.logprint("rebooting") - machine.reset() myvals = hpfuncs.queryall() hpfuncs.logprint("initial read") for i in myvals: From 4b9ef5faaf91e31723762a0098ffa9f2bcfd604b Mon Sep 17 00:00:00 2001 From: zz04303 <44981174+zz04303@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:54:58 +0200 Subject: [PATCH 9/9] Update heatpump.py change to EPOCH watchdog time --- main/heatpump.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/main/heatpump.py b/main/heatpump.py index 061599e..e17aa29 100644 --- a/main/heatpump.py +++ b/main/heatpump.py @@ -1,7 +1,6 @@ from main import hpfuncs from main.ota_updater import OTAUpdater from machine import UART -from machine import RTC global uart uart = UART(1, 9600) @@ -157,11 +156,7 @@ async def firstrun(client): firstrun = True while True: await asyncio.sleep(60) - rtc = RTC() - t = rtc.datetime() - #(2020, 4, 22, 2, 8, 43, 38, 88387) - # yyyy, m, dd, ?, h, mm, ss, ms - timestamp = str(t[2]) + "-" + str(t[1]) + "-" + str(t[0]) + " " + str(t[4]) + ":" + str(t[5]) + ":" + str(t[6]) + "." + str(t[7]) + timestamp = str(time.time()+946684800) await client.publish(config['maintopic'] + '/watchdog', timestamp) hpfuncs.logprint("running watchdog..")