Python Serial Port



1. Introduction
2. Settings file
3. Run Python Serial Port

1. Introduction

The Python serial port can be used to connect any serial port to our service. The source code for this project can be downloaded from https://github.com/webifi-me/WebifiPythonSerialPort.git.

2. Settings file

The settings.ini file is used to store all the settings for the script. The following section describes all the settings:

Setting Description
connectname The connect name used for the connection. If you are unsure about these details please check our How It Works and Getting Started guides.
password The connect password used for this connection
network The network or networks this device will communicate on. The serial port data received by this script will be sent to all these networks and data received from any of these networks will be sent to the serial port. Normally you would have this script running on its own dedicated network, for example RPi for your Raspberry Pi.
encryption If set to 1 then all data over the internet will be SSL/TLS encrypted
useWebsocket If set to 1 then WebSocket will be used for communication. If set to 0 then Long Polling will be used.
datatype The data type sent with all data from this instance. This is handy if you have multiple devices on the same network and need the receiving device to easily differentiate where the data is coming from.
logDirectory If you need messages to be logged then this field must be set to a valid directory. This is used for debugging the Python scripts.
serport The name of the serial port that will be opened by this script. On Windows it will be something like: COM23. On your Raspberry Pi it will probably be /dev/ttyUSB0 if you have a USB virtual serial port connected or /dev/ttyAMA0 for the GPIO serial port (For the RPi 3 ttyAMA0 is used for Bluetooth, the GPIO serial port is called /dev/ttyS0).
parity The serial port parity.
baudrate The serial port baud rate.
numbits The number of data bits for the serial port
stopbits The number of stop bits for the serial port

3. Run Python Serial Port

Before running this script please make sure the Webifi Python libraries are installed:
pip install webifi

You also need the pySerial module to be able to communicate with the serial port:
pip install pyserial

There are two versions of the program: WebifiPythonSerialPort_p27.py for Python 2.7 and WebifiPythonSerialPort_p3.py for Python 3. To use the script on Python 2.7 enter the following:
python WebifiPythonSerialPort_p27.py
If the connection is established to the Webifi server then the message: Connection successful will be displayed. Now you will be able to communicate with your serial port over the internet. Press Control-C to close the program.

By default the script will use the settings.ini file. If you have multiple ini files you can specify which file to use, for example:
python WebifiPythonSerialPort_p27.py my_init_file.ini

We also have an install script for Python 2.7 on Linux. For example, if you have a Raspberry Pi and you would like the serial port to connect to the Webifi service each time the device starts then you can use this script. When you run the script it will ask for the following details:

  • Connect name
  • Password
  • Network
  • Whether encryption should be used
  • Serial port name
  • Baud rate

To install the script run:
sudo install_p27.sh

To uninstall the script:
sudo uninstall_p27.sh