FireBeetle Board-ESP32试用----HttpServer&Touch IO

[复制链接]
2097|1
手机看帖
扫描二维码
随时随地手机跟帖
BinWin|  楼主 | 2018-3-10 14:59 | 显示全部楼层 |阅读模式
本帖最后由 BinWin 于 2018-3-10 15:00 编辑

      通常情况下会想要通过访问网页来实现对某些设备的操作和控制,在arm平台上建立httpserver,使用lwip协议栈的话开启SSI和CGI就可以比较容易的实现上述想法。那么在arduino中,同样可以创建网络服务器,一下举个例子
      需要的库就是FireBeetle Board-ESP32提供的<WiFi.h>,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("<h2 align = 'left'>LED Control</h2>");
            client.print("<a href=\"/H\">Turn On The LED</a><br>");
            client.print("<a href=\"/L\">Turn Off The LED</a><br>");

            // 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是很好用的


相关帖子

巧克力娃娃| | 2018-3-15 17:13 | 显示全部楼层

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

16

主题

58

帖子

0

粉丝