ESP8266 under MicroPython : using the MOD-IO board (UEXT connector)

FR: Voici une traduction d'un précédent article pour nos amis d'Olimex.
ENG: Here a translation of a previous article for our Olimex's Friends.

Some weeks ago, we did release an article about "MicroPython on the ESP8266-EVB (Evaluation Board) from Olimex" which explained how to use the relay and button of the ESP8266-EVB board.
Now we will explore some of the possibilities available with the UEXT connector and UEXT extension board.
Olimex's ESP8266-EVB
UEXT connector and extension boards
The interest of ESP8266-EVB reside in the UEXT port! It is an 10 pin's IDC connector which carry an I2C bus, a SPI bus and serial interface (asynchronous connexion).
UEXT mapping on the ESP8266
Thank to the mapping between the UEXT connector and the ESP8266, we will be able to take the controle of UEXT interfaces with MicroPyhton. The best for the end we would not have to care about wiring! just plug and play with the board :-)

The code will be compatible with all MicroPython plateform running at 3.3V logic. If you don't have any UEXT interface, you just have to wire the I2C bus on the UEXT connector.

MOD-IO Board
MOD-IO is a development board allowing you to add relays & analog and digital inputs on any board exposing an UEXT connector (or bus I2C). The board has 4 relais, 4 opto-isolated digital inputs, 4 analog input (3.3v Max). Except for the 3 analog inputs, the MOD-IO board is fully compatible with the 24V industrial voltage.
The MOD-IO board
The MOD-IO boards can be daisy chained and are adressables. So you can use several MOD-IO boards together!
Thanks to the UEXT connector, wiring the boards as never been so easy! (kind useful for learning/teaching purpose).
Wire together a carte MOD-IO, UEXT Splitter and ESP8266-EVB
MOD-IO Library
We did wrote the modio MicroPython library to ease the usage of MOD-IO board. The library is available on the landing GitHub page dedicated to MODIO board under MicroPython.

Simply upload the modio.py file on your MicroPython microcontroler.

Test the MOD-IO board under MicroPython
# Using the Olimex's MOD-IO board with an ESP8266 under MicroPython
#
# Shop: http://shop.mchobby.be/product.php?id_product=1408
# GitHub: https://github.com/mchobby/esp8266-upy/tree/master/modio

from machine import I2C, Pin
from time import sleep_ms
from modio import MODIO

# the I2C bus on the UEXT connector
i2c = I2C( sda=Pin(2), scl=Pin(4) )
brd = MODIO( i2c ) # default address=0x58

# === Read analog inputs ===========================
for input_index in range( 4 ):
    print( 'Analog %s : %s Volts' %( input_index,brd.analogs[input_index] ) ) 

brd.analogs.raw = True
for input_index in range( 4 ):
    print( 'Analog %s : %s of 1023' %( input_index,brd.analogs[input_index] ) ) 

print( 'Read all analog inputs, RAW mode, in one shot' )
print( brd.analogs.states )

print( 'Read the voltages, in VOLTS, all analog inputs in one shot' )
brd.analogs.raw = False # Reactivate voltage conversion 
print( brd.analogs.states )

# === Read all Opto-Isolated inputs ================
print( 'Read all the opto-isolated inputs' )
print( brd.inputs.states )
print( 'Read the third opto-isolated input' )
# Python! so indexed from 0!
print( brd.inputs[2] )

# === RELAYS ======================================
# Activate the relays REL1 and REL3 (Python! so indexed from 0)
print( 'Change the relays by index' )
brd.relais[0] = True
brd.relais[2] = True
print( 'Relays[0..3] states : %s' % brd.relais.states ) 
sleep_ms( 2000 )
# Swith off all the relays
brd.relais.states = False 

print( 'Switch a relay - one at the time')
for irelay in range( 4 ):
    print( '   relays %s' % (irelay+1) )
    brd.relais[irelay] = True # Switch on
    sleep_ms( 1000 )
    brd.relais[irelay] = False # Switch off
    sleep_ms( 500 )

print( 'update all relays in one shot' )
brd.relais.states = [True, True, False, True]
sleep_ms( 2000 )
print( 'Activate all the relays' )
brd.relais.states = True
sleep_ms( 2000 )
print( 'Unactivate all the relays' )
brd.relais.states = False

print( "That's the end folks")

Connect several MOD-IO
You can connect together several MOD-IO boards. However, you will need to change the address of each board to avoids address conflict on the I2C bus. The MOD-IO board store its I2C address inside the EEPROM. So, the address could be changed with a dedicated function.

The following example change the address from 0x58 to 0x22.
# Change the I2C address from the Olimex's MOD-IO board to 0x22
#
# Shop: http://shop.mchobby.be/product.php?id_product=1408
# GitHub: https://github.com/mchobby/esp8266-upy/tree/master/modio 

from machine import I2C, Pin
from modio import MODIO

i2c = I2C( sda=Pin(2), scl=Pin(4) )
brd = MODIO( i2c, addr=0x58 )
brd.change_address( 0x22 )

Please note that address change is instantaneous. As a result, the I2C ACKnowledgement is not send from the 0x58 address but from the newer 0x22 (which results into a transmission error in MicroPython... nevertheless the address is changed).

Tutorials and GitHub
You may find many more information inside our French tutorial and the product sheets.
Where to buy