Skip to content

Commit 2903569

Browse files
authored
Add files via upload
1 parent 2b604bc commit 2903569

File tree

7 files changed

+399
-0
lines changed

7 files changed

+399
-0
lines changed

US_water_level/USblack.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
## US BLACK version 20191214 for JSN-SRT04 Ultrasonic sensor
2+
# added: ignore 0 duration measures
3+
4+
import pycom #import the module that allows to use the pycom features, especially the LED
5+
import time
6+
import utime #import the module that allowed to use micro second clock, made for measuring the time between trigger and echo
7+
import machine
8+
import config
9+
from machine import Pin
10+
from math import *
11+
12+
timeout=65 #stop loop after 65 ms (max value = 65 000 us which is around 22 meters!)
13+
14+
class measureUSblack:
15+
def single_measure(trigpin,echopin):
16+
#initialization
17+
echo = Pin(echopin,mode=Pin.IN) #initialize the echo Pin from the one specified in config file
18+
trigger = Pin(trigpin,mode=Pin.OUT) #initialize the trigger Pin from the one specified in config file
19+
trigger(0) #turn off trigger pin for 2µs before starting the single_measure
20+
utime.sleep_us(2)
21+
22+
#sending a sound wave with the trigger Pin for 20µs (10µs for HCSR04T)
23+
trigger(1)
24+
utime.sleep_us(20) # 20 microseconds for the trigger (specific to this sensor)
25+
trigger(0)
26+
27+
#waiting for echo Pin to receive the rising edge of the sound wave
28+
startT=utime.ticks_ms() #used for the timeout test
29+
timediff=0 #used for the timeout test
30+
while echo() == 0 and (timediff<timeout):
31+
timediff=utime.ticks_ms()-startT #used for the timeout test
32+
pass
33+
34+
start=utime.ticks_us() #used to measure the duration of the pulse
35+
36+
if timediff<timeout: #if the first loop didn"t timemout, 2nd loop
37+
timediff=0
38+
startT=utime.ticks_ms()
39+
#waiting for echo Pin to receive the receding edge of the sound wave
40+
while echo() == 1 and (timediff<timeout):
41+
timediff=utime.ticks_ms()-startT
42+
pass
43+
finish=utime.ticks_us() #used to measue the duration of the pulse
44+
utime.sleep_ms(50) #pause to make sure there is no interferences with the next measure
45+
if timediff>=timeout:
46+
dtime=0 #loop timeout: invalid measure
47+
#print("problem measuring with timediff= %d ms" % timediff)
48+
else:
49+
dtime = (finish-start)/2
50+
return dtime #time is in µs
51+
52+
def advanced_measuretime(trigpin,echopin):
53+
54+
time_samples = [] #initialize a list to store the measures
55+
for count in range(config.nb_samples):
56+
newduration=measureUSblack.single_measure(trigpin,echopin)
57+
#print(newduration)
58+
if newduration !=0: #ignore duration of 0
59+
time_samples.append(newduration) #make a single measure and append it in the list
60+
#print("TimeSimple= %d us" % time_samples[count])
61+
62+
if len(time_samples) !=0: #if all the measures = 0, there is no table!!
63+
time_samples= sorted(time_samples) #sort the list
64+
med = time_samples[int(len(time_samples)/2)]
65+
mean = sum(time_samples, 0.0)/len(time_samples)
66+
dev = sqrt(sum([(x-mean)**2 for x in time_samples])/len(time_samples))
67+
if mean<700 or mean>29999:
68+
return (0,0,0)
69+
else:
70+
return (med,mean,dev)
71+
else:
72+
return (0,0,0)

US_water_level/boot.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# boot.py -- run on boot-up
2+
from network import Bluetooth
3+
from network import WLAN
4+
import pycom
5+
6+
if pycom.wifi_on_boot() == True:
7+
pycom.wifi_on_boot(False)
8+
9+
wlan = WLAN()
10+
wlan.deinit()
11+
12+
bluetooth = Bluetooth()
13+
bluetooth.deinit()
14+
15+
pycom.heartbeat(False) #disable the pycom heartbeat

US_water_level/config.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
########################
2+
# CONFIG FILE !!!!
3+
########################
4+
# PARAMETERS
5+
nb_samples= 25 # Number of samples for the advanced measure of the HC-SR04
6+
loraok= True # Send data through Lora ?
7+
sigfoxok=False # Send data through Sigfox ?
8+
needsleep= True # Enable deep sleep
9+
powersaving= True # Optimize the code for power saving
10+
isDHT11=False # True for DTH11 and False for DTH22
11+
batcoeff=1.98 # Realvoltage=batcoeff * measurevoltage
12+
13+
# SIGFOX CONFIGURATION
14+
# ==> device needs to be registered on SIGFOX !!!
15+
# cf. sigfox website
16+
17+
# LORAWAN create an ABP authentication params to secure datas
18+
TTNregion="AS923" # "AU915" or "AS923"
19+
# device name: 'Saturday Night Fever'
20+
dev_addr='???????'
21+
nwk_swkey='????????????????????????????????'
22+
app_swkey='????????????????????????????????'
23+
sf = 7 #spreading factor (integer between 7 and 12)
24+
dr = 5 #Data Rate
25+
26+
# PINs
27+
DHT = 'P23' # old pin 'P10'
28+
relay = 'P22' #old pin 'P9'
29+
echo = 'P20'
30+
trig = 'P19'
31+
voltmeter='P16'
32+
33+
# led colors
34+
OFF = 0x000000
35+
WHITE=0xFFFFCC
36+
RED = 0xff0000
37+
BLUE = 0x0000ff
38+
GREEN = 0x00ff00
39+
YELLOW = 0x7f7f00
40+
PURPLE = 0x7f007f
41+
ORANGE = 0xFF9900

US_water_level/counter

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
001

US_water_level/dth.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import time
2+
import pycom
3+
from machine import enable_irq, disable_irq, Pin
4+
5+
class DTHResult:
6+
'DHT sensor result returned by DHT.read() method'
7+
8+
ERR_NO_ERROR = 0
9+
ERR_MISSING_DATA = 1
10+
ERR_CRC = 2
11+
12+
error_code = ERR_NO_ERROR
13+
temperature = -1
14+
humidity = -1
15+
16+
def __init__(self, error_code, temperature, humidity):
17+
self.error_code = error_code
18+
self.temperature = temperature
19+
self.humidity = humidity
20+
21+
def is_valid(self):
22+
return self.error_code == DTHResult.ERR_NO_ERROR
23+
24+
class DTH:
25+
'DHT sensor (dht11, dht21,dht22) reader class for Pycom'
26+
27+
#__pin = Pin('P3', mode=Pin.OPEN_DRAIN)
28+
__dhttype = 0
29+
30+
def __init__(self, pin, sensor=0):
31+
self.__pin = Pin(pin, mode=Pin.OPEN_DRAIN)
32+
self.__dhttype = sensor
33+
self.__pin(1)
34+
time.sleep(1.0)
35+
36+
def read(self):
37+
# pull down to low
38+
self.__send_and_sleep(0, 0.019)
39+
data = pycom.pulses_get(self.__pin,100)
40+
self.__pin.init(Pin.OPEN_DRAIN)
41+
self.__pin(1)
42+
#print(data)
43+
bits = []
44+
for a,b in data:
45+
if a ==1 and 18 <= b <= 28:
46+
bits.append(0)
47+
if a ==1 and 65 <= b <= 75:
48+
bits.append(1)
49+
#print("longueur bits : %d " % len(bits))
50+
if len(bits) != 40:
51+
return DTHResult(DTHResult.ERR_MISSING_DATA, 0, 0)
52+
#print(bits)
53+
# we have the bits, calculate bytes
54+
the_bytes = self.__bits_to_bytes(bits)
55+
# calculate checksum and check
56+
checksum = self.__calculate_checksum(the_bytes)
57+
if the_bytes[4] != checksum:
58+
return DTHResult(DTHResult.ERR_CRC, 0, 0)
59+
# ok, we have valid data, return it
60+
[int_rh, dec_rh, int_t, dec_t, csum] = the_bytes
61+
if self.__dhttype==0: #dht11
62+
rh = int_rh #dht11 20% ~ 90%
63+
t = int_t #dht11 0..50°C
64+
else: #dht21,dht22
65+
rh = ((int_rh * 256) + dec_rh)/10
66+
t = (((int_t & 0x7F) * 256) + dec_t)/10
67+
if (int_t & 0x80) > 0:
68+
t *= -1
69+
return DTHResult(DTHResult.ERR_NO_ERROR, t, rh)
70+
71+
def __send_and_sleep(self, output, mysleep):
72+
self.__pin(output)
73+
time.sleep(mysleep)
74+
75+
def __bits_to_bytes(self, bits):
76+
the_bytes = []
77+
byte = 0
78+
79+
for i in range(0, len(bits)):
80+
byte = byte << 1
81+
if (bits[i]):
82+
byte = byte | 1
83+
else:
84+
byte = byte | 0
85+
if ((i + 1) % 8 == 0):
86+
the_bytes.append(byte)
87+
byte = 0
88+
#print(the_bytes)
89+
return the_bytes
90+
91+
def __calculate_checksum(self, the_bytes):
92+
return the_bytes[0] + the_bytes[1] + the_bytes[2] + the_bytes[3] & 255

US_water_level/main.py

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#########################################
2+
#### WETLANDIA water level monitoring ###
3+
#### version 3.1 - dec. 2019 ###
4+
#########################################
5+
## Mind4Stormwater project: http://mind4stormwater.org,
6+
## feel free to use, reuse, modify, share!!
7+
## University of Melbourne / INSA Lyon
8+
## Frédéric Cherqui - [email protected]
9+
#########################################
10+
11+
import config
12+
import utime
13+
import time
14+
import pycom
15+
from Lora import _LoraAU915
16+
from Lora import _LoraAS923
17+
from network import Sigfox
18+
from USblack import measureUSblack
19+
import socket
20+
import binascii #module that makes conversion between binary and Ascii
21+
import ustruct
22+
import machine
23+
from machine import Pin
24+
from machine import ADC
25+
from dth import DTH
26+
# from machine import WDT
27+
#
28+
# wdt = WDT(timeout=65000) #START WATCHDOG
29+
# print("watchdog ON")
30+
# wdt.feed() #RESET WATCHDOG
31+
# print("watchdog +")
32+
33+
#INIT EVERYTHING
34+
pycom.rgbled(config.OFF)
35+
donepin = Pin(config.relay,mode=Pin.OUT)
36+
donepin(0)
37+
adc = ADC()
38+
39+
#CONFIG COMMUNICATION
40+
if config.powersaving==False:
41+
pycom.rgbled(config.WHITE)
42+
utime.sleep(1)
43+
if config.sigfoxok==True:
44+
sigfox = Sigfox(mode=Sigfox.SIGFOX, rcz=Sigfox.RCZ4) # init Sigfox for: RCZ1 (Europe), RCZ4 (Australia)
45+
s = socket.socket(socket.AF_SIGFOX, socket.SOCK_RAW) # create a Sigfox socket
46+
s.setblocking(True) # make the socket blocking
47+
s.settimeout(30) #timeout 10 seconds
48+
s.setsockopt(socket.SOL_SIGFOX, socket.SO_RX, False) # configure it as uplink only
49+
if config.loraok==True and config.TTNregion=="AU915":
50+
s = _LoraAU915.setupLora()
51+
if config.loraok==True and config.TTNregion=="AS923":
52+
s = _LoraAS923.setupLora()
53+
54+
################################
55+
### LOOP IF NO NEED TO SLEEP ###
56+
################################
57+
while (True):
58+
#START PROGR
59+
#LOAD MEASURE counter
60+
f = open("counter")
61+
num=f.read()
62+
f.close()
63+
f=open("counter","w")
64+
newcounter=int(num)+1
65+
if newcounter>65000:
66+
newcounter=0
67+
f.write(str(newcounter))
68+
f.close()
69+
if config.powersaving==False: #and result.is_valid()
70+
print("Measure # ", newcounter)
71+
pycom.rgbled(config.RED)
72+
utime.sleep(0.5)
73+
pycom.rgbled(config.OFF)
74+
utime.sleep(0.2)
75+
pycom.rgbled(config.RED)
76+
utime.sleep(0.5)
77+
pycom.rgbled(config.OFF)
78+
us1med,us1mean,us1dev = measureUSblack.advanced_measuretime(config.trig,config.echo)
79+
if config.powersaving==False:
80+
print("Ultrasonic time = "+ str(us1med)+ " us")
81+
print("Ultrasonic distance = "+ str(us1med*0.34)+ " mm")
82+
if config.isDHT11==True:
83+
th = DTH(config.DHT,0) #for DHT11 sensor (blue)
84+
nameDTH="11"
85+
else:
86+
th = DTH(config.DHT,1) #for DHT22 sensor (white)
87+
nameDTH="22"
88+
# time.sleep(2)
89+
result = th.read()
90+
if config.powersaving==False: #and result.is_valid()
91+
print("Temperature DHT"+nameDTH+" = %d C" % result.temperature)
92+
print("Humidity DHT"+nameDTH+" = %d %%" % result.humidity)
93+
meanbatt=0
94+
j=0
95+
if config.powersaving==False:
96+
pycom.rgbled(config.BLUE)
97+
while j < 5:
98+
utime.sleep(0.2)
99+
batt = adc.channel(attn=3, pin=config.voltmeter)
100+
meanbatt += batt.voltage()/1000
101+
j += 1
102+
batt_volt = (meanbatt/j)*config.batcoeff #multiplcator coef to adjust the real value of the battery voltage
103+
if config.powersaving==False:
104+
print("Battery Voltage = %.3f V" % batt_volt)
105+
if config.powersaving==False:
106+
pycom.rgbled(config.PURPLE)
107+
utime.sleep(0.2)
108+
pycom.rgbled(config.OFF)
109+
utime.sleep(0.1)
110+
pycom.rgbled(config.PURPLE)
111+
utime.sleep(0.2)
112+
pycom.rgbled(config.OFF)
113+
print("Sending message...")
114+
#######################################
115+
####### FAKE VALUES TO TEST ###########
116+
#us1med=750
117+
#result.temperature=12.2
118+
#result.humidity=23.56
119+
#######################################
120+
newcounter_1 = int(newcounter//256)
121+
newcounter_2 = int(newcounter%256)
122+
us1_1 = int(us1med//256)
123+
us1_2 = int(us1med%256)
124+
#minimun number of bytes (for Sigfox google file)
125+
batt12 = int(batt_volt*25)
126+
temp12 = int(result.temperature*5)
127+
humi12 = int(result.humidity*2)
128+
s.send(bytes([newcounter_1, newcounter_2, us1_1,us1_2,batt12,temp12,humi12]))
129+
utime.sleep(0.1)
130+
if config.powersaving==False:
131+
print("Message:",newcounter_1, newcounter_2, us1_1,us1_2,batt_volt,result.temperature,result.humidity)
132+
print("Message sent through SIGFOX:",bool(config.sigfoxok))
133+
print("Message sent through LoRa - ",config.TTNregion," :",bool(config.loraok))
134+
print("------------------------------------")
135+
pycom.rgbled(config.GREEN)
136+
utime.sleep(0.2)
137+
pycom.rgbled(config.OFF)
138+
utime.sleep(0.1)
139+
pycom.rgbled(config.GREEN)
140+
utime.sleep(0.2)
141+
pycom.rgbled(config.OFF)
142+
utime.sleep(0.1)
143+
if config.needsleep == True:
144+
if config.powersaving==False:
145+
print("going to sleep now")
146+
pycom.rgbled(config.BLUE)
147+
while True:
148+
donepin(0)
149+
utime.sleep(0.1)
150+
donepin(1)
151+
utime.sleep(0.1)
152+
# print("waiting 10 seconds before a new measure")
153+
# utime.sleep(10) #waiting 5 minutes between measures if sleep is not needed...

0 commit comments

Comments
 (0)