锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

arduino esp32 读福申甲醛传感器

时间:2022-09-13 03:30:00 甲醛等传感器

arduino esp32 读福申甲醛传感器

    • 想法
    • 源代码

想法

arduino需求描述可以通过几行代码来完成。
缺点是不能Debug。
继续监测甲醛的变化。
使用杜邦线连接,需要转换传感器输出线焊接杜邦线。
杜邦线是一公一母。

源代码

可以开启软AP,也可连接到无线路由器。
买到的UNO D1 R32 ,无线性能差,距离路由器2米,信号强度超过-90。
将网络改为这里AP。

#include  #include  #include  #include  #include   #define ESP_getChipId() ((uint32_t)ESP.getEfuseMac()) #define MAX_CLIENTS 4  // http using namespace httpsserver; HTTPServer secureServer = HTTPServer(); void handleRoot(HTTPRequest * req, HTTPResponse * res); char crc(String s);  int data = 0; float value = 0; String buffer = ""; String buffer2 = ""; String result;  class ChatHandler : public WebsocketHandler { 
           public:     static WebsocketHandler* create();
    void onClose();
};
ChatHandler* activeClients[MAX_CLIENTS];

void setup() { 
        
  // Initialize the slots
  for (int i = 0; i < MAX_CLIENTS; i++) activeClients[i] = nullptr;

  Serial.begin(9600);
  Serial2.begin(9600);

  // wifi
  IPAddress ip(192, 168, 3, 1);
  IPAddress gw(192, 168, 3, 1);
  IPAddress mask(255, 255, 255, 0);
  WiFi.softAPConfig(ip, gw, mask);
  String chip_id = "ESP_" + String(ESP_getChipId());
  const char *ssid = chip_id.c_str();
  WiFi.softAP(ssid);

  // http
  ResourceNode * nodeRoot = new ResourceNode("/", "GET", &handleRoot);
  secureServer.registerNode(nodeRoot);
  // chat register
  WebsocketNode * chatNode = new WebsocketNode("/chat", &ChatHandler::create);
  secureServer.registerNode(chatNode);

  secureServer.start();

  //
}

void loop() { 
        
  // 读甲醛传感器
  while (Serial2.available() > 0) { 
        
    data = Serial2.read();
    Serial.println(data);

    buffer += (char)data;
    buffer2 += String(data, HEX) + " ";

  }  // while 0

  if (buffer.charAt(0) == 0xff && buffer.length() == 9) { 
        
    // 验证crc
    char crcValue = crc(buffer);
    if (crcValue == buffer.charAt(8))
      value = (float)(buffer.charAt(4) * 256 + buffer.charAt(5)) / 1000.0;
    else
      Serial.println("crc not equal");
    buffer = "";
    result = "结果:" + String(value) + ",数据:" + buffer2;
    buffer2 = "";
  }  // if 1

  if (buffer.length() >= 9)
    buffer = "";

  Serial2.flush();

  // web
  secureServer.loop();

  // 发送数据
  for (int i = 0; i < MAX_CLIENTS; i++) { 
        
    if (activeClients[i] != nullptr) { 
        
      activeClients[i]->send(result.c_str(), WebsocketHandler::SEND_TYPE_TEXT);
    }
  }

  // delay
  delay(100);

  // loop
}


void handleRoot(HTTPRequest * req, HTTPResponse * res) { 
        
  // Status code is 200 OK by default.
  // We want to deliver a simple HTML page, so we send a corresponding content type:
  res->setHeader("Content-Type", "text/html");

  // The response implements the Print interface, so you can use it just like
  // you would write to Serial etc.
  res->println("");
  res->println("");
  res->println("Hello World!");
  res->println("");
  res->println("

甲醛读数

"
); res->print("

服务器已运行 "); res->print((int)(millis() / 1000 / 60), DEC); res->println(" 分.

"
); res->println("

数值(毫克/立方米.):

"
); res->print("
"); res->print(value, DEC); res->println("
"
); res->println(""); res->println(""); res->print( " \n" ); } WebsocketHandler * ChatHandler::create() { Serial.println("Creating new chat client!"); ChatHandler * handler = new ChatHandler(); for (int i = 0; i < MAX_CLIENTS; i++) { if (activeClients[i] == nullptr) { activeClients[i] = handler; break; } } return handler; } // When the websocket is closing, we remove the client from the array void ChatHandler::onClose() { for (int i = 0; i < MAX_CLIENTS; i++) { if (activeClients[i] == this) { activeClients[i] = nullptr; } } } // CRC计算 char crc(String s) { int sum = 0; for (int i = 1; i < s.length() - 1; i++) sum += s[i]; int value = ~sum + 1; return value & 0xff; }
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章