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

Sonoff switch #6

Open
shenery opened this issue Nov 3, 2016 · 91 comments
Open

Sonoff switch #6

shenery opened this issue Nov 3, 2016 · 91 comments

Comments

@shenery
Copy link

shenery commented Nov 3, 2016

Do you think this code could modified to work with the sonoff switch. http://blog.squix.org/2016/01/esp8266-products-hitting-market.html

It has 1M of flash and uses esp8266

@shenery
Copy link
Author

shenery commented Nov 9, 2016

Or do you think it will just work? I'm getting one to test in the next couple of weeks.

@joeman2116
Copy link

It works fine on a sonoff. I just used the arduino ide software to program it - I used the nodemcu.9v on the arduino type - I use them with the alexa echo dot

Great pieces of software...

Joe

@shenery
Copy link
Author

shenery commented Nov 14, 2016

I received 4 today and had a quick go. Using arduino I had it on generic esp8266 board and 1m flash. I changed the relay pin to pin 12 in the code and was getting error esp com mem failed or something like that. Was the pin number all you changed? And do I have to change the board type to nodemcu 9v and that will be me sorted? Really excited about this. An Alexa switch for £5! :-)

@shenery
Copy link
Author

shenery commented Nov 14, 2016

I used a fdti 3.3v

@joeman2116
Copy link

I also use a ftdi 3.3v as the programmer.
I tried type as a generic 1meg flash but got reboot problems.
So I used the nodemcu.9 choice.

Change the ssid
Change password
Change device name to (your choice)

Pins
I use d6 for the relay and d7 for the led.(optional)

Joe

On Mon, Nov 14, 2016 at 6:12 PM, shenery notifications@github.com wrote:

I used a fdti 3.3v


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#6 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AWDpY8E2NrAWaVDSVncauzQh4zxYCaCKks5q-NzSgaJpZM4KoUCC
.

@joeman2116
Copy link

The esp mem code error -likely means your device is not in flash mode

TO flash:

Press and hold the button, do not release: while holding it down, plug in
the programmer, then release button.

Joe

On Mon, Nov 14, 2016 at 6:49 PM, joe basque joeman2116@gmail.com wrote:

I also use a ftdi 3.3v as the programmer.
I tried type as a generic 1meg flash but got reboot problems.
So I used the nodemcu.9 choice.

Change the ssid
Change password
Change device name to (your choice)

Pins
I use d6 for the relay and d7 for the led.(optional)

Joe

On Mon, Nov 14, 2016 at 6:12 PM, shenery notifications@github.com wrote:

I used a fdti 3.3v


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#6 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AWDpY8E2NrAWaVDSVncauzQh4zxYCaCKks5q-NzSgaJpZM4KoUCC
.

@shenery
Copy link
Author

shenery commented Nov 14, 2016

Ok thanks for the advice. I will give it a try today.

Cheers
Steven

@shenery
Copy link
Author

shenery commented Nov 15, 2016

I tried nodemcu 0.9 esp-12. 1m spiffs and I'm still getting error mem esp com failed. Also d6 doesn't work for me. Isn't the relay gpio 12? It doesn't even compile when I have it as d6 but when I change to 12 it compiles fine.

@joeman2116
Copy link

D6 not d6 - my mistake gpio12 is D6

Ok, mem esp error - well that is a serial comm problem which means unit is
not in pgm mode or your wire of the tx and rx pins on your ftdi may not be
correct to the sonoff.

I leave the nodemcu .9 at 3m spiffs but 1meg is fine... The error your
displaying is not related to those selections.

It is a communication issue related to wiring and or not in correct mode to
flash.

Joe

On Tue, Nov 15, 2016 at 3:46 AM, shenery notifications@github.com wrote:

I tried nodemcu 0.9 esp-12. 1m spiffs and I'm still getting error mem esp
com failed. Also d6 doesn't work for me. Isn't the relay gpio 12? It
doesn't even compile when I have it as d6 but when I change to 12 it
compiles fine.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#6 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AWDpY8mGgkHGQf1Je0eUjP13SLlFvtfmks5q-WNCgaJpZM4KoUCC
.

@shenery
Copy link
Author

shenery commented Nov 17, 2016

Ok so I've been spending a bit more time on this. I have all your settings loaded. I have a fT232RL ftdi like this on http://m.ebay.co.uk/itm/Latest-Version-FT232BL-FTDI-USB-to-TTL-Serial-Adapter-for-Arduino-UK-Seller-/181797979805?nav=SEARCH connected to the sonoff. All wires connected and have been checked numerous times. I have it plugged into my tablet via USB otg and I have also tried through a powered USB hub and I am still getting the espcomm mem failed error. I occasionally get a java lang nullpointerexception error as well. Do you think it could be a power supply issue? Kind regards
Steven

@shenery
Copy link
Author

shenery commented Nov 17, 2016

Also I press the button on the sonos and keep holding it until I plug it in to the tablet and there is no blinking led. I assume that's me in flash mode?

@joeman2116
Copy link

It still appears your system is not in flash mode.
Your steps look fine.

  1. Make sure you have selected the correct serial port.. Check windows
    system manager under com ports.
    2 Make sure the serial monitor is not open.

Joe

On Thu, Nov 17, 2016, 1:52 PM shenery notifications@github.com wrote:

Also I press the button on the sonos and keep holding it until I plug it
in to the tablet and there is no blinking led. I assume that's me in flash
mode?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#6 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AWDpY0zIb6cBhDdvoGljj60zVHc_wmI5ks5q_JRYgaJpZM4KoUCC
.

@joeman2116
Copy link

The powersupply will not usually be a problem when doing a flash
It mostly acts up when the esp8266 is running and using the sotware and
wifi.

For your programmer, tx am rx are the typical error area. If all fails
revese the tx and rx wires
Programming note:
Vcc 3.3v
Gnd
Tx
Rx
Are the only wires required.

Joe

On Thu, Nov 17, 2016, 2:20 PM joe basque joeman2116@gmail.com wrote:

It still appears your system is not in flash mode.
Your steps look fine.

  1. Make sure you have selected the correct serial port.. Check windows
    system manager under com ports.
    2 Make sure the serial monitor is not open.

Joe

On Thu, Nov 17, 2016, 1:52 PM shenery notifications@github.com wrote:

Also I press the button on the sonos and keep holding it until I plug it
in to the tablet and there is no blinking led. I assume that's me in flash
mode?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#6 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AWDpY0zIb6cBhDdvoGljj60zVHc_wmI5ks5q_JRYgaJpZM4KoUCC
.

@shenery
Copy link
Author

shenery commented Nov 18, 2016

I've tried all of that. Jumper is on the 3.3v. Vcc is the 3.3v. Checked in device manager which com port is recognised when I connect. Serial monitor window not opened. I saw on this forum that some people were getting the same error and fixed it with clearing the flash with esptool before flashing the new firmware? Do you have any experience with this? I can't see esptool in the programmer of arduino ide.

@shenery
Copy link
Author

shenery commented Nov 18, 2016

Sorry forgot to post the forum. http://tech.scargill.net/itead-slampher-and-sonoff/

@joeman2116
Copy link

Well all sounds good
I have used esptool to clear mem. Esptool is another program. Just google
and download

Sometimes an esp wont work so i use the esptool to refresh it.

Wish you had another regular esp nodemcu to see if you could flash it.
Since they have the programmer builtin, this would establish your serial
programmer is good.
Have you ever successfully flashed any esp8266?

On Fri, Nov 18, 2016, 3:06 PM shenery notifications@github.com wrote:

Sorry forgot to post the forum.
http://tech.scargill.net/itead-slampher-and-sonoff/


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#6 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AWDpYxSfyV16Id-rc1LAeQNYue3kkh_xks5q_fcqgaJpZM4KoUCC
.

@shenery
Copy link
Author

shenery commented Nov 24, 2016

ok so I got a bit further today. I got this.
83936 bytes from C:\Users\Shenery\AppData\Local\Temp\arduino_build_786479/sinric.ino.bin to flash at 0x00000000
erasing flash
size: 045520 address: 000000
first_sector_index: 0
total_sector_count: 70
head_sector_count: 16
adjusted_sector_count: 54
erase_size: 036000
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
setting serial port timeouts to 15000 ms
setting serial port timeouts to 1000 ms
espcomm_send_command: receiving 2 bytes of data
writing flash
................................................................................ [ 28% ]
................................................................................ [ 57% ]
................................................................................ [ 86% ]
...................................... [ 100% ]
starting app without reboot
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
espcomm_send_command: receiving 2 bytes of data
closing bootloader
flush start
setting serial port timeouts to 1 ms
setting serial port timeouts to 1000 ms
flush complete
I plugged in a lamp and I get nothing though.

when I bring up serial monitor it says f5, fo, f5 f0 and there is no indication that the esp is connected to my network. when scan my network there is no esp device

@joeman2116
Copy link

joeman2116 commented Nov 24, 2016 via email

@shenery
Copy link
Author

shenery commented Nov 24, 2016

I set up esptool but not 100% of the command to use to clear the flash. Tried a few but didn't work. I have a blank.bin file saved in the esptool directory. I have installed team viewer and could maybe try that over the weekend if you have time. If you have a command to use in esptool though I could try that?

Kind regards
Steven

@shenery
Copy link
Author

shenery commented Nov 24, 2016

And when I try the erase flash command in esptool I get esptool error: argument --port/-p: expected one argument

@shenery
Copy link
Author

shenery commented Nov 26, 2016

Ok so I've been having fun getting this going. I found the right command for esptool and have cleared the flash, I was then able to upload the firmware again. I've realised that there is a certain way that you have to wire the lamp in to so that the sonoff is powered via the mains. I scanned devices in alexa and it found my lamp. I asked Alexa to turn on box and she replied ok. But nothing happens the light stays off. I have tested the fuse the bulb and tried in another lamp but it doesn't turn on and I've no idea why.
I'm nearly there but not sure what to do next.

@shenery
Copy link
Author

shenery commented Nov 26, 2016

Scrap that last post. I forgot to change the gpio pin again and now it works. If anyone's having bother esptool helped out a lot. Basically in my experience you have to use esptool to clear flash before you upload this code. Thank you joeman for all your help it works perfect no :-)

@joeman2116
Copy link

joeman2116 commented Nov 26, 2016 via email

@shenery
Copy link
Author

shenery commented Nov 28, 2016

Yeah it works really good.
My wifis been down all day. What a coincidence eh? Lol. This happening has gave me anurgancy to try and work out how to override the relay with say a hardware switch or maybe you can use the program button?
Has anyone done this that you know of?

Kind
Regards

@kakopappa
Copy link
Owner

You can use a push button.
https://www.arduino.cc/en/tutorial/switch

when HIGH call turnOnRelay();
when LOW call turnOffRelay();

@shenery
Copy link
Author

shenery commented Nov 29, 2016

I'm not very good with adding code so not sure where to put it in the script. Where would it go? And would I be able to use the program button on gpio 0 for this? If not would the switch go at the mains side or the lamp side?

Kind regards
Steven

@joeman2116
Copy link

joeman2116 commented Nov 29, 2016 via email

@shenery
Copy link
Author

shenery commented Nov 29, 2016

I have found this piece of code from sonoff boilerplate firmware. Looks like the code to use the button on gpio 0 to switch the relay.
Can I just add this somewhere in the code?
//delay(200);
//Serial.println(digitalRead(SONOFF_BUTTON));
switch (cmd) {
case CMD_WAIT:
break;
case CMD_BUTTON_CHANGE:
int currentState = digitalRead(SONOFF_BUTTON);
if (currentState != buttonState) {
if (buttonState == LOW && currentState == HIGH) {
long duration = millis() - startPress;
if (duration < 1000) {
Serial.println("short press - toggle relay");
toggle();
} else if (duration < 5000) {
Serial.println("medium press - reset");
restart();
} else if (duration < 60000) {
Serial.println("long press - reset settings");
reset();
}
} else if (buttonState == HIGH && currentState == LOW) {
startPress = millis();
}
buttonState = currentState;
}
break;
}

}

https://github.com/tzapu/SonoffBoilerplate/blob/master/SonoffBoilerplate.ino

@terrag42
Copy link

terrag42 commented Nov 29, 2016

I tried out this code last night. It works fine for tactile switches. I tried it with one of those cheapo switches with a spring. It worked but there was a lot of rapid turn on / off because there is no debounce in the code.

@burhantetik
Copy link

Hello,

I need the compiled bin file of sinric.ino. Can please someone send it to my mail? burhant@gmail.com

@TCRAKN
Copy link

TCRAKN commented Jan 22, 2017

@shenery

I'm not familiar with OpenHab, though suspect it'll need to communicate with it via some version of MQTT.

Check out posts by Peter Scargill at http://tech.scargill.net/ and Tinkerman http://tinkerman.cat/
They have done some great work with ESP8266 and ITead devices. Peter Scargill has interfaced various devices to Node Red, something similar to OpenHab.

Hope this helps
Abdul-Khaliq

@shenery
Copy link
Author

shenery commented Jan 22, 2017

@TCRAKN Hey thanks for getting back. Yeah I've seen those sites before. Would there be a way to incorporate MQTT into this code as well?

@TCRAKN
Copy link

TCRAKN commented Jan 23, 2017

@shenery

I wouldn't know where to begin with MQTT. I have seen a number of repositories related to Sonoff and MQTT, but can't recall which ones.

I now need to find a way to control WS2812 LEDs with an ESP, and maybe with a Sonoff too. There are plenty of accessible I/O pins to do so. Any ideas anyone? Oh, with Alexa commands too...

Ciao for now.
Abdul-Khaliq

@TCRAKN
Copy link

TCRAKN commented Jan 23, 2017

@shenery

I've done some trawling, and these may be of use to you:

https://github.com/arendst/Sonoff-MQTT-OTA-Arduino
https://bitbucket.org/xoseperez/espurna

@MacGyver1rjq104
Copy link

@shenery
Thank you for sharing, however, my experience with xoseperez stuff is not great. He wrote the code and only tells us to use PlatformIO which I am not wanting to learn. Ardunio does not work with his code so I can't get it to load up.

@TCRAKN
Copy link

TCRAKN commented Jan 26, 2017

@MacGyver1rjq104

Xose has an Arduino Wiki here that looks quite clear:
https://bitbucket.org/xoseperez/espurna/wiki/ArduinoIDE.md

You can choose which "modules" you want to include in your build, compile and upload.
He uses the Fauxmo library for Amazon Echo control. I've no experience of it, so can't comment.

By the way, Itead Studio have hinted that they're going to release a firmware update for the Sonoff that'll include Amazon Echo control. What that entails is anyone's guess!
http://support.iteadstudio.com/support/discussions/topics/11000009015/page/2?url_locale=

Best
Abdul-Khaliq

@aneisch
Copy link

aneisch commented Jan 26, 2017

@TCRAKN, can you post a link to the Sonoff version? Your link has expired.

@TCRAKN
Copy link

TCRAKN commented Jan 26, 2017

@farsonic
Copy link

@TCRAKN Thanks for uploading this....great piece of work and functions well. I had to move one of the functions before it would compile...might be an issue with 1.6.6 of the IDE and will upgrade to test.

I've mounted my Sonoff into an external/waterproof housing and have it controlling pool lights. I see you have enabled the relay to be enabled using the push button. Can this be wired up to an external momentary switch to allow the relay to be triggered? Would like to mount this on my case.

F

@farsonic
Copy link

@TCRAKN also, forgot to say. I tested this with the official WeMo app and it doesn't detect this device. I wasn't expecting it to, but thought I'd verify. Any chance this could be implemented?

@TCRAKN
Copy link

TCRAKN commented Jan 29, 2017

@farsonic
Sure, you can wire up a switch in parallel to the existing one.

I'm not familiar with the WeMo protocols, but it shouldn't be too difficult to implement by someone that knows how to.

I've updated the sketch (below) to correctly illuminate the LED. I'd assumed that a HIGH would illuminate it! Not so. And also, to switch on the relay before attempting WiFi connectivity, rather than after, which caused the switch-on to be somewhat delayed after applying power to the Sonoff.

Sonoff_WeMo_Alexa_Single-Github.ino.zip

Best
Abdul-Khaliq

@farsonic
Copy link

Thanks, I'll look to get some time this week to look into programming an alternate switch for the relay. Shouldn't be to difficult. I'll do some snooping on the WeMo protocols. Already with your code running it is simple to trigger on/off from the command line using something like this

curl --data "request:"1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1">1</u:SetBinaryState></s:Body></s:Envelope>" http://192.168.0.173/upnp/control/basicevent1

@TCRAKN
Copy link

TCRAKN commented Jan 30, 2017

I have to make it clear that I've only added the rudimentary switch and LED functionality to Aruna's original code in this repo. Kudos is his! @kakopappa

Abdul-Khaliq

@farsonic
Copy link

farsonic commented Feb 1, 2017

I've modified the code enough now that it can be discovered by the third party WeMo app called QuickConnect. Seems Quickconnect uses port 49152 which is the standard Wemo port number for its web server. However Alexa will only communicate to it on port 80?

Is there a way to have the ESP8266WebServer listening on two ports?

F

@joeman2116
Copy link

joeman2116 commented Feb 1, 2017 via email

@1rvy
Copy link

1rvy commented Feb 1, 2017

farsonic, its easy enough to run multiple web servers, I have mine with three, one for manual toggling of GPIO and two for Alexa to turn on two different GPIO connected devices.

//define the webserver ports

const unsigned int portMulti = 1900;
const unsigned int webserverPort = 9876;
const unsigned int webserverPort2 = 9877;
const unsigned int webserverPort3 = 49153;

//define the webservers

ESP8266WebServer webserver(80); //Manual web page
ESP8266WebServer server(webserverPort); //Alexa GPIO4
ESP8266WebServer server2(webserverPort2); //Alexa GPIO5
ESP8266WebServer server3(webserverPort3); //testing with app

//In loop enable service

webserver.handleClient(); //Webserver on port 80
server.handleClient(); //Webserver for Alexa Discovery of Study Lamp
server2.handleClient(); //Webserver for Alexa Discovery of Study LED
server3.handleClient(); //Webserver for Wemo App Discovery

// Then for every instance of the following:

server.
HandleNotFound.
handleUpnpControl.
handleSetupXml.
handleRoot.
responseToSearchUdp.

you need to replicate the contents of original one and I simply add a number to the end for each new webserver (find and replace is your friend here!)

e.g.

server2.
HandleNotFound2.
handleUpnpControl2.
handleSetupXml2.
handleRoot2.
responseToSearchUdp2.

I've been playing with the "Wemo Widgets" app to try and add control for the devices on another webserver listening on 49153 but no joy. What have you had to edit to get the app to work with XML/SOAP?

@farsonic
Copy link

farsonic commented Feb 2, 2017 via email

@MacGyver1rjq104
Copy link

@farsonic It works with Alexis as well? When you get it finish, would you be willing to share it? My wife is wanting me to get rid of Alexia because she is so soft spoken and it doesn't work well with her, so I told her I would reprogram all the relays with the ability of an app on her phone once one comes out. This will save my project! Thank you!

@farsonic
Copy link

farsonic commented Feb 2, 2017 via email

@MacGyver1rjq104
Copy link

@farsonic I can't find Quick Connect. Is it the samsung app?

@farsonic
Copy link

farsonic commented Feb 2, 2017 via email

@farsonic
Copy link

farsonic commented Feb 2, 2017 via email

@MacGyver1rjq104
Copy link

Any progress? I have struggle to find solutions. Is there a way to download Wemo's firmware and mod it to work with Sonoff? I believe it works with both the app and Alexa

@farsonic
Copy link

farsonic commented Feb 4, 2017 via email

@Shooter66six
Copy link

i had a thought, can this be flashed onto the esp 8285 ?

@Svengali86
Copy link

Hi @TCRAKN,

I can't get the nodemcu to connect to my wifi with your edited code, tried increasing the time but still fails.

Any pointers?

With best regards

@adweavers12
Copy link

adweavers12 commented Feb 23, 2017 via email

@Svengali86
Copy link

Hi, sorry, my comment wasn't directed at you.

I've got it to work now, by copying the additional switch elements into the original code that worked.

Interestingly my device did not like being discovered with my circuit connected.

@hipnotyx
Copy link

Hi team, sorry if I ask in the wrong place. Does anyone know where to find a code for the sonoff dual with interlocking method and to work with echo dot? I need for controll electrical shutters.
I chose sonoff because it is complete for connection to 220V. Dual because one relay could be used to open and another to close shutter.
The interlocking mode for security reasons. Thank you

@ChezanV
Copy link

ChezanV commented Jun 4, 2019

I think I found the error that generated the restart on ESP12.
I edited switch.cpp, on line 109, i replaced this
server->send(200, "text/plain", eventservice_xml.c_str());
with
server->send(200, "text/xml", eventservice_xml.c_str());
Now works ok with nodemcu1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests