ESP32連接云服務(wù)器【ESP32+寶塔面板】
??????????相關(guān)文章??????????
ESP32連接MQ Sensor實現(xiàn)氣味反應(yīng)
?? https://blog.csdn.net/ws15168689087/article/details/131365573ESP32+MQTT+MySQL實現(xiàn)發(fā)布訂閱【氣味數(shù)據(jù)收集】
?? https://blog.csdn.net/ws15168689087/article/details/131627595個人云服務(wù)器搭建MQTT服務(wù)器
?? https://blog.csdn.net/ws15168689087/article/details/131571433ESP32開發(fā)板引腳介紹【附有引腳使用實例】
?? https://blog.csdn.net/ws15168689087/article/details/131654327
?????內(nèi)容1:背景
ESP32-WROOM-32
??ESP32 是一款由 Espressif Systems 開發(fā)的低功耗、高性能、可擴(kuò)展的嵌入式微控制器,可用于構(gòu)建各種物聯(lián)網(wǎng) (IoT) 應(yīng)用。ESP32 芯片基于 ARM Cortex-M 內(nèi)核,具有 32 位地址空間,支持 Wi-Fi、藍(lán)牙和其他無線連接,以及多種外設(shè)接口,如 GPIO、定時器、PWM、串口等。相關(guān)設(shè)備
???♀?ESP32+云服務(wù)器+Arduino IDE
?????內(nèi)容2:服務(wù)器配置
1??配置第一步:
??因為本次我們需要通過寶塔面板,將腳本掛上去
?????因此,有關(guān)云服務(wù)器和寶塔面板的初始配置,這里將直接跳過??
2??配置第二步:
??首先是本次需要掛在服務(wù)器上的腳本代碼??Server.py??import socket import datetime server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) server.bind(('xxx.xxx.xxx.xxx',xxxx)) #這里是綁定要監(jiān)聽的地址(內(nèi)網(wǎng)ip)和端口 server.listen(5) #開始監(jiān)聽 表示可以使用五個鏈接排隊 print('listen') while True: #conn就是客戶端鏈接過來而在服務(wù)端為期生成的一個鏈接實例 conn,addr = server.accept() #等待鏈接,多個鏈接的時候就會出現(xiàn)問題,其實返回了兩個值 print(conn, addr) try: data = conn.recv(1024) #接收數(shù)據(jù) if data: print('recive:',data.decode()) #打印接收到的數(shù)據(jù) except ConnectionResetError as e: print('關(guān)閉了正在占線的鏈接!') break # conn.close()
3??配置第三步:
??接著,在寶塔面板的軟件商店中導(dǎo)入????Python項目管理器????
??然后新建一個文件夾【例如下圖中的esp】,并將腳本文件放入文件夾中
???之后,我們在Python項目管理器中添加一個Python項目
??其中項目路徑等內(nèi)容,可以參照下圖填寫??
4??配置第四步:
??首先查看并復(fù)制剛才文件的MD5_venv??
??接著打開終端連續(xù)輸入下方代碼??cd /xxx/xxx/ #先轉(zhuǎn)到所在目錄 btpython -m venv MD5_venv #這里有些人用python3 -m 但是我報錯了,就用了btpython -m
??到這里,你可以到文件夾中的bin目錄中,可以找到已經(jīng)存在了activate文件
??此時你已經(jīng)可以將腳本掛到定時任務(wù)中執(zhí)行
5??配置第五步:
??最后,我們將添加腳本任務(wù)
??腳本的內(nèi)容自行修改??
?至此,服務(wù)器端的配置結(jié)束
?????內(nèi)容3:ESP32配置
??相關(guān)代碼:
#include <Arduino.h> #include <WiFi.h> #include <WiFiClient.h> const char* ssid = "xxxx"; //WIFI名稱 const char* password = "xxxx"; //WIFI密碼 const char* host = "xxx.xxx.xxx.xxx"; //服務(wù)器的IP地址 const int port = 8081; //端口號 int num=0; void setup() { Serial.begin(115200); Serial.print("Connecting to "); Serial.println(ssid); /* connect to your WiFi */ WiFi.begin(ssid, password); /* wait until ESP32 connect to WiFi*/ while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected with IP address: "); Serial.println(WiFi.localIP()); } void loop() { delay(1000); Serial.print("connecting to "); Serial.println(host); /* Use WiFiClient class to create TCP connections */ WiFiClient client; if (!client.connect(host, port)) { Serial.println("connection failed"); return; } String msg ="test:"+String(num++); Serial.println(msg); client.println(msg); //client.stop(); }
上傳到ESP32開發(fā)板上,可以看見連接成功!
文章來源:http://www.zghlxwxcb.cn/news/detail-561724.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-561724.html
到了這里,關(guān)于ESP32連接云服務(wù)器【W(wǎng)ebSocket】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!