DHT22温湿度传感器的使用

使用python读取dht22温湿度传感器的值。

 import sys
 import Adafruit_DHT
 import requests
 import json
 url = 'https://www.3gcomet.com/iot/iot_receive.php'
 sensor = Adafruit_DHT.DHT22
 pin = 4
 humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
 if humidity is not None and temperature is not None:
     temp = '{:.1f}'.format(temperature)
     humi = '{:.1f}'.format(humidity)
     payload = {"ac":'iotofict',"loc":'CloudDC',"tem":temp,"hum":humi}
     response = requests.post(url=url,data=payload)
     print(response.text)
     #print('Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temperature, humidity))
 else:
     print('Failed to get reading. Try again!')
     sys.exit(1)

python读取串口信息

python读取配置文件中串口的名称,然后打开串口接受读卡器信息,再提交到某个网页上。

reader.ini:
[Default]
port=/dev/ttyUSB0

serial-test.py:

#!/usr/bin/python
import serial
import urllib2
import ConfigParser

def hexShow(argv):
    result = ''
    hLen = len(argv)
    for i in xrange(hLen):
        hvol = ord(argv[i])
        hhex = '%02x'%hvol
        result += hhex
#    print 'hexShow:',result
    return result

config = ConfigParser.ConfigParser()
config.readfp(open('reader.ini'))

sPort = config.get("Default","port")

ser = serial.Serial(sPort,9600)
while ser.isOpen():
    sdata = ser.read(5)
    cardid = hexShow(sdata)
#    print cardid
    cidurl = "http://127.0.0.1/cid.php?cid="+cardid

    res_data = urllib2.urlopen(cidurl)
    res = res_data.read()
    print res