Created
April 7, 2014 19:27
-
-
Save robertcedwards/10035675 to your computer and use it in GitHub Desktop.
Smashed the serial echo demo from PyBBIO into the MQTT BBB example from Mosquitto…boom, serial to MQTT on the BBB
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
# serial_echo.py - Alexander Hiam - 4/15/12 | |
# | |
# Prints all incoming data on Serial2 and echos it back. | |
# | |
# Serial2 TX = pin 21 on P9 header | |
# Serial2 RX = pin 22 on P9 header | |
# | |
# This example is in the public domain | |
from bbio import * | |
import serial | |
import mosquitto | |
import os | |
broker = "solr.t1p1.com" | |
port = 1883 | |
def setup(): | |
# Start Serial2 at 9600 baud: | |
Serial2.begin(115200) | |
#MQTT callbacks | |
def on_connect(rc): | |
if rc == 0: | |
#rc 0 successful connect | |
print "Connected" | |
else: | |
raise Exception | |
def on_publish(val): | |
print "Published ", val | |
#called on exit | |
#close serial, disconnect MQTT | |
def cleanup(): | |
print "Ending and cleaning up" | |
Serial2.close() | |
mqttc.disconnect() | |
def loop(): | |
if (Serial2.available()): | |
mypid = os.getpid() | |
client_uniq = str(mypid) | |
mqttc = mosquitto.Mosquitto(client_uniq) | |
data = '' | |
while(Serial2.available()): | |
data += Serial2.read() | |
delay(5) | |
mqttc.connect(broker, port, 60, True) | |
mqttc.publish("rooms/15", data) | |
delay(200) | |
run(setup, loop) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment