diff --git a/berry/nextion.be b/berry/nextion.be index 5ad8e8f..8235948 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 = bytes().fromstring("PS") var flash_mode var ser @@ -14,6 +16,35 @@ 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 + 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) + 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') @@ -24,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) @@ -40,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 @@ -80,6 +121,7 @@ class Nextion : Driver def screeninit() log("NSP: Screen Initialized") + tasmota.publish_result("{\"Init\": \"true\"", "RESULT") end def every_100ms() @@ -100,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") @@ -111,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 @@ -126,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); }