websocket python

Estimated reading time: 9 minute(s)

Welcome back, this tutorial post is a continuation to the previous post where I implemented a flutter mobile application that communicates with a websocket server as a client. If you missed that post, you can catch up from here.

You might be asking where is the websocket server. Anyway, in this post I will show you how to develop a websocket server using python programming language on a Raspberry Pi 3.

The Webserver receives messages from the Client which in turn is used in controlling devices (ON/OFF light/LED, ON/OFF alarm or fan). So lets install the necessary packages:

Installing Libraries and Modules

You will need to import these modules/libraries before anything else: gpiozero and pigpio is already preinstall in raspbian stretch OS.

from simple_websocket_server import WebSocketServer, WebSocket
from gpiozero import MotionSensor, LED, PWMLED, Servo
import pigpio
import Adafruit_DHT
from time import sleep
import threading

import RPi.GPIO as GPIO
import Adafruit_DHT
from time import sleep
import threading

The pigpio library is needed to control servo motors. Both gpiozero and RPi.GPIO servo interfaces have proven to be very ineffective and malfunctions when used with servo motors.

https://gist.github.com/nkenna/84d2c71f6c6f13468e07a5abbb566806

The pins to be used have to set. If you observe, the PIR motion sensors are initialized using RPi.GPIO BCM board numbering. This is to show you that you can either use gpiozero or RPi.GPIO library.

Calling the getTemp() will retrieve sensor data (temperature and humidity) from the DHT22 sensor. The values are formatted and assigned to the global variables.

https://gist.github.com/nkenna/8a71b3ffb4d2fa0db35c2434f79880a8

p_action() is where motion detection operation is carried out. The state of the two sensors is first checked. if the state is HIGH and the ARMED variable is TRUE, a message is sent/broadcasted to any connected or listening client while the alarm is also activated.

If the state is HIGH and the ARMED variable is FALSE, a message is sent/broadcasted to any connected or listening client and alarm is not activated.

This function handles messages coming from the clients. Since the clients sends specific messages to control specific devices. The server can now perfectly execute the command and respond to the client accordingly.

Once a device/client is connected, a new thread is started for PIR motion detections. This is make the flow more sublime. The values of the temperature and humidity sensor is sent to the client as well.

You don’t want to be executing the script from terminal each time you restart your Pi. You can add the script to crontab so as to start the script on each reboot.

Streaming Camera Feed

I had to use the tutorial and post from pyimagesearch to implement that. He has nice tutorial how to install opencv and other modules. Don’t forget to add the script to crontab so as to start the server automatically on reboot.

Don’t forget to join us for the next tutorial post and feel free to drop your comments if you happen to encounter any issue. The github repo for this post is available HERE.

LEAVE A REPLY

Please enter your comment!
Please enter your name here