xoseperez / espurna

Home automation firmware for ESP8266-based devices
http://tinkerman.cat
GNU General Public License v3.0
3k stars 636 forks source link

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

Closed knopserl closed 6 years ago

knopserl commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

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

knopserl commented 6 years ago

@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 commented 6 years ago

accidently closed the issue, sorry

gn0st1c commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

can you zip and attach your original firmware.bin ?

knopserl commented 6 years ago

yes of course: SP1-original.zip

It's from that company https://www.amazon.de/Steckdose-Homecube-intelligente-Verbrauchsanzeige-funktioniert/dp/B076Q2LKHG/ref=sr_1_fkmr0_1?ie=UTF8&qid=1522349887&sr=8-1-fkmr0&keywords=homecube+SP1 which is same like that https://www.amazon.de/Intelligente-Stromverbrauch-Fernsteurung-Schaltbare-Energieklasse/dp/B076WZQS4S/ref=sr_1_1?ie=UTF8&qid=1522350030&sr=8-1&keywords=gosund+sp1 or that https://www.amazon.de/intelligente-COOSA-Stromverbrauch-funktioniert-Fernsteurung/dp/B077RTJJH1/ref=sr_1_fkmr0_1?s=diy&ie=UTF8&qid=1522350072&sr=1-1-fkmr0&keywords=coosa+sp1

from where are you? I'm from Austria.

knopserl commented 6 years ago

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

gn0st1c commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

knopserl commented 6 years ago

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

lobradov commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

knopserl commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

BL0937.zip

knopserl commented 6 years ago

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

sp1-web-status-3003

knopserl commented 6 years ago

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 commented 6 years ago

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

gn0st1c commented 6 years ago

i found a wifi plug with hjl markings. i'll be flashing and testing tomorrow. it's better than the guess work.

knopserl commented 6 years ago

ok, good luck, I'll be watching this thread. Strage it seem the original manufacturer want to hide informations. The ESP8266EX has no labeling (blank metal case) and the energy monitor has a strage name HJL-01 (no found in the Web).

BTW, I have found another flaw, but no sure if it has to do with this device: When the modem changes the WAN IP adress, the plug seem to get and stay disconnected. It does not reconnect to WiFi, need to replug it to mains then it works again. Strage because why shouldthe WAN IP change has in impact to the WLAN connections of the SP1. I need to verify that again to be sure or there might be anothe rProblem with the WiFI Stability.

gn0st1c commented 6 years ago

good news, i got it working.

with my HJL-01 device the SEL, CF and CF1 had pull up resistors. it looks like your device has too. r17,r18,r20 & r15, r19, r21 perhaps?, can you check?

if so, my code has this and i'll leave it there for the time being. pinMode(_cf, INPUT_PULLUP); pinMode(_cf1, INPUT_PULLUP); _attach(this, _cf, FALLING); _attach(this, _cf1, FALLING);

knopserl commented 6 years ago

hello gn0st1c great job again. sorry I was out the last couple of hours.

that is very good news, as you had a device with the same engery meter chip and I think also the same schematic. regarding the pull up, yes I have the same resistors, please look at the picture I have posted above and I have copied it again here. Check if you have the same values. What do I need to do now? Where is the code to change in the HLW8012.cpp file?

Is it in that code? void HLW8012::begin( unsigned char cf_pin, unsigned char cf1_pin, unsigned char sel_pin, unsigned char currentWhen, bool use_interrupts, unsigned long pulse_timeout ) {

_cf_pin = cf_pin;
_cf1_pin = cf1_pin;
_sel_pin = sel_pin;

_current_mode = currentWhen;
_use_interrupts = use_interrupts;
_pulse_timeout = pulse_timeout;

pinMode(_cf_pin, INPUT_PULLUP);
pinMode(_cf1_pin, INPUT_PULLUP);
pinMode(_sel_pin, OUTPUT);

or best, can you send me your source files? I have changed/inserted that but gives me a copile error (_attach' was not declared in this scope).

grafik

What device do you have? has it also a product name SP1 (smart plug 1) on the backside? Thats what I have and many brands have the same product name SP1.

gn0st1c commented 6 years ago

@knopserl : those codes goes somewhere else.

i don't like spending too much time in interrupts so i'll clean up my code and PR as soon as possible.


is this how they are connected? (10 and 10k voltage divider which makes almost no difference)

sp1

knopserl commented 6 years ago

@gn0st1c : exactly as you described it. Wiring and resistor values (10 and 10k). Waiting for your code and instructions. What chip is that now? a BL0937 or a HJL-01? In the original firmware there is a reference to a bl0937.c and not to an hjl-01.

knopserl commented 6 years ago

@gn0st1c : If you don't mind and the code cleanup and PR takes some time, it would be nice if you could send me your intermediate but working version of HJL-01 driver file?

swilson86 commented 6 years ago

This SP1 is also available with an American style outlet and seems to be similar in basic functionality to the American style ITead S31 (which I have not seen in a European version yet). Jose is working as I write this on the S31. Thanks to all of you, knopserl, gn0st1c, and Jose for all the days that you give us that make me feel like I am a child in a candy store... I feel your energy!!

knopserl commented 6 years ago

@swilson86 you're welcome. I think the SP1 support will be a big improvement of this project as it's availble in many brands and seem to be and become more and more common. I also have the ITEAD S31 on my radar but we need a European version. I have already contacted ITEAD as there are many requests for an EU version. On the other hand, if the SP1 support is finalized - thanks for gn0st1c great support - you might not need the S31 or the EU version as the SP1 can do the same and is very cheap (14€). I waiting now for the completing of the energy chip driver from gn0st1c then I can use my 4 pieces of SP1 (different brands, ame HW)

gn0st1c commented 6 years ago

sorry for the belated reply.

1) last weekend, my testing device died with a bang (bigclive would be proud). so i ordered a new one, along with s31 and couple of new plugs & esp devices.

2) instead of a stand alone library, i thought it would be better if a single library handled alternative chips. thus, i changed my code so that @xoseperez 's HLW8012 library would work for HLW8012, CSE7759 and BL0937.

if you don't like to wait, just give me your mail address, i can send you the library and you can test it.

knopserl commented 6 years ago

@gn0st1c : Thanks for your reply and that you have not forgot me :-) Just kidding but I really appreciate your great work but also can't wait to get your solution for testing. My mail adress is knopserl-at-yahoo.de I would be happy to do some testing meanwhile. I think your idea makes sense if you combine multiple energy monitor chips to one sensor library if most of the code can be shared and controlled by one or a few build parameter.

gn0st1c commented 6 years ago

just to inform those following this issue; yesterday, thanks to @knopserl being patient with me and sending several debug outputs, we managed to get the driver work as excepted.

i'm now waiting for my new wifi plug to arrive so i can connect it to my oscilloscope and verify some of my findings. then we'll release the driver into the wild :)

knopserl commented 6 years ago

credits go to @gn0st1c as he provided me with the ESP8266 pinout and he developed the driver for the new (unknown before we started) energy monitor. I was just assisting him, which is the least I could do, as I have started the thread asking for supporting the device(s). With the joined work, we can get a whole interesting family of smart plugs with energy monitor supported/added to espurna. A couple of my colleagues will now also order the SP1 plugs as they are now with the espurna support interesting for them. Even much more attractive than the Sonoff S20.

@gn0st1c: greetings from Wuxi/China :-) - away from home

stif commented 6 years ago

Any news about the new driver? I ordered some SP1 plugs as well and i am very interested in your work, well done!

knopserl commented 6 years ago

the driver is in beta testing and works in principle. We have to be patient and wait. It will hopefully included in one of the next versions. gn0st1c is working on it and need some more time.

gn0st1c commented 6 years ago

@stif: i'm on medical leave and i'm supposed to stay away from things emit light. i'm getting better and hopefully finish the driver soon.

ahmeaqas commented 6 years ago

@gn0st1c May you please share your modified library? I have Sonoff Pow with cse7759 chip inside? Kindly email me at ahmed [at] hobbytronics.pk

ColinShorts commented 6 years ago

@HobbytronicsPK have you tried the SONOFF_POW_R2 firmware, it has a CSE7766 afaik... Check issue #761

kuppe234 commented 6 years ago

@gn0st1c , I have a Coosa SP1. Can you post your library for BL0937 here. I know some who are interested. You can also send an email to splint[at]gmx.net.

ahmeaqas commented 6 years ago

@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 commented 6 years ago

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

knopserl commented 6 years ago

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.