Introduction: Program Sonoff Using Blockly

About: thanks

Sonoff is a cheap ($4.85) wireless component built by http://www.iteadstudio.com.

It is also hackable, that is you can flash it with your own program.

This instructable will show you how to create you own .lua program, using blockly, that can be loaded onto the sonoff.

To wire up a sonoff for programming you can follow one of the many online tutorials such as the one located here: http://tech.scargill.net/itead-slampher-and-sonoff

To flash the sonoff with an operating system I use nodemcu_flasher32bit.exe (attached), this operating system is contained in the nodemcu_float_0.9.6-dev_20150704.bin file (attached). The single sonoff momentary depress switch should be pressed and power cycled to place the sonoff in program mode.

Once the operating system is loaded, your program made with blockly can be loaded using LuaLoader (attached).

Step 1: Using Blockly to Create Custom .lua Programs

Blockly (https://developers.google.com/blockly/) is an amazing free javascript program built by google that can be used to design programs. I used it with MIT App inventor 2 to create android apps. Kids use "scratch" during their "Hour of code" (https://hourofcode.com/us/learn) to learn programming. Scratch is based on blockly. I have created a blockly solution that will create .lua code for the sonoff and esp8266 (in general).

Attached is a blockly.zip file that contains all the javascript code as well as an example .xml file. To use it, unzip the blockly.zip to you laptop and navigate to the demos/code directory. Then, using Internet Explorer version 11 or later open index.html file. This will bring up a development environment similar to the attached picture.

Press the load button and enter: "sonoff.xml" this will load an example solution I created for the sonoff that turns the relay (connected to gpio 6) on or off.

You can then manipulate the blocks and look at the lua tab to see the created lua code.

Copy and paste this .lua code to an init.lua file and upload this to the sonoff using lua_loader

Step 2: Testing Out the Sonoff Using the Raspberry Pi

The sonoff needs to be commanded on or off. I also created a raspberry pi solution (using php and apache web server) to allow a person to configure and command a set of esp8266 based devices (such as the sonoff). To test the sonoff, download the 2 gb zipped .img file located here: https://mega.nz/#!bkJmUbJA!DRGMBhhNsRee5qcLjW7xvRkY9QK5cPCJxlUzxsGR2aU

After download, unzip and write the .img file to a 8gb mini sd (SANDisk) card. Use Win32DiskImager.exe(https://sourceforge.net/projects/win32diskimager/) to write the .img file to the mini sd card.

When placed in a raspberry pi model 3 the pi will create a wireless access point with ssid="pipboy2000" and password="" which can be logged in to by your laptop. Then open a browser to 172.24.1.1/Paulware to access the sonoff.