Raspberry Pi+阿里云函数计算 树莓派实现温湿度传感器机器人实时钉钉群推送

# -*- coding: UTF-8 -*-
import RPi.GPIO as GPIO
import time
import json
import requests
import MySQLdb
channel = 11
data = []           
j = 0             

GPIO.setmode(GPIO.BOARD)      

time.sleep(1)          

GPIO.setup(channel, GPIO.OUT)

GPIO.output(channel, GPIO.LOW)
time.sleep(0.02)        
GPIO.output(channel, GPIO.HIGH)

GPIO.setup(channel, GPIO.IN)

while GPIO.input(channel) == GPIO.LOW:
    continue

while GPIO.input(channel) == GPIO.HIGH:
    continue

while j < 40:
    k = 0
    while GPIO.input(channel) == GPIO.LOW:
        continue

    while GPIO.input(channel) == GPIO.HIGH:
        k += 1
        if k > 100:
            break

    if k < 8:          
        data.append(0)
    else:
        data.append(1)

    j += 1

print ("sensor is working.")
print (data)              

humidity_bit = data[0:8]      
humidity_point_bit = data[8:16]
temperature_bit = data[16:24]
temperature_point_bit = data[24:32]
check_bit = data[32:40]

humidity = 0
humidity_point = 0
temperature = 0
temperature_point = 0
check = 0

for i in range(8):
    humidity += humidity_bit[i] * 2 ** (7 - i)              
    humidity_point += humidity_point_bit[i] * 2 ** (7 - i)
    temperature += temperature_bit[i] * 2 ** (7 - i)
    temperature_point += temperature_point_bit[i] * 2 ** (7 - i)
    check += check_bit[i] * 2 ** (7 - i)

tmp = humidity + humidity_point + temperature + temperature_point       

if check == tmp:                               
    print ("temperature : ", temperature, ", humidity : " , humidity)
else:                                       
    print ("wrong")
    print ("temperature : ", temperature, ", humidity : " , humidity, " check : ", check, " tmp : ", tmp)

GPIO.cleanup()
import MySQLdb
conn = MySQLdb.connect(host='localhost',
            port=3306,
            user='root',
            passwd='*******',
            db='test')
cur = conn.cursor()
value = [temperature,humidity,str(time.strftime('%A %X %Z',time.localtime(time.time())))]
cur.execute("insert into dht (dht_tem,dht_hum,dht_time) values(%s,%s,%s)",value)
conn.commit()
conn.rollback()
import requests
import json
db = MySQLdb.connect(host='rm-bp1o234262j82g26fro.mysql.rds.aliyuncs.com',
port=3306,
user='dht_zzx',
passwd='Xks19981025',
db='dht')
cur_db = db.cursor()
cur_db.execute("insert into dht11 (dht_tem,dht_hum,dht_time) values (%s,%s,%s)",value)
db.commit()



# 打开数据库连接
db = MySQLdb.connect(host="rm-bp1o234262j82g26fro.mysql.rds.aliyuncs.com",
	user="dht_zzx", 
	passwd="********", 
	db="dht", 
	charset='utf8' )
# 使用cursor()方法获取操作游标 
cursor = db.cursor()

# SQL 查询语句
sql = "SELECT * FROM dht11"
try:
   cursor.execute(sql)
   results = cursor.fetchall()
   set=1
   for it in results:
	if set==len(results):			
		for i in range(len(it)):
			print it[i]
	else: 
		set+=1
except:
   print "Error: unable to fecth data"

cursor.close()
db.close()
url = 'https://oapi.dingtalk.com/robot/send?access_token=abfdb21af2b580e974fa6c7b4a73231cfe0b455e182450b6e557ec470fca6e71' 
HEADERS = {
"Content-Type": "application/json ;charset=utf-8 "
}


String_textMsg = {
"msgtype": "markdown",
"markdown": {"title": "温湿度传感器",
"text":"温湿度传感器上报\n"
"> 设备位置:" + "**********\n\n"+
"> 设备编号:" +"bixby001\n\n"+
"> 实时温度:"+str(it[0])+"\n\n"+
"> 相对湿度:"+str(it[1])+"\n\n"+
"> "+str(it[2])+"发布by[物联网平台]\n\n"},
}
String_textMsg = json.dumps(String_textMsg,ensure_ascii=False)
res = requests.post(url, data=String_textMsg, headers=HEADERS)
print(res.text)

python 爬虫实现 非nodejs  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值