SONOFF Basic using exposed GPIO14 for reed-contact or sensors

1,473 views
Skip to first unread message

Dieter

unread,
Jan 26, 2018, 3:43:48 AM1/26/18
to MppDevices

Hello all,

Hello Mike,

 

I've read on the following link:

https://www.superhouse.tv/21-six-sonoff-secrets/

 

... that on the SONOFF Basic (that I'm quite a fan of with Mikes AutomationManager) the "GPIO14"-Sensor Pin is directly exposed at a Pin. (Besides the 4 Pins that we are using for flashing)

 

I'm wondering if it might be possible to use this to connect eg. a reed switch. (Similar as for the WEMO-Maker)

 

Mike already explained how to set up such a device with a WEMOS with an relay-board on his page... but after I read this about the exposed "GPIO14" I was wondering If it might be also possible by using a SONOFF Basic which I like better and would be cheaper.

 

Unfortunately my electronic skills are quite "limited" :-)  

....  I wasn't able to get a reed-contact working after I set up a SONOFF Basic as a MPP-MAKER device and changed the Sensor Signal-Parameter to "14".

The Reed contact between Ground and GPIO14 contact wasn't detected.

 

Therefore I was wondering If anybody in this MPPDevices Group has already figured out something similar and might be able to help me.

.... or explain to me if this idea is complete nonsense :-)

 

 

Regards

 

Dieter

Mike P

unread,
Jan 26, 2018, 11:23:52 AM1/26/18
to MppDevices
Hey, that's pretty cool.  I poked through the sonoff basic schematics - pin 5 on the programming header is marked as connected to SCL which does appear to be connected to GPIO14.  That would make it very useful as a garage door controller with a sensor.

GPIO14 is selected using pin 5 in the MppMaker (the internal pin assignments from the espressif SDK we use don't match the GPIO numbers nor the external pin numbers for reasons that I've never understood...).

BTW, for anyone that's using the MppController I've just updated it to allow the control of the GPIO pins so it could be used to check inputs and outputs.

DougC

unread,
Jan 26, 2018, 2:25:30 PM1/26/18
to MppDevices
Hi. Sounds an interesting idea. When I got my first Sonoff basic and doing the initial flashing I even soldered on a header pin to the fifth position anticipating that I'd get round to trying something out. Never did that yet and pleased to see you, Dieter, giving it a go. Look forward to your progress.


Doug

Dieter

unread,
Jan 26, 2018, 5:31:16 PM1/26/18
to MppDevices
Thanks for that explanation and the support Mike...  strange thing with all that numbers of pins mixed for a newbie :-)
... but finally I got it!

Here are the single steps that I did to setup a SONOFF Basic as Maker (with sensor)

1) Solder not only the 4 pins that you need for flashing some connectors but also to the 5th pin (besides Ground) 
2) Wiring for simple Reed-contact: Connect one wire of the Reed-contact to that 5th pin "GPIO14 (Sensor-Pin5) and the other one to Ground.
3) After standard-flashing of MPP-Firmware change device to "Maker"
4) Connect the device with "MppController"
5) Choose GPIO: "5/GPIO14" and "Set Input" to "PullUp"
6) Restart the device

--> Afterwards the reed-contact provided the status  to AP

Yeah! :-)


Dieter

Dieter

unread,
Jan 27, 2018, 10:24:15 AM1/27/18
to MppDevices

:-(

It seems that I was "celebrating" too early.

 

After I tried today with 3 different devices there seems something not working properly with evaluating the input signal.

 

After restarting the device the change of the input at GPIO14 jumps properly from high to low as soon as I connect it to ground. 

But after I disconnect the ground again the sensor doesn't jump back to high again.

Only after I set the output manually to "1" with "MppController" the sensor works fine and it recognizes continuously all changes of high/low properly. 

... But after a restart it works again only once (from high to low) .... and stays there.

 

I thought that "PULLUP" setting is exactly what should put up the signal again if ground is not connected... but this obviously doesn’t work properly in my case.

 

All that stuff is new to me... so it might be only a minor issue (at least I hope) ...and somone from you might be able to give a hint how to solve it.

 

Dieter

DougC

unread,
Jan 27, 2018, 2:22:30 PM1/27/18
to MppDevices
Hi Dieter. I was spurred on to try this out. Did all you suggested and got the same result. Fix is fairly straightforward though.

Depending on whether you want the sensor to default to 'on' or 'off' you can force it to stay that way by putting a high value (eg 100K) resistor from the sensor pin to ground (holds it off) or to the 3v3 line (holds it on).

Doug


Mike P

unread,
Jan 27, 2018, 3:54:05 PM1/27/18
to MppDevices
An external pullup resister (to +3V3) does makes the most sense to me too.  This lets you keep the reed switch between gnd and the input, better than trying to run 3V3 out to the sensor, and the external one may be more reliable.

The ESP8266 defaults to float, so my guess is that the pullup setting was working until the next power cycle (or maybe reset) and then went back to float.

I'll add "pullup" as an option in MppSensor.

Dieter

unread,
Jan 27, 2018, 5:05:08 PM1/27/18
to MppDevices
HI Mike,
Hi Doug,

Sounds great, If I'll find a suitable resistor I'll give it a try tomorrow.
Will be even more convenient if that parameter will be possible to be defined in Mikes next update..?. Great news!

Thanks for your help.

Dieter

Mike P

unread,
Jan 28, 2018, 12:26:36 PM1/28/18
to MppDevices
Turns out it was more than the default, I was forcing float.  I've added a parameter to MppMaker and MppSensor "PullUp" which if set and true will use that instead of FLOAT.

If you get the current update for the firmware you can add it as a custom property (use "true", no quotes).  The next AM beta will manage it as a binary property.

Dieter

unread,
Jan 29, 2018, 4:10:17 PM1/29/18
to MppDevices
Perfect Mike that you've managed to add a parameter. Great support from you.
I will test it on the next weekend.
That workoround with the resistor works already for me.

Thanks

DougC

unread,
Jan 30, 2018, 4:39:45 AM1/30/18
to MppDevices
I thought I'd have a fiddle around with this to see if I could make a simple light sensor.  Nothing too clever and I hope to be able to fit the components within the Sonoff itself.

No pretty diagram I'm afraid. Using a breadboard I set up a series of resistors between gnd and vcc with the gpio pin connected in the middle. The Sonoff itself is mains powered.

gnd --- R1 -- R2  -- gpio14 -- R3 -- vcc

R1 fixed 10KOhm
R2 variable 0 - 10KOhm
R3 light dependent (~.5KOhm in full light  ~50MOhm in dark)

So far so good. At the moment I get about .5 volts on the gpio14 pin when I shade the LDR with my fairly opaque finger and 3+ volts if I shine a torch on it. 

The Sonoff seems to react to this and in Automation Manager I see the sensor status flipping between on and off as I'd expect.  But it fails every now and then.

So all is not quite right.
With the resistance bridge still in place across gnd - vcc. I can simply short the gpio pin to gnd or vcc to flip the sensor state. I can do this as often as I like and it never fails. On the other hand, if I connect the gpio pin into the resistance bridge as per my sketch above it works a few times but then stops responding. After a few seconds, the sonoff led flashes once and then it recovers. From AM I see an error message ECONNREFUSED (connection refused)

I've not as yet got much in the way of diagnostics. I guess I might see something useful if I connect rx and tx to my USB adaptor and fire up lualoader. 

My question at this point is really if there is something fundamentally silly about what I've set up. I can see that the input signal I'm producing is not a clear cut on/off but does that matter? 

Mike P

unread,
Jan 30, 2018, 6:09:47 PM1/30/18
to MppDevices
The flash sounds like the device is restarting - connection refused or unknown request could be reported by AM.

Yes, you'll probably be able to see more if you have the USB port.  MppController is pretty close to the LuaLoader now.  Curtis found that windows often downloads the 32bit java by default, so if you're having trouble with it see if you need the 64bit java.  The dll that I provide for download is the 64bit version.

If you see a "panic" message that'll be a bug on my part.  If you see it shows the restart message it probably means something in hardware upset it, though watch for any out of memory indication.  I'm wondering, with that setup, whether noise is triggering so many interrupts that the chip runs out of memory.

I'd be tempted to try it directly, without the resistors, though a 10K between VCC and gpio14 is probably a good idea. If you're not using one you'll need the latest firmware and AM beta to use PullUp.
What should happen is when the light shines the sensor conducts and GPIO14 is drawn to ground reporting OFF.  When dark the pullup resistor overpowers the 50M and you get 3V3 on the input for ON.
Of course the sensor and the sonoff also need to share gnd.

Do double check the max current supported by the sensor -  it comes through a 10K pullup so will be 0.3ma which is pretty tiny.  I *think* the pullup on the sonoff is around 5k-10k if I read properly so the sensor needs to be able to handle about 0.6ma to be safe (which is still pretty tiny).

DougC

unread,
Jan 31, 2018, 3:05:47 PM1/31/18
to MppDevices
Some small progress. I took the suggestion of forcing 64bit Java to install and that seems to have got MppController working for me. Or at least it has on a very old and flaky laptop running Windows 7. My current laptop is afflicted with Windows 10 which seems to be, well, plain awkward. 

I got a few log entries from MppController that I'll mail to MikeP presently. A few 'unknown errors' and the occasional 'panic' message may offer some clues. In the meantime I'll try different combinations of the components to see what if any difference that makes.

Dieter

unread,
Jan 31, 2018, 3:32:28 PM1/31/18
to MppDevices
I had also some issues to get MPPController running on my Win10 System. Installing 64 bit version first didn't work. What helped was that I first deinstalled previous installation and during new 64bit installation I've chosen a different location (different drive). Afterwards it worked.... strange.

Dieter

Mike P

unread,
Jan 31, 2018, 4:44:55 PM1/31/18
to MppDevices
Good the MppController is sorted, the log is very helpful.

I've posted an MppMaker and MppSensor fix that suppresses the extra interrupts when the sensor bounces that should help with the crash.

DougC

unread,
Jan 31, 2018, 11:01:20 PM1/31/18
to MppDevices
Awesome Mike. I still see some repeating of the sensor triggering in the log but massively fewer than before. I see no failures/reboots even if I strobe the sensor with my torch as fast as I can. So your workaround is totally effective.

I still need to a bit of work on my design though! Still some noise in the system. Also I expect I'll have some issues with undesirable flip flopping in marginal light conditions, but I feel that's all sortable now.


thanks again
Doug

Dieter

unread,
Feb 4, 2018, 9:06:43 AM2/4/18
to MppDevices

Hi Mike,

 

today I've tried to get my reed-contact working on my SONOFF Basic without a pullup-resistor.

But I still were not able to manage it. :-(

 

Here is what I did:

1) Updated Firmware Maker-Firmware to 1.1.4 (including 5.1.4 ESP-Firmware)

2) Set Properties: PullUp=true

3) Set SensorPin=5

4) Connected reed-contact directly between GPIO14 and ground (without any resistor)

 

After reboot of the device everything works like it should .... but only for one Sensor-Switch from high to low.

Afterwards the signal stays at low.

I've connected MPPController and monitored the data. --> I will send in to you per mail.

 

It works fine after I set the signal manually on high with MPPController..... Then the sensor gets on / off like it should depending on the reed-contact.

But as soon as I reboot it doesn't work anymore.


 

Did I anything wrong?

Hope you can help me Mike.

 

Regards

 

Dieter

Dieter

unread,
Feb 6, 2018, 4:37:25 PM2/6/18
to MppDevices

 

Hi Mike,

Hi Doug,

 

Awesome news! 

Finally my Sonoff basic is working without a pullup resistor. :-)

 

Mike found the issue in the code why the pullup didn't work properly.... and provided an updated version yesterday on his page for the "MPPMaker.lua" ... with this file everything works fine!

 

Thank you very much for your great support Mike!  .... It's really amazing that you always find solutions for all that weird issues :-)

 

Now I'm going to order some additional SONOFFs ... could think of several usages with sensors :-)  

 

Dieter

Curtis E

unread,
Feb 10, 2018, 5:57:45 PM2/10/18
to MppDevices
well, congratulations, but I still can't get a simple switch to work between gnd and 14 (5)

I have sensor as 5, pullup true and nothing works by grounding 5.

I really need to get past this, kind of crazy. On a wemos it works with a sensor easily, but not on the sonoff.

Help please.  I must be missing something simple.

Thanks,

Curtis

Curtis E

unread,
Feb 10, 2018, 6:29:58 PM2/10/18
to MppDevices
Forgot to confirm this is as MppMaker.

If I just load MppSensor, grounding 5 triggers the relay just fine.

Dieter

unread,
Feb 10, 2018, 6:33:25 PM2/10/18
to MppDevices
Hi Curtis,

That's strange. I've got it working also on a second sonoff basic without problem.
I assume you've updated to the latest mppmaker firmware already, right? I've manually downloaded the last version from Mike's page after he corrected the small bug.... Then it worked for me. I don't have a guess why it doesn't work at your sonoff.

Dieter

Curtis E

unread,
Feb 10, 2018, 6:51:49 PM2/10/18
to MppDevices
yes, 1.1.5 Maker.  I am going to wire another and see what happens.

Thanks

Curtis E

unread,
Feb 10, 2018, 7:46:05 PM2/10/18
to MppDevices
exactly the same issue.  If I set it up as a sensor, grounding 14(5) changes the state of the relay.

Set up as Maker, grounding 14(5) does absolutely nothing.

I feel certain I a missing something that will make me feel foolish, lol, but will be happy to try anything.

I have added an external resistor just in case, with no results.

Thanks guys!

Curtis E

unread,
Feb 10, 2018, 10:36:38 PM2/10/18
to MppDevices
Got it!

No one can ever ask me what I did wrong.......

However, it would have been very nice if someone would have spelled out that I needed to add "relay follows sensor"

Some of us need more guidance than others....

Curtis

Curtis E

unread,
Feb 10, 2018, 10:42:40 PM2/10/18
to MppDevices
Mike, 

Can a momentary switch be used? This set up is great for using a normal off/on toggle switch, but a momentary that changed the state no matter what it is would make for a nice install on other projects.  It seems that this is already available but I an not finding it.  Perhaps I saw it somewhere else.

Thanks,

Curtis

Mike P

unread,
Feb 11, 2018, 12:07:34 PM2/11/18
to MppDevices
Hi Curis, glad you've got it sorted :).

Triggering the relay onboard is a convenience feature but not absolutely necessary.  It's also possible to see the GPIO input state by watching the AM Server, the state changes should be immediate.

To add a momentary function I'd have to add debouncing on the interrupt and maybe retain state - this adds a lot of code which eats up more memory. I avoid onboard logic as much as possible by offloading those kinds of functions to the AM Server - a rule to watch for the sensor ON event with an action to toggle the relay should be almost instantaneous.
Reply all
Reply to author
Forward
0 new messages