sonoffDualR3v2 - ESP32 and BL0939 power meter on board

All that relates to the H/W
Post Reply
MarioL
Posts: 21
Joined: Sun Mar 21, 2021 8:38 am
Has thanked: 256 times
Been thanked: 34 times

sonoffDualR3v2 - ESP32 and BL0939 power meter on board

Post by MarioL »

Hi,
I flashed the "Sonoff Dual R3 v2 pow" module with Annex32 CAN 1.51.6 LFS, the status of the inputs is monitored and the outputs are driven correctly according to following association.
'GPIO25 Serial Tx
'GPIO26 BL0939 Rx
'GPIO00 Button 1
'GPIO32 Switch 1 (input)
'GPIO33 Switch 2 (input)
'GPIO13 LedLink (blue)
'GPIO27 Relay 1 (output)
'GPIO14 Relay 2 (output)
Unfortunately I am not able to read data from BL0939 power meter. If anyone has relevant experience with BL0939, I would be grateful for code sharing.


salve,
Ho flashato il modulo "Sonoff Dual R3 v2 pow" con Annex32 CAN 1.51.6 LFS, lo stato degli ingressi si monitorano e le uscite si pilotano correttamente secondo la seguente associazione.
'GPIO25 Serial Tx
'GPIO26 BL0939 Rx
'GPIO00 Button 1
'GPIO32 Switch 1 (input)
'GPIO33 Switch 2 (input)
'GPIO13 LedLink (blue)
'GPIO27 Relay 1 (output)
'GPIO14 Relay 2 (output)
Purtroppo non riesco a leggere i dati del power meter BL0939. Se qualcuno ha già sperimentato questo dispositivo gli sarei grato per la condivisione del codice.
You do not have the required permissions to view the files attached to this post.
User avatar
Electroguard
Posts: 855
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 273 times
Been thanked: 321 times

Re: sonoffDualR3v2 - ESP32 and BL0939 power meter on board

Post by Electroguard »

Don't know about that specific device, but it is generally advisable to disconnect the serial lines before rebooting after flashing from a serial UART to prevent them affecting the gpio's or boot status.
RonS
Posts: 115
Joined: Thu Mar 02, 2023 10:15 pm
Location: germany
Has thanked: 64 times
Been thanked: 23 times

Re: sonoffDualR3v2 - ESP32 and BL0939 power meter on board

Post by RonS »

Good job! Sonoff is one of the most used and well-known modules for control and monitoring - I use some of them myself and can rate the good signal stability positively. However, I haven't flashed one with Annex yet - but I see a future in this experiment. That's why I googled and found the data sheet, maybe that helps to read out via I2C, for example

https://evelta.com/content/datasheets/3 ... SOP16L.pdf

have a nice day
Ron
Modules : 3xESP32-Cam MB (Chip"DM ESP32 S" ),AI-Thinker Audio Kit (ES8388), ESP32 Dev Kit with Display
MarioL
Posts: 21
Joined: Sun Mar 21, 2021 8:38 am
Has thanked: 256 times
Been thanked: 34 times

Re: sonoffDualR3v2 - ESP32 and BL0939 power meter on board

Post by MarioL »

Tanks very much Ron, Your link was very helpful, here's the result.



Code: [Local Link Removed for Guests]

'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'!                              !!! ATTENZIONE !!!
'IL DISPOSITIVO SI BRUCIA! se connetti gli ingressi S1 ed S2 a potenziale diverso da Lin, IL DISPOSITIVO SI BRUCIA!
'                                !!! ATTENTION !!!
'         DEVICE BURN! ifyou connect inputs S1 and S2 to different potential than Lin DEVICE BURN!
'                                !!!  PERICOLO !!!
' Non connettere contemporaneamente il programmatore e l'alimentazione di rete, PRESENTE TENSIONE DI RETE!
'                                !!!! CAUTION !!!!
'     AC LIVE! Do not connect AC power and the flasher connection at the same time, AC LIVE!
''
'!
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

' *************************************************************************************
' *****                                 File Info                                 *****
' *************************************************************************************
'    Filename: /sonoffDualR3v2+BL0939
'    Date    : 23/12/2023
'    Version : 1
'    Edit. by: MarioL.
'    Function: turn ON OFF out lines, monitor Input status  and monitor BL0939 measurement
'    Firmware: Annex32 CAN 1.51.6 LFS
'    Hardware: Sonoff Dual R3 v2 Power Monitoring Switch Module (DUALR3)
'    Doc.Ref.: Annex32 WIFI RDS Help Version 1.48.2, BL0939 datasheet
'    Note    :
' ***************************************************************************************

' sonoffDualR3v2 pow  GPIO  Connections
serTX = 25    'GPIO25  Serial Tx
BL0939rx = 26 'GPIO26  BL0939 Rx
Button1 = 0   'GPIO00  Button 1
Switch1 = 32  'GPIO32  Switch 1
Switch2 = 33  'GPIO33  Switch 2
ledPin = 13   'GPIO13  LedLinki
Relay1 = 27   'GPIO27  Relay 1
Relay2 = 14   'GPIO14  Relay 2

PIN.MODE Button1, INPUT
PIN.MODE Switch1, INPUT
PIN.MODE Switch2, INPUT
PIN.MODE ledPin, OUTPUT
PIN.MODE Relay1, OUTPUT
PIN.MODE Relay2, OUTPUT
serial2.mode 4800, serTX, BL0939rx ' set serial port #2 to 4800 pinTX, pinRX

Button1Stat = PIN(Button1)   'button1 (GPIO0 status)
Switch1Stat = PIN(Switch1)   'input switch1 status, see caution!
Switch2Stat = PIN(Switch2)   'input switch2 status, see caution!
Relay1Stat  = 0 ' Sonoff pin(Relay1)
Relay2Stat  = 0 ' Sonoff pin(Relay2)
HEADR = 0      'BL0939 read frame header
I_RMS_1 = 0    'BL0939 RMS current channel 1
I_RMS_2 = 0    'BL0939 RMS current channel 2
V_RMS = 0      'BL0939 RMS voltage
WATT_1 = 0     'BL0939 RMS power load channel 1
WATT_2 = 0     'BL0939 RMS power load channel 2
TEMP_in = 0    'BL0939 internal temperature
CHCKSMs = 0    'BL0939 checksum send
CHCKSMc = 0    'data received checksum calculated
dateTime$ = date$ + "  " + time$
IOBUFF.DIM(0, 35) 'dimension the I/O buffer 0 with 36 bytes
onserial2 lbl_Rcv_Decd_BL0939        ' event receiver data, decode data
onhtmlreload lblDrawPage
timer0 500, lblUpdateStatus
gosub lblDrawPage
pause 2000

wait



lblUpdateStatus:  'UPDATE INPUT STATUS AND FLASH LED
pin(ledPin) = 0              'turn OFF led
SERIAL2.BYTE &h55, &hAA  'send command "read Data" from BL0939
Button1Stat = PIN(Button1)
Switch1Stat = PIN(Switch1)
Switch2Stat = PIN(Switch2)
dateTime$ = date$ + "  " + time$ ' update date time
Relay1Stat = PIN(Relay1)
Relay2Stat = PIN(Relay2)
pause 50
pin(ledPin) = 1              'turn ON led
return


lblDrawPage: 'simple web page
cls
autorefresh 500
A$ = |<center>|
A$ = A$ + |<h2><font color=red> DEVICE BURN! if you connect inputs S1 or S2 to different potential than Lin, DEVICE BURN!</font></h2>|
A$ = A$ + |<H1>Sonoff Dual R3 v2 pow - BL0939 power monitor on board</H1>|
A$ = A$ + |Time Keeper from server Network Time Protocol | + textbox$(DateTime$) + |<br><br>|
A$ = A$ + |V_RMS (V): |+textbox$(V_RMS,"IDshort")+|&nbsp &nbsp &nbsp TEMP_in (°C): |+textbox$(TEMP_in,"IDshort")+ |<br><br>|
A$ = A$ + |&nbsp &nbsp Button1Status:| + led$(Button1Stat)
A$ = A$ + |&nbsp &nbsp Switch1Status:| + led$(Switch1stat)
A$ = A$ + |&nbsp &nbsp Switch2Status:| + led$(Switch2stat)
A$ = A$ + |&nbsp &nbsp &nbsp (green=float, red=active)<br><br>|
A$ = A$ + |<br><br>Relay1Status: | + led$(Relay1Stat) + | (green=ON, red=OFF) &nbsp &nbsp| + button$("Toggle Relay 1", lblToggleR1)
A$ = A$ + |&nbsp &nbsp I_RMS_1 (A): |+textbox$(I_RMS_1,"IDshort") +|&nbsp &nbsp WATT_1 (W): |+textbox$(WATT_1,"IDshort")
A$ = A$ + |<br><br>Relay2Status: | + led$(Relay2Stat) + | (green=ON, red=OFF) &nbsp &nbsp| + button$("Toggle Relay 2", lblToggleR2)
A$ = A$ + |&nbsp &nbsp I_RMS_2 (A): |+textbox$(I_RMS_2,"IDshort") +|&nbsp &nbsp WATT_2 (W): |+textbox$(WATT_2,"IDshort")
A$ = A$ + |</center>|
a$ = a$ + cssid$("IDlong","width:200px;") + cssid$("IDshort","width:50px;")+ cssid$("ID100","width:90%;")
html A$ : A$ = ""
return




lblToggleR1:   'toggle RELAY 1
PIN(Relay1) = 1-PIN(Relay1) ' toggles the value of Relay1 pin
return


lblToggleR2:   'toggle RELAY 2
PIN(Relay2) = 1-PIN(Relay2) ' toggles the value of Relay2 pin
return



lbl_Rcv_Decd_BL0939:
'#######################################################################################################################
'#from       https://github.com/peff74/Sonoff-Dual-R3/blob/main/README.md
''One full measurement frame from bl0939: 35 Byte (0-34) received aftercommand  &h55, &hAA (2 Byte) trasmitted.

''  Byte:  00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
''  Data:  55 3a 12 0  f0 2f 0  0  0  0  8c 61 3c bb 3  0  fc 10 0  0  0  0  b6 0  0  0  0  0  e2 1  0  7d 2  0  df
''
''  Byte     Data example      Function          How to calculate
'                                                Hex Val to Dec val    correction formula            Result
''  -----   -------------- ------------- ---------------------------------------------------------------------
''  00       0x55              HEADER
''  01-03    0x3a 0x12 0x00    I_FAST_RMS[A]
''  04-06    0xf0 0x2f 0x00    I_RMS[A]          002ff0 --> 12272     DecVal /266013                --> 0,046A
''  07-09    0x00 0x00 0x00    I_RMS[B]
''  10-12    0x8c 0x61 0x3c    V_RMS             3C618C --> 3957132   DecVal /17159                 --> 231V
''  13-15    0xbb 0x03 0x00    I_FAST_RMS[B]
''  16-18    0xfc 0x10 0x00    WATT[A]           0010fc --> 4348      DecVal/713                    --> 6,1W
''  19-21    0x00 0x00 0x00    WATT[B]
''  22-24    0xb6 0x00 0x00    CF_CNT[A]         0000b6 --> 182       DecVal*0.000163               --> 0,0296kWh
''  25-27    0x00 0x00         CF_CNT[B]
''  28-29    0xe2 0x01         TPS1[internal]      01e2 --> 482      ((85* DecVal - 5440)/448)-45   --> 34,31°C
''  30       0x00              Free
''  31-32    0x7d 0x02         TPS2[external]
''  33       0x00              Free
''  34       0xdf              CHECKSUM
'#######################################################################################################################
't1 = millis
'pause 10

serial2.read_iobuff(0)    ' read the incoming data in the buffer 0
'size = iobuff.len(0) : wlog "received "; size; " bytes"
HEADR = iobuff.read(0,0) 'extract header (&H55)

'' calculate CHCKSMc and compare for data validation unnecessary for monitoring
'CHCKSMs = iobuff.read(0,34) 'extract checksum byte sended from BL0939
'CHCKSMc = &H55
'for i = 0 to 33
'CHCKSMc = CHCKSMc + iobuff.read(0,i)
'next i
'CHCKSMc = not CHCKSMc
'CHCKSMc = CHCKSMc and &HFF
'if CHCKSMs <> CHCKSMc then
'  I_RMS_1 = -99999    'BL0939 RMS current channel 1
'  I_RMS_2 = -99999    'BL0939 RMS current channel 2
'  V_RMS = -99999      'BL0939 RMS voltage
'  WATT_1 = -99999     'BL0939 RMS power load channel 1
'  WATT_2 = -99999     'BL0939 RMS power load channel 2
'  TEMP_in = -99999    'BL0939 internal temperature
'  return
'end if

I_RMS_1 = (iobuff.read(0,4) + (iobuff.read(0,5)*256) + (iobuff.read(0,6)*65536))/266013
I_RMS_1 = cint(I_RMS_1 * 1000)/1000   'limit decimal to 1  milliampere
I_RMS_2 = (iobuff.read(0,7) + (iobuff.read(0,8)*256) + (iobuff.read(0,9)*65536))/266013
I_RMS_2 = cint(I_RMS_2 * 1000)/1000   'limit decimal to 1  milliampere
V_RMS = (iobuff.read(0,10) + (iobuff.read(0,11)*256) + (iobuff.read(0,12)*65536)) / 17159
V_RMS = cint(V_RMS * 10)/10   'limit decimal to 100  milliVolt
WATT_1 =  (iobuff.read(0,16) + (iobuff.read(0,17)*256) + (iobuff.read(0,18)*65536))/713
WATT_1 = cint(WATT_1 * 10)/10   'limit decimal to 100  milliWatt
WATT_2 =  (iobuff.read(0,19) + (iobuff.read(0,20)*256) + (iobuff.read(0,21)*65536))/713
WATT_2 = cint(WATT_2 * 10)/10   'limit decimal to 100  milliWatt
TEMP_in = cint(((85*(iobuff.read(0,28) + (iobuff.read(0,29)*256))- 5440)/448)-45)
'TEMP_in = (((85*(iobuff.read(0,28) + (iobuff.read(0,29)*256)))-5440)/448)-45
'CHCKSMs = iobuff.read(0,34) 'extract checksum byte sended from BL0939



't2 = millis-t1 : wlog "calc.time="; t2
'wlog CHCKSMs, CHCKSMc
'wlog "V_RMS: "; V_RMS
'wlog "I_RMS_1:";I_RMS_1, "I_RMS_2:";I_RMS_2
'wlog "WATT_1:";WATT_1, "WATT_2:";WATT_2
'wlog "TEMP_in:"; TEMP_in

return

end

Merry Christmas
You do not have the required permissions to view the files attached to this post.
Post Reply