From 955397da666bcc52b4b8726c99b6eb906b9deb7f Mon Sep 17 00:00:00 2001 From: Michael Andreasen Date: Fri, 21 Jan 2022 16:32:28 +0000 Subject: [PATCH 1/2] --- berry/nextion.be | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/berry/nextion.be b/berry/nextion.be index 5ad8e8f..8cf6293 100644 --- a/berry/nextion.be +++ b/berry/nextion.be @@ -1,10 +1,12 @@ -# Sonoff NSPanel Tasmota (Nextion with Flashing) driver v0.02 | code by peepshow-21 +# Sonoff NSPanel Tasmota (Nextion with Flashing) driver v0.03 | code by peepshow-21 # based on; # Sonoff NSPanel Tasmota driver v0.47 | code by blakadder and s-hadinger + class Nextion : Driver static CHUNK_FILE = "nextion" + static header = "PS" var flash_mode var ser @@ -14,6 +16,34 @@ class Nextion : Driver var tot_read var last_per + def crc16(data, poly) + if !poly poly = 0xA001 end + # CRC-16 MODBUS HASHING ALGORITHM + var crc = 0xFFFF + for i:0..size(data)-1 + crc = crc ^ data[i] + for j:0..7 + if crc & 1 + crc = (crc >> 1) ^ poly + else + crc = crc >> 1 + end + end + end + return crc + end + + def encode(payload) + var b = bytes() + b += self.header + b.add(nsp_type) # add a single byte + b.add(size(payload), 2) # add size as 2 bytes, little endian + b += bytes().fromstring(payload) + var msg_crc = self.crc16(b) + b.add(msg_crc, 2) # crc 2 bytes, little endian + return b + end + def encodenx(payload) var b = bytes().fromstring(payload) b += bytes('FFFFFF') From d0fefe2c8b4c39c96bba64107275bff9088c2332 Mon Sep 17 00:00:00 2001 From: Michael Andreasen Date: Mon, 24 Jan 2022 13:04:51 +0000 Subject: [PATCH 2/2] --- berry/nextion.be | 71 ++++++++++++++++--- .../proto/nsflash/MakeChunksFrame.java | 2 + 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/berry/nextion.be b/berry/nextion.be index 8cf6293..8235948 100644 --- a/berry/nextion.be +++ b/berry/nextion.be @@ -6,7 +6,7 @@ class Nextion : Driver static CHUNK_FILE = "nextion" - static header = "PS" + static header = bytes().fromstring("PS") var flash_mode var ser @@ -36,6 +36,7 @@ class Nextion : Driver def encode(payload) var b = bytes() b += self.header + var nsp_type = 0 # not used b.add(nsp_type) # add a single byte b.add(size(payload), 2) # add size as 2 bytes, little endian b += bytes().fromstring(payload) @@ -54,7 +55,17 @@ class Nextion : Driver import string var payload_bin = self.encodenx(payload) self.ser.write(payload_bin) - log(string.format("NSP: Nextion command sent = %s",str(payload_bin)), 3) + log(string.format("NSP: Nextion command sent = %s",str(payload_bin)), 3) + end + + def send(payload) + var payload_bin = self.encode(payload) + if self.flash_mode==1 + log("NSP: skipped command becuase still flashing", 3) + else + self.ser.write(payload_bin) + log("NSP: payload sent = " + str(payload_bin), 3) + end end def getPage(url) @@ -70,7 +81,7 @@ class Nextion : Driver else s = nil retry = retry + 1 - log("NSP: HTTP retry reuired") + log("NSP: HTTP retry required") end wc.close() end @@ -110,6 +121,7 @@ class Nextion : Driver def screeninit() log("NSP: Screen Initialized") + tasmota.publish_result("{\"Init\": \"true\"", "RESULT") end def every_100ms() @@ -130,7 +142,7 @@ class Nextion : Driver var per = (self.tot_read*100)/self.flash_size if (self.last_per!=per) self.last_per = per - tasmota.publish_result(string.format("{\"NSPanel\":{\"Flashing\":{\"complete\": %d}}}",per), "RESULT") + tasmota.publish_result(string.format("{\"Flashing\":{\"complete\": %d}}",per), "RESULT") end if (self.tot_read==self.flash_size) log("NSP: Flashing complete") @@ -141,13 +153,13 @@ class Nextion : Driver else if msg == bytes('000000FFFFFF88FFFFFF') self.screeninit() - elif msg[0]==0x4A # J - var jm = string.format("{\"NSPanel\":{\"JSON\":\"%s\"}}",msg[1..-1].asstring()) + elif msg[0]==0x7B + var jm = string.format("{\"json\":%s}",msg[0..-1].asstring()) tasmota.publish_result(jm, "RESULT") - elif msg[0]==0x54 # T + elif msg[0]==0x07 # T tasmota.cmd("buzzer 1,1") else - var jm = string.format("{\"NSPanel\":{\"Nextion\":\"%s\"}}",str(msg[0..-4])) + var jm = string.format("{\"nextion\":\"%s\"}",str(msg[0..-4])) tasmota.publish_result(jm, "RESULT") end end @@ -156,6 +168,7 @@ class Nextion : Driver end def begin_file_flash() + self.flash_mode = 1 var f = open("test.bin","w") f.close() while self.tot_read nextion.set_power()) +tasmota.add_rule("power2#state", /-> nextion.set_power()) +tasmota.cmd("Rule3 1") # needed until Berry bug fixed +tasmota.add_rule("Time#Minute", /-> nextion.set_clock()) +tasmota.cmd("State") + + diff --git a/src/systems/proto/nsflash/MakeChunksFrame.java b/src/systems/proto/nsflash/MakeChunksFrame.java index 7794b76..7b714a0 100644 --- a/src/systems/proto/nsflash/MakeChunksFrame.java +++ b/src/systems/proto/nsflash/MakeChunksFrame.java @@ -83,6 +83,8 @@ private void build() { props.setProperty(PROP_FILE, tftFile.getAbsolutePath()); props.setProperty(PROP_DIR, chunkDir.getAbsolutePath()); props.store(new FileWriter(propsFile), null); + outFile.close(); + is.close(); } catch (Exception ex) { Logger.getLogger(MakeChunksFrame.class.getName()).log(Level.SEVERE, null, ex); }