def send_analogdata(data): print("Send Data =", data) lora.frame_counter = lora.frame_counter + 1 c = CayenneLPP() c.addAnalogInput(1, lora.frame_counter) c.addAnalogInput(2, myl) c.addAnalogOutput(1, data) #c.addTemperature(1, 23.5) # Add temperature read to channel 1 # c.addAnalogInput(1,lora.frame_counter) #c.addTemperature(2, 22.7) # Add another temperature read to channel 2 #c.addRelativeHumidity(3, 88.5) # Add relative humidity read to channel 3 frame = c.getBuffer() # Get bytes #Send data packet lora.send_data(frame, len(frame), lora.frame_counter) print(lora.frame_counter) display.fill(0) display.text('Sent LPP Data to Lora!', 0, 0, 1) display.text('Sending ' + str(data) + " / " + str(lora.frame_counter), 0, 15, 1) print('Data sent!') display.show() time.sleep(10) display.fill(0)
def __init__(self, state, lora_socket=None, http_config=None, wlan_agent=None, default_unit = None): self._state = state self._lora_socket = lora_socket self._lora_buffer = CayenneLPP(size=51, sock=self._lora_socket) self._http_config = http_config self._default_unit = default_unit self._http_buffer = self.build_basic_http_payload() if self._http_config is not None: self._http_url = self._http_config['url'] self._wlan_agent = wlan_agent
print(" ") print("**************") print("CSS811 values:") print("**************") print('eCO2: %d ppm, TVOC: %d ppb' % (s.eCO2, s.tVOC)) print(" ") #time.sleep(1) #d.fill(0) d.text('eCO2 ppm', 0, 30) d.text(str(s.eCO2), 70, 30) d.text('TVOC ppb', 0, 40) d.text(str(s.tVOC), 70, 40) d.show() time.sleep(3) c = CayenneLPP() c.addTemperature(1, float(temp)) # Add temperature read to channel 1 c.addRelativeHumidity( 2, float(hum)) # Add relative humidity read to channel 2 c.addBarometricPressure(3, float(pa)) c.addLuminosity(4, s.eCO2) c.addLuminosity(5, s.tVOC) #cno=len(ubinascii.hexlify(c.getBuffer())) b = (ubinascii.hexlify(c.getBuffer())) #cno=len(b) print('---------Send Status------------') print("AT+NMGS={0},{1}\r\n".format(int(len(b) / 2), (b.decode('utf-8')))) uart.write("AT+NMGS={0},{1}\r\n".format(int(len(b) / 2), (b.decode('utf-8')))) p = ure.search('FRMPayload:(.+?) \r\n', uart.read().decode('utf-8'))
uart.write("AT+APPSKEY=0000A0280C659A07B30A4E3CA4CF06EC\r\n") print(uart.read()) uart.write("AT+NWKSKEY=0000B1896490A1B402A0BDA6AFD86991\r\n") print(uart.read()) uart.write("AT+ACTIVATE=0\r\n") #ABP Activate print(uart.read()) cnt = 1 while True: print("Packet No #{}".format( cnt ) ) temp,pa,hum = bme.values print("********BME280 values:") print("temp:",temp," Hum:",hum ,"PA:", pa) c = CayenneLPP() c.addTemperature(1, float(temp)) c.addRelativeHumidity(2, float(hum)) c.addBarometricPressure(3, (float(pa)*100)) d = (ubinascii.hexlify(c.getBuffer())) print(" — — — — -Start Send Status — — — — — — ") print("AT+NMGS={0},{1}\r\n".format(int(len(d)/2),(d.decode("utf-8")))) uart.write("AT+NMGS={0},{1}\r\n".format(int(len(d)/2),(d.decode("utf-8")))) #print(uart.read()) p = ure.search("0100(.+?)\r\n", uart.read().decode("utf-8")) try: print (p.group(0)) pgroup=(p.group(0))
d.show() if s.data_ready(): print("**************") print("CSS811 values:") print("**************") print('eCO2: %d ppm, TVOC: %d ppb' % (s.eCO2, s.tVOC)) d.text('eCO2 ppm', 0, 30) d.text(str(s.eCO2), 70, 30) d.text('TVOC ppb', 0, 40) d.text(str(s.tVOC), 70, 40) d.show() #time.sleep(3) c = CayenneLPP() c.addTemperature(1, float(temp)) c.addBarometricPressure(2, float(pa)) c.addTemperature(3, float(tsensor.temperature)) c.addRelativeHumidity(4, float(tsensor.relative_humidity)) c.addLuminosity(5, s.eCO2) c.addLuminosity(6, s.tVOC) c.addDigitalOutput(7, 1) c.addAnalogOutput(8, 120.0) b = (ubinascii.hexlify(c.getBuffer())) print('************ Sending Data Status **************') led.value(1) ATresp = sendATcommand("AT+NMGS={0},{1}".format(int(len(b) / 2), (b.decode('utf-8')))) print('********Finish Sending & Receiving Data Status******')
def get_drone_data(self): payload = CayenneLPP() gps = self.vehicle.location.global_relative_frame imu = self.vehicle.raw_imu ned = self.vehicle.location.local_frame attitude = self.vehicle.attitude payload.addGPS(1, gps.lat, gps.lon, gps.alt) payload.addGyrometer(3, imu.xgyro, imu.ygyro, imu.zgyro) payload.addAccelerometer(5, imu.xacc, imu.yacc, imu.zacc) payload.addNED(7, ned.north, ned.east, ned.down) payload.addAttitude(9, attitude.pitch, attitude.yaw, attitude.roll) print("------------") print(gps) print(ned) print(imu) print(attitude) print("------------") cayenne_format_payload = binascii.hexlify( payload.getBuffer()).decode('utf8') print(cayenne_format_payload) return cayenne_format_payload
def send_all_pi_data(temp, cpu, ram, myl): print("Send all Pi Data =", str(temp)) lora.frame_counter = lora.frame_counter + 1 c = CayenneLPP() c.addAnalogInput(1, lora.frame_counter) c.addAnalogInput(2, myl) c.addTemperature(1, temp) c.addAnalogOutput(1, cpu) c.addAnalogOutput(1, ram) frame = c.getBuffer() # Get bytes #Send data packet lora.send_data(frame, len(frame), lora.frame_counter) print("lora frame counter", lora.frame_counter) display.fill(0) display.text('Sent LPP Data to Lora!', 0, 0, 1) display.text('temp,cpu,ram t=' + str(temp), 0, 10, 1) display.text('lora frame = ' + str(lora.frame_counter), 0, 20, 1) print('Data All Pi sent!') display.show() time.sleep(10)
count = 1 while True: print("\r\n\r\nPacket No #{}".format(count)) pot_value = pot.read() percent = 100 - (100 * (pot_value - WaterValue) / (AirValue - WaterValue)) print(" Soil Moisture (%):" + str(percent)) percentT = str(round(percent, 2)) print("Round Soil Moisture to 2 digid(%):" + percentT) oled.clear() oled.show_text(0, 0, "#", 24) oled.show_text(10, 0, str(count), 24) oled.show_text(30, 24, percentT, 24) oled.show_text(78, 24, "% ", 24) oled.show() c = CayenneLPP() c.addAnalogInput(1, round(percent, 2)) b = ubinascii.hexlify(c.getBuffer()) print("************ Sending Data Status **************") led.value(1) ATresp = sendATcommand("AT+NMGS={0},{1}".format( int(len(b.decode("utf-8")) / 2), (ubinascii.hexlify(c.getBuffer())).decode("utf-8"), )) print("********Finish Sending & Receiving Data Status******") led.value(0) count = count + 1 time.sleep(10.0)
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 4) # Set the Counter for re-checking GPS check_gps = utime.time() while True: utime.sleep(2) print('Attempt to Send') pm25, pm10 = run_sds011.run_sensor() print('Air Quality PM2.5: {}, PM10: {}'.format(pm25, pm10)) # make the socket blocking # (waits for the data to be sent and for the 2 receive windows to expire) s.setblocking(True) # send some data c = CayenneLPP() c.addAnalogOutput(2, acc.pitch()) c.addAnalogOutput(25, pm25) c.addAnalogOutput(10, pm10) # Check if GPS data was found if coords[0] is not None: coords = gps.coordinates() if coords[0] is not None and coords[1] is not None: c.addGPS(5, coords[0], coords[1], 0) s.send(bytes(c.getBuffer())) print('SENT') # make the socket non-blocking # (because if there's no data received it will block forever...) s.setblocking(False) # every 10 seconds i want it to blink
cnt = 1 while True: print("Packet No #{}".format(cnt)) # temp,pa,hum = bme.values temp = bme.temperature hum = bme.humidity pres = bme.pressure vbat = adc.read_core_vbat() # read MCU VBAT print(vbat) print("********BME280 values:") print("temp:", temp, " Hum:", hum, "PA:", pres) c = CayenneLPP() c.addTemperature(1, float(temp)) c.addRelativeHumidity(1, float(hum)) c.addAnalogInput(1, float(vbat)) c.addBarometricPressure(1, (float(pres) * 100)) d = (ubinascii.hexlify(c.getBuffer())) print(" — — — — -Start Send Status — — — — — — ") print("AT+NMGS={0},{1}\r\n".format(int(len(d) / 2), (d.decode("utf-8")))) uart.write("AT+NMGS={0},{1}\r\n".format(int(len(d) / 2), (d.decode("utf-8")))) #print(uart.read()) # p = ure.search("0100(.+?)\r\n", uart.read().decode("utf-8")) # try:
class Sender: def __init__(self, state, lora_socket=None, http_config=None, wlan_agent=None, default_unit = None): self._state = state self._lora_socket = lora_socket self._lora_buffer = CayenneLPP(size=51, sock=self._lora_socket) self._http_config = http_config self._default_unit = default_unit self._http_buffer = self.build_basic_http_payload() if self._http_config is not None: self._http_url = self._http_config['url'] self._wlan_agent = wlan_agent def build_basic_http_payload(self): payload = { "sender_id": "radio_module_01", "datapoints": [] } if self._default_unit is not None: payload["default_unit"] = self._default_unit return payload def send_items(self): if self._state.rf_mode == RF_MODES.LORA: log("Send temps to lora server.") debug("Payloadsize: {}".format(self._lora_buffer.get_size()), self._state.debug_mode) self._lora_buffer.send(reset_payload=True) elif self._http_config is not None and (self._state.rf_mode == RF_MODES.WLAN_AP or self._state.rf_mode == RF_MODES.WLAN_CLIENT): if (self._http_url is None): raise ValueError("Cant make a request without a target url.") try: log("Send temps to http server {}.".format(self._http_url)) res = post(self._http_url, json=self._http_buffer) log("Succed to send data to http server. Status: {} Response: {}".format(res.status_code, res.reason)) except ValueError: log('Host url seems to be incorrect.') raise except OSError as err: log('Error by sending message to http server:') if err.errno == -1: log('No Connection') else: log(err) except Exception as exc: log('Unknown error by sending message to http server.') print_exception(exc) self._http_buffer = self.build_basic_http_payload() else: debug("Send no data, because wrong rf mode.", self._state.debug_mode) def add_temperature(self, value, channel, unit=None): if self._state.rf_mode == RF_MODES.LORA: self._lora_buffer.add_temperature(value, channel) elif self._state.rf_mode == RF_MODES.WLAN_AP or self._state.rf_mode == RF_MODES.WLAN_CLIENT: payload = { "label": "temperature_sensor_" + zfill(channel, 3), "quantity": value } # set unit if temp values are not in the majority if unit is not None: payload['unit'] = unit self._http_buffer['datapoints'].append(payload) else: debug("Not implemented rf mode.", self._state.debug_mode) def is_within_size_limit(self, size): if self._state.rf_mode == RF_MODES.LORA: return self._lora_buffer.is_within_size_limit(size) elif self._state.rf_mode == RF_MODES.WLAN_AP or self._state.rf_mode == RF_MODES.WLAN_CLIENT: return True else: debug("Not implemented rf mode.", self._state.debug_mode) def get_size(self): if self._state.rf_mode == RF_MODES.LORA: return self._lora_buffer.get_size() elif self._state.rf_mode == RF_MODES.WLAN_AP or self._state.rf_mode == RF_MODES.WLAN_CLIENT: return len(list(self._http_buffer.items())) else: debug("Not implemented rf mode.", self._state.debug_mode)