Last active
April 16, 2020 21:49
-
-
Save scttnlsn/e9345e99d969652d5d0c4947f4669475 to your computer and use it in GitHub Desktop.
Raspberry Pi RFM69 to MQTT gateway
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
# pip install spidev RPI.GPIO | |
# git clone https://github.com/etrombly/RFM69.git | |
# | |
# 19 MOSI | |
# 21 MISO | |
# 23 SCK | |
# 22 DI00 | |
# 24 NSS | |
import time | |
import paho.mqtt.client as mqtt | |
from RFM69.RFM69 import RFM69 | |
from RFM69.RFM69registers import * | |
RADIO_NODE_ID = 1 | |
RADIO_NETWORK_ID = 100 | |
RADIO_HIGH_POWER = True | |
RADIO_INT_PIN = 22 | |
RADIO_RST_PIN = 24 | |
RADIO_ENCRYPT_KEY = 'ABCDEFGHIJKLMNOP' | |
radio = RFM69(RF69_915MHZ, RADIO_NODE_ID, RADIO_NETWORK_ID, RADIO_HIGH_POWER, intPin=RADIO_INT_PIN, rstPin=RADIO_RST_PIN) | |
radio.setHighPower(RADIO_HIGH_POWER) | |
radio.encrypt(RADIO_ENCRYPT_KEY) | |
def on_connect(client, userdata, flags, rc): | |
pass | |
def on_message(client, userdata, msg): | |
pass | |
client = mqtt.Client() | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.connect('localhost', 1883, 60) | |
client.loop_start() | |
try: | |
while True: | |
radio.receiveBegin() | |
while not radio.receiveDone(): | |
time.sleep(0.1) | |
message = ''.join([chr(byte_val) for byte_val in radio.DATA]) | |
print(message) | |
if radio.ACKRequested(): | |
radio.sendACK() | |
client.publish('rfm69', message) | |
time.sleep(0.1) | |
except KeyboardInterrupt: | |
radio.shutdown() | |
client.loop_stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment