A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By dresdner353
#64894 Hi folks,
I'm new to the ESP-8266 dev, having only been introduced to the idea last December when chatting to a friend. Since then, after ordering bits and bobs via Aliexpress, I've got a first stab of a project up and running and there for anyone to take a look.

Having looked at a lot of existing projects and gone through the many good sample sketches, I decided I'd try to build a simple firmware that lets me configure an ESP-8266 in AP mode with my desired WIFI creds and other config. Once the device then connects to my LAN, I wanted to have it auto-discovered by a python server running on a Pi or other computer.

The command structure for instructing the devices to do things would be HTTP-based with a JSON feel to it. The objective I have is to get to a point where I could introduce a device onto the LAN and have a server immediately know what to do to control it. So the idea behind using JSON was to use it to identify the device and its capabilities to the server side.

Just in the last few days, I've deployed the first two devices which are both the standard Sonoff switches in more secure housings and are being used to turn on two up-lighters 30 mins after sunset and off again at 2AM. I'll try to develop this idea over the coming months. Ideally I want to get to a point where the python server is also running a web server that provides a single representation of all discovered devices and lets me turn on/off any switches, read sensors and also define timed actions etc for automated control.

The sonoff in the more secure housing was achieved by routing 3-core mains flex into the box, securing with a cable tie and hot glue. I also took a tap from the earth onto a lasso around the switch for safety.
Image

Then the switch I used was a momentary switch with integrated LED. I soldered the switch pins to the Sonoff onboard switch contacts and snipped the onboard LED, routing its pins to the integrated LED contacts on the switch.

Image

Have to say, I'm quite pleased with the results and usability.

You power the device up, the LED flashes at a medium rate for 5 seconds.. you've got that 5 to press the button to put the device into AP mode for setup. When you press the button, the LED switches to a fast rate to indicate the AP mode.

If you enter AP mode, just attach to the esp8266-<cpuid> SSID, no password and it will use captive DNS to get you to a config screen where you set a zone name, wifi creds and names for the switches and their initial states on boot. The example here shows 4 switches. In reality only the first is actually doing anything. The others are dummies that do nothing. I've kept them in for testing. The definition for the set of switches is in the sketch code. If you leave their names empty at config stage, they effectively are disabled from then on.
Image

Once you click apply, the device stores config in eeprom and reboots... the LED flashes medium rate for 5sec (still allowing you press the button for AP mode), then flashes at a slower rate as it tries to connect to the configured Wifi. The button then acts only as a manual on/off switch for the relay and also activates the LED as you toggle into the on position.

The device registers on MDNS and DNS-SD with the discovery string "JBHASD" and "tcp". I've included some sample python scripts to show how you can detect the devices using zeroconf.

Then you can manipulate the devices by accessing their JSON url to get the current device details and also to pass in control and state directives in GET or POST form so that you can turn on/off the desired switch. The response in all cases will be the current state of affairs after the change is made.

$ curl 'http://192.168.12.196/json'
{ "name": "esp8266-9840833", "zone": "Playroom", "controls": [{ "name": "Uplighter", "type": "switch", "state": 1 }] }
$ curl 'http://192.168.12.196/json?control=Uplighter&state=0'
{ "name": "esp8266-9840833", "zone": "Playroom", "controls": [{ "name": "Uplighter", "type": "switch", "state": 0 }] }
$ curl 'http://192.168.12.196/json?control=Uplighter&state=1'
{ "name": "esp8266-9840833", "zone": "Playroom", "controls": [{ "name": "Uplighter", "type": "switch", "state": 1 }] }

The example above also helps describe the idea behind the self discovery. We get a name, Zone and details on each control the device has. That type is only switch for now, but can be enhanced for sensors, sliders, etc. It should be possible for me to end up with a server script that gives us a web page with all discovered devices and their controls.

In terms of the firmware, the following in-memory array shows how you setup the desired switch layout you need. Read those entries as name, relay pin, LED pin, manual pin, initial state and current state. The entries with the -1 values for PINs are the dummies or could represent switches that have fewer capabilities.. such as relay only, LED only etc.

struct gpio_switch gv_switch_register[] = {
{ "mains", 12, 13, 0, 1, 0 }, // Standard Sonoff
{ "Fred", -1, -1, -1, 0, 0 }, // dummy switch
{ "Barney", -1, -1, -1, 0, 0 }, // dummy switch
{ "Wilma", -1, -1, -1, 0, 0 }, // dummy switch
{ NULL, -1, -1, -1, -1, -1} // terminator.. never delete this
};

So it makes it easy to adapt this sketch to different pin layouts as needed. You've still got the AP mode then to give names to these controls depending where you deploy it and what purpose each switch has.

here's a link to the project so far. It should work off the cuff on a Sonoff and be easy to adapt for other variants of the 8266.

https://github.com/dresdner353/jbhasd