arduino环境esp32跑freertos系统实现触摸检测及wifi控制

这个程序内容是通过touch0,touch4,touch5(由于使用touch1,touch2测试发现经常出现误触发)触摸传感器检测是否有触摸事件,有的话则向串口发送对应数据,wifi 工作在客户端模式下,通过使用同一网络wifi的电脑或手机等登录对应ip地址之后便可以控制pin5的高低电平,当成功控制之后会返回open与close数据回到电脑或手机,便可以远程知道是否已经成功控制。
同时注意在每个任务函数的后尾需加上vTaskDelete(NULL);
否则大概率会遇上这个报错问题:Guru Meditation Error: Core 1 panic’ed (IntegerDivideByZero). Exception was unhandled.
且会导致esp32频繁重启。

程序如下:
#include <WiFi.h>

#include <WiFiClient.h>

#include <WiFiServer.h>

const char* ssid = “gzlhs.com”;//wifi名称
const char* password = “XXXXXXXX”;//wifi密码

WiFiServer server(80);//http端口

#if CONFIG_FREERTOS_UNICORE
#define ARDUINO_RUNNING_CORE 0
#else
#define ARDUINO_RUNNING_CORE 1
#endif

int threshold = 25;
bool touch0detected = false;
bool touch1detected = false;
bool touch2detected = false;

// 定义任务函数
void Tasktouch( void *pvParameters );
void Taskled( void *pvParameters );
void Taskwifi( void *pvParameters );

void wifi_int()
{
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED)
    {
    delay(500);
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP()); //输出IP地址到串口
    server.begin(); 
    }

}
void touch_int()
{

touchAttachInterrupt(T0, gotTouch0, threshold); //IO4
touchAttachInterrupt(T4, gotTouch1, threshold); //IO13
touchAttachInterrupt(T5, gotTouch2, threshold); //IO12
}

void gotTouch0(){ //触摸中断0服务函数
touch0detected = true;
}

void gotTouch1(){ //触摸中断1服务函数
touch1detected = true;
}

void gotTouch2(){ //触摸中断2服务函数
touch2detected = true;
}

void touch_judge()
{
if(touch0detected){
touch0detected = false;
Serial.println(“T0”);
delay(500);
}
if(touch1detected){
touch1detected = false;
Serial.println(“T1”);
// Serial.println(touchRead(T4));
delay(500);
}
if(touch2detected){
touch2detected = false;
Serial.println(“T2”);
//Serial.println(touchRead(T5));
delay(500);
}
}
void setup() {

Serial.begin(115200);
pinMode(5,OUTPUT);
pinMode(15,OUTPUT);
wifi_int();
delay(1000);

xTaskCreatePinnedToCore( //创建任务
Tasktouch //任务
, “Tasktouch” // 任务名字,随便起
, 1024 // 堆栈内存
, NULL
, 1 // 优先级,0为最低
, NULL
, ARDUINO_RUNNING_CORE); //允许执行

xTaskCreatePinnedToCore( //创建任务
Taskled
, “Taskled”
, 1024
, NULL
, 2
, NULL
, ARDUINO_RUNNING_CORE); //允许执行

xTaskCreatePinnedToCore( //创建任务
Taskwifi
, “Taskwifi”
, 1024
, NULL
, 0
, NULL
, ARDUINO_RUNNING_CORE); //允许执行
}
void loop()
{
// Empty. Things are done in Tasks.
delay(10);
WiFiClient client = server.available(); // listen for incoming clients

if (client) { // if you get a client,
//Serial.println(“New Client.”); // print a message out the serial port
String currentLine = “”; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client’s connected
if (client.available()) { // if there’s bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write©; // print it out the serial monitor
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("Click <a href= "/H\">here</a > to turn the LED on pin 15 on.<br>");
        client.print("Click <a href=\"/L\">here</a > to turn the LED on pin 15 off.<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
    }

    // Check to see if the client request was "GET /H" or "GET /L":
    if (currentLine.endsWith("GET /H")) {
      digitalWrite(15, HIGH);               // GET /H turns the LED on
      client.write("PIN15 open"); //返回数据给HTTP网址,证实已成功驱动
      client.stop();
    }
    if (currentLine.endsWith("GET /L")) {
      digitalWrite(15, LOW);                // GET /L turns the LED off
      client.write("PIN15 close");
      client.stop();
    }
  }
}
// close the connection:
client.stop();
 // Serial.println("Client Disconnected.");

}

delay(300);
}

void Tasktouch(void *pvParameters) // 触摸检测并触发中断
{
(void) pvParameters;

while(1)
{
touch_int();
touch_judge();
}
vTaskDelete( NULL );
}

void Taskled(void *pvParameters) // LED闪烁
{
(void) pvParameters;
while(1)
{
digitalWrite(5,HIGH);
delay(500);
digitalWrite(5,LOW);
delay(500);
}
vTaskDelete( NULL );
}
void Taskwifi(void *pvParameters)//未使用
{
(void) pvParameters;
while(1)
{
delay(1000);

vTaskDelete( NULL );
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值