Xiaomi Mijia example

If doesn't fit into any other category ....
Post Reply
User avatar
Electroguard
Posts: 277
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 55 times
Been thanked: 129 times

Xiaomi Mijia example

Post by Electroguard »

Francesco, I finally got around to flashing a Xiaomi Mijia sensor with the custom firmware earlier, then trying it with your BLE example... and am pleased to say that it works great.
Except the script didn't seem to keep running very long.

So I tried disabling WDT, but it still kept stopping without error after a while.

I modded the example to WLOG (instead of PRINT) each received set of parameters (normally every 10 secs) plus the time received (time from startup, not internet time, cos is on an isolated subnet), and RAMFREE (to spot any memory leakage).
It only ran for about 38 minutes, then just stopped without any error message or memory loss (see enclosed screendump).

This is running on your PCB with TFT touchscreen BTW, which normally runs a TFT gui thermostat script I'm developing for days without having problems.
Have you had problems on your Liligo Epaper esp32 ? (perhaps its PSRAM makes a difference)

Edit - I've just modded the script to show on the TFT rather than wlog the output, so maybe that will keep it running longer.

Update - ran all night writing to TFT, so the problem must have been caused by using WLOG even though no ERROR and plenty of RAMFREE.

BLE.jpg
You do not have the required permissions to view the files attached to this post.
cicciocb
Site Admin
Posts: 414
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 92 times
Been thanked: 297 times
Contact:

Re: Xiaomi Mijia example

Post by cicciocb »

Hi Robin,
I've seen the problem so I'm trying to fix it
User avatar
Electroguard
Posts: 277
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 55 times
Been thanked: 129 times

Re: Xiaomi Mijia example

Post by Electroguard »

That is good news Francesco, cos the gizmo's are so good that I had already ordered another 2 (making 5 in total).
But my development script keeps rebooting randomly regardless of whether output is to TFT or html webpage... so it is not specifically a TFT problem or a 'webby' problem.

Occasionally it keeps running overnight (either to TFT or web page), but more usually the device reboots after a few minutes or even a few seconds.
And sometimes the device disconnects from the browser, but the TFT display keeps going for several hours, then the device eventually reboots.
Is not memory leakage though, so perhaps is an unwanted interaction between ble and wifi.

Sometimes it is possible to reconnect the browser to the device, but often the device must be rebooted before it can reconnect.
Sometimes I've closed down the Editor window so their is no browser connection, but still the device randomly reboots after hours or even minutes.
And usually when I try to reconnect the browser with the device still running ok and updating the TFT every 10 secs, the browser will not reconnect until after a device reboot.

I've got my fingers crossed.

PS. It's a bit hacked around by now, but this is what I've been using:

Code: [Local Link Removed for Guests]

option.wdt 0
debuglevel=0  '0=don't wlog BLE devices (normal), 2,3,4 wlog various debug info
sensors$="ATC_9AFEA8.Data=Inside ATC_BEF7AB.Data=Outside abc=Freezer"
records=0
web=1       '0=disable webpage output, 1=enable webpage output
tft=1       '0=disable tft display output, 1=enable tft display output
ram=0
'---- User variables are above 
sensors=word.count(sensors$," ")
wlog "Looking for "+str$(sensors)+" sensors"
dim info$(sensors): dim id$(sensors): dim name$(sensors): dim temp$(sensors): dim hum$(sensors): dim batt$(sensors): dim cnt$(sensors)
if records=1 then dim coldest$(sensors): dim whencoldest$(sensors): dim warmest$(sensors): dim whenwarmest$(sensors) 
for sensor=1 to sensors
 info$ = trim$(word$(sensors$,sensor," "))
 if debuglevel=4 then wlog info$
 id$(sensor) = word$(info$,1,"=")
 name$(sensor) = word$(info$,2,"=")
 temp$(sensor) = str$(sensor)
 hum$(sensor) = "xx"
 batt$(sensor) = "xx"
 cnt$(sensor) = ""
 if records=1 then 
  coldest$(sensor) = "xx.x": whencoldest$(sensor) = " at hh:mm:ss on dd/mm/yy" 
  warmest$(sensor) = "xx.x": whenwarmest$(sensor) = " at hh:mm:ss on dd/mm/yy"  
  coldest$(sensor) = "": whencoldest$(sensor) = "" 
  warmest$(sensor) = "": whenwarmest$(sensor) = ""  
 endif
next sensor
t$=time$
if web=1 then onhtmlreload webpage: onhtmlchange webpage: gosub webpage

if tft=1 then gosub tftpage
onbluetooth ble_receive
bluetooth.scan 10
timer0 1000,clock
wait


clock:
t$=time$
'if web=1 then refresh 
if tft=1 then gui.settext clk, t$
return

webpage:
cls
autorefresh 1000
a$=|<br><br>|
a$=a$ + |<div style='display: table;margin-right:auto;margin-left:auto;text-align:center;'>|
a$=a$ + textbox$(t$,"tim") + "<br>"
a$=a$ + cssid$("tim","width:70;text-align:center;")
a$=a$ + |<table id='sensors' align='center' style="background:AliceBlue; border:.5px solid darkgrey; border-collapse: collapse; font-size: 90%;"|
s1$=info$(1): a$=a$ + "<tr><td>" + textbox$(s1$,"sen") + "</td></tr>"
if sensors >= 2 then s2$=info$(2): a$=a$ + "<tr><td>" + textbox$(s2$,"sen") + "</td></tr>"
if sensors >= 3 then s3$=info$(3): a$=a$ + "<tr><td>" + textbox$(s3$,"sen") + "</td></tr>"
a$=a$ + cssid$("sen","width:250;text-align:center;")
a$ = a$ + "</table></div>"
html a$
return

tftpage:
gui.init 6, 0 
gui.autorefresh 100, 1    
if ram=1 then gui.textline(0,2,320, 30, str$(ramfree),3,cyan,black,0,5,10)
clk=gui.textline(0,2,220, 30, time$,3,white,black,0,5,10)
s1=gui.textline(0,50,320, 30, name$(1),3,black,cyan,white,4,10)
if sensors >= 2 then s2=gui.textline(0,100,320, 30, name$(2),3,black,cyan,white,4,10)
if sensors >= 3 then s3=gui.textline(0,150,320, 30, name$(3),3,black,cyan,white,4,10)
if ram=1 then mem=gui.textline(0,200,320, 30, str$(ramfree),3,yellow,black,0,5,10)
return

showit:
if tft=1 then
 if sensor=1 then gui.settext s1,info$(1)
 if sensor=2 then gui.settext s2,info$(2)
 if sensor=3 then gui.settext s3,info$(3)
 if ram=1 then gui.settext mem,str$(ramfree)
endif
if web=1 then 
 t$=time$
 s1$ = info$(1)
 s2$ = info$(2)
 s3$ = info$(3)
endif
if records=1 then
 if (val(temp$(sensor)) < val(coldest$(sensor))) or (coldest$(sensor) = "") then
  coldest$(sensor) = temp$(sensor): whencoldest$(sensor) = " at " + time$ + " on " + date$
 endif 
 if (val(temp$(sensor)) > val(warmest$(sensor))) or (warmest$(sensor) = "") then
  warmest$(sensor) = temp$(sensor): whenwarmest$(sensor) = " at " + time$ + " on " + date$
 endif 
endif
return

ble_receive:
select case bluetooth.status
case 1: wlog "BLE Device Connected"
case 2: wlog "BLE Device Disconnected"
case 3:
 bluetooth.read_iobuff(0)
 for z = 1 to iobuff.len(0)
  print iobuff.read(0, z-1),
 next z
case 4:
 print "scan terminated"
 a$ = bluetooth.scanresult$
 if debuglevel = 3 then wlog a$: end select: bluetooth.scan 10: return
 for sensor=1 to sensors  
  info$(sensor)=json$(a$, id$(sensor))
  if debuglevel = 1 then wlog "Sensor="; str$(sensor), info$(sensor) 
  if info$(sensor)  = "not found" then
   info$(sensor)=name$(sensor)+" not found"
  else
   a = iobuff.fromhex(0, info$(sensor))
   temp = iobuff.read(0, 6) * 256 + iobuff.read(0, 7)
   if (temp > 32767) then temp =  temp - 65536
   temp$(sensor) = str$(temp/10)
   hum$(sensor) =  str$(iobuff.read(0, 8))
   batt$(sensor) = str$(iobuff.read(0, 9))
   cnt$(sensor) =  str$(iobuff.read(0,12))
   info$(sensor) = name$(sensor)+": t="+temp$(sensor)+" h="+hum$(sensor)+" b="+batt$(sensor)'+" "+time$
   if records=1 then
    if (val(temp$(sensor)) < val(coldest$(sensor))) or (coldest$(sensor) = "") then
     coldest$(sensor) = temp$(sensor): whencoldest$(sensor) = " at " + time$ + " on " + date$
    endif 
    if (val(temp$(sensor)) > val(warmest$(sensor))) or (warmest$(sensor) = "") then
     warmest$(sensor) = temp$(sensor): whenwarmest$(sensor) = " at " + time$ + " on " + date$
    endif 
   endif
  endif
  if debuglevel = 2 then wlog "sensor"+str$(sensor)+" "+info$(sensor)
  gosub showit
 next sensor
 bluetooth.scan 10 
end select
return
cicciocb
Site Admin
Posts: 414
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 92 times
Been thanked: 297 times
Contact:

Re: Xiaomi Mijia example

Post by cicciocb »

HI Robin,
I worked recently on a new release based on the latest esp32-arduino SDK.
This release contains many fixes and improvements but several changes that required many modifications / adaptations to work.
In parallel I fixed some snags into some libraries that was causing the module to reboot.
This new release looks more stable and shows more RAM available.
I was able to make it work for almost all except for the CAN bus and the image recognition.
I did not had the time to check all the functionalities of the module, I focused in particular on the bluetooth / telegram / mp3 stuff that works well.

So, this release should be considered as a version to test but, I'm quite sure that should be OK for your program.

I'm posting this release in the "Software Updates" area.
User avatar
Electroguard
Posts: 277
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 55 times
Been thanked: 129 times

Re: Xiaomi Mijia example

Post by Electroguard »

Well done sir, I will give it a try.

I think I may have found a way around the rebooting problem anyway - by using wifi.sleep... I'm still doing soak testing, but it hasn't rebooted since.

Instead of using bluetooth.scan at the bottom of the onbluetooth ble_receive, I'm branching to gosub mywify, which does wifi.awake then sends the gizmo data via udp before doing wifi.sleep again, then bluetooth scan and wait.
So wifi is not active during bluetooth receives.

Is it possible for bluetooth to be similarly put to sleep and awakened while wifi is in use ?
cicciocb
Site Admin
Posts: 414
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 92 times
Been thanked: 297 times
Contact:

Re: Xiaomi Mijia example

Post by cicciocb »

Try the new release before
User avatar
Electroguard
Posts: 277
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 55 times
Been thanked: 129 times

Re: Xiaomi Mijia example

Post by Electroguard »

A big attraction of the gizmo custom firmware is ability to broadcast to multiple devices rather than only pairing with just one.
So I'm currently running the 2 TFT pcbs side by side, one still soak-testing on 1.43.5, the other on the latest 1.43.6... will see what's still running tomorrow morning
The update seems much more responsive BTW.
Post Reply