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 · 68 comments

Comments

Projects
None yet
@knopserl

knopserl commented Mar 28, 2018

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

This comment has been minimized.

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

This comment has been minimized.

knopserl commented Mar 28, 2018

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

knopserl commented Mar 29, 2018

@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

This comment has been minimized.

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

This comment has been minimized.

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 closed this Mar 29, 2018

@knopserl knopserl reopened this Mar 29, 2018

@knopserl

This comment has been minimized.

knopserl commented Mar 29, 2018

accidently closed the issue, sorry

@gn0st1c

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Contributor

gn0st1c commented Mar 29, 2018

can you zip and attach your original firmware.bin ?

@knopserl

This comment has been minimized.

knopserl commented Mar 29, 2018

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

@gn0st1c

This comment has been minimized.

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

This comment has been minimized.

knopserl commented Mar 29, 2018

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

This comment has been minimized.

Contributor

gn0st1c commented Mar 29, 2018

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

@knopserl

This comment has been minimized.

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

This comment has been minimized.

Contributor

lobradov commented Mar 29, 2018

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

This comment has been minimized.

knopserl commented Mar 30, 2018

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

This comment has been minimized.

Contributor

gn0st1c commented Mar 30, 2018

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

@knopserl

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

knopserl commented Mar 30, 2018

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

This comment has been minimized.

Contributor

gn0st1c commented Mar 30, 2018

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

@HobbytronicsPK

This comment has been minimized.

HobbytronicsPK commented May 14, 2018

@gn0st1c I have my own very light firmware ESPMetRED, which I am using since years. I want to use Sonoff Pow with it. That's why I need that standalone library.

@gn0st1c

This comment has been minimized.

Contributor

gn0st1c commented May 14, 2018

@HobbytronicsPK : there is no standalone library for cse7766.
but you can use this code to develop your own.

@knopserl

This comment has been minimized.

knopserl commented May 14, 2018

aaahh, the thread I've opened gets more and more interest. The COOSA SP1 is one of the plenty of companies (beside Gosund, Homecube etc.) who is selling SP1 derivates and yes it has the BL0937 chip which is labeled with HJL-01 :-)
Thanks to gn0st1c who "reverse engineered" the chip and updated the HLW8012 driver to support this energy monitor. But is still in beta.

@HobbytronicsPK

This comment has been minimized.

HobbytronicsPK commented May 16, 2018

@gn0st1c Have no clue, how to use it. If you have worked with it already, Can you please describe, how to use it?

@stif

This comment has been minimized.

stif commented May 28, 2018

Hi @gn0st1c, hope you are feeling better now. Do you have any news about the driver? I am happy to do some beta testing if you send me your code to stif[at]gmx.at

@doodah33

This comment has been minimized.

doodah33 commented Jun 10, 2018

Plus 1 vote. Would love to see this driver released into the wild, even if it is in beta form. I'm seeing more and more of these pop up at great prices. Using Espurna is way far more preferable to using the bundled app.

Couple more US examples using this chip:

https://www.amazon.com/Smart-Plug-Wifi-Mini-VANZAVANZU/dp/B078PHD6S5
https://www.amazon.com/Compatible-Assistant-Monitor-Required-Function/dp/B077R18JXG

@xoseperez

This comment has been minimized.

Owner

xoseperez commented Jun 11, 2018

@gn0st1c You might want to release the driver as it is now. We could help debugging it...

@knopserl

This comment has been minimized.

knopserl commented Jun 17, 2018

@gn0st1c and @xoseperez I'm testing it already since I got the library and I would say it works so far and should be tested by a larger test community. I would also support that it goes to the dev branch.

@xoseperez xoseperez referenced this issue Jun 20, 2018

Closed

ESPurna duda #941

@gn0st1c

This comment has been minimized.

Contributor

gn0st1c commented Jun 20, 2018

i haven't noticed the latest messages. thanks to @knopserl letting me know.

@knopserl uses the driver on his 7 plugs so i'll merge it.

@doodah33

This comment has been minimized.

doodah33 commented Jun 20, 2018

Woo hoo! Thanks, @gn0st1c

xoseperez added a commit that referenced this issue Jun 20, 2018

@xoseperez

This comment has been minimized.

Owner

xoseperez commented Jun 20, 2018

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

This comment has been minimized.

doodah33 commented Jun 21, 2018

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

This comment has been minimized.

Owner

xoseperez commented Jun 21, 2018

@Camposmansi

This comment has been minimized.

Camposmansi commented Jun 21, 2018

xoseperez eres un crack, el puto amo jajaja

Anxious I am waiting for the bin

@doodah33

This comment has been minimized.

doodah33 commented Jun 22, 2018

@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

This comment has been minimized.

Camposmansi commented Jun 22, 2018

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

This comment has been minimized.

Owner

xoseperez commented Jun 22, 2018

Released with 1.13.0. Closing.

@xoseperez xoseperez closed this Jun 22, 2018

@knopserl

This comment has been minimized.

knopserl commented Jun 23, 2018

a great and successfull thread!

@Camposmansi

This comment has been minimized.

Camposmansi commented Jun 25, 2018

Y cual es el BIN?

And what is the BIN?

@nightfly0815

This comment has been minimized.

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

This comment has been minimized.

Camposmansi commented Jun 25, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment