当前位置:首页 > 公司新闻


yabo体育-首页品牌新闻

物联网:用Python传送温湿度信息到AWSIoT

yabo体育-首页

与IoT涉及的云端服务为数众多,例如:ThingSpeak、WoT.City、AWS、IBMBluemix、MediaTekMCS、GoogleFirebase等,并在利用这些云端服务可在对数据展开数据可视化、数据分析与其它的应用于,而本文用Python将利用RaspberryPi收集到的温湿度信息传输到AWSIoT。情境材料与打算1.打算RaspberryPi3ModelB开发板2.打算MicroSD(已加装Raspbian)3.将MicroSD取出到RaspberryPi中4.原作好网络5.加装GrovePi+6.加装Grove–TemperatureandHumiditySensor(D4)7.加装GROVE-LCDRGBBACKLIGHT(I2C-2)8.加装IDE(SublimeTextorVisualStudioCode)Sensor,LCD,GrovePi+与RaspberryPi相连如下图AWSIoT末端Step1.到AWS网站申请人账号Step2.登入AWS网站Step3.页面ServicesAWSIoTStep4.页面CreatearesourceStep5.页面Createathing输出thingNameCreateStep6.页面刚刚创建的thingStep7.页面ConnectadeviceStep8.页面Node.jsGeneratecertificateandpolicyStep9.iTunesprivate与publickeysStep10.页面ConfirmconnectingStep11.页面ReturntoThingDetailRaspberryPi末端Step1.利用Python加载温湿度信息并传输到AWSIoT#****************************************************#ImportPackage#****************************************************importtimeimportdatetimeimportpaho.mqtt.clientaspahoimportjsonimportsslimportsyssys.path.append(/home/pi/rpi/code/Package)importgrovepifromgrove_rgb_lcdimport*#****************************************************#SetPinNo,AWSConfig#****************************************************sensor=4blue=0#TheBluecoloredsensor.white=1#TheWhitecoloredsensor.connflag=False#****************************************************#SetAWSConnection#****************************************************defon_connect(client,userdata,flags,rc):globalconnflagconnflag=Trueprint(Connectionreturnedresult:+str(rc))defon_message(client,userdata,msg):print(msg.topic++str(msg.payload))mqttc=paho.Client()mqttc.on_connect=on_connectmqttc.on_message=on_messageawshost=a3iprlpgye4dmu.iot.us-west-2.amazonaws.comawsport=8883clientId=sensorDatathingName=sensorDatacaPath=./root-CA.crtcertPath=./000cd28455-certificate.pem.crtkeyPath=./000cd28455-private.pem.keymqttc.tls_set(caPath,certfile=certPath,keyfile=keyPath,cert_reqs=ssl.CERT_REQUIRED,tls_version=ssl.PROTOCOL_TLSv1_2,ciphers=None)mqttc.connect(awshost,awsport,keepalive=60)mqttc.loop_start()#****************************************************#PublishAWS#****************************************************whileTrue:[temp,humidity]=grovepi.dht(sensor,blue)print(temp=%.02fChumidity=%.02f%%%(temp,humidity))t=time.time();date=datetime.datetime.fromtimestamp(t).strftime(%Y%m%d%H%M%S)ifconnflag==True:mqttc.publish(topic/sensorData,json.dumps({time:date,temperature:temp,humidity:humidity}),qos=1)else:print(waitingforconnection...)time.sleep(1)Step2.将PythonCode与Keys传输到RaspberryPiStep3.继续执行刚刚传遍RaspberryPi中的Python代码,python檔名.pyStep4._yabo首页。

yabo体育

yabo首页

yabo体育-首页

本文来源:yabo体育-首页-ursulahess.com

yabo首页