def save_task(): while True: time.sleep(Conf.save_interval) #60 seconds #format to ['device_id', 'date', 'time', 'Tmp', 'RH', 'PM2.5','PM10', 'PM1.0','Lux', 'CO2', 'TVOC'] pairs = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S").split(" ") format_data_list = [Conf.DEVICE_ID,pairs[0],pairs[1],TEMP,HUM,PM25_AE,PM1_AE,PM10_AE,Illuminance,CO2,TVOC] try: pi.save_data(path,format_data_list) #save to host pi.save_to_SD(format_data_list) #save to SD card print("message saved!") except: print("Fail to save message!")
def upload_task(): while True: time.sleep(Conf.upload_interval) #300 seconds pairs = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S").split(" ") ##remove GPS if we're not using it #if(use_GPS == 1): # msg = "|gps_lat=" + str(Conf.gps_lat) + "|s_t0=" + str(TEMP) + "|app=" + str(Conf.APP_ID) + "|date=" + pairs[0] + "|s_d2=" + str(PM1_AE) + "|s_d0=" + str(PM25_AE) + "|s_d1=" + str(PM10_AE) + "|s_h0=" + str(HUM) + "|device_id=" + Conf.DEVICE_ID +"|s_g8=" + str(CO2) + "|s_gg=" + str(TVOC) + "|gps_lon="+ str(Conf.gps_lon) +"|ver_app=" + str(Conf.ver_app) + "|time=" + pairs[1] #else: # msg = "|s_t0=" + str(TEMP) + "|app=" + str(Conf.APP_ID) + "|date=" + pairs[0] + "|s_d2=" + str(PM1_AE) + "|s_d0=" + str(PM25_AE) + "|s_d1=" + str(PM10_AE) + "|s_h0=" + str(HUM) + "|device_id=" + Conf.DEVICE_ID +"|s_g8=" + str(CO2) + "|s_gg=" + str(TVOC) + "|ver_app=" + str(Conf.ver_app) + "|time=" + pairs[1] #rewrite message packing msg = "" if (use_GPS == 1): msg = msg + "|gps_lon=" + str(Conf.gps_lon) + "|gps_lat=" + str( Conf.gps_lat) if (use_CO2 == 1): msg = msg + "|s_g8=" + str(CO2) msg = msg + "|s_t0=" + str(TEMP) + "|app=" + str( Conf.APP_ID) + "|date=" + pairs[0] + "|s_d2=" + str( PM1_AE) + "|s_d0=" + str(PM25_AE) + "|s_d1=" + str( PM10_AE) + "|s_h0=" + str( HUM) + "|device_id=" + Conf.DEVICE_ID + "|s_gg=" + str( TVOC) + "|ver_app=" + str( Conf.ver_app) + "|time=" + pairs[1] print("message ready") restful_str = Conf.Restful_URL + "topic=" + Conf.APP_ID + "&device_id=" + Conf.DEVICE_ID + "&key=" + Conf.SecureKey + "&msg=" + msg try: r = requests.get(restful_str) print("send result") print(r) print("message sent!") except: print("internet err!!") #save after upload / makesure data will be synchronize format_data_list = [ Conf.DEVICE_ID, pairs[0], pairs[1], TEMP, HUM, PM25_AE, PM1_AE, PM10_AE, Illuminance, CO2, TVOC ] try: pi.save_data(path, format_data_list) #save to host pi.save_to_SD(format_data_list) #save to SD card print("send message saved!") except: print("Fail to save sent message!")
def upload_task(): while True: time.sleep(Conf.upload_interval) #300 seconds pairs = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S").split(" ") msg = "" if ((gps_lat != "") and (gps_lon != "")): msg = msg + "|gps_lon=" + gps_lon + "|gps_lat=" + gps_lat if (CO2 != 65535): msg = msg + "|s_g8=" + str(CO2) msg = msg + "|s_t0=" + str(TEMP) + "|app=" + str( Conf.APP_ID ) + "|date=" + pairs[0] + "|s_d0=" + str(PM25_AE) + "|s_h0=" + str( HUM) + "|device_id=" + Conf.DEVICE_ID + "|s_gg=" + str( TVOC) + "|ver_app=" + str(Conf.ver_app) + "|time=" + pairs[1] if ((Leq != 0) and (Leq != float("inf"))): msg = msg + "|s_s0=" + str(Leq_Median) + "|s_s0M=" + str( Leq_Max) + "|s_s0m=" + str(Leq_Min) + "|s_s0L=" + str(Leq) print("message ready") restful_str = Conf.Restful_URL + "topic=" + Conf.APP_ID + "&device_id=" + Conf.DEVICE_ID + "&key=" + Conf.SecureKey + "&msg=" + msg try: r = requests.get(restful_str) print("send result") print(r) print("message sent!") except: print("internet err!!") #save after upload / makesure data will be synchronize format_data_list = [ Conf.DEVICE_ID, pairs[0], pairs[1], TEMP, HUM, PM25_AE, PM1_AE, PM10_AE, Illuminance, CO2, TVOC, Leq, Leq_Max, Leq_Min, Leq_Median, gps_lat, gps_lon ] try: pi.save_data(path, format_data_list) #save to host pi.save_to_SD(format_data_list) #save to SD card print("send message saved!") except: print("Fail to save sent message!")
def save_task(): while True: time.sleep(Conf.save_interval) #60 seconds #format to ['device_id', 'date', 'time', 'Tmp', 'RH', 'PM2.5','PM10', 'PM1.0','Lux', 'CO2', 'TVOC' ,'Leq','Leq_Max','Leq_Min','Leq_Median','gps_lat','gps_lon'] pairs = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S").split(" ") #format_data_list = [Conf.DEVICE_ID,pairs[0],pairs[1],TEMP,HUM,PM25_AE,PM1_AE,PM10_AE,Illuminance,CO2,TVOC,Leq,Leq_Max,Leq_Min,Leq_Median,gps_lat,gps_lon] #mod for temp/RH plugin format_data_list = [ Conf.DEVICE_ID, pairs[0], pairs[1], TEMP, HUM, PM25_AE, PM1_AE, PM10_AE, Illuminance, CO2, TVOC, temp_data, rh_data, gps_lat, gps_lon ] #print("********") #print(format_data_list) #print("********") try: pi.save_data(path, format_data_list) #save to host pi.save_to_SD(format_data_list) #save to SD card print("message saved!") except: print("Fail to save message!(regular save)")
def upload_task(): while True: time.sleep(Conf.upload_interval) #300 seconds pairs = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S").split(" ") msg = "" #mqtt_msg = "" if ((gps_lat != "") and (gps_lon != "")): msg = msg + "|gps_lon=" + gps_lon + "|gps_lat=" + gps_lat #mqtt_msg = mqtt_msg + "\",\"gps_lon\":\"" + gps_lon + "\",\"gps_lat\":\"" + gps_lat if (CO2 != 65535): msg = msg + "|s_g8=" + str(CO2) #mqtt_msg = mqtt_msg + "\",\"s_g8\":\"" + str(CO2) msg = msg + "|s_t0=" + str(TEMP) + "|app=" + str( Conf.APP_ID ) + "|date=" + pairs[0] + "|s_d0=" + str(PM25_AE) + "|s_h0=" + str( HUM) + "|device_id=" + Conf.DEVICE_ID + "|s_gg=" + str( TVOC) + "|ver_app=" + str(Conf.ver_app) + "|time=" + pairs[1] #mqtt_msg = mqtt_msg + "\",\"s_t0\":\"" + str(TEMP) + "\",\"app\":\"" + str(Conf.APP_ID) + "\",\"date\":\"" + pairs[0] + "\",\"s_d0\":\"" + str(PM25_AE) + "\",\"s_h0\":\"" + str(HUM) + "\",\"device_id\":\"" + Conf.DEVICE_ID + "\",\"s_gg\":\"" + str(TVOC) + "\",\"ver_app\":\"" + str(Conf.ver_app) + "\",\"time\":\"" + pairs[1] #if(Leq != 0): # msg = msg + "|s_s0=" + str(Leq_Median) + "|s_s0M=" + str(Leq_Max) + "|s_s0m=" + str(Leq_Min) + "|s_s0L=" + str(Leq) if (temp_data != 0): msg = msg + "|add_temp=" + str(temp_data) + "|add_RH=" + str( rh_data) #mqtt_msg = mqtt_msg + "\",\"add_temp\":\"" + str(temp_data) + "\",\"add_RH\":\"" + str(rh_data) #use mosquitto_pub to publish data print("message ready!!") #mqtt_msg = "{" + mqtt_msg + "\"}" # json format #mqtt_msg = mqtt_msg.replace(",", "", 1) #mqtt_msg = mqtt_msg.replace("\"", "", 1) #p = subprocess.Popen("mosquitto_pub -h " + os.environ.get('BROKER_IP') + " -m " + mqtt_msg + " -t test/sensor1 -q 0 -i \"" + Conf.DEVICE_ID + "\" -d \ #--tls-version tlsv1.3 --cafile /home/MAPS6_MVP/certs/CA.crt --cert /home/MAPS6_MVP/certs/publisher.crt --key /home/MAPS6_MVP/certs/publisher.key", shell=True) #p.wait() #print("message sent!!") restful_str = Conf.Restful_URL + "topic=" + Conf.APP_ID + "&device_id=" + Conf.DEVICE_ID + "&key=" + Conf.SecureKey + "&msg=" + msg try: r = requests.get(restful_str) print("send result") print(r) print("message sent!") except: print("internet err!!") #save after upload / makesure data will be synchronize #format_data_list = [Conf.DEVICE_ID,pairs[0],pairs[1],TEMP,HUM,PM25_AE,PM1_AE,PM10_AE,Illuminance,CO2,TVOC,Leq,Leq_Max,Leq_Min,Leq_Median,gps_lat,gps_lon] #mod for temp/RH plugin format_data_list = [ Conf.DEVICE_ID, pairs[0], pairs[1], TEMP, HUM, PM25_AE, PM1_AE, PM10_AE, Illuminance, CO2, TVOC, temp_data, rh_data, gps_lat, gps_lon ] try: pi.save_data(path, format_data_list) #save to host pi.save_to_SD(format_data_list) #save to SD card print("send message saved!") except: print("Fail to save sent message!(upload)") traceback.print_exc()