www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

首頁 > 評測 > 快捷開發(fā) 任性連接 :ESP32 Thing開發(fā)板評測

快捷開發(fā) 任性連接 :ESP32 Thing開發(fā)板評測

  • 作者:zhanzr21
  • 來源:21ic
  • [導讀]
  • 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)絡之后需要用戶按一下用戶按鈕,一切正常的話就開始下載默認頁面.大致結果如此:

20.png

 

圖 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)友評論