首頁(yè) > 評(píng)測(cè) > 快捷開(kāi)發(fā) 任性連接 :ESP32 Thing開(kāi)發(fā)板評(píng)測(cè)

快捷開(kāi)發(fā) 任性連接 :ESP32 Thing開(kāi)發(fā)板評(píng)測(cè)

  • 作者:zhanzr21
  • 來(lái)源:21ic
  • [導(dǎo)讀]
  • ESP32 Thing是SparkFun推出的一款針對(duì)物聯(lián)網(wǎng)無(wú)線應(yīng)用的開(kāi)發(fā)板,它的體積較小,具備WiFi與藍(lán)牙的雙重連接方式,并且可以通過(guò)Arduino IDE來(lái)開(kāi)發(fā)。

3.5 第二個(gè)程序:連接WiFi網(wǎng)絡(luò)獲取HTTP頁(yè)面

這個(gè)程序連接至WiFi網(wǎng)絡(luò)并且下載一個(gè)網(wǎng)站(比如lujuzi.cn)的默認(rèn)首頁(yè).代碼如下:

#include

// WiFi network name and password:

const char * networkName = "WIFI路由器SSID名稱(chēng)";

const char * networkPswd = "WIFI密碼";

// Internet domain to request from:

const char * hostDomain = "lujuzi.cn";

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();

}

注意填寫(xiě)WIFI的SSID與密碼, 編譯下載. 先連接網(wǎng)絡(luò), 成功連接WiFI網(wǎng)絡(luò)之后需要用戶(hù)按一下用戶(hù)按鈕,一切正常的話就開(kāi)始下載默認(rèn)頁(yè)面.大致結(jié)果如此:

20.png

 

圖 HTTP程序輸出

3.6 第三個(gè)程序:UDP發(fā)送

這個(gè)程序連接WiFi網(wǎng)絡(luò),并且向服務(wù)端的UDP端口定時(shí)發(fā)送隨機(jī)數(shù).代碼如下:

/*

* This sketch sends random data over UDP on a ESP32 device

*

*/

#include

#include

// WiFi network name and password:

const char * networkName = "WIFI路由器SSID名稱(chēng)";

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 = "開(kāi)網(wǎng)絡(luò)助手的主機(jī)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)友評(píng)論

  • 聯(lián)系人:巧克力娃娃
  • 郵箱:board@21ic.com
  • 我要投稿
  • 歡迎入駐,開(kāi)放投稿

熱門(mén)標(biāo)簽
項(xiàng)目外包 more+