-
Notifications
You must be signed in to change notification settings - Fork 3
/
moteino_node.py
68 lines (58 loc) · 2.12 KB
/
moteino_node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# Sample Moteino RFM69 Node sketch, with ACK and optional encryption
# Ported from Felix Rusu's original example code.
import Adafruit_BBIO.GPIO as GPIO
from radios import rfm69
import time
NODEID = 2 #unique for each node on same network
NETWORKID = 100 #the same on all nodes that talk to each other
GATEWAYID = 1
#Match frequency to the hardware version of the radio on your Moteino (uncomment one):
#FREQUENCY = rfm69.RF69_433MHZ
#FREQUENCY = rfm69.RF69_868MHZ
FREQUENCY = rfm69.RF69_915MHZ
ENCRYPTKEY = "sampleEncryptKey" #exactly the same 16 characters/bytes on all nodes!
IS_RFM69HW = True #uncomment only for RFM69HW! Leave out if you have RFM69W!
ACK_TIME = 30 # max # of ms to wait for an ack
RETRIES = 5
TRANSMITPERIOD = 150 #transmit a packet to gateway so often (in ms)
payload = "123 ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LED_PIN = "P9_11"
def main():
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.output(LED_PIN, GPIO.LOW)
radio = rfm69.RFM69()
radio.initialize(FREQUENCY,NODEID,NETWORKID)
if IS_RFM69HW:
radio.setHighPower(IS_RFM69HW) #uncomment only for RFM69HW!
radio.encrypt(ENCRYPTKEY)
print "Transmitting at {} Mhz...".format(433 if FREQUENCY==rfm69.RF69_433MHZ else (868 if FREQUENCY==rfm69.RF69_868MHZ else 915))
lastPeriod = 0
sendSize = 0
while True:
#check for any received packets
if (radio.receiveDone()):
print "[{}] {} [RX_RSSI: {}]".format(radio.SENDERID, radio.DATA, radio.RSSI)
if (radio.ACKRequested()):
radio.sendACK()
print " - ACK sent"
Blink(LED_PIN, .003)
currPeriod = radio.millis()/TRANSMITPERIOD
if (currPeriod != lastPeriod):
lastPeriod = currPeriod
s = "Sending[{}]: {} ".format(sendSize, payload[:sendSize])
if (radio.sendWithRetry(GATEWAYID, payload, sendSize, retries=RETRIES, retryWaitTime=ACK_TIME)):
s += "ok!"
else:
s += "nothing..."
print s
sendSize = (sendSize + 1) % 31
Blink(LED_PIN, .003)
def Blink(PIN, DELAY_MS):
# Flicker will be visible even with no sleep.
#time.sleep(DELAY_MS)
GPIO.output(PIN, GPIO.HIGH)
#time.sleep(DELAY_MS)
GPIO.output(PIN, GPIO.LOW)
if __name__ == "__main__":
main()