Friday, July 13, 2012

Controlling the pHduino with a Python-Jabberbot script

Introduction

This is a simple suggestion to control the pHduino over Internet using the  Python-Jabberbot framework. It could be interesting, for example, if you want to monitor the pH of an aquarium from your work or using your smartphone from anywhere.

Of course you can change the python jabberbot script to control other devices over USB-Serial and an IM connection. It works well for slow streaming of messages. XMPP protocol is open, widely accepted, and works fine with python for multi platform solutions. There are many clients that use the XMPP protocol.

You will need:
  • a pHduino working.
  • a computer (Linux or Windows) with an internet connection and a USB port with the virtual comm port (VCP) working.
  • two XMPP instant messages accounts (jabber.org, Google Talk...) and changing messages between them.
  • the Python interpreter with the modules python-setuptools, python-serial, python-xmpp, and python-jabberbot.
  • The python jabberbot script to control the pHduino.

I described the process for Linux and Windows. I did not test it for Mac but the users could test the Windows procedure.

Software dependence

Python 2.7.3

Python Setuptools 0.6c11

Python Serial 2.6

Python XMPP 0.5.0-rc1

Python Jabberbot 0.15

Check for packages updates and pay attention with the Python version.

pHduino

I am assuming the pHduino working properly by USB-Serial port.

First of all, it is important change the pHduino parameter interval_ms from 1000 to 10000 or more. The reason is to not flood the IM with too many messages to not difficult the human reading and reduce the traffic of messages on the Internet. To do this you need a serial terminal to set the parameter. My suggestions: cutecom for Linux (http://cutecom.sourceforge.net/), terminalbpp for Windows (https://sites.google.com/site/terminalbpp/), and screens for Mac.

Select baudrate 115200 for the firmware version v041 and No Line End to send strings. Open the connection and type

cmd interval_ms 10000

You will listen a beep. Now, you have a message by serial every 10s.

XMPP accounts

You need two IM accounts that uses the XMPP protocol. One account for pHduino and another one for you access the pHduino from anywhere. The accounts could be both from Google Talk, or both from Jabber.org, or one from the Google Talk and another one from the Jabber.org. Anyway, what is important is the XMPP protocol. Don't forget to add the contacts in the lists of each other.

Software installing for Linux

For Linux users the software install is simple because you can find all programs in the repositories. The package name could be different if you are using a different Linux distribution. I am using the Ubuntu 12.04.

Probably, the Python interpreter is already installed.

Now, you need the python serial module. Open a terminal and type

sudo apt-get install python-serial

Install the the Jabberbot. The xmpppy (python-xmpp) dependence will be installed.

sudo apt-get install python-jabberbot

In case your Linux distribution does not have the modules jabberbot and xmpppy in the repository, you must download, build, and install them. To do this, install the python-setuptools.

sudo apt-get install python-setuptools

Software installing for Windows

I tested the the python jabberbot script in an Windows 7 64-bits and it worked.

Python

Download and install the Python 2.7.3 Windows Installed (Windows binary -- does not include source) from

http://www.python.org/download/

There is a detail to work: be sure the python directory distribution is in the PATH environment variable. Go to Start -> Control Painel -> System -> Advanced System Settings -> Environment Variables. Look for the PATH variable at System Variables and edit it adding at the end of the line (take care to not overwrite your settings). The procedure is similar for Windows XP or Vista. Append the following string.

C:\Python27;

To test, open a command prompt (Start -> Programs -> Acessories -> Command prompt) and type python and press enter. You should have the python interpreter. Type quit() to quit.

pyserial

To open a serial connection with the pHduino install the python-serial module. I installed the old version pyserial-2.5.win32.exe from

http://sourceforge.net/projects/pyserial/files/pyserial/2.5/

Setup-tools

Next, install the python-setuptools to build and install the xmpp and jabberbot modules. Download and execute the file setuptools-0.6c11.win32-py2.7.exe from

http://pypi.python.org/pypi/setuptools/

No problem if the file is win32 binary and the Windows 7 is 64-bit.

xmpppy

You must build and install the module by the sources because there is no exe or msi file. Download the xmpppy-0.5.0rc1.tar.gz from

http://xmpppy.sourceforge.net/

I suggest you install the 7-zip because it can unpack .tar.gz (or .tgz) files. Download the 64-bit version if your Windows is 64-bit.

http://www.7-zip.org/

Right click on xmpppy-0.5.0rc1.tar.gz and extract here option. This operation generates a .tar file. Again, right click and extract here. Copy the xmpppy-0.5.0rc1 directory to C: and rename it to xmpppy just to make the job easy.

Open a command prompt and type

cd C:\xmpppy
python setup.py build
python setup.py install

You will see a list of directories during the install process. Do not forget to remove this directory from the C:\ when all is finished.

Jabberbot

Download the jabberbot-0.15.tar.gz from

http://thp.io/2007/python-jabberbot/

Again, extract it in the same way of xmppy and rename it to jabberbot. Move it to C:/ and type

cd C:\jabberbot
python setup.py build
python setup.py install

Again, you will see the list of directories during the install process. Remove the jabberbot directory when the job is done.

Python Jabberbot script

This python script is simple because the jabberbot framework simplify the configurations. There is a difference between the python jabberbot framework (the python module) and the python jabberbot script (the code to control the pHduino).



You can download the phduino_jabberbot_v01.py from the pHduino project.

In the script above, you need edit the username, password, and recipient variables. username and password is the account for the computer with the pHduino to connect by USB. recipient is your account that receive the messages from the pHduino. Edit the serial_port and baud_rate. Save it and run it by clicking or by typing the command on the command prompt or terminal

python phduino_jabberbot_v01.py

You will see the initial messages from the script. Let's test from your IM account.

Open your xmpp account in the same computer or in another computer using your favorite IM client. When you finish the logging, you will receive messages like

18.6 375 27 6.53 1 0
18.6 375 25 6.56 1 0
18.6 375 25 6.56 1 0
18.6 375 25 6.56 1 0
18.6 375 27 6.53 1 0

Description of the columns: (1) temperature (celcius), (2) ADC value, (3) electrochemical potential (mV), (4) pH, (5) alarm enabled flag, and (6) alarm flag (0 for pH in the limits and 1 for pH out of the limits).

pHduino operation

For the help, type

cmd help

and you will see

cmd help
cmd phduino start|stop
cmd interval_ms INT
cmd set_temp_sensor on|off
cmd set_temp FLOAT
cmd set_ph_alarm PH_INF PH_SUP
cmd ph_alarm on|off
cmd reset

If you want stop the strings, send the command

cmd phduino stop

To return to receive them

cmd phduino start

To set the pH range for the alarm type

cmd set_ph_alarm 6.0 8.0

This example set the alarm range from pH 6.0 to 8.0.

To turn the alarm on, type

cmd ph_alarm on

You will see the alarm flag with value 1 and the pHduino will start beeping indicating that the pH is out of range. Remember that without a pH sensor connected you will see the pH saturated (pH 15.03 or similar value) and out of range like

18.5 834 -437 15.03 0 0

To turn the alarm off, type

cmd ph_alarm off

If you have a thermostatized system (system with the temperature controlled), you don't need the temperature sensor. Just set the temperature and disable the temperature sensor. For example

cmd set_temp 20.0
cmd set_temp_sensor off

To reset all parameters to default values, type

cmd reset

And remember: pH notation is lowercase "p" and  uppercase "H".
Good hacks.

2 comments:

  1. Hi i have my own pH meter which i construct using Atlas Scientific pH stamp and arduino. can u suggest a way to display the pH reading in graphical view? or Labview perhaps...

    ReplyDelete
  2. http://phduino.blogspot.com.br/2010/04/pyphpot-python-script-v01-released.html

    ReplyDelete