-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
194 lines (172 loc) · 6.01 KB
/
main.py
File metadata and controls
194 lines (172 loc) · 6.01 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Version 1.6 of the PJB Sensor Code
# Including battery Voltage
# Peter Brammer. - 2020-12-13
from network import LoRa
import machine
import utime
import socket
import binascii
import struct
import ubinascii
import pycom
import time
import config
READING_FREQ_IN_MIN = 10 # equals 5 mins
# READING_FREQ_IN_MIN = 10 # equals 10 mins
# READING_FREQ_IN_MIN = 0.01 # 1 min
# package header, B: 1 byte for deviceID, I: 1 byte for int, 1 Byte for int
CODE_VERSION = 1.7
_LORA_PKG_FORMAT = "BII"
DEVICE_ID = 0x01
# Max Value from Sensor when 100% wet
SENSOR_100 = 720
# LoRa constants
APP_EUI_KEY = config.APP_EUI_KEY
APP_KEY_VALUE = config.APP_KEY_VALUE
DEV_EUI_VALUE = config.DEV_EUI_VALUE
# Lora ABP Parameters from TTN
DEV_ADDR = config.DEV_ADDR
NWK_SWKEY = config.NWK_SWKEY
APP_SWKEY = config.APP_SWKEY
LORA_FREQUENCY = 916800000
LORA_NODE_DR = 5
def setup_adc():
try:
adc = machine.ADC()
adc.init(bits=12)
sensor = adc.channel(pin='P13', attn=machine.ADC.ATTN_11DB)
except Exception as e:
print('Exception Data Start')
print(e)
return sensor
def adc_battery():
adc1 = machine.ADC()
# Create an object to sample adc on pin 16 with attennuation of 11db
adc_c = adc1.channel(attn=3, pin='P16')
adc_samples = []
# take 100 samples and append them into a list
for count in range(100):
adc_samples.append(int(adc_c()))
adc_samples = sorted(adc_samples)
# take the center list row value (median average)
adc_median = adc_samples[int(len(adc_samples)/2)]
# apply the function to scale to volts
adc_median = adc_median * 2 / 4095 / 0.3275
# Convert adc_median to an int and multiply by 100
adc_median = int(adc_median * 100)
# print(adc_samples)
return adc_median
def setup_power_pin():
power = machine.Pin('P19', machine.Pin.OUT)
power.value(0)
return power
def join_via_abp(lora):
# create an ABP authentication params
dev_addr_in_bytes = struct.unpack(">l", binascii.unhexlify(DEV_ADDR))[0]
nwk_swkey_in_bytes = binascii.unhexlify(NWK_SWKEY)
app_swkey_in_bytes = binascii.unhexlify(APP_SWKEY)
# join a network using ABP (Activation By Personalization)
lora.join(activation=LoRa.ABP, auth=(dev_addr_in_bytes, nwk_swkey_in_bytes, app_swkey_in_bytes))
def join_via_otaa(lora):
app_eui = ubinascii.unhexlify(APP_EUI_KEY)
app_key = ubinascii.unhexlify(APP_KEY_VALUE)
dev_eui = ubinascii.unhexlify(DEV_EUI_VALUE)
for i in range(0,8):
lora.remove_channel(i)
for i in range(16,65):
lora.remove_channel(i)
for i in range(66,72):
lora.remove_channel(i)
# Join the network using OTAA Authentication
lora.join(activation=LoRa.OTAA, auth=(dev_eui, app_eui, app_key), timeout=0)
def create_lora_socket():
lora_socket = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
lora_socket.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
lora_socket.setblocking(False)
lora_socket.bind(1)
# lora_socket.settimeout(5) # Set timeout to be 5 seconds
return lora_socket
def send_message(sensor_reading, battery_voltage):
print('Create Lora Socket')
lora_socket = create_lora_socket()
pkt = struct.pack(_LORA_PKG_FORMAT, DEVICE_ID, sensor_reading, battery_voltage)
try:
print('sending message')
lora_socket.send(pkt)
time.sleep(5.0)
except Exception as e:
print(e)
lora_socket.setblocking(False)
return lora_socket
def receive_message(lora_sct):
rx_pkt = lora_sct.recv(64)
# Check if a downlink was received
if len(rx_pkt) > 0:
print("Downlink data on port 200: ", rx_pkt)
else:
print("Nothing Received")
return
def read_sensor(sensor, power_pin):
# take multiple readings and take the average to get a more reliable reading
print('reading sensor')
READING_DELAY_IN_S = 1
NUM_READINGS = 10
total = 0
for i in range(0, NUM_READINGS):
power_pin.value(1)
utime.sleep(READING_DELAY_IN_S)
sensor_reading = sensor.value()
# print('Moisture value: {0}'.format(sensor.value()))
total += sensor_reading
power_pin.value(0)
average_reading = int(total/NUM_READINGS)
print('Average value: {0}'.format(average_reading))
# convert to Percent - Max value set by SENSOR_100
moisture_percent = int((average_reading/SENSOR_100) * 100)
print('Moisture Percentage: {0}'.format(moisture_percent))
return moisture_percent
def main():
print('Code Version {0}'.format(CODE_VERSION))
# setup lopy4 pins
print('Sensor Set Up')
sensor = setup_adc()
power = setup_power_pin()
#intialize lora object
print('Establish LoRa')
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.AU915)
# set the 3 default channels to the same frequency
lora.add_channel(0, frequency=LORA_FREQUENCY, dr_min=0, dr_max=5)
lora.add_channel(1, frequency=LORA_FREQUENCY, dr_min=0, dr_max=5)
lora.add_channel(2, frequency=LORA_FREQUENCY, dr_min=0, dr_max=5)
# lora = setup_single_lora_channel(lora)
lora.nvram_restore()
# disable LED Heartbeat (so we can control the LED)
pycom.heartbeat(False)
# Set LED to RED
pycom.rgbled(0x7f0000)
if not lora.has_joined():
join_via_otaa(lora)
# join_via_abp(lora)
while not lora.has_joined():
utime.sleep(2.5)
print('Not yet joined...')
if utime.time() > 100:
print("Possible Timeout")
machine.reset()
pass
# We are Online set LED to Green
pycom.rgbled(0x007f00)
print('Join successful Getting ready to send!')
else:
print('Lora already established')
sensor_reading = read_sensor(sensor, power)
lipo_voltage = adc_battery()
print("Battery Voltage: ",lipo_voltage)
lora_socket = send_message(sensor_reading, lipo_voltage)
# receive_message(lora_socket)
utime.sleep(1)
lora.nvram_save()
print('Entering Deep Sleep')
machine.deepsleep(int(READING_FREQ_IN_MIN*60*1000))
if __name__ == '__main__':
main()