如何使用Python在HTTP上编写JSON数据?
我正在使用Raspberry Pi 4通过计算机的移动热点连接到服务器。我想将Raspberry Pi读取的JSON数据发布到Raspberry Pi使用Python代码连接的IP的HTTP页面上如何使用Python在HTTP上编写JSON数据?,python,json,Python,Json,我正在使用Raspberry Pi 4通过计算机的移动热点连接到服务器。我想将Raspberry Pi读取的JSON数据发布到Raspberry Pi使用Python代码连接的IP的HTTP页面上 import requests, Adafruit_DHT, time, json from apscheduler.schedulers.background import BackgroundScheduler sensor = Adafruit_DHT.AM2302 #DHT11/DHT22/
import requests, Adafruit_DHT, time, json
from apscheduler.schedulers.background import BackgroundScheduler
sensor = Adafruit_DHT.AM2302 #DHT11/DHT22/AM2302
pin = 4
url = 'http://192.168.137.86:3350/temperature_humidity'
latest_humidity = 0.0
latest_temperature = 0.0
headers = {
'content-type': 'application/json',
}
params = (
('priority', 'normal'),
)
def write_hist_value_callback():
hum, temp = Adafruit_DHT.read_retry(sensor, pin)
if hum is not None and temp is not None:
if temp < -40.0 or temp > 80.0:
print "Ignoring out of range temperature: {0:0.1f}*".format(temp)
else:
latest_temperature = temp
if hum < 0.0 or hum > 100.0:
print "Ignoring out of range humidity: {0:0.1f}%".format(hum)
else:
latest_humidity = hum
payload = {'temperature': latest_temperature ,'humidity': latest_humidity}
print json.dumps(payload)
print "Ignoring first 2 sensor values to improve quality..."
for x in range(2):
Adafruit_DHT.read_retry(sensor, pin)
print "Creating interval timer. This step takes almost 2 minutes on the Raspberry Pi..."
#create timer that is called every n seconds, without accumulating delays as when using sleep
scheduler = BackgroundScheduler()
#scheduler.add_job(write_hist_value_callback, 'interval', seconds=60)
scheduler.start()
print "Started interval timer which will be called the first time in {0} seconds.".format(60);
try:
while True:
hum, temp = Adafruit_DHT.read_retry(sensor, pin)
if hum is not None and temp is not None:
if temp < -40.0 or temp > 80.0:
print "Ignoring out of range temperature: {0:0.1f}*".format(temp)
else:
latest_temperature = temp #"{:.2f}".format(temp)
if hum < 0.0 or hum > 100.0:
print "Ignoring out of range humidity: {0:0.1f}%".format(hum)
else:
latest_humidity = hum #"{:.2f}".format(hum)
payload = {'temperature': latest_temperature ,'humidity': latest_humidity}
print json.dumps(payload)
time.sleep(60)
except (KeyboardInterrupt, SystemExit):
scheduler.shutdown()
导入请求、Adafruit\u DHT、时间、json
从apscheduler.schedulers.background导入BackgroundScheduler
传感器=Adafruit_DHT.AM2302#DHT11/DHT22/AM2302
引脚=4
url='1〕http://192.168.137.86:3350/temperature_humidity'
最新湿度=0.0
最新温度=0.0
标题={
“内容类型”:“应用程序/json”,
}
参数=(
(‘优先级’、‘正常’),
)
def write_hist_value_callback():
hum,temp=Adafruit\u DHT.读取\u重试(传感器,针脚)
如果嗡嗡声不是无且温度不是无:
如果温度<-40.0或温度>80.0:
打印“忽略超出范围的温度:{0:0.1f}*”。格式(温度)
其他:
最新温度=温度
如果嗡嗡声<0.0或嗡嗡声>100.0:
打印“忽略超出范围的湿度:{0:0.1f}%”。格式(hum)
其他:
最新湿度=嗡嗡声
有效负载={'temperature':最新温度,'湿度':最新湿度}
打印json.dumps(有效负载)
打印“忽略前2个传感器值以提高质量…”
对于范围(2)内的x:
Adafruit\u DHT.读取\u重试(传感器,引脚)
打印“创建间隔计时器。这一步在覆盆子圆周率上大约需要2分钟…”
#创建每n秒调用一次的计时器,而不会像使用睡眠时那样累积延迟
调度程序=背景调度程序()
#计划程序.添加作业(写入历史值\u回调'interval',秒=60)
scheduler.start()
打印“启动的间隔计时器,它将在{0}秒内第一次调用。”。格式(60);
尝试:
尽管如此:
hum,temp=Adafruit\u DHT.读取\u重试(传感器,针脚)
如果嗡嗡声不是无且温度不是无:
如果温度<-40.0或温度>80.0:
打印“忽略超出范围的温度:{0:0.1f}*”。格式(温度)
其他:
最新温度=temp#“{.2f}”。格式(temp)
如果嗡嗡声<0.0或嗡嗡声>100.0:
打印“忽略超出范围的湿度:{0:0.1f}%”。格式(hum)
其他:
最新湿度=hum#“{.2f}”。格式(hum)
有效负载={'temperature':最新温度,'湿度':最新湿度}
打印json.dumps(有效负载)
时间。睡眠(60)
除了(键盘中断、系统退出):
scheduler.shutdown()
我需要做什么才能让它在URL上写JSON?我需要在其上编写JSON的URL是
http://192.168.137.86:3350/temperature_humidity
(这是连接到internet的板的IP)也许你应该看看HTTP POST请求和请求。POST
。似乎您导入了请求
,但没有对其进行任何处理。我以前尝试过使用request.post,但问题是我找不到正确的URL将其发布到。我想知道在哪里可以找到允许我发布它的URL。我不知道如何设置。也许你应该看看HTTP POST请求和请求。POST
。似乎您导入了请求
,但没有对其进行任何处理。我以前尝试过使用request.post,但问题是我找不到正确的URL将其发布到。我想知道在哪里可以找到允许我发布它的URL。我不知道如何设置它。