Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Very quick and _dirty_ Aranet4 to MQTT
# save this in your /home/pi
# look for DEVICE MAC and MQTT Broker config below
#
# ble code from here
# https://dzone.com/articles/using-python-gatttool-and-bluetooth-low-energy-wit
#
# mqtt from here
# http://www.steves-internet-guide.com/into-mqtt-python-client/
#
# Auto run it every 5 minutes
# sudo crontab -e is the task
# */5 * * * * python /home/pi/testAranet4_RPi.py
#
# Check Homy integration is switched on in the Aranet4 app!
# Look for Aranet MAC using
# >sudo hcitool lescan
# D7:68:2B:2B:3C:BC Aranet4 ####
#
import pexpect
import time
import paho.mqtt.client as mqtt #import the client1
import time
import sys
DEVICE = "##:##:##:##:##:##"
broker_address = "mqtt_server"
broker_username = "username"
broker_psw = "password"
# Run gatttool interactively.
#print("Run gatttool...")
try:
child = pexpect.spawn("gatttool -t random -b " + DEVICE + " -l high --char-read -a 0x38")
except pexpect.TIMEOUT:
print "timeout, giving up."
sys.exit("could not connect to Aranet over BLE") # if something goes wrong
child.expect("Characteristic value/descriptor: ", timeout=10)
child.expect("\r\n", timeout=10)
print(int(child.before[3:5]+child.before[0:2],16)),
print("ppm")
client = mqtt.Client("RPi")
client.username_pw_set(broker_username, broker_psw)
client.connect(broker_address) #connect to broker
client.loop_start() #start the loop
json_body = {
"co2": int(child.before[3:5]+child.before[0:2],16),
"temp": float(int(child.before[9:11]+child.before[6:8],16)/20.0)
}
client.publish("aranet4", json.dumps(json_body))
time.sleep(4) # wait ... because example had it ;)
client.loop_stop() #stop the loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.