首页 > 评测 > 快捷开发 任性连接 :ESP32 Thing开发板评测

快捷开发 任性连接 :ESP32 Thing开发板评测

  • 作者:zhanzr21
  • 来源:21ic
  • [导读]
  • ESP32 Thing是SparkFun推出的一款针对物联网无线应用的开发板,它的体积较小,具备WiFi与蓝牙的双重连接方式,并且可以通过Arduino IDE来开发。

3.5 第二个程序:连接WiFi网络获取HTTP页面

这个程序连接至WiFi网络并且下载一个网站(比如www.21ic.com)的默认首页.代码如下:

#include

// WiFi network name and password:

const char * networkName = "WIFI路由器SSID名称";

const char * networkPswd = "WIFI密码";

// Internet domain to request from:

const char * hostDomain = "www.21ic.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与密码, 编译下载. 先连接网络, 成功连接WiFI网络之后需要用户按一下用户按钮,一切正常的话就开始下载默认页面.大致结果如此:

20.png

 

图 HTTP程序输出

3.6 第三个程序:UDP发送

这个程序连接WiFi网络,并且向服务端的UDP端口定时发送随机数.代码如下:

/*

* 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 = "开网络助手的主机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原创,未经许可禁止转载!

网友评论