当前位置:首页 > 嵌入式 > 开发板
[导读]本文展示了如何使用Arduino的IDE在Firebeetle ESP32中构建一个网络服务器以及使用touchIO来配置一个触摸按键。

通常情况下会想要通过访问网页来实现对某些设备的操作和控制,在arm平台上建立httpserver,使用lwip协议栈的话开启SSI和CGI就可以比较容易的实现上述想法。那么在arduino中,同样可以创建网络服务器,一下举个例子

需要的库就是FireBeetle Board-ESP32提供的,http使用常用80端口,接下来使用库函数来发送请求和回复应答就可以

if (c == '\n') { // if the byte is a newline character

// if the current line is blank, you got two newline characters in a row.

// that's the end of the client HTTP request, so send a response:

if (currentLine.length() == 0) {

// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)

// and a content-type so the client knows what's coming, then a blank line:

client.println("HTTP/1.1 200 OK");

client.println("Content-type:text/html");

client.println();

// the content of the HTTP response follows the header:

client.print("

LED Control

");

 

client.print("Turn On The LED
");

client.print("Turn Off The LED
");

// The HTTP response ends with another blank line:

client.println();

// break out of the while loop:

break;

} else { // if you got a newline, then clear currentLine:

currentLine = "";

}

} else if (c != '\r') { // if you got anything else but a carriage return character,

currentLine += c; // add it to the end of the currentLine

}

以上实现了发送http头和内容,假如要控制一个led,还需要发送点亮或者关闭的请求,然后解析后执行IO动作。

以下就是对应的IO操作,2端口就是D9,连接到板载的LED.

// Check to see if the client request was "GET /H" or "GET /L":

if (currentLine.endsWith("GET /H")) {

digitalWrite(2, HIGH); // GET /H turns the LED on

}

if (currentLine.endsWith("GET /L")) {

digitalWrite(2, LOW); // GET /L turns the LED off

}

无线开启后搜索程序中制定的AP并连接,然后我们可以访问模块在路由器中获得的IP地址,调试程序在串口也会输出这些信息。

点击链接就可以操作模块上的LED了,这就是实现网页控制的基本原型。除此之外,还有值得一提的地方是FireBeetle Board-ESP32 提供了多达 10 个电容式传感器 GPIO,能够探测由手指或其他物品直接接触或 接近而产生的电容差异。这种低噪声特性和电路的高灵敏度设计适用于较小的触摸板,可以直接用于触摸开关。

接下来测试一下是否如实。

查找库文件,简单书写下面代码

//触摸IO测试

void setup() {

Serial.begin(115200);

delay(1000); // give me time to bring up serial monitor

Serial.println("FireBeetle Board-ESP32 Touch Test");

}

//10 个具有触摸传感器的IO,D0-D9,即T0-T9

void loop(){

//读取IO 状态

Serial.println(touchRead(T2)); // get value using T0->D9

delay(1000);

}

然后直接用手去触摸IO,通过串口监视器看到如下,在有手指触摸的时候会变为高电平

 


 

这样的功能可以的确可以尝试作为电容触摸按键了。后续准备还是回到C环境下了,不过不否定Arduino是很好用的

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭