from LTR329ALS01 import LTR329ALS01 from MPL3115A2 import MPL3115A2, ALTITUDE, PRESSURE import pycom import time pycom.heartbeat(False) py = Pysense() # display the reset reason code and the sleep remaining in seconds # possible values of wakeup reason are: # WAKE_REASON_ACCELEROMETER = 1 # WAKE_REASON_PUSH_BUTTON = 2 # WAKE_REASON_TIMER = 4 # WAKE_REASON_INT_PIN = 8 print("Wakeup reason: " + str(py.get_wake_reason()) + "; Approximate sleep remaining: " + str(py.get_sleep_remaining()) + " sec") time.sleep(0.5) # enable wakeup source from INT pin py.setup_int_pin_wake_up(False) # enable activity and also inactivity interrupts, using the default callback handler py.setup_int_wake_up(True, True) acc = LIS2HH12() mp = MPL3115A2( py, mode=ALTITUDE ) # Returns height in meters. Mode may also be set to PRESSURE, returning a value in Pascals mpp = MPL3115A2(
# setup wifi client wlan = WLAN(mode=WLAN.STA) wlan.connect(wifi_ssid, auth=(WLAN.WPA2, wifi_key), timeout=5000) while not wlan.isconnected(): machine.idle() # clock gating to reduce power consumption print("Connected to WiFi\n") pycom.rgbled(0x00FF00) # green LED to confirm wifi connection time.sleep(1) pycom.rgbled(0x000000) #disable LED # create our Pysense instance py = Pysense() # deep sleep mode of the board disables USB # we use an interrupt on the button to stop code execution and retrieve USB control to upload new code if py.get_wake_reason() == WAKE_REASON_PUSH_BUTTON: print("Stopping program") sys.exit() # initialize light sensor # 1X gain is 1lux to 64k lux range. Measurement over 500ms with samples every 100ms lt = LTR329ALS01(py, gain=LTR329ALS01.ALS_GAIN_1X, integration=LTR329ALS01.ALS_INT_100, rate=LTR329ALS01.ALS_RATE_500) time.sleep(0.5) # give time to sensor init # light() returns blue & red wavelength lux values as tuple if lt.light() > (lux_threshold, lux_threshold): pycom.rgbled(0xFF0000) # blink red time.sleep(3)
battery = int(round(self.py.read_battery_voltage(),4)*10000-33000).to_bytes(2,'little') #Quinto Elemento Lista: Voltaje (cuatro decimales) light = int(self.lt.light()[0]).to_bytes(2,'little') #Primer Elemento Lista: Luminosidad (entero) reading = light+pressure+humidity+temperature+battery #Union de tipos bytes return reading #------------------------------------------------------------------------------# #Codigo principal #Desactiva el heartbeat app_eui = binascii.unhexlify('70B3D57ED0009F73') #ID de la app. (Seleccionada por el usuario) dev_eui = binascii.unhexlify('70B3D5499CE967FB') app_key = binascii.unhexlify('054BFCAC2632EB70D56F4BCBB8D95F02') #Clave de la app para realizar el handshake. Única para cada dispositivo. ajuste = 5 #Numero de segundos para que el intervalo sea exacto en el Network Server #TODO: REAL TIME py = Pysense() #------------------------------------------------------------------------------# #Según el modo de inicio, se realizan unas serie de acciones u otras. if py.get_wake_reason() == WAKE_REASON_TIMER: #Si despierta tras deepsleep print('Woke from a deep sleep R:%d'%(WAKE_REASON_TIMER)) try: sleep_time = pycom.nvs_get('sleep_time') #Obtiene el valor de la variable sleep_time guardado en NVRAM data_rate = pycom.nvs_get('data_rate') except Exception: #No se consigue obtener el valor (ERROR INFO: https://forum.pycom.io/topic/1869/efficiency-of-flash-vs-nvram-and-some-nvs-questions/3) print("Error: Sleep Time / Data Rate could not be recovered. Setting default value") sleep_time = 300 #Se le da el valor por defecto data_rate = 5 pycom.nvs_set('sleep_time', sleep_time) #Guarda el valor por defecto de sleep_time en NVRAM pycom.nvs_set('data_rate', data_rate) print("SleepTime & Data Rate recovered") try: n = Node(sleep_time,data_rate,py) #Crea una instancia de Node
pass #------------------------------------------------------------------------------# #Codigo principal dev_eui = binascii.unhexlify('006A76B0778AEDA7') app_eui = binascii.unhexlify( '70B3D57ED0009ABB') #ID de la app. (Seleccionada por el usuario) app_key = binascii.unhexlify( '08D62712D816F1C28B7E6EA39E711209' ) #Clave de la app para realizar el handshake. Única para cada dispositivo. py = Pysense() #Según el modo de inicio, se realizan unas serie de acciones u otras. if (py.get_wake_reason() == WAKE_REASON_ACCELEROMETER): print('Accelerometer Interrupt.') try: data_rate = pycom.nvs_get('data_rate') except (0): print("Error: Value could not be restore") data_rate = 5 pycom.nvs_set('data_rate', data_rate) pass n = Node(data_rate, py) #Crea una instancia de Node n.readsens() elif (py.get_wake_reason() == WAKE_REASON_PUSH_BUTTON): uart = UART(0, 115200) #Se activa la UART os.dupterm(uart) wlan = WLAN()