I’ve started putting together a Python library to allow people to use the Economatics Smart Box serial interfaces from a modern computer. Forgive any quality issues, I usually only bodge things together for my own use.
Based on the responses from the “ReadSensorTable” command in my Economatics Smart Box SB-04, I’ve concluded that the following official sensor exist :
Index
Name
Val1
Val2
Units
3
Temp range 1
Tmp
Temp
degC
4
Voltage range 1
V
Volts
mV
5
Temp range 2
Tmp
Temp
degC
6
Voltage range 2
V
Volts
volts
7
Temp range 3
Tmp
Temp
degC
8
Voltage range 3
V
Volts
volts
9
Temp range 4
Tmp
Temp
degC
10
Sound (standard)
Snd
Sound
?
11
pH
pH
pH
?
13
Position
Pos
Postn
deg
17
Light (standard)
Lt
Light
?
21
R’Humidity
Hmd
Humid
%RH
22
Sound range 1
Snd
Sound
dbA
23
Light range 1
Lt
Light
Lux
24
Sound range 2
Snd
Sound
dbA
25
Light range 2
Lt
Light
Lux
26
Barometric Pressure
Atm
Atmos
mBars
27
Light range 3
Lt
Light
Lux
28
User adaptor
UA
User
?
29
0-1 Volt adapator
V
Adapt
?
30
Temp (low range)
Tmp
Temp
degC
31
Light gate
LG
LGate
?
33
Temp (standard)
Tmp
Temp
degC
There is more data being fed back by the box regarding these sensor types, but it is a little bit of a pig to understand as my terminal is reading it as escape characters. I suspect it would be easier to understand if I had some official sensors myself. I suspect that some experimentation is in order! I’ll connect a variable voltage to the detection pin and watch what sensor types the box detects at each voltage. With an ADC connected to my computer I might be able to automate the process… but that is probably more effort than it is worth.
Working with the Economatics Smart Box Serial Interface on a Modern Computer
Well, I had all sorts of fancy plans for how I would reverse engineer the serial protocol to control my ebay purchase “Smart Box” (some of which I will document at some point as they’re useful). Ultimately, I had so much difficulty installing MS .net 1.1 in Wine / PlayOnLinux that I went back to trawling the internet looking for alternative software to wire tap.
Smart Box
I struck gold when I found the following website :
The website includes a download of example code, the core of which is in VB6 and includes many serial commands for controlling the Smart Box (SB-04 version – I understand that older versions work differently, so be warned!), as well as an instruction manual that goes some way to explaining how the box data formats work.
After a bit of messing about in Python, it was fairly trivial to test a number of commands and witness the results. The only major feature I’m not currently sure about is the Analogue Inputs – I don’t know what the pinout is of the 5 pin DIN sockets, and so can’t easily test them. Perhaps a bit of circuit board investigation will help with this.
In summary, I have found the following :
Description
Command
Parameter
Response
Python Example
Set All Motors
10
8 bit value. Note, each pair of bits represents a motor. 01=left, 10=right and 00=stop
n/a
ser.write(chr(10)+chr(0b01010101)) # rotate all motors to the left
Motor <x> Left
12
1 to 4
n/a
ser.write(chr(12)+chr(1)) # rotate motor ‘A’ to the left
Motor <x> Right
13
1 to 4
n/a
ser.write(chr(13)+chr(1)) # rotate motor ‘A’ to the right
Motor <x> Stop
14
1 to 4
n/a
ser.write(chr(14)+chr(1)) # stop motor ‘A’
Set All Digital Outputs
20
8 bit value
n/a
ser.write(chr(20)+chr(0b11111111)) # turn all digital outputs on
Get Analogue <x>
40 (TBC)
1 to 4
8 bit value??
ser.write(chr(40)+chr(1)) # send me analogue input ‘A’
Set Low Resolution Analogue
45 (TBC)
n/a
n/a
ser.write(chr(45)) # set low resolution analogue mode
Get All Digital Inputs
90
n/a
8 bit value. Note the command seems to respond with three bytes, although the first identifies the status of each input.
ser.write(chr(90)) # send me the status of the digital inputs
Get Digital Input <x>
91 (TBC)
1 to 8
0 or 1 (in 8 bit value)
ser.write(chr(45)+chr(1)) # send me the status of digital input 0
The following example switches on motor ‘A’ for 5 seconds, stops it for 0.5 seconds, runs it backwards for 2 seconds and then stops it. Note the serial port will need setting – I have used ‘/dev/ttyUSB0’ as this is the correct port for my USB to RS232 adapter on my Linux machine.
#!/usr/bin/python
# https://elephantandchicken.co.uk/stuffandnonsense
# 04/03/2020
import time
import serial
# This example tests motor output A
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.write(chr(12)+chr(1))
time.sleep(5)
ser.write(chr(14)+chr(1))
time.sleep(0.5)
ser.write(chr(13)+chr(1))
time.sleep(2)
ser.write(chr(14)+chr(1))
ser.close()
The following example reads the status of all digital inputs (aka “Digital Sensors”) 20 times.
#!/usr/bin/python
# https://elephantandchicken.co.uk/stuffandnonsense
# 04/03/2020
import time
import serial
# This example reads in the digital sensors and displays their status
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
# Flash Digital Outputs to show that the program is running
ser.write(chr(20)+chr(255)) # all on
time.sleep(0.1)
ser.write(chr(20)+chr(0)) # all off
time.sleep(0.1)
i = 0
while i < 20:
ser.write(chr(90)+chr(1)) # request digital input status
time.sleep(1) # wait (ages) for the response
readVal = 0 # clear variables
readByte = 0
readByte = ord(ser.read(1)) # read the first waiting byte in the buffer
while ser.inWaiting() > 0: # if there is more data in the buffer
readVal = ord(ser.read(1)) # read it to clear it
print "result : ",format(readByte, '08b') # print the first byte in boolean format
i+=1 # increment counter for the while loop
ser.close()
I still have some more work to do, regarding confirming exactly how these commands work (for example, what are the second and third bytes from the digital read response?). Additionally, I suspect that it is possible to set the voltages, but don’t know how. I’m (absolutely) guessing that command 11 might be motor speed.
Additionally, please contact me if you know any more details of the control scheme, or the analogue port pinout.
I’ve been doing some experiments in making Python GUIs using Glade. This is about as far ask I got – I’ve not gone back to do more after I got distracted by something else!
Glade Python GUI
If I remember correctly, the data is hourly temperature data.
A while back I wrote a post about using an FTDI breakout board as a GPIO board from Python. Since then, I have discovered that there are USB relay boards based on this concept available from China. SainSmart seem to sell them, but they are available on ebay, Banggood and Aliexpress. The boards are interesting as they do not contain a microcontroller – just an FTDI chip and an ULN2803 array of Darlington pairs to drive the relay coils.
SainSmart 8 Channel USB Relay Board
Using the setup previously detailed in my other post, it is possible to control one of these boards from Linux. The process is exactly the same as using one of the little red USB UART adapter boards.
Plug the board into a 12v DC supply, and your computer’s USB.
Run the following example (as root / with sudo if you haven’t set up permissions for your user to access the device).
#!/usr/bin/env python
import os
import sys
import ftdi1 as ftdi
import time
ftdic = ftdi.new()
if ftdic ==0:
print( 'new failed: %d', ret )
os._exit( 1 )
# try to list ftdi devices 0x6010 or 0x6001
ret, devlist = ftdi.usb_find_all( ftdic, 0x0403, 0x6010 )
if ret <= 0:
ret, devlist = ftdi.usb_find_all( ftdic, 0x0403, 0x6001)
if ret < 0:
print( 'ftdi_usb_find_all failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )
os._exit( 1 )
print( 'Number of FTDI devices found: %d\n' % ret )
curnode = devlist
i = 0
while( curnode != None ):
ret, manufacturer, description, serial = ftdi.usb_get_strings( ftdic, curnode.dev )
if ret < 0:
print( 'ftdi_usb_get_strings failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )
os._exit( 1 )
print( 'Device #%d: manufacturer="%s" description="%s" serial="%s"\n' % ( i, manufacturer, description, serial ) )
curnode = curnode.next
i += 1
# open usb
ret = ftdi.usb_open( ftdic, 0x0403, 0x6001 )
if ret < 0:
print( 'unable to open ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )
os._exit( 1 )
# bitbang
ret = ftdi.set_bitmode( ftdic, 0xff, ftdi.BITMODE_BITBANG )
if ret < 0:
print( 'Cannot enable bitbang' )
os._exit( 1 )
for i in range( 8 ):
val = 2**i
#print( 'enabling bit #%d (0x%02x)' % (i, val) )
ftdi.write_data( ftdic, chr(val), 1 )
time.sleep ( 0.5 )
ftdi.write_data( ftdic, chr(0x00), 1)
time.sleep ( 0.2 )
ftdi.disable_bitbang( ftdic )
print( '' )
# close usb
ret = ftdi.usb_close( ftdic )
if ret < 0:
print( 'unable to close ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )
os._exit( 1 )
print ('device closed')
ftdi.free( ftdic )
This code is lightly modified from the code found here, without which I’d have struggled.
HID USB Relay Board
Similar boards exist which use an Atmel microcontroller interfacing with the computer through a software HID implementation. These boards are cheaper, but otherwise similar on the relay side and in appearance.
Red USB Relay Board
These boards can be controlled from Linux by installing the “usbrelay” program from the repository (on Ubuntu and Debian at least) using “sudo apt-get install usbrelay”. Otherwise the developer has a github page for the project here : https://github.com/darrylb123/usbrelay. Once installed and with the device connected, simply typing “sudo usbrelay” (without quote marks) at your command line reports back on connected devices. One of my devices calls itself “VXCMF”, while another calls itself “6QMBS”. Where a device is called “VXCMF”, the first two relays can be turned on with the following command “sudo usbrelay VXCMF_1=1 VXCMF_2=1” and off again with “sudo usbrelay VXCMF_1=0 VXCMF_2=0”.
If you want to use the boards without sudo, you’ll have to add it to /etc/udev/rules.d. There are detailed instructions on the github linked above, but in short, if you’re using a Debian family OS, put the following in a file called “50-usbrelay.rules” in the folder /etc/udev/rules.d :
Then make sure your user is in the plugdev group and restart. If you’re using Windows or macOS, there are other instructions in forks such as this one https://github.com/corerd/usbrelay, although it looks a little old.
Both boards detailed in this post are available with various numbers of relays. The red boards are probably easier to use as it is simply a case of installing usbrelay. Hopefully the instructions here and in my previous post can help people get their boards running. I haven’t tested, but both should work fine with a Raspberry Pi and many other SBCs.
During a discussion with my dad about doing GPIO style projects from a regular, full fat computer, I was telling him about the Adafruit FT232H Breakout board. These boards allow you to use several different serial protocols such as SPI, I2C and UART as well as provide a GPIO mode if you are willing to get involved enough. It provides an elegant solution to connecting external electronic devices, because it doesn’t require any software to be loaded into the device itself – all your code is running on your computer. The one downside is that, here in the UK at least, these boards cost in the region of £15, meaning that personally I would usually just grab a cheap Arduino and write some kind of pass-through program for most things I’d want to do.
Adafruit FT232H Breakout
Yesterday evening, I installed the various software identified in Adafruit’s “Learning System” page relating to the product to allow me to set up various pins as inputs and outputs from a python program, in a similar style to how you might use the GPIO pins on a Raspberry Pi. Looking at what I needed to install and the fact that you had to specifically tell some of the libraries what chip you were connecting got me thinking that perhaps some of the software tools would work more generally with FTDI chips. I knew it was possible to use FTDI chips such as the FT232RL (commonly used in USB to UART serial adapters) as external USB connected GPIO, but my previous investigations hadn’t found clear (and simple enough) instructions to get anywhere in the time I had available. Today I dug out a cheap FT232RL based board and decided to have a play. At £1.38 each, these boards don’t exactly break the bank, and I have a pile of them already. I also like that you can easily switch the voltage using the jumper on these boards.
FT232RL USB to Serial Board
The following instructions relate specifically to Linux (Ubuntu 17.10), but following the instructions given on the Adafruit website may get you up and running on Windows or MacOS. Hardware-wise, any sensible computer with USB, a USB cable (Type A to Mini-B), a breadboard, a run-of-the-mill LED, an appropriate current limiting resistor (I used a 560 Ω resistor, which should be fine in most cases) and a FT232RL based board similar to the one above. The circuit is as follows (note the +ve side of the LED is identified by the longer lead, and the -ve by a flat on the side of the lens) :
FT232RL Circuit
Following the Adafruit instructions for Linux, carefully enter the following commands in a terminal :
sudo apt-get update
sudo apt-get install build-essential libusb-1.0-0-dev swig cmake python-dev libconfuse-dev libboost-all-dev
wget http://www.intra2net.com/en/developer/libftdi/download/libftdi1-1.2.tar.bz2
tar xvf libftdi1-1.2.tar.bz2
cd libftdi1-1.2
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX="/usr/" -DPYTHON_INCLUDE_DIR="/usr/include/python2.7" -DPYTHON_LIBRARIES="/usr/lib/python2.7/" ../
make
sudo make install
This installs the dependencies, fetches the source code for libftdi1, compiles it and finally installs it.
To test that everything installed correctly, launch the python interpreter by typing “python” (excluding quotes) at the command line and pressing enter, then type “import ftdi1” and press enter. If there are no errors, then it installed just fine. Exit the interpreter by typing “exit()” and pressing enter.
Now would probably be a good time to return to your home directory and perhaps create a folder for your python project. We’ll then create a new file called LEDBlink.py and open it in nano (a text editor) :
cd ~
mkdir ftdiGPIO
cd ./ftdiGPIO/
nano ./LEDBlink.py
Once you have the empty file open in the window, enter the following python program (which is based on the code found here). Note you can copy and paste from here – but use the copy button in the top right of the code container to ensure consistent formatting :
#!/usr/bin/env python
import ftdi1 as ftdi
import time
ftdic = ftdi.new()
# try to list ftdi devices 0x6010 or 0x6001
ret, devlist = ftdi.usb_find_all( ftdic, 0x0403, 0x6010 )
if ret <= 0:
ret, devlist = ftdi.usb_find_all( ftdic, 0x0403, 0x6001)
# open usb
ret = ftdi.usb_open( ftdic, 0x0403, 0x6001 )
# bitbang
ret = ftdi.set_bitmode( ftdic, 0xff, ftdi.BITMODE_BITBANG )
for i in range( 64 ):
ftdi.write_data( ftdic, chr(0x02), 1)
time.sleep ( 0.5 )
ftdi.write_data( ftdic, chr(0x00), 1)
time.sleep ( 0.2 )
ftdi.disable_bitbang( ftdic )
# close usb
ret = ftdi.usb_close( ftdic )
print ('device closed')
ftdi.free( ftdic )
This is a basic program with no error checking – it assumes that a device of the correct type is connected an available. Note that it will probably need to be run as root to access the hardware. The program finds and connects to the FT232RL (assuming it is plugged in), then blinks the LED 64 times, being on for 0.5 seconds and off for 0.2 seconds each time. Once the code is entered, press ctrl-x, then “y” and finally enter to exit and save. Check that everything is wired up as follows :
FT232RL Circuit Breadboard
If everything is OK, plug the USB into the computer and enter the following commands :
chmod +x ./LEDBlink.py
sudo ./LEDBlink.py
The first command sets permissions to allow the python program to be executed, the second executes it. If all goes well you’ll be asked for your password and then the LED will start blinking.
If it doesn’t, then… erm… well it worked on my machine…
The following code does the same thing, but includes error checking. A lot of it!
#!/usr/bin/env python
import os
import sys
import ftdi1 as ftdi
import time
ftdic = ftdi.new()
if ftdic ==0:
print( 'new failed: %d', ret )
os._exit( 1 )
# try to list ftdi devices 0x6010 or 0x6001
ret, devlist = ftdi.usb_find_all( ftdic, 0x0403, 0x6010 )
if ret <= 0:
ret, devlist = ftdi.usb_find_all( ftdic, 0x0403, 0x6001)
if ret < 0:
print( 'ftdi_usb_find_all failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )
os._exit( 1 )
print( 'Number of FTDI devices found: %d\n' % ret )
curnode = devlist
i = 0
while( curnode != None ):
ret, manufacturer, description, serial = ftdi.usb_get_strings( ftdic, curnode.dev )
if ret < 0:
print( 'ftdi_usb_get_strings failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )
os._exit( 1 )
print( 'Device #%d: manufacturer="%s" description="%s" serial="%s"\n' % ( i, manufacturer, description, serial ) )
curnode = curnode.next
i += 1
# open usb
ret = ftdi.usb_open( ftdic, 0x0403, 0x6001 )
if ret < 0:
print( 'unable to open ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )
os._exit( 1 )
# bitbang
ret = ftdi.set_bitmode( ftdic, 0xff, ftdi.BITMODE_BITBANG )
if ret < 0:
print( 'Cannot enable bitbang' )
os._exit( 1 )
for i in range( 64 ):
#val = 2**i
#print( 'enabling bit #%d (0x%02x)' % (i, val) )
#ftdi.write_data( ftdic, chr(val), 1 )
#time.sleep ( 1 )
ftdi.write_data( ftdic, chr(0x02), 1)
time.sleep ( 0.5 )
ftdi.write_data( ftdic, chr(0x00), 1)
time.sleep ( 0.2 )
ftdi.disable_bitbang( ftdic )
print( '' )
# close usb
ret = ftdi.usb_close( ftdic )
if ret < 0:
print( 'unable to close ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdic ) ) )
os._exit( 1 )
print ('device closed')
ftdi.free( ftdic )
Line 51 turns on the LED connected to the RX pin on my board which is the second pin out of 8. Pins are defined as follows :
Obviously these are binary sequence in Hex, meaning each bit represents a pin. To turn on multiple pins at once, just add the values together. 0x00 on line 53 turns all pins off.
The example code I previously mentioned was the basis of this program also contains a commented out section showing how to use pins as inputs. It is commented out because it doesn’t work with Python 3, but should work fine under Python 2.x.