跳到主要內容

 自 109 / 2 /25 起啟用

物聯網 / 多通道氣體感測器 / 氣體感測資料上傳
#include "ESP8266WiFi.h"  //匯入無線網路程式庫
#include "SSD1306.h"  //匯入OLED程式庫

#include "Multichannel_Gas_GMXXX.h"
#include "Wire.h"
SSD1306  display(0x3C, D2, D1); //宣告使用OLED螢幕物件(位址,SDA,SCL)==SDA>D2,SCL>D1
GAS_GMXXX< TwoWire > gas;
int NO2_value, C2H5CH_value, VOC_value, CO_value;       //定義要儲存氣體感測器數值的變數
long previousTime = 0;   //用來保存前一次狀態的時間
long interval = 2000;   //讀取間隔時間,單位為毫秒(miliseconds)
const char *API_KEY = "3HP49CL176OSQ9AC";  //ThingSpeak取得的apiKey,請更換為各組之apikey
const char *HOST = "api.thingspeak.com";  //定義連線至thingspeak網站
const char* MY_SSID = "TCGS-IOT";    //無線基地台SSID名稱
const char* MY_PWD = "22205108";    //無線基地台密碼

void setup()
{
  display.init(); //螢幕初始化
  display.flipScreenVertically();   //螢幕垂直翻轉

  display.setFont(ArialMT_Plain_16);  //16字體
  display.setTextAlignment(TEXT_ALIGN_LEFT);   //置左對齊
  gas.begin(Wire, 0x08); // use the hardware I2C
  connectWifi();  //呼叫網路連線函數
}

void loop()
{
  
unsigned long currentTime = millis();   //將當前的時間存入變數currentTime
  
if(currentTime - previousTime > interval) {   //如果當前時間扣除前一次保留時間超過間隔時間(interval)就進入執行程式

     // GM102B NO2 sensor
     NO2_value = gas.getGM102B();     
     if (NO2_value > 999) NO2_value = 999;
 
     // GM302B C2H5CH sensor
     C2H5CH_value = gas.getGM302B();
     if (C2H5CH_value > 999) C2H5CH_value = 999;
 
     // GM502B VOC sensor
     VOC_value = gas.getGM502B();
     if (VOC_value > 999) VOC_value = 999;
 
     // GM702B CO sensor
     CO_value = gas.getGM702B();
     if (CO_value > 999) CO_value = 999;

     previousTime = currentTime;   //離開if時更新時間
     send_Data();   //呼叫上傳資料函數
   }
   
display.clear(); //清除螢幕
   
display.drawString(0, 0, "NO2:"+String(NO2_value)+"ppm"); //x=0,y=0位置顯示二氧化氮(NO2)的值
   
display.drawString(0, 16, "C2H5CH:"+String(C2H5CH_value)+"ppm"); //x=0,y=16位置顯示乙醇(C2H5CH)的值
   
display.drawString(0, 32, "VOC:"+String(VOC_value)+"ppm"); //x=0,y=32位置顯示揮發性有機化合物(VOC) 的值
   
display.drawString(0, 48, "CO:"+String(CO_value)+"ppm"); //x=0,y=48位置顯示一氧化碳(CO)的值
   
display.display();//螢幕顯示畫面
}

//網路連線函數
void connectWifi()
{
  Serial.print("Connecting to "+*MY_SSID);
  WiFi.begin(MY_SSID, MY_PWD); //連線至MY_SSID無線基地台,MY_PWD為連線密碼
  while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  display.clear();   //清除螢幕
  display.drawString(0, 0, "connect Wifi...");   //將connect Wifi...字串放入暫存區
  display.display();   //螢幕顯示畫面
  
}  
}

//傳送資料至ThingSpeak網站
void send_Data()
{  
   WiFiClient client;  // 設定 ESP8266 作為 Client 端

  if ( !client.connect( HOST, 80 ) ) {
    Serial.println("connection failed");
    return;
  } else {
      String URL = "GET /update?key=";   // 以GET方式上傳資料到 ThingSpeak伺服器
             URL += API_KEY;
             URL += "&field1=";     //field1上傳二氧化氮NO2數據
             URL += NO2_value;
             URL += "&field2=";     //field2上傳乙醇C2H5CH數據
             URL += C2H5CH_value;
             URL += "&field3=";     //field3上傳揮發性有機化合物VOC數據
             URL += VOC_value;
             URL += "&field4=";     //field4上傳一氧化碳CO數據
             URL += CO_value;
             URL += "  HTTP/1.1\n";
    
    client.print( URL + "Host: " + HOST + "\r\n" +
                  "Connection: close\r\n\r\n" );

    delay(10);  // 等待一下下

    // 接收ThingSpeak伺服器的回應
    unsigned long timeout = millis();
    while (client.available() == 0) {
      if (millis() - timeout > 5000) {
        Serial.println(">>> Client Timeout !");
        client.stop();
        return;
      }
    }    
    // Read all the lines of the reply from server and print them to Serial
    // while(client.available()){
      String line = client.readStringUntil('\r');
      Serial.print(line);
    // }
    Serial.println();
    Serial.println("closing connection");
  }
}

消息公佈欄

時間類別單位標題發佈點閱
跳至網頁頂部