发送多个传感器值到python服务器

时间:2020-06-16 09:01:56

标签: python sockets arduino esp8266

我是python和IoT方面的新手。我想将ESP8266附带的DHT11的湿度和温度值发送到python服务器,但是由于某种原因,我想将湿度和温度打印在不同的变量上。例如2个变量。我收到了有关拆分字符串的内容,但仍然无法弄清楚。

#include <ESP8266WiFi.h>
#include <dht.h>

WiFiClient client;
dht DHT;

client.print("Temp: " + String(DHT.temperature, 0) + " °C");
client.print("Humid: " + String(DHT.humidity, 0) + "%");

Python(这是我的期望):

while True:
    try:
        humid, temp = conn.recv(2048).decode("utf-8")
        print(humid)
        print(temp)

    except Exception as e:
        print(e)
        break

这是输出。奇数行是温度,偶数行是潮湿

如果有更好的解决方案,请告诉我。对不起我的语言,英语不是我的主要语言

1 个答案:

答案 0 :(得分:0)

改为尝试以下代码:

while True:
try:
    humid, temp = conn.recv(2048).decode("utf-8").split("\n")
    print(humid)
    print(temp)

except Exception as e:
    print(e)
    break
相关问题