python dht11_raspberry_ubidots.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python dht11_raspberry_ubidots.py相关的知识,希望对你有一定的参考价值。

from ubidots import ApiClient
import RPi.GPIO as GPIO
import time
import wiringpi2 as wiringpi  
 
def bin2dec(string_num):
    return str(int(string_num, 2))

def finish():
    wiringpi.digitalWrite(0, 0)
	exit()

def start():
    wiringpi.digitalWrite(0, 1)

wiringpi.wiringPiSetup()  
wiringpi.pinMode(0, 1)

start()

data = []
 
GPIO.setmode(GPIO.BCM)
 
GPIO.setup(4,GPIO.OUT)
GPIO.output(4,GPIO.HIGH)
time.sleep(0.025)
GPIO.output(4,GPIO.LOW)
time.sleep(0.02)
 
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
for i in range(0,500):
    data.append(GPIO.input(4))
 
bit_count = 0
tmp = 0
count = 0
HumidityBit = ""
TemperatureBit = ""
crc = ""
 
api = ApiClient(token='API_TOKEN')
temp = api.get_variable('TEMP_VARIABLE_TOKEN')
humidity = api.get_variable('HUMIDITY_VARIABLE_TOKEN')

try:
    while data[count] == 1:
            tmp = 1
            count = count + 1

    for i in range(0, 32):
            bit_count = 0

            while data[count] == 0:
                    tmp = 1
                    count = count + 1

            while data[count] == 1:
                    bit_count = bit_count + 1
                    count = count + 1

            if bit_count > 3:
                    if i>=0 and i<8:
                            HumidityBit = HumidityBit + "1"
                    if i>=16 and i<24:
                            TemperatureBit = TemperatureBit + "1"
            else:
                    if i>=0 and i<8:
                            HumidityBit = HumidityBit + "0"
                    if i>=16 and i<24:
                            TemperatureBit = TemperatureBit + "0"

except:
    print "ERR_RANGE"
    finish()

try:
    for i in range(0, 8):
            bit_count = 0

            while data[count] == 0:
                    tmp = 1
                    count = count + 1

            while data[count] == 1:
                    bit_count = bit_count + 1
                    count = count + 1

            if bit_count > 3:
                    crc = crc + "1"
            else:
                    crc = crc + "0"
except:
    print "ERR_RANGE"
    finish()

Humidity = bin2dec(HumidityBit)
Temperature = bin2dec(TemperatureBit)	
 
if int(Humidity) + int(Temperature) - int(bin2dec(crc)) == 0:
	print "Humidade: "+ Humidity +"%"
	print "Temperatura: "+ Temperature +"C"
        temp.save_value({"value": Temperature})
        humidity.save_value({"value": Humidity})
else:
	print "ERR_CRC"

finish()

以上是关于python dht11_raspberry_ubidots.py的主要内容,如果未能解决你的问题,请参考以下文章

树莓派连接DHT11温湿度传感器(python)

12-开发板接入小五物联实现远程控制(stm32采集温湿度传感器DHT11)

树梅派硬件编程_DTH11温湿度检测

《嵌入式 - 模块》RW007 WIFI模块联网与使用

《嵌入式 - 模块》RW007 WIFI模块联网与使用

STM32G4备战蓝桥杯嵌入式---模块配置---DHL11(拓展板)