flask/MongoDB 使用树莓派 pi3 的本地服务器出错 - raspbian os

flask/MongoDB error on local server using raspberry pi3 - raspbian os

我已经使用 flask 和 mongoDB 创建了一个本地服务器,它在 windows 上运行良好,但是当我将我的代码移动到 raspberry pi 时,我遇到了一个错误我不知道为什么会这样。

我使用的代码:

1) 对于烧瓶服务器

from flask import Flask
from flask import jsonify
from flask import request
import pymongo
import time
import datetime
import json

app = Flask(__name__)


client = pymongo.MongoClient("localhost", 27017)
db = client['mqtt-db']
obs_collection = db['mqtt-collection']


@app.route("/obs")
def obs():
    data_str = request.args.get("data")
    print data_str
    data = json.loads(data_str)
    print data
    data["date"] = datetime.datetime.now()
    obs_collection.save(data)
    return "success"


@app.route("/get_obs")
def get_obs():
    res = []
    for row in obs_collection.find():
        del row['_id']
        res.append(row)
    return jsonify(res)


@app.route("/delete_all")
def delete_all():
    res = obs_collection.delete_many({})
    return jsonify({"deleted": res.deleted_count})

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)

2) 使用 mqtt 协议将消息插入 db 的脚本:

import paho.mqtt.client as mqtt
import pymongo
import json
import datetime

topic = "sensor"
host = "10.0.0.6"


client = pymongo.MongoClient("localhost", 27017)
db = client['mqtt-db']
mqtt_collection = db['mqtt-collection']


# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe(topic)

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    data_str = str(msg.payload)
    data = json.loads(data_str)

    print data_str
    print data
    data["date"] = datetime.datetime.now()
    mqtt_collection.save(data)
    print(msg.topic+" "+str(msg.payload))



client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect(host, 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()

当我尝试使用 "get_obs" 函数从服务器检索数据时发生错误。 错误是:"Value Error: dictionary update sequence element #0 has length 4; 2 is required"

感谢您的帮助。

正如@davidism 所建议的,解决方案是更新到最新版本的 Flask