ESP8266 based web configurable wifi general purpose control (Part I)

From AT commands to firmware controlled

The kind of new ESP8266 wifi module is not only cheap, about 3-4 dollars, its also a pretty versatile module that enables tons of remote control applications (yeah, internet of things). The first project that came to mind is to move my antenna switch control from being run by a RaspberryPi board and an 8 relay board, which makes the project pass the 100 dollar mark, to being controlled itself by a wifi board that can be programmed. Well, the ESP8266 was the solution for me, and cheap! I will describe the project, and although it is not finished yet, I will get as far as I can and then continue updating as I progress.

 Getting started

There is plenty of material out there to get anyone going. The key things to know is that you will need a USB to serial module to initially talk to the ESP8266. Any FTDI232 based module will pretty much work. Then you need to chose a serial terminal to send commands to the serial module that will send commands to the ESP8266. Something like CoolTerm or SSCOM32 would do the job. I use SSCOM on Windows and CoolTerm on the Mac, simply because CoolTerm for Windows is not working for me (crashes). Also, when you start copying LUA code into the module, CoolTerm does a great job.

My setup

ESP8266 of course, pic below.

ESP8266_V091

Any USB to serial module would do it. Here is one:

FTDIModule

 

Now connect them as below. Note that GPIO0 should be left floating except for firmware upgrade.

esp8266-reflash-firmware copy

Getting ready to load new firmware

Connect your ESP8266 module as follows:

  • Vcc = 3.3V (needs around 300-400mA peak)
  • Gnd = -ve ground
  • CH_PD = Chip enable so always +ve
  • RST = Leave floating or ground to reset
  • GPIO0 = Normally floating but this needs to be grounded when you start the update.
  • UTXD = Tx data connect to Txd on FTDI/Serial interface
  • URXD = Rx data connect to Rx of FTDI/Serial interface

You also need to download the new firmware and update tool from ElectroDragon: http://goo.gl/uHM6aO The firmware tool is called XTCOM UTIL.zip

Before you start the update, make sure your ESP8266 is working normally as follows:

  1. Connect-up as above but leave GPIO0 floating.
  2. Access the serial interface with a terminal program such as PuTTY or Termite.
  3. Set the terminal s/w baud rate to either 57600 or 115200 (depends on whether you have an early or later model). The CoolComponents models are 115200.
  4. Power-up the ESP8266 and you should see some readable text indicating that it’s ready. If you get nonsense try changing the baud rate.
  5. If all is well, try some AT commands.
  6. Try AT+RST which should re-start the module.
  7. Try AT+GMR which should give the firmware version.
  8. NB: When entering AT commands there are no spaces and you cannot use backspace to correct errors as each character is sent as it is typed!

If all is well, you can move on to the firmware update.

Loading the LUA interpreter firmware

This project requires firmware programming in LUA, so the next thing we will do is load the firmware. Here you can find more detailed instructions on how to load firmware and a number of tools to do it. Lets download the LUA firmware from here. And the flash tool from here. Flash the module making sure you pull down GPIO0, and turn the module off and on; flash start memory is 0x000000.
Disconnect GPIO0, turn the module off and on, If everything went smooth, now you can type node.restart() on the serial terminal (I’m using CoolTerm on Mac for this example) and you should get a response like in the picture below.

LUA_reboot

Simple LUA example

Lets look at very simple example of programming the module. We will use the startup functionality built in the LUA firmware as follows:

  1. create a file named init.lua
  2. write LUA commands into this file
  3. close the file

This file will get executed every time the module boots. This gives you a lot of possibilities if you want to use the module as a server or a device responding to events. A complete reference to the ESPressif SDK can be found here. Lets write some simple code.

– Open the file for writing
file.open(“init.lua”,”w”)
— Write a simple text message that will be echoed to the terminal
file.writeline([[print(“Some simple message”)]])
— Set the mode to SOFTAP
file.writeline([[wifi.setmode(wifi.SOFTAP)]])
— Get the new mode and print it
file.writeline([[print(“ESP8266 mode is: ” .. wifi.getmode())]])
— Close the file
file.close()

Put these code in a file (textfile) and we will send the text file to the ESP8266 using CoolTerm. Make sure that the Connection->Options>transmit settings allow a delay so the lines can be transmitted without error, as follows:

CooltermSettings

Select Connection->Options->Send textfile and send the above file. You should get the output below:

Exmaple_output

Yeah! All working! Now lets get a bit more complex.

A more elaborated LUA example: GPIO0 blinker with reset signal on GPIO2

We will write some code that sets up GPIO0 and GPIO2, creates a TCP server and turns GPIO0 on and off ‘n’ number of times according to the value sent through TCP, upon completion of the sequence it turns GPIO2 off and on so it can be use as a reset signal. Here it goes:

– Open the file for writing
file.open(“init.lua”,”w”)
— Open the file for writing
file.writeline([[print(“GPIO0 blinker and GPIO2 reset”)]])
— Open GPIO0 and 2 for output
file.writeline([[gpio.mode(8,gpio.OUTPUT)]])
file.writeline([[gpio.mode(9,gpio.OUTPUT)]])
— Wait a bit to ensure completion
file.writeline([[tmr.delay(10) ]])
— Cycle GPIO2 so it can be use as reset signal
file.writeline([[gpio.write(8,gpio.HIGH)]])
file.writeline([[tmr.delay(10)]])
file.writeline([[gpio.write(8,gpio.LOW)]])
— Create TCP server
file.writeline([[sv=net.createServer(net.TCP, 30) ]])
— Listen on port 9999, and callback function ‘c’
file.writeline([[sv:listen(9999,function(c)]])
— Wait until payload ‘pl ‘ is received
file.writeline([[c:on(“receive”, function(c, pl)]])
— Check for valid number received (should be between 1 and 16 for this example)
file.writeline([[if tonumber(pl) ~= nil then]])
file.writeline([[if tonumber(pl) >= 1 and tonumber(pl) <= 16 then]])
file.writeline([[print(tonumber(pl))]])
file.writeline([[tmr.delay(10)]])
— Send another reset event on GPIO2
file.writeline([[gpio.write(8,gpio.HIGH)]])
file.writeline([[gpio.write(8,gpio.LOW)]])
— Loop according to the number received
file.writeline([[for count =1,tonumber(pl) do]])
file.writeline([[ print(count)]])
file.writeline([[tmr.delay(10) ]])
— Blink GPIO0
file.writeline([[ gpio.write(9,gpio.LOW)]])
file.writeline([[tmr.delay(10)]])
file.writeline([[gpio.write(9,gpio.HIGH)]])
file.writeline([[c:send(“Sequence finished”) ]])
file.writeline([[end]])
file.writeline([[end]])
file.writeline([[end]])
file.writeline([[c:send(“Action completed”) ]])
file.writeline([[end)]])
file.writeline([[end)]])
file.close()

Lets send the file to the module. You should see the output below:

Blinker

Lets do a node.restart(), you should see the first welcome message and the program will go into wait for an event to come from port 9999.

blinkerRestart

Now you need to connect the module to the network, so lets issue the following commands. You will need your wifi router SSID and password :

wifi.setmode(wifi.STATION)
wifi.sta.config(“your_SSID”,”your_password”)
print(wifi.sta.getip())

SettingIP1

To test the events receiving part, you can use any old TCP terminal emulator, I use ‘TCP Test Tool” for iPhone, and it allows me to connect and send data to the module. Here is a screen picture of the iPhone app, connected to 192.168.1.49 port 9999 and sending the number 8, receiving back the messages.

TCP_Test_Tool

Now lets see what we received on the serial terminal:

ReceivingTest1

And there you go, we received the number eight and cycled through the blinker 8 times, that means GPIO0 cycled 8 times while GPIO2 cycled once to indicate a reset at the end of the sequence.

Conclusion

The ESP8266 is a cheap but powerful self contained wifi computer. It can be controlled through firmware upload and if you chose to do it with the LUA firmware, things get quite simple and results are achieved. The possibilities are limitless. On the next posting I will explain how to program the ESP8266 so a USB to serial module and a terminal is not necessary: it will all be done via web. Also, I will explain how to drive a binary counter and control 4 relays (multi on/multi off) using GPIO0 as control and GPIO2 as reset. This way the ESP8266 really turns into a powerful device that can be added to any automation project and its easy to program on site.

References

I tried to list all the helpful resources I found, I’ll update as more show up.

ESP8266 specs, descriptions, etc, by Nurdsape
Firmware that lets you easily control the GPIO0 and GPIO2 pins, by Electrodragon
ESP8266 community GitHub space. Web server example, Arduino example, bcc compiler and code examples
Firmware upgrade detailed instructions by UKHASnet
More documentation, SDK, firmware, flash tool, XTCOM tool, etc
More tools, documents, toolchain for Linux and Windows
LUA based interactive firmware, examples and tools by NODEMCU
SSCOM32E.exe serial terminal
ESP8266 and LUA examples  by Scargill
Problems with FTDI drivers? Check this out
ESP8266 specs, descriptions, tools from Electrodragon

Posted in Uncategorized | Leave a comment

Ham Radio Wireless Antenna Selector

As a spinoff the speaker control project, I have developed a wireless antenna selctor. It handles 4 different antennas, selectable through web browser or an iphone app.
A Raspberry Pi is used as the main controller running apache, python and php. A Custard Pi board with 8 relays is used to communiate with the Raspberry Pi and handle the switching of the antenna lines. Shortly I will post pictures of the project and app.
photo

Posted in Uncategorized | Leave a comment

Co-creation and crowdsourcing in Oil and Gas software: paving the future for highly technical software.

The challenges faced by the Oil and Gas industry are enormous. The easy reservoirs have been found and are mostly depleted. Oil companies need to replace their reserves, hence they need to find new proven assets. Whatever is left to be found is well hidden, difficult to detect, difficult to identify, and extremely difficult to model and develop. Existing reservoirs that are currenlty being produced need to be handle with extreme care, ensuring that oil or gas recovery is maximized and production is optimized. Fortunately, a new class of reservoirs entered the picture a decade or so ago: unconventionals. Unconventional reservoirs are a class of reservoir type that fundamentally differs from classic reservoirs. Unconventional reservoirs are plenty and distributed across the globe. They have brought a new hope for asset replacement. In unconventional reservoirs, Oil or gas is trapped in very low porosity and very low permeability medium. The science for exploring and modeling these reservoirs is much less developed than that for conventional reservoirs. Producing these reservoirs require also new techniques such as fracking, expensive and resource intense. Optimization of these production techniques is of utmost importance.
Going forward we will increasingly be looking in more challenging environments, reservoir with only become most complex and the technology and science needed to successful exploit them with need to continue to grow at a rapid pace to keep up.
Software is pervasive across these wide spectrum of big challenges. There is no single person, group or company that can provide solutions to all these challenges. Collaboration is needed to converge to efficient solutions that solve the problems at hand. But collaboration is not enough. Efficient solutions need diversity of approach, different points of view that compete but also build upon each other making the end solution much more efficient and powerful.
Oil and gas professionals and experts who use software to address their challenges must work together to find solutions, but to be able to do it within the boundaries and policies of their organizations, a proper software solution is indispensible.
Since the introduction of computers in the geosciences world (oil and gas, exploration through production) back in the 70s and 80s, the standard software product used were single software packages that did something (or somethings) very well in the user’s opinion. Using this single, monolithic packages, the user had access to what he or she considered to be the best technology available. The IT departments loved it because there was a single infrastructure to maintain, and data managers enjoyed it due to the fact that there was one data storage to maintain. This single monolithic packages did not allow other players to come and deliver technology on top of them. They were close. As things got more and more complex towards the end of the 80s and through the 90s, single packages could not address all the needs of the geoscientists, so they opted for bringing in different packages. Each package, usually from a different vendor, will offer best in class technology according to the user’s criteria. This is a scenario that is frightening for IT and Data Managers. Usually each different product needs a different IT infrastructure and a different data management scheme. As for the single package, it was not possible to have 3rd parties developing technology on top of them. Closed systems.
But things kept getting more and more complex. To the point the industry realized that there was no way a single vendor or even a handful of large vendors, could deliver all the technology needed to solve the huge challenges faced. A different paradigm was needed. The new paradigm was a combination of software platform and extensibility. A platform could be defined as a major piece of software, as an operating system, an operating environment, or a database, under which various smaller application programs can be designed to run through an extensibility layer. Oil and gas companies, by means of using a platform with robust extensibility capabilities, can then consume technology from many 3rd party vendors, not only technologies produced by the platform vendor. This diversity of technologies give the oil and gas company a much larger spectrum of choices. Additionally, oil and gas companies can develop their own technology on top of the platform.
The combination of platform and extensibility brings the opportunity for new business models in oil and gas software. Small vendors, even a one man shop, can now produce differentiating technology and deliver it to large companies through the platform, provided the platform is open to anyone to deliver technology on top of it. Even more, several vendors can deliver solutions in the same space, which creates a healthy competition that naturally advances the quality of the solutions.
An open and extensible platform is a petri dish for innovation. By allowing small and big players alike to develop and deploy technology to such a demanding, difficult and mature industry as oil and gas, it naturally nurtures co-creation, because it enables anyone to participate in the improvement of the workflows that can be executed within the platform. Modules from different vendors can participate in a single workflow, adding value to the end user. A successful extensible platform for oil and gas will enable co-creation and crowdsourcing within a very conservative industry and will shape the future of how very complex challenges are addressed efficiently.

Posted in Uncategorized | Tagged , , , , , , | Leave a comment

Study the behaviour of technical communities by tracking their movement through wearables

This project has the goal to study the behavior of technical communities by tracking their movement through a wearable device and a number of readers. For organizations that are dealing with complex technical challenges it is extremely important that the technical population interacts and collaborates in order to exchange ideas and solutions . This sharing and collaboration intensifies and improves the quality and the quantity of innovation. A particular setup at the workplace will influence the way people move, gather and interact. Studying these movements, how they are influenced by changes in the physical environment and subsequently how it correlates to productivity and creativity gives a tremendous insight on how to foster innovation.
The project will implement the tracking hardware and software and it will gather population movement information as well as population behavior. Other metrics will be establish to understand how changes affect innovation and productivity. The collected data and interpretations results will be shared with the backers.
I have this project posted in Kickstarter at https://www.kickstarter.com/projects/horacio/social-behavior-in-technical-communities

Posted in Uncategorized | Leave a comment

Part I: iPhone controlled home speakers switch using the RaspberryPi and the CustardPi relay board

To get started, you need to have apache2, php and mysql loaded on your raspberry pi as we will be using web access and web services and a small database to store configuration data to support multiple iPhones controlling the speaker switch.
Then you need to get familiar with the Custard Pi relay board and install all the support software on your Raspberry Pi to be able to control the Custard Pi via Python. Here is a good explanation on how to get all this done http://www.slideshare.net/SeggySegaran/custard-pi-6i2c
The scripts that turn the relays on and off work in a very simple way. The 8 relays on the Custard Pi board are grouped in pairs to support stereo setups. Relays 0-1 are switch 1, relays 2-3 are switch 2, and so on. If you want to turn switch 1 on, the scripts will ensure all relays are turned off and then relays 0-1 are turned on, so switch 1 is on. The same logic is applied to the other relays.
Here is the python code to turn switch 1 on (relays 0-1). The Python files are located in the main directory that you choose under a directory named CustardPy, filename is switch1.py:

#1/usr/bin/env/python
import RPi.GPIO as GPIO
import time
import cpi6
GPIO.setmode(GPIO.BOARD)
#start program
board1=cpi6.add1
cpi6.setasoutput(board1)
cpi6.setbit(board1,cpi6.ONrelay0)
cpi6.setbit(board1,cpi6.ONrelay1)
cpi6.clrbit(board1,cpi6.OFFrelay2)
cpi6.clrbit(board1,cpi6.OFFrelay3)
cpi6.clrbit(board1,cpi6.OFFrelay4)
cpi6.clrbit(board1,cpi6.OFFrelay5)
cpi6.clrbit(board1,cpi6.OFFrelay6)
cpi6.clrbit(board1,cpi6.OFFrelay7)
GPIO.cleanup()
import sys
sys.exit()

and here is the php code to call the python script, filename is switch1.php and should be placed in the main directory:


exec (“sudo python CustardPi/switch12345.py”)

You will call the php file from your browser or a networked app (iOS for example) to turn on the speaker switch.
The code for the other combinations is :

To turn on switches 1 and 2
#1/usr/bin/env/python
import RPi.GPIO as GPIO
import time
import cpi6
GPIO.setmode(GPIO.BOARD)
#start program
board1=cpi6.add1
cpi6.setasoutput(board1)
cpi6.setbit(board1,cpi6.ONrelay0)
cpi6.setbit(board1,cpi6.ONrelay1)
cpi6.setbit(board1,cpi6.ONrelay2)
cpi6.setbit(board1,cpi6.ONrelay3)
cpi6.clrbit(board1,cpi6.OFFrelay4)
cpi6.clrbit(board1,cpi6.OFFrelay5)
cpi6.clrbit(board1,cpi6.OFFrelay6)
cpi6.clrbit(board1,cpi6.OFFrelay7)
GPIO.cleanup()
import sys
sys.exit()

To turn on switches 1 and 3:
#1/usr/bin/env/python
import RPi.GPIO as GPIO
import time
import cpi6
GPIO.setmode(GPIO.BOARD)
#start program
board1=cpi6.add1
cpi6.setasoutput(board1)
cpi6.setbit(board1,cpi6.ONrelay0)
cpi6.setbit(board1,cpi6.ONrelay1)
cpi6.clrbit(board1,cpi6.OFFrelay2)
cpi6.clrbit(board1,cpi6.OFFrelay3)
cpi6.setbit(board1,cpi6.ONrelay4)
cpi6.setbit(board1,cpi6.ONrelay5)
cpi6.clrbit(board1,cpi6.OFFrelay6)
cpi6.clrbit(board1,cpi6.OFFrelay7)
GPIO.cleanup()
import sys
sys.exit()

and so on and so forth, you get the gist.

The iPhone code to control the switches can be accessed here.

Posted in Uncategorized | Leave a comment

iPhone controlled home speakers switch using the RaspberryPi and the CustardPi relay board

This is a project that I had in mind since we moved to the new house in the Houston Heights and we ended up with speakers throughout the house. Then I got a Sonos system for my birthday, which works great and can be easily controlled by iPhone or iPad o laptop. We got a standard push button speaker switch box but I thought it would be really cool to control the speakers via the iPhone since that is how I control the Sonos music.
The project started by using the server I have already setup on my RaspberryPi which can respond to php commands from inside or outside my network. Then I added a relay board CustardPi, wrote the php web service that controls the CustardPi, the Python scripts and the iPhone app that communicated with the web service and sues the commands. The relays then respond to the iPhone app and switch the speakers on and off.
Full description coming on my next post.

Posted in Uncategorized

iPhone home temperature logger

Our AC/Heating system is driving me nuts and I can’t get it calibrated in order to keep a balanced temperature around the house, kid’s rooms are boiling while bedroom and living room are freezing so I developed a prototype of an iPhone temperature logger using the http://www.weatherdirect.com/ system. The goal is to help me look at historical temperatures and then calibrate each room air flow to equalize as much as possible the temps. The system is comprised of:

- 4 thermometers
– base station
– Mysql database to hold the temperature and humidity data
– PHP service that retrieves the temperature from the Weather Direct site and stores it in the database at 5 minute intervals
– a PHP service that retrieves the name, temperatures, humidity and timestamp data for posting on the iPhone application
– iPhone app putting it all together

Next phase is automation of the air flow. Still thinking…

Here is a screenshot of the app.

photo22

Posted in Uncategorized | 1 Comment