首页 > 解决方案 > Raspberry Pi:“ValueError:无法解码任何 JSON 对象”

问题描述

我正在尝试设置一个树莓派,它通过 DHT22 传感器测量我房间的温度和湿度。我在 youtube 上找到了一个教程,其中上传了脚本和其他所有内容,但是 python 脚本不起作用......我是 python 新手,我真的不知道现在该怎么做。我已经在寻找解决方案,尤其是在 stackoverflow 上,但我没有找到任何适合我的方法。

错误消息如下所示:

python Temperature_Server_DHT.py

Traceback (most recent call last):
  File "Temperature_Server_DHT.py", line 49, in <module>
    SensorInforations = sensorCheckIfRegistered(Device_ID)
  File "Temperature_Server_DHT.py", line 37, in sensorCheckIfRegistered
    SensorData = json.loads(data)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

我的python脚本包含以下内容:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# import sys module
import sys
import json
import urllib
import time
import socket
import Adafruit_DHT

#Conf
BaseURL= "http://127.0.0.1/php/Backend.php"
NameSensor= "DHT22"
#

sensor = Adafruit_DHT.DHT22
ip = [l for l in ([ip for ip in     socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")]    [:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in     [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)$
hostname = socket.gethostname()

def insertTempdata(TSensor_ID, Temperature, Humidity):
    urllib.urlopen(BaseURL + "?Call=AddTempData&TSensor_ID=" + "\"" +     TSensor_ID + "\"" + "&Temperature="+ Temperature + "&Humidity=" + Humidity)

def deviceCheckIfRegistered(IP, Hostname):
        DeviceID = urllib.urlopen(BaseURL + "?Call=getDeviceID&IP=" + IP +     "&Hostname=" + Hostname ).read()
        print (DeviceID)
        if(DeviceID != "null"):
            return DeviceID
        else:
            return ""

def sensorCheckIfRegistered(DeviceID):
        url= BaseURL + "?Call=getRegisteredTSensors&Device_ID=" + DeviceID
        data =  urllib.urlopen(url).read()
        print (data)
        if(data != "null"):
            SensorData = json.loads(data)
            return SensorData
        else:
            return ""

def readDHTSensors(TSensor_ID, GPIO):
    humidity, temperature = Adafruit_DHT.read_retry(sensor, GPIO)
    print 'Temperature: {0:0.1f}*C Humidity:     {1:0.1f}%'.format(temperature,humidity)
    insertTempdata(TSensor_ID, str(temperature), str(humidity))

if __name__ == '__main__':
    Device_ID = deviceCheckIfRegistered(ip, hostname)
    SensorInforations = sensorCheckIfRegistered(Device_ID)
    if(Device_ID != "" and SensorInforations != ""):
        try:
            while True:
                for element in SensorInforations:
                    if(str(element['Sensor']) == NameSensor):
                        readDHTSensors(element['TSensor_ID'], element['GPIO'])
                time.sleep(1800)
        except (KeyboardInterrupt, SystemExit):
            # quit python script
            print("Killed")
            sys.exit(0)
    else:
        print("Device not registered")
        sys.exit(0)

我希望您最终能帮助我,如果您需要我的任何其他东西,请告诉我!

标签: pythonjsonraspberry-pi

解决方案


推荐阅读