boncey / Flickr4Java

Java API For Flickr. Fork of FlickrJ
BSD 2-Clause "Simplified" License
176 stars 155 forks source link

#FiPy #Pysense sensor to TTN #626

Closed Moalidinar closed 2 years ago

Moalidinar commented 2 years ago

Subject of the issue

Hi All I am doing my Hon's Project on IoT and LoRa network, I am trying to send my data over LoRa using VS(pymark) I have found some code here helped me but the problem is now the code I am using now below it has an error when I try to run it its stop in line52, I am trying to send the reading from the sensors to TTN so with sorting the reading by the name of the sensor type or colours I am not sure how to get that the code below managed to work it out but still not working any help please?

Please copy paste the log here if available

import time
import pycom
import machine
from network import LoRa
import socket
import ubinascii

from LIS2HH12 import LIS2HH12
from SI7006A20 import SI7006A20
from LTR329ALS01 import LTR329ALS01
from MPL3115A2 import MPL3115A2,ALTITUDE,PRESSURE
from pycoproc import Pycoproc
pycom.heartbeat(False)
pycom.rgbled(0x0A0A08) # white

# Initialise LoRa in LORAWAN mode.
# Please pick the region that matches where you are using the device:
# Asia = LoRa.AS923
# Australia = LoRa.AU915
# Europe = LoRa.EU868
# United States = LoRa.US915
lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)

# create an OTAA authentication parameters, change them to the provided credentials
app_eui = ubinascii.unhexlify('0000000000000000')
app_key = ubinascii.unhexlify('5FD4AB619D3D90D3B8B2927A0117700B')
#uncomment to use LoRaWAN application provided dev_eui
dev_eui = ubinascii.unhexlify('70B3D54990FF3C32')

# Uncomment for US915 / AU915 & Pygate
# 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 a network using OTAA (Over the Air Activation)
#uncomment below to use LoRaWAN application provided dev_eui
#lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
lora.join(activation=LoRa.OTAA, auth=(dev_eui, app_eui, app_key), timeout=0)

# wait until the module has joined the network
while not lora.has_joined():
    time.sleep(2.5)
    print('Not yet joined...')

print('Joined')
# create a LoRa socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)

# set the LoRaWAN data rate
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)

# make the socket blocking
# (waits for the data to be sent and for the 2 receive windows to expire)
s.setblocking(True)

#pybytes strings

pybytes_enabled = False
if 'pybytes' in globals():
    if(pybytes.isconnected()):
        print('Pybytes is connected, sending signals to Pybytes')
        pybytes_enabled = True

py = Pycoproc()

alt = MPL3115A2(py,mode=ALTITUDE) # Returns height in meters. Mode may also be set to PRESSURE, returning a value in Pascals
print("MPL3115A2 temperature: " + str(alt.temperature()))
print("Altitude: " + str(alt.altitude()))
pres = MPL3115A2(py,mode=PRESSURE) # Returns pressure in Pa. Mode may also be set to ALTITUDE, returning a value in meters
print("Pressure: " + str(pres.pressure()))
# send to pybytes

dht = SI7006A20(py)
print ('Temperature type',type(dht.temperature()))
print("Temperature: " + str(dht.temperature())+ " deg C and Relative Humidity: " + str(dht.humidity()) + " %RH")
print("Dew point: "+ str(dht.dew_point()) + " deg C")
#change to your ambient temperature
t_ambient = 24.4
print("Humidity Ambient for " + str(t_ambient) + " deg C is " + str(dht.humid_ambient(t_ambient)) + "%RH")

li = LTR329ALS01(py)
light = li.light()
print ('light type',type(li.light()))
print("Light (channel Blue lux, channel Red lux): " + str(light[0]) + '   ' +str(light[1]))
pybytes.send_signal(0, light[0]) # channel Blue
pybytes.send_signal(1, light[1]) # channel Red

acc = LIS2HH12(py)
print ('acceleration type',type(acc.acceleration()))
print("Acceleration: " + str(acc.acceleration()))
print("Roll: " + str(acc.roll()))
print("Pitch: " + str(acc.pitch()))

print("Battery voltage: " + str(py.read_battery_voltage()))

# make the socket non-blocking
# (because if there's no data received it will block forever...)
s.setblocking(False)

# get any data received (if any...)
data = s.recv(64)
print(data)

while(True):
    lit = li.light()
    acce = acc.acceleration()
    s.send(bytes([int(dht.temperature())]))
    s.send(bytes([int(lit[0])]))
    s.send(bytes([int(lit[1])]))
    s.send(bytes([int(acce[0])]))
   # s.send(bytes([int(acce[1])]))
    #s.send(bytes([int(acce[2])]))
    print("Sent data to and TTN")    
    time.sleep(15)