Exemplo n.º 1
0
def save_dht22_values():
	global _temperature, _humidity, _date_save_dht22
	_humidity, _temperature = get_dht22_values()
	
	_date_save_dht22 = datetime.datetime.now()

	print "Gateway TC : "+_temperature+" C | HU : "+_humidity+" % at "+str(_date_save_dht22)
	
	#save values from the gateway box's DHT22 sensor, if _mongodb is true
	if(_dht22_mongo):
		#saving data in a JSON var
		str_json_data = "{\"th\":"+_temperature+", \"hu\":"+_humidity+"}"
	
		#creating document to add
		doc = {
			"type" : "DATA_GW_DHT22",
			"gateway_eui" : _gwid, 
			"node_eui" : "gw",
			"snr" : "", 
			"rssi" : "", 
			"cr" : "", 
			"datarate" : "", 
			"time" : _date_save_dht22,
			"data" : json.dumps(json.loads(str_json_data))
		}
	
		#adding the document
		add_document(doc)
Exemplo n.º 2
0
def save_dht22_values():
    global _temperature, _humidity, _date_save_dht22
    _humidity, _temperature = get_dht22_values()

    _date_save_dht22 = datetime.datetime.utcnow()

    print "Gateway TC : " + _temperature + " C | HU : " + _humidity + " % at " + str(
        _date_save_dht22)

    #save values from the gateway box's DHT22 sensor, if _mongodb is true
    if (_mongodb):
        #saving data in a JSON var
        str_json_data = "{\"th\":" + _temperature + ", \"hu\":" + _humidity + "}"

        #creating document to add
        doc = {
            "type": "DATA_GW_DHT22",
            "gateway_eui": _gwaddr,
            "node_eui": "gw",
            "snr": "",
            "rssi": "",
            "cr": "",
            "datarate": "",
            "time": _date_save_dht22,
            "data": json.dumps(json.loads(str_json_data))
        }

        #adding the document
        add_document(doc)
Exemplo n.º 3
0
def get_dht22():

    try:
        _dht22_mongo = gw_json_array["status_conf"]["dht22_mongo"]
    except KeyError:
        _dht22_mongo = False

    if (_dht22_mongo):
        global add_document
        from MongoDB import add_document

    print "post status get DHT22: get gateway temperature and humidity level"
    #read values from dht22 in the gateway box
    sys.path.insert(0, os.path.expanduser('./sensors_in_raspi/dht22'))
    from read_dht22 import get_dht22_values

    _humidity, _temperature = get_dht22_values()

    _date_save_dht22 = datetime.datetime.now()

    print "post status get DHT22: Gateway TC : " + _temperature + " C | HU : " + _humidity + " % at " + str(
        _date_save_dht22)

    #save values from the gateway box's DHT22 sensor, if _mongodb is true
    if (_dht22_mongo):
        #saving data in a JSON var
        str_json_data = "{\"th\":" + _temperature + ", \"hu\":" + _humidity + "}"

        #creating document to add
        doc = {
            "type": "DATA_GW_DHT22",
            "gateway_eui": _gwid,
            "node_eui": "gw",
            "snr": "",
            "rssi": "",
            "cr": "",
            "datarate": "",
            "time": _date_save_dht22,
            "data": json.dumps(json.loads(str_json_data))
        }

        #adding the document
        add_document(doc)
Exemplo n.º 4
0
                    #creating document to add
                    doc = {
                        "type": ptypestr,
                        "gateway_eui": _gwaddr,
                        "node_eui": src,
                        "snr": SNR,
                        "rssi": RSSI,
                        "cr": cr,
                        "datarate": "SF" + str(sf) + "BW" + str(bw),
                        "time": now,
                        "data": json.dumps(json.loads(str_json_data))
                    }

                    #adding the document
                    add_document(doc)

                    print("MongoDB: saving done")

                sensor_entry = 'sensor%d' % (src)
                msg_entry = 'msg%d' % (seq)

                #upload data to firebase
                firebase_uploadSingleData(firebase_msg, sensor_entry,
                                          msg_entry, now)

            elif (
                    ch == '!'
            ):  #log on thingspeak, grovestreams, sensorcloud and connectingnature

                ldata = getAllLine()
Exemplo n.º 5
0
def main(ldata, pdata, rdata, tdata, gwid):

	now = datetime.datetime.utcnow()
	
	# this is common code to process packet information provided by the main gateway script (i.e. post_processing_gw.py)
	# these information are provided in case you need them	
	arr = map(int,pdata.split(','))
	dst=arr[0]
	ptype=arr[1]				
	src=arr[2]
	seq=arr[3]
	datalen=arr[4]
	SNR=arr[5]
	RSSI=arr[6]
	
	arr = map(int,rdata.split(','))
	bw=arr[0]
	cr=arr[1]
	sf=arr[2]

	# this part depends on the syntax used by the end-device
	# we use: thingspeak_channel#thingspeak_field#TC/22.4/HU/85... 
	#		  ##TC/22.4/HU/85... or TC/22.4/HU/85... or thingspeak_channel##TC/22.4/HU/85... or #thingspeak_field#TC/22.4/HU/85... to use some default value
				
	# get number of '#' separator
	nsharp = ldata.count('#')			
	#no separator
	if nsharp==0:
		#will use default channel and field
		data=['','']
		
		#contains ['', '', "s1", s1value, "s2", s2value, ...]
		data_array = data + re.split("/", ldata)		
	elif nsharp==1:
		#only 1 separator
		
		data_array = re.split("#|/", ldata)
		
		#if the first item has length > 1 then we assume that it is a channel write key
		if len(data_array[0])>1:
			#insert '' to indicate default field
			data_array.insert(1,'');		
		else:
			#insert '' to indicate default channel
			data_array.insert(0,'');		
	else:
		#contains [channel, field, "s1", s1value, "s2", s2value, ...]
		data_array = re.split("#|/", ldata)	
		
	#just in case we have an ending CR or 0
	data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\n', '')
	data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\0', '')	
	
	#test if there are characters at the end of each value, then delete these characters
	i = 3
	while i < len(data_array) :
		while not data_array[i][len(data_array[i])-1].isdigit() :
			data_array[i] = data_array[i][:-1]
		i += 2

	print("MongoDB: removing obsolete entries")
	
	#check if new month
	remove_if_new_month(now)

	print("MongoDB: saving the document in the collection...")
	
	#saving data in a JSON var
	str_json_data = "{"
	#start from the first nomenclature
	iteration = 2
	while iteration < len(data_array)-1 :
		#last iteration, do not add "," at the end
		if iteration == len(data_array)-2 :
			str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]
		else :
			str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]+", "
		iteration += 2
	str_json_data += "}"

	#creating document to add
	doc = {
		"type" : ptype,
		"gateway_eui" : gwid, 
		"node_eui" : src,
		"snr" : SNR, 
		"rssi" : RSSI, 
		"cr" : cr, 
		"datarate" : "SF"+str(sf)+"BW"+str(bw),
		"time" : now,
		"data" : json.dumps(json.loads(str_json_data))
	}

	#adding the document
	add_document(doc)

	print("MongoDB: saving done")	
Exemplo n.º 6
0
def main(ldata, pdata, rdata, tdata, gwid):

    now = datetime.datetime.utcnow()

    # this is common code to process packet information provided by the main gateway script (i.e. post_processing_gw.py)
    # these information are provided in case you need them
    arr = map(int, pdata.split(','))
    dst = arr[0]
    ptype = arr[1]
    src = arr[2]
    seq = arr[3]
    datalen = arr[4]
    SNR = arr[5]
    RSSI = arr[6]

    arr = map(int, rdata.split(','))
    bw = arr[0]
    cr = arr[1]
    sf = arr[2]

    # this part depends on the syntax used by the end-device
    # we use: TC/22.4/HU/85...
    #
    # but we accept also a_str#b_str#TC/22.4/HU/85...
    # or simply 22.4 in which case, the nomemclature will be DEF

    # get number of '#' separator
    nsharp = ldata.count('#')
    nslash = 0

    #will field delimited by '#' in the MongoDB
    data = ['', '']

    #no separator
    if nsharp == 0:
        # get number of '/' separator on ldata
        nslash = ldata.count('/')

        #contains ['', '', "s1", s1value, "s2", s2value, ...]
        data_array = data + re.split("/", ldata)
    else:
        data_array = re.split("#", ldata)

        # only 1 separator
        if nsharp == 1:
            # get number of '/' separator on data_array[1]
            nslash = data_array[1].count('/')

            # then reconstruct data_array
            data_array = data + re.split("/", data_array[1])

        # we have 2 separators
        if nsharp == 2:
            # get number of '/' separator on data_array[2]
            nslash = data_array[2].count('/')

            # then reconstruct data_array
            data_array = data + re.split("/", data_array[2])

    #just in case we have an ending CR or 0
    data_array[len(data_array) - 1] = data_array[len(data_array) - 1].replace(
        '\n', '')
    data_array[len(data_array) - 1] = data_array[len(data_array) - 1].replace(
        '\0', '')

    #test if there are characters at the end of each value, then delete these characters
    i = 3
    while i < len(data_array):
        while not data_array[i][len(data_array[i]) - 1].isdigit():
            data_array[i] = data_array[i][:-1]
        i += 2

    print("MongoDB: removing obsolete entries")

    #check if new month
    remove_if_new_month(now)

    print("MongoDB: saving the document in the collection...")

    #saving data in a JSON var
    str_json_data = "{"

    # add here the RSSI
    str_json_data += "\"RSSI\" : " + str(RSSI) + ", "

    #start from the first nomenclature
    iteration = 2

    if nslash == 0:
        # no nomenclature, use DEF
        str_json_data += "\"DEF\" : " + data_array[iteration]
    else:
        while iteration < len(data_array) - 1:
            #last iteration, do not add "," at the end
            if iteration == len(data_array) - 2:
                str_json_data += "\"" + data_array[
                    iteration] + "\" : " + data_array[iteration + 1]
            else:
                str_json_data += "\"" + data_array[
                    iteration] + "\" : " + data_array[iteration + 1] + ", "
            iteration += 2

    str_json_data += "}"

    #creating document to add
    doc = {
        "type": ptype,
        "gateway_eui": gwid,
        "node_eui": src,
        "snr": SNR,
        "rssi": RSSI,
        "cr": cr,
        "datarate": "SF" + str(sf) + "BW" + str(bw),
        "time": now,
        "data": json.dumps(json.loads(str_json_data))
    }

    #adding the document
    add_document(doc)

    print("MongoDB: saving done")
Exemplo n.º 7
0
def main(ldata, pdata, rdata, tdata, gwid):

	now = datetime.datetime.utcnow()
	
	# this is common code to process packet information provided by the main gateway script (i.e. post_processing_gw.py)
	# these information are provided in case you need them	
	arr = map(int,pdata.split(','))
	dst=arr[0]
	ptype=arr[1]				
	src=arr[2]
	seq=arr[3]
	datalen=arr[4]
	SNR=arr[5]
	RSSI=arr[6]
	
	arr = map(int,rdata.split(','))
	bw=arr[0]
	cr=arr[1]
	sf=arr[2]

	# this part depends on the syntax used by the end-device
	# we use: thingspeak_channel#thingspeak_field#TC/22.4/HU/85... 
	#		  ##TC/22.4/HU/85... or TC/22.4/HU/85... or thingspeak_channel##TC/22.4/HU/85... or #thingspeak_field#TC/22.4/HU/85... to use some default value
				
	# get number of '#' separator
	nsharp = ldata.count('#')			
	#no separator
	if nsharp==0:
		#will use default channel and field
		data=['','']
		
		#contains ['', '', "s1", s1value, "s2", s2value, ...]
		data_array = data + re.split("/", ldata)		
	elif nsharp==1:
		#only 1 separator
		
		data_array = re.split("#|/", ldata)
		
		#if the first item has length > 1 then we assume that it is a channel write key
		if len(data_array[0])>1:
			#insert '' to indicate default field
			data_array.insert(1,'');		
		else:
			#insert '' to indicate default channel
			data_array.insert(0,'');		
	else:
		#contains [channel, field, "s1", s1value, "s2", s2value, ...]
		data_array = re.split("#|/", ldata)	
		
	#just in case we have an ending CR or 0
	data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\n', '')
	data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\0', '')	
	
	#test if there are characters at the end of each value, then delete these characters
	i = 3
	while i < len(data_array) :
		while not data_array[i][len(data_array[i])-1].isdigit() :
			data_array[i] = data_array[i][:-1]
		i += 2

	print("MongoDB: removing obsolete entries")
	
	#check if new month
	remove_if_new_month(now)

	print("MongoDB: saving the document in the collection...")
	
	#saving data in a JSON var
	str_json_data = "{"
	
	# add here the RSSI
	str_json_data += "\"RSSI\" : "+str(RSSI)+", "
	
	#start from the first nomenclature
	iteration = 2
	while iteration < len(data_array)-1 :
		#last iteration, do not add "," at the end
		if iteration == len(data_array)-2 :
			str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]
		else :
			str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]+", "
		iteration += 2
	str_json_data += "}"
	
	#creating document to add
	doc = {
		"type" : ptype,
		"gateway_eui" : gwid, 
		"node_eui" : src,
		"snr" : SNR, 
		"rssi" : RSSI, 
		"cr" : cr, 
		"datarate" : "SF"+str(sf)+"BW"+str(bw),
		"time" : now,
		"data" : json.dumps(json.loads(str_json_data))
	}

	#adding the document
	add_document(doc)

	print("MongoDB: saving done")	
Exemplo n.º 8
0
				
					#creating document to add
					doc = {
						"type" : ptypestr,
						"gateway_eui" : _gwaddr, 
						"node_eui" : src,
						"snr" : SNR, 
						"rssi" : RSSI, 
						"cr" : cr, 
						"datarate" : "SF"+str(sf)+"BW"+str(bw), 
						"time" : now,
						"data" : json.dumps(json.loads(str_json_data))
					}
				
					#adding the document
					add_document(doc)
				
					print("MongoDB: saving done")
				
				sensor_entry='sensor%d'% (src)
				msg_entry='msg%d' % (seq)	
				
				#upload data to firebase
				firebase_uploadSingleData(firebase_msg, sensor_entry, msg_entry, now)
				
			elif (ch=='!'): #log on thingspeak, grovestreams, sensorcloud and connectingnature
	
				ldata = getAllLine()
				
				# get number of '#' separator
				nsharp = ldata.count('#')			
Exemplo n.º 9
0
def main(ldata, pdata, rdata, tdata, gwid):

	now = datetime.datetime.utcnow()
	
	# this is common code to process packet information provided by the main gateway script (i.e. post_processing_gw.py)
	# these information are provided in case you need them	
	arr = map(int,pdata.split(','))
	dst=arr[0]
	ptype=arr[1]				
	src=arr[2]
	seq=arr[3]
	datalen=arr[4]
	SNR=arr[5]
	RSSI=arr[6]
	
	arr = map(int,rdata.split(','))
	bw=arr[0]
	cr=arr[1]
	sf=arr[2]

	# this part depends on the syntax used by the end-device
	# we use: TC/22.4/HU/85...
	#
	# but we accept also a_str#b_str#TC/22.4/HU/85...
	# or simply 22.4 in which case, the nomemclature will be DEF
				
	# get number of '#' separator
	nsharp = ldata.count('#')
	nslash=0

	#will field delimited by '#' in the MongoDB
	data=['','']
							
	#no separator
	if nsharp==0:
		# get number of '/' separator on ldata
		nslash = ldata.count('/')
				
		#contains ['', '', "s1", s1value, "s2", s2value, ...]
		data_array = data + re.split("/", ldata)
	else:				
		data_array = re.split("#", ldata)
		
		# only 1 separator
		if nsharp==1:
			# get number of '/' separator on data_array[1]
			nslash = data_array[1].count('/')
		
			# then reconstruct data_array
			data_array=data + re.split("/", data_array[1])	

		# we have 2 separators
		if nsharp==2:
			# get number of '/' separator on data_array[2]
			nslash = data_array[2].count('/')
		
			# then reconstruct data_array
			data_array=data + re.split("/", data_array[2])
		
	#just in case we have an ending CR or 0
	data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\n', '')
	data_array[len(data_array)-1] = data_array[len(data_array)-1].replace('\0', '')	
	
	#test if there are characters at the end of each value, then delete these characters
	i = 3
	while i < len(data_array) :
		while not data_array[i][len(data_array[i])-1].isdigit() :
			data_array[i] = data_array[i][:-1]
		i += 2

	print("MongoDB: removing obsolete entries")
	
	#check if new month
	remove_if_new_month(now)

	print("MongoDB: saving the document in the collection...")
	
	#saving data in a JSON var
	str_json_data = "{"
	
	# add here the RSSI
	str_json_data += "\"RSSI\" : "+str(RSSI)+", "
	
	#start from the first nomenclature
	iteration = 2
	
	if nslash==0:
		# no nomenclature, use DEF
		str_json_data += "\"DEF\" : "+data_array[iteration]
	else:
		while iteration < len(data_array)-1 :
			#last iteration, do not add "," at the end
			if iteration == len(data_array)-2 :
				str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]
			else :
				str_json_data += "\""+data_array[iteration]+"\" : "+data_array[iteration+1]+", "
			iteration += 2
		
	str_json_data += "}"
	
	#creating document to add
	doc = {
		"type" : ptype,
		"gateway_eui" : gwid, 
		"node_eui" : src,
		"snr" : SNR, 
		"rssi" : RSSI, 
		"cr" : cr, 
		"datarate" : "SF"+str(sf)+"BW"+str(bw),
		"time" : now,
		"data" : json.dumps(json.loads(str_json_data))
	}

	#adding the document
	add_document(doc)

	print("MongoDB: saving done")