首頁 > 評測 > 快捷開發(fā) 任性連接 :ESP32 Thing開發(fā)板評測
快捷開發(fā) 任性連接 :ESP32 Thing開發(fā)板評測
- [導讀]
- ESP32 Thing是SparkFun推出的一款針對物聯(lián)網(wǎng)無線應用的開發(fā)板,它的體積較小,具備WiFi與藍牙的雙重連接方式,并且可以通過Arduino IDE來開發(fā)。
3.5 第二個程序:連接WiFi網(wǎng)絡獲取HTTP頁面
這個程序連接至WiFi網(wǎng)絡并且下載一個網(wǎng)站(比如m.dunminwenhua.com)的默認首頁.代碼如下:
#include
// WiFi network name and password:
const char * networkName = "WIFI路由器SSID名稱";
const char * networkPswd = "WIFI密碼";
// Internet domain to request from:
const char * hostDomain = "m.dunminwenhua.com";
const int hostPort = 80;
const int BUTTON_PIN = 0;
const int LED_PIN = 5;
void setup()
{
// Initilize hardware:
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
// Connect to the WiFi network (see function below loop)
connectToWiFi(networkName, networkPswd);
digitalWrite(LED_PIN, LOW); // LED off
Serial.print("Press user button 0 to connect to ");
Serial.println(hostDomain);
}
void loop()
{
if (digitalRead(BUTTON_PIN) == LOW)
{ // Check if button has been pressed
while (digitalRead(BUTTON_PIN) == LOW)
; // Wait for button to be released
digitalWrite(LED_PIN, HIGH); // Turn on LED
requestURL(hostDomain, hostPort); // Connect to server
digitalWrite(LED_PIN, LOW); // Turn off LED
}
}
void connectToWiFi(const char * ssid, const char * pwd)
{
int ledState = 0;
printLine();
Serial.println("Connecting to WiFi network: " + String(ssid));
WiFi.begin(ssid, pwd);
while (WiFi.status() != WL_CONNECTED)
{
// Blink LED while we're connecting:
digitalWrite(LED_PIN, ledState);
ledState = (ledState + 1) % 2; // Flip ledState
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void requestURL(const char * host, uint8_t port)
{
printLine();
Serial.println("Connecting to domain: " + String(host));
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port))
{
Serial.println("connection failed");
return;
}
Serial.println("Connected!");
printLine();
// This will send the request to the server
client.print((String)"GET / HTTP/1.1\r\n" +
"Host: " + String(host) + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0)
{
if (millis() - timeout > 5000)
{
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while (client.available())
{
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
client.stop();
}
void printLine()
{
Serial.println();
for (int i=0; i<30; i++)
Serial.print("-");
Serial.println();
}
注意填寫WIFI的SSID與密碼, 編譯下載. 先連接網(wǎng)絡, 成功連接WiFI網(wǎng)絡之后需要用戶按一下用戶按鈕,一切正常的話就開始下載默認頁面.大致結果如此:
圖 HTTP程序輸出
3.6 第三個程序:UDP發(fā)送
這個程序連接WiFi網(wǎng)絡,并且向服務端的UDP端口定時發(fā)送隨機數(shù).代碼如下:
/*
* This sketch sends random data over UDP on a ESP32 device
*
*/
#include
#include
// WiFi network name and password:
const char * networkName = "WIFI路由器SSID名稱";
const char * networkPswd = "WIFI密碼";
//IP address to send UDP data to:
// either use the ip address of the server or
// a network broadcast address
const char * udpAddress = "開網(wǎng)絡助手的主機IP";
const int udpPort = 20000;
//Are we currently connected?
boolean connected = false;
//The udp library class
WiFiUDP udp;
void setup(){
// Initilize hardware serial:
Serial.begin(115200);
//Connect to the WiFi network
connectToWiFi(networkName, networkPswd);
}
void loop(){
int tmpRand;
//only send data when connected
if(connected){
//Send a packet
udp.beginPacket(udpAddress,udpPort);
tmpRand = rand();
udp.printf("UDP Demo,It is a random number: %d\n", tmpRand);
-
- 本文系21ic原創(chuàng),未經(jīng)許可禁止轉(zhuǎn)載!
網(wǎng)友評論
- 聯(lián)系人:巧克力娃娃
- 郵箱:board@21ic.com
- 我要投稿
-
歡迎入駐,開放投稿
-
人均百萬?英偉達中國員工收入曝光! 2024-08-29
-
《黑神話:悟空》玩家硬盤升級攻略:提升游戲體驗,暢享3A大作 2024-08-29
-
數(shù)睿數(shù)據(jù)參加《系統(tǒng)與軟件工程 低代碼開發(fā)平臺通用技術要求》國家標準編制 2024-08-29
- NRF52810藍牙數(shù)字耳機找人定制
預算:¥30005天前
- 125KW模塊式PCS軟硬件外包開發(fā)
預算:¥1100000015小時前
- 12V汽車啟動電源項目BMS設計
預算:¥50000023小時前
- 數(shù)據(jù)可視化軟件 開發(fā)
預算:¥5000023小時前
- PLC項目調(diào)試修改
預算:¥100001天前
- 起動電機控制器開發(fā)
預算:¥1100001天前