我正在AWS IoT上为两个MCU传感器设置SNS推送通知。
现在,当湿度较低时,我会收到警报,因此我想包含温度/湿度数据,但是它给我的值是none或抛出错误无效的JSON。我该如何解决?
工作代码:
def myShadowUpdateCallback(payload, responseStatus, token):
print()
print('UPDATE: $aws/things/' + SHADOW_HANDLER +
'/shadow/update/#')
print("payload = " + payload)
print("responseStatus = " + responseStatus)
print("token = " + token)
channel = 21
GPIO.setup(channel, GPIO.IN)
while True:
if GPIO.input(channel):
myDeviceShadow.shadowUpdate(
'{"state":{"reported":{"moisture":"low"}}}',
myShadowUpdateCallback, 5)
else:
myDeviceShadow.shadowUpdate(
'{"state":{"reported":{"moisture":"okay"}}}',
myShadowUpdateCallback, 5)
电子邮件结果:
{"state":{"reported":{"moisture":"low"}},"metadata":{"reported":{"moisture":{"timestamp":1548870624}}},"version":5167,"timestamp":1548870624,"clientToken":"b8a2"}
为使DH22温度/湿度传感器正常工作,我添加了:
import Adafruit_DHT
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 20)
已更改:
def myShadowUpdateCallback(payload, responseStatus, token):
print()
print('UPDATE: $aws/things/' + SHADOW_HANDLER +
'/shadow/update/#')
print("payload = " + payload)
print("responseStatus = " + responseStatus)
print("token = " + token)
print('Temperature={}*F Humidity={}%'.format(temperature, humidity))
while True:
if GPIO.input(channel):
myDeviceShadow.shadowUpdate(
'{"state":{"reported":{"moisture":"low","humidity":{"0":"0.1f"},"temperature":{"1":"0.1f"}}}}',
myShadowUpdateCallback, 5)
else:
myDeviceShadow.shadowUpdate(
'{"state":{"reported":{"moisture":"okay","humidity":{"0":"0.1f"},"temperature":{"1":"0.1f"}}}}',
myShadowUpdateCallback, 5)
错误:
当湿度正常时,温度和湿度显示:
Temperature=None*F Humidity=None%
当湿度低时,我会得到一个错误:
myShadowUpdateCallback, ValueError: Invalid JSON file.
答案 0 :(得分:1)
看起来{0:0.1f}部分不是有效的json。 也许将其更改为{“ 0”:“ 0.1f”}或其他有效的json。