Skip to content

Commit

Permalink
Merge pull request #1 from peepshow-21/version-2
Browse files Browse the repository at this point in the history
Version 2
  • Loading branch information
peepshow-21 authored Jan 24, 2022
2 parents 0b91475 + d0fefe2 commit d707b85
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 9 deletions.
101 changes: 92 additions & 9 deletions berry/nextion.be
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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')
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -80,6 +121,7 @@ class Nextion : Driver

def screeninit()
log("NSP: Screen Initialized")
tasmota.publish_result("{\"Init\": \"true\"", "RESULT")
end

def every_100ms()
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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<self.flash_size
Expand All @@ -137,12 +180,15 @@ class Nextion : Driver
end

def begin_nextion_flash()
self.sendnx('DRAKJHSUYDGBNCJHGJKSHBDN')
self.sendnx('recmod=0')
self.sendnx('recmod=0')
self.sendnx("connect")
self.flash_mode = 1
end

def start_flash(url)
self.last_per = -1
self.flash_mode = 1
self.chunk_url = url
import string
var file = (string.format("%s/%s.txt",self.chunk_url,self.CHUNK_FILE))
Expand All @@ -154,6 +200,29 @@ class Nextion : Driver
self.begin_nextion_flash()
end

def set_power()
var ps = tasmota.get_power()
for i:0..1
if ps[i] == true
ps[i] = "1"
else
ps[i] = "0"
end
end
var json_payload = '{ "switches": { "switch1": ' + ps[0] + ' , "switch2": ' + ps[1] + ' } }'
log('NSP: Switch state updated with ' + json_payload)
self.send(json_payload)
end

def set_clock()
var now = tasmota.rtc()
var time_raw = now['local']
var nsp_time = tasmota.time_dump(time_raw)
var time_payload = '{ "clock": { "date":' + str(nsp_time['day']) + ',"month":' + str(nsp_time['month']) + ',"year":' + str(nsp_time['year']) + ',"weekday":' + str(nsp_time['weekday']) + ',"hour":' + str(nsp_time['hour']) + ',"min":' + str(nsp_time['min']) + ' } }'
log('NSP: Time and date synced with ' + time_payload, 3)
self.send(time_payload)
end

end

var nextion = Nextion()
Expand All @@ -177,3 +246,17 @@ end

tasmota.add_cmd('Nextion', send_cmd)

def send_cmd2(cmd, idx, payload, payload_json)
nextion.send(payload)
tasmota.resp_cmnd_done()
end

tasmota.add_cmd('Screen', send_cmd2)

tasmota.add_rule("power1#state", /-> 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")


2 changes: 2 additions & 0 deletions src/systems/proto/nsflash/MakeChunksFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit d707b85

Please sign in to comment.