Raspberry Pi:“ ValueError:无法解码JSON对象”

时间:2018-09-06 09:50:44

标签: python json raspberry-pi

我正在尝试设置一个树莓派,该树莓派通过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)

如果您需要我的帮助,我希望您能最终帮助我,告诉我!

0 个答案:

没有答案