-
Notifications
You must be signed in to change notification settings - Fork 0
/
chirp+lora.py
127 lines (104 loc) · 2.9 KB
/
chirp+lora.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import time
import binascii
import pycom
import socket
from machine import I2C
from machine import Pin
from struct import unpack
from network import LoRa
from network import WLAN
# Hardcoded values for colors at lower brightness.
off = 0x000000
red = 0x220000
green = 0x002200
blue = 0x000022
# Hardcoded values declaring variables for emon.itu.dk.
tempVar = b'\xAA'
moistVar = b'\xBB'
lightVar = b'\xCC'
# Values for SDA and SCL pins positions.
SDA = Pin.exp_board.G17
SCL = Pin.exp_board.G16
#values for PIR
p_in=Pin(Pin.exp_board.G10, mode=Pin.IN, pull=Pin.PULL_UP)
class Chirp:
def __init__(self, address):
self.i2c = I2C(0, I2C.MASTER, baudrate=10000)
self.address = address
def get_reg(self, reg):
val = unpack('<H', (self.i2c.readfrom_mem(self.address, reg, 2)))[0]
return (val >> 8) + ((val & 0xFF) << 8)
def moist(self):
return self.get_reg(0)
def temp(self):
return self.get_reg(5)
def light(self):
self.i2c.writeto(self.address, '\x03')
time.sleep(1.5)
return self.get_reg(4)
class LoRaNetwork:
def __init__(self):
# Turn off hearbeat LED
pycom.heartbeat(False)
# Initialize LoRaWAN radio
self.lora = LoRa(mode=LoRa.LORAWAN)
# Connect to sensors.
#wlan = WLAN(mode=WLAN.STA)
# Uncomment next line to disable wifi
#wlan.deinit()
# go for fixed IP settings (IP, Subnet, Gateway, DNS)
# Set network keys
app_eui = binascii.unhexlify('70B3D57EF0003F19')
app_key = binascii.unhexlify('0EFCC322B67F7BC848E683AD0A27F64A')
# Join the network
self.lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
#pycom.rgbled(red)
# Loop until joined
while not self.lora.has_joined():
print('Not joined yet...')
pycom.rgbled(off)
time.sleep(0.1)
#pycom.rgbled(red)
pycom.rgbled(red)
time.sleep(0.1)
pycom.rgbled(green)
time.sleep(0.1)
pycom.rgbled(blue)
time.sleep(0.1)
pycom.rgbled(off)
time.sleep(2)
print('Joined')
#pycom.rgbled(blue)
self.s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
self.s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
self.s.setblocking(True)
self.bytesarraytemp = bytearray(2)
#sensor
addr = 0x20 #or 32
self.chirp = Chirp(addr)
def convertbytes(self, data):
self.bytesarraytemp[0] = (data & 0xFF00) >> 8
self.bytesarraytemp[1] = (data & 0x00FF)
return self.bytesarraytemp
def senddata(self):
while True:
print("temp")
print(self.chirp.temp())
count = self.s.send(tempVar+self.convertbytes(self.chirp.temp()))
#print(count)
print("moist")
globalMoist=self.chirp.moist()
print(globalMoist)
count = self.s.send(moistVar+self.convertbytes(self.chirp.moist()))
#print(count)
print("light")
print(self.chirp.light())
count = self.s.send(lightVar+self.convertbytes(self.chirp.light()))
#print(count)
#pycom.rgbled(green)
#time.sleep(0.5)
#pycom.rgbled(blue)
start = LoRaNetwork()
#start thread function
while True :
start.senddata()