Created
March 25, 2020 03:30
-
-
Save sidwarkd/282fa9ee820663f1da3cc731e69434e4 to your computer and use it in GitHub Desktop.
Connecting Multiple Raspberry Pi Clients to a Raspberry Pi Server with Socket.IO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Running on Raspberry Pi 1 | |
import socketio | |
sio = socketio.Client() | |
def send_sensor_readings(): | |
while True: | |
sio.emit('my_message', {'temp':75}) | |
sio.sleep(5) | |
@sio.event | |
def connect(): | |
print('connection established') | |
sio.start_background_task(send_sensor_readings) | |
@sio.event | |
def disconnect(): | |
print('disconnected from server') | |
sio.connect('http://localhost:5000', headers={'device_id':'client1'}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run this code on Raspberry Pi 2 | |
import socketio | |
sio = socketio.Client() | |
def send_sensor_readings(): | |
while True: | |
sio.emit('my_message', {'humidity':40}) | |
sio.sleep(5) | |
@sio.event | |
def connect(): | |
print('connection established') | |
sio.start_background_task(send_sensor_readings) | |
@sio.event | |
def disconnect(): | |
print('disconnected from server') | |
sio.connect('http://localhost:5000', headers={'device_id':'client2'}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Raspberry Pi 3 | |
import socketio | |
sio = socketio.Client() | |
def send_sensor_readings(): | |
while True: | |
sio.emit('my_message', {'temp':80}) | |
sio.sleep(5) | |
@sio.event | |
def connect(): | |
print('connection established') | |
sio.start_background_task(send_sensor_readings) | |
@sio.event | |
def disconnect(): | |
print('disconnected from server') | |
sio.connect('http://localhost:5000') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Running on the Raspberry Pi server | |
import eventlet | |
import socketio | |
sio = socketio.Server() | |
app = socketio.WSGIApp(sio) | |
def get_device_id(environ): | |
return environ.get('HTTP_DEVICE_ID', None) | |
@sio.event | |
def connect(sid, environ): | |
device_id = get_device_id(environ) or sid | |
sio.save_session(sid, {'device_id': device_id}) | |
print('{} is connected'.format(device_id)) | |
@sio.event | |
def my_message(sid, data): | |
session = sio.get_session(sid) | |
print('Received data from {}: {}'.format(session['device_id'], data)) | |
@sio.event | |
def disconnect(sid): | |
print('disconnect ', sid) | |
if __name__ == '__main__': | |
eventlet.wsgi.server(eventlet.listen(('', 5000)), app, log_output=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment