2018年12月2日 星期日

ESP32 MQ-2 LPG CO SMOKE Sensor Web Server







/*********
 * ESP32 Board & MQ-2 Sensor
  Rui Santos
  Complete project details at http://randomnerdtutorials.com
*********/
// Load Wi-Fi library
#include <WiFi.h>
#include <MQ2.h>
//change this with the pin that you use
int pin = 36;   //ADC0
int lpg, co, smoke;
MQ2 mq2(pin);

// Replace with your network details
//const char* ssid = "YOUR_NETWORK_NAME";
//const char* password = "YOUR_NETWORK_PASSWORD";
const char* ssid     = "alex9ufo";
const char* password = "alex9981";

// Web Server on port 80
WiFiServer server(80);
unsigned long lastConnectionTime = 0;            // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds

// Temporary variables
static char LpgTemp[7];
static char CoTemp[7];
static char SmokeTemp[7];

// only runs once on boot
void setup() {
  // Initializing serial port for debugging purposes
  Serial.begin(115200);
  delay(10);
  mq2.begin();

  // Connecting to WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Starting the web server
  server.begin();
  Serial.println("Web server running. Waiting for the ESP IP...");
  delay(10000);

  // Printing the ESP IP address
  Serial.println(WiFi.localIP());
}

// runs over and over again
void loop() {
  // Listenning for new clients
  WiFiClient client = server.available();
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  /*read the values from the sensor, it returns
  *an array which contains 3 values.
  * 1 = LPG in ppm
  * 2 = CO in ppm
  * 3 = SMOKE in ppm
  */
  float* values= mq2.read(true); //set it false if you don't want to print the values in the Serial
  //lpg = values[0];
  float lpg = mq2.readLPG();
  //co = values[1];
  float co = mq2.readCO();
  //smoke = values[2];
  float smoke = mq2.readSmoke();
  dtostrf(lpg, 6, 2, LpgTemp);           
  dtostrf(co, 6, 2, CoTemp);       
  dtostrf(smoke, 6, 2, SmokeTemp);
  // You can delete the following Serial.print's, it's just for debugging purposes
  Serial.print("LPG: ");
  Serial.print(lpg);
  Serial.print(" ppm\t Co: ");
  Serial.print(co);
  Serial.print(" ppm\t Smoke: ");
  Serial.print(smoke);
  Serial.println(" ppm ");
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean blank_line = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
   
        if (c == '\n' && blank_line) {

            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            // your actual web page that displays temperature and humidity
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            client.println("<head></head><body><h1>ESP32 - MQ-2 LPG ,  Co and Smoke </h1><h3> LPG: ");
            client.println(LpgTemp);
            client.println("ppm </h3><h3> CO: ");
            client.println(CoTemp);
            client.println("ppm </h3><h3> Smoke: ");
            client.println(SmokeTemp);
            client.println("%</h3><h3>");
            client.println("</body></html>");   
            break;
        }
        if (c == '\n') {
          // when starts reading a new line
          blank_line = true;
        }
        else if (c != '\r') {
          // when finds a character on the current line
          blank_line = false;
        }
      }
    }
    // closing the client connection
    delay(1);
    client.stop();
    Serial.println("Disonnected");
 
  }



沒有留言:

張貼留言

2024產專班 作業2

 2024產專班 作業2   1. 系統圖       ESP32+MFRC522 組成RFID Reader 可以將RFID卡片的UID 透過 MQTT協定    上傳(發行 主題 (:topic) alex9ufo/2024/RFID/RFID_UID  ,, Payload...