Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SP1: very attractive universal multi-company smart plug with energy monitor! #737

Closed
knopserl opened this issue Mar 28, 2018 · 79 comments
Closed
Labels
enhancement New feature or request help wanted Extra attention is needed sensors
Milestone

Comments

@knopserl
Copy link

Hi, I have found a very interesting universal smart power plug called SP1. This hase a very good rating and is available from many companies based on a base product SP1. You can find it easily in the web or on Amzon and it's offered from many companies like: Gosund, Homeplug, Coosa etc. It's always the same product and same PCB, I have opened it and soldered power and RX/TX and read the chip id: It's a ESP8266EX with 1M flash and it uses the Smart Life app and a Chinese cloud.
It offers switch and power meter. Unfortunatly it has an energy meter chip labeled (3 lines) which you don't find in the web: HJL-01, J1749CYH, D797480E. Based on the parts around and wiring, I guess its a HLW8012.
It would be extremly attractive to get that working with espurna (might require some reverse engineering) but I'll try it but would be nice if I could get some help to get faster to a result.
Here some links and pictures from inside the plug:

http://www.gosund.com/index.php?m=content&c=index&a=show&catid=6&id=5
https://www.amazon.de/Steckdose-Homecube-intelligente-Verbrauchsanzeige-funktioniert/dp/B076Q2LKHG
https://www.amazon.de/intelligente-COOSA-Stromverbrauch-funktioniert-Fernsteurung/dp/B077RTJJH1/ref=sr_1_1?s=diy&ie=UTF8&qid=1522268132&sr=1-1&keywords=coosa
US version is SP2 (I guess same electronics PCB)
https://www.amazon.com/COOSA-Monitoring-Function-Campatible-Assiatant/dp/B0788W9TDR/ref=sr_1_fkmr0_3?s=hi&ie=UTF8&qid=1522267864&sr=1-3-fkmr0&keywords=coosa+SP1

grafik

grafik
20180312_223241
20180313_072417

@gn0st1c
Copy link
Contributor

gn0st1c commented Mar 28, 2018

that is definitely HLW8012

CURRENT_RESISTOR and VOLTAGE_RESISTOR_DOWNSTREAM values already matches with espurna's default.

you should follow the VOLTAGE_RESISTOR_UPSTREAM resistors.

@knopserl
Copy link
Author

Nice that you confirm that. I have build and uploaded a new firmware called SP1. No I have the problem, that the WiFi works, but no relay, LED and energy monitor because I don't know at all which pins are used at the ESP8266EX. There is not descripting on the board (beside 3.3V, GND, RX, TX and IO0). SO I would need endless build attempts. It would be great if the pins could be soft configured via EEPROM. I have created a new issue for that. Do you think that can work or do you have any othe hint which helps to speed up or is effective?

@gn0st1c
Copy link
Contributor

gn0st1c commented Mar 29, 2018

@knopserl : (for your pins image)
right pins top to bottom: en, 14, 13, 0, 4, RX(3), 3v3
left pins top to bottom: tout, 12, 15, 2, 5, TX, gnd

@knopserl
Copy link
Author

knopserl commented Mar 29, 2018

@gn0st1c : you are incrediable and saved me a lot of time! My idea whould have been to write a sketch to set/read all the ports and and measure the signal at the PCB pinout using the serial line for manual interaction. How do you know that pinout? Do you know the PCB or do you have an SP1 or what magic have you applied?
I'll now try to find out how the pins are connected to the relay, the red/blue LED, the push button and the three HLW8012 connection and will set then the proper HW pinouts in the espurna project. If that works, you are my hero and we have a new popular device supported by espurna!

grafik

@gn0st1c
Copy link
Contributor

gn0st1c commented Mar 29, 2018

@knopserl :
i know the pins because i've met this esp several times before :)
this may also help you; gpio via Terminal


i'm sure you already know these but;

button: check if one of the pins is connected to gnd or vcc. (there may also be a resistor for pull-up/down. this is important for definitions like BUTTON_DEFAULT_HIGH, BUTTON_SET_PULLUP
). trace the remaining pin.

led: again check for gnd or vcc, trace the remaining pin. (there is most surely a current limiting resistor for the led)

relay: there is surely a flyback diode in parallel to relay pins. trace it back to the transistor and transistor to esp. (there is most surely a current limiting resistor for the transistor)

@knopserl
Copy link
Author

@gn0st1c : You're totaly right with your hints. I'm a big step further, but still need some more help, so please be a bit more patient with me :-)

I think I have found out the pins and their functions.
I have updated the pins definition in hardware.h and uploaded the new firmware.
Something is now working a couple of things not.
I can remotely (via the Web) switch on/off the relay and the LED red is on/off. But I cannot switch the relay using the physical pin. Also the power meter does not show any value (GPIO 12, 5, 14).
If I press the button long, then it does a reset and starts again the AP. So I guess the button pin is correct but I can't switch on manuall (which I think should work right?)

Here are the setting in hardware.h I'm using:
#elif defined(SP1)

// Info
#define MANUFACTURER        "Homecube"
#define DEVICE              "SP1"

// Buttons
#define BUTTON1_PIN         13
#define BUTTON1_MODE        BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH
#define BUTTON1_RELAY       2

// Relays
#define RELAY1_PIN          15
#define RELAY1_TYPE         RELAY_TYPE_NORMAL

// LEDs
#define LED1_PIN            0
#define LED1_PIN_INVERSE    2

// HLW8012
#ifndef HLW8012_SUPPORT
#define HLW8012_SUPPORT     1
#endif
#define HLW8012_SEL_PIN     12
#define HLW8012_CF1_PIN     14
#define HLW8012_CF_PIN      5

#define HLW8012_VOLTAGE_R_UP            ( 2 * 1000000 )  // Upstream voltage resistor

and here the updated pin picture
sp1-pinout

thanks a lot in advance
Heinz

@gn0st1c
Copy link
Contributor

gn0st1c commented Mar 29, 2018

it seems only setting BUTTON1_RELAY to 1 should fix your problems. (you have 1 button and 1 relay)

@knopserl
Copy link
Author

knopserl commented Mar 29, 2018

@gn0st1c you are just fantastic! The button on/off works now and I did an OTA updload/upgrade.
Now I need your help to solve the power meter issue and I'm happy and a new espurna devive is born.
Any hints? Could it be, that the pins are still wrong numbered (so far most pins you speficed are correct), only the there SEL, CF, CF1 might not fit. I will verify a third time. I'm also 90% sure that the chip is a HLW8012 but could it be that it is another chip with 8 pins with similar schematic?

sp1-power-meter-chip

@knopserl knopserl reopened this Mar 29, 2018
@knopserl
Copy link
Author

accidently closed the issue, sorry

@gn0st1c
Copy link
Contributor

gn0st1c commented Mar 29, 2018

@knopserl :
just make sure you got the pins right. (right of the IC, top to bottom = SEL, CF1, CF, GND)

also here is the data sheet for your ESP8266 (S3)
ESP8266-S3.pdf

@knopserl
Copy link
Author

knopserl commented Mar 29, 2018

@gn0st1c : here is the screenshot of the web interface, it shows some values but they are nonsens. I have connected a LED lamp with 0,1A and voltage is 230V.
I have checks the pinout of the HLW8012. This is what I excactly did.
I double checked again and pins 8,7,6 on the power meter IC (SEL, CF1, CF) is connected to 12, 14, 5 of the MCU. So I have no moer ideas. Maybe it is another chip, or what else could be?

sp1-web-status

hlw8012-chip-pinout

@gn0st1c
Copy link
Contributor

gn0st1c commented Mar 29, 2018

can you zip and attach your original firmware.bin ?

@knopserl
Copy link
Author

you can also order one device and I will pay for it (via paypal or whatever you prefer)

@gn0st1c
Copy link
Contributor

gn0st1c commented Mar 29, 2018

well, this was a very interesting decompiling session :)

the chip is BL0937. it's pin compatible with HLW8012. you learn something everyday.
luckily, we can code a new sensor driver based on the current HLW8012 sensor code.

and here is the datasheet;
BL0937_Datasheet_V1.01.pdf.zip

@knopserl
Copy link
Author

you impress me more and more!
So it's pin compatible but not signal/code compatible right?
Who (is we) will code a new sensor driver and how much effort is that?
Again the question/offer: Do you need such a device? I will pay you one! Just let me know if and how we can do it (I can e.g. send you the money via Paypal).
I'm really interested in which country you are located (if it is not too private for you).
Anyhow, thanks so far! Do you agree that this a very nice devices? If you take the ones from the last link It's about 14€. Quite ok for a smart plug with power monitor.

@gn0st1c
Copy link
Contributor

gn0st1c commented Mar 29, 2018

@knopserl : i'm in Turkey.
i'll have a look at the code tomorrow.

@knopserl
Copy link
Author

knopserl commented Mar 29, 2018

good luck for tomorrow and and greetings from Austria to Turkey, it's an honor to work together with you

@lobradov
Copy link
Contributor

Coding seems to be the same, but the calculation, or more precisely, reference values seem to be different (HLW8012 reference voltage is 2.43V while that of BL0937 is 1.218V, as example)

Maybe there's a way to use original HLW8012 library and just change reference values?

@gn0st1c - all kudos for decompiling/rev engineering. 🥇

@knopserl
Copy link
Author

what concerns me a bit is, that the resulted values using the HLW8012 code are completly nonsens or look like it does not work at all (see the Web page screen shot above)
examples:
current: 11A should be 0.05A
voltage: 0V should be 230V
power: 125767W should be 10W
other values are 0 or invalid.
But maybe because of the reference voltage is different the calculated values are totally invalid and not just wrong.

Yes kudos goes to @gn0st1c he is doing a great reverse engineering job and gave me the essential hints.
The switch is working but energy monitor has to be solved. I'm really looking forward and hope we get that solved.

I think it's really worth to get this device hacked/supported as it is very well rated/advertised/ranked in many articels and sold under many different brands. I have so far not found anything I like more.

@gn0st1c
Copy link
Contributor

gn0st1c commented Mar 30, 2018

i've "blindly" coded a driver but it needs testing :)

@knopserl
Copy link
Author

knopserl commented Mar 30, 2018

of course, I'm the tester, so how do I get it and how to I include it in my project (I use visual studio code)?

@gn0st1c
Copy link
Contributor

gn0st1c commented Mar 30, 2018

to use it in espurna, you have to add a handler as well. i'll do that later.

so just for testing purposes, you can just rename files & variables of the attached files and overwrite your current HLW lib folder. (find replace BL0937 to HLW8012)
then you can compile as before.

please note that, i just wrote the code last night and never did a second control pass. it may not work.
BL0937.zip

@knopserl
Copy link
Author

knopserl commented Mar 30, 2018

I have copied the files to the folder:
vscode\espurna\code.piolibdeps\HLW8012\src
renamed both to HLW8012.h and HLW8012.cpp and rebuild the project which of course gave compile errors as the HLW8012 was no longer known, so I renamed all BL0937 in the header and cpp file and then it successfully build the new firmware. After the upload, all values are now 0.

@gn0st1c
Copy link
Contributor

gn0st1c commented Mar 30, 2018

try this. if this doesn't work, too you'll have to wait for tonight when i have the time.

BL0937.zip

@knopserl
Copy link
Author

knopserl commented Mar 30, 2018

this is the result of the latest build
expected values: 230V, 0.05A, 10-12W

sp1-web-status-3003

@knopserl
Copy link
Author

I have now used another SP1 with the Smartlife App (China cloud) and it shows: 230.8V, 46mA, 6.2W
Is there anything we can debug to come further? How can I support you?

@gn0st1c
Copy link
Contributor

gn0st1c commented Mar 30, 2018

no worries, we'll make it work. but later tonight. :)

@xoseperez
Copy link
Owner

HI
I received a Blitzwolf BW-SHP2 (https://www.blitzwolf.com/Wifi-Smart-Socket-EU-p-244.html) yesterday. It has the very same board inside with an HJL01 power monitoring chip. While waiting for @gn0st1c PR to the HLW8012 library to support both I managed to make it work with the same library without modifications, only small changes in how ESPurna configures it.

As @lobradov pointed out, the HJL01/BL0937 has different configuration parameters but I also found out the SEL pin is inverted (compared to the HLW8012 boards I have checked so far) and the interrupt won't work unless it monitors FALLING edges. Fortunately, all these changes can be configured from ESPurna without the need to changed the library.

I have committed my changes to the sensor branch, will join them to dev (and master) as soon as I have some feedback from you.

Of course, once @gn0st1c pushes his PR that would be the preferred way to implement it.

@xoseperez xoseperez added this to the 1.13.0 milestone Jun 20, 2018
@doodah33
Copy link

Well would you look at that

espurna

This is a 75 watt light bulb

BTW, my plugs have the following pin config:

    #define HLW8012_SEL_PIN     3
    #define HLW8012_CF1_PIN     14
    #define HLW8012_CF_PIN      5

@xoseperez
Copy link
Owner

@doodah33 What device is yours? A Vanzavanzu (https://www.amazon.com/Smart-Plug-Wifi-Mini-VANZAVANZU/dp/B078PHD6S5)?

@Camposmansi
Copy link

xoseperez eres un crack, el puto amo jajaja

Anxious I am waiting for the bin

@doodah33
Copy link

@xoseperez that is exactly it. There are a ton that look like that one, but I couldn't vouch that they have the same components inside.

@Camposmansi
Copy link

Pido perdon por mi ingles
Es exactamente el mismo, pero por lo que entiendo estais todavia investigando el tema, estoy ansioso por el bin

I apologize for my English
It's the same, but that's why I understand you're still investigating the subject, I'm looking forward to the bin

@xoseperez
Copy link
Owner

Released with 1.13.0. Closing.

@knopserl
Copy link
Author

a great and successfull thread!

@Camposmansi
Copy link

Y cual es el BIN?

And what is the BIN?

@nightfly0815
Copy link

nightfly0815 commented Jun 25, 2018

You have to use the blitzwolf.bin
I also flashed this sp1 socket and it worked!
https://www.amazon.de/Intelligente-Steckdose-Ablue-Funktioniert-Assistant/dp/B076DRFRZC/ref=sr_1_5?s=diy&ie=UTF8&qid=1529908498&sr=1-5&keywords=wlan+steckdose+smart+life
The Voltage and the Power seams to be ok.
I just have to check the ampere, with a multimeter and calculate the values.
I´m not 100% sure if the ampere are shown correct. I saw yesterday that the decimal place was not correct. In the webif I shows 0.035A, but I think it should 0,35A. I will measure and calculate it, when I have a 100% resistive consumer.
I will give a feedback what I found out.

regards from Austria ;-)

@Camposmansi
Copy link

Oohhh gracias, y no lo vi, buscaba algo mas comun como SP1

Oohhh thanks, and I did not see it, I was looking for something more common like SP1

@filzek
Copy link

filzek commented Sep 12, 2019

Hi guys, do anyone know what is the power supply ic used on these smartplugs????

@kuppe234
Copy link

kuppe234 commented Sep 13, 2019 via email

@gn0st1c
Copy link
Contributor

gn0st1c commented Sep 13, 2019

@kuppe234 :
mp150 web
mp150 pdf

@cjheath
Copy link

cjheath commented Sep 24, 2019

Some folk mentioned they had difficulty finding the manufacturer and data sheet for the BL0937. The manufacturer is Shanghai Belling Co, Ltd. and the data sheet is available on thejir web site here: http://www.belling.com.cn/en/product_info.html?id=138

@gn0st1c
Copy link
Contributor

gn0st1c commented Sep 24, 2019

@cjheath : also, #737 (comment)

@cjheath
Copy link

cjheath commented Sep 25, 2019

@gn0st1c yes of course thanks, but some folk like to find the manufacturer too. This chip is unusually difficult to find.

@Wim3d
Copy link

Wim3d commented Oct 6, 2020

well, this was a very interesting decompiling session :)

the chip is BL0937. it's pin compatible with HLW8012. you learn something everyday.
luckily, we can code a new sensor driver based on the current HLW8012 sensor code.

and here is the datasheet;
BL0937_Datasheet_V1.01.pdf.zip

For future reference, here the english datasheet of the BL0937 can be found
https://www.belling.com.cn/media/file_object/bel_product/BL0937/datasheet/BL0937_V1.02_en.pdf

@hanzoh
Copy link

hanzoh commented Mar 11, 2021

Thank you guys for getting the BL0937 to work. I am currently trying to do the same for esphome, and I get the correct results.
What I still don't understand is, how you got to these multipliers:

    #define HLW8012_CURRENT_RATIO       25740
    #define HLW8012_VOLTAGE_RATIO       313400
    #define HLW8012_POWER_RATIO         3414290

So @xoseperez approach is to use the untouched HLW8012 library (which uses the HLW8012 formulas to calculate the values) and then apply these ratios on top of it, correct?
@gn0st1c How did you modify the HLW8012 library to get to the correct values, that @knopserl confirmed?
If I read this thread correctly, you never released it, right?

I would just like to understand and then document, why the factor for calculating pulse width (in microseconds) into power is 1707145 (3414290 / pulse_width_in_us / 2).

@kai-tub
Copy link

kai-tub commented Apr 12, 2021

@hanzoh have you ever published the results for esphome?

@hanzoh
Copy link

hanzoh commented Apr 12, 2021

No, I gave up as the results were unsatisfying. The original Gosund firmware gave much better values than any custom Firmware. I don’t know if they use something other than the Interrupt approach.

@Mbilal337
Copy link

Hi, @xoseperez @gn0st1c , wonder full thread. Really learned a lot from this. I'm making my own custom board using BL0937 with esp32. The problem I'm facing is this that I'm unable to get any pulse from IC both load connect or disconnected.
Can you please guide what I might be doing wrong here ?
My circuit is as per datasheet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed sensors
Projects
None yet
Development

No branches or pull requests