2016年12月30日 星期五

ThingSpeak + ESP12 Test Board + 細懸浮微粒(PM2.5)

細懸浮微粒(PM2.5)

目的:

使用ESP8266  ESP-12 Tools Board 類比輸入A0來讀取PM2.5感測器的電壓值,轉換成PM2.5 之值,GPIO16 讀取DHT11溫度溼度感測器


硬體 (電子元件):

1)麵包板 x 1
2)ESP8266  ESP-12 Tools Board 主板 x 1
3)DHT11 x1 溫度溼度感測器
4)SHARP 原裝夏普 GP2Y1010AU0F PM2.5感測器 x1     +  
220uf  150歐姆

5) 3.3V/5V Linear Regulator Voltage x1



軟體: 

1)Arduino IDE 1.6.5版本
2) DHT.zip
3) ThingSpeak API Key + Channel Setting
4) 留意  ESP8266  ESP-12 Tools Board --uploading 接線
5) Arduino IDE  ESP8266 board from Tools > Board > Generic ESP8266 Module







//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//參考 http://www.arduinesp.com/thingspeak  
#include <ESP8266WiFi.h>
#include <dht.h>   
// replace with your channel's thingspeak API key, 
String apiKey = "TW60RSJ9YFYLQRGK";   // your channel's thingspeak API key
const char* ssid = "74170287";        // your WIFI SSID
const char* password = "24063173";    // your WIFI Password
const char* server = "api.thingspeak.com";
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#define dht_dpin 16  //定義訊號要從進來  
dht DHT;   
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int measurePin = A0; // ADC  Connect dust sensor to Arduino A0 pin 
int ledPower = 14;   //Connect 3 led driver pins of dust sensor to Arduino D2
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
long updateInterval = 250;               //傳送資料時間間隔)
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
float h=0;
float t=0;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
WiFiClient client;
 
void setup() {                
  Serial.begin(115200);
  delay(10);
  pinMode(dht_dpin,INPUT);
  pinMode(measurePin,INPUT); 
  pinMode(ledPower,OUTPUT);   
  
  WiFi.begin(ssid, password);
 
  Serial.println();
  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");
  
}
 
 
void loop() {
//++++++++++++++++++++++++++++++++++++++++++++++
  DHT.read11(dht_dpin);   //去library裡面找DHT.read11  
  h = DHT.humidity;
  t = DHT.temperature;
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

//++++++++++++++++++++++++++++++++++++++++++++++
  digitalWrite(ledPower,LOW); // power on the LED
  delayMicroseconds(samplingTime);
  voMeasured = analogRead(measurePin); // read the dust value
  delayMicroseconds(deltaTime);
  digitalWrite(ledPower,HIGH); // turn the LED off
  delayMicroseconds(sleepTime);
  // 0 - 5V mapped to 0 - 1023 integer values  5V change into 3.3V
  // recover voltage
  calcVoltage = voMeasured * (3.3 / 1024.0);   //5.0V change into 3.3
  // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
  // Chris Nafis (c) 2012
  if (calcVoltage < 0.583) {
       dustDensity = 0;
       }
   else{
        dustDensity = 6 * calcVoltage / 35 - 0.1;
         }
 
  dustDensity=dustDensity * 1000  ;     // 這裡將數值呈現改成較常用的單位( ug/m3 )
//++++++++++++++++++++++++++++++++++++++++++++++
   // dustDensity = 0.1714 * calcVoltage - 0.1;     // 6/35=0.1714 
  
  if (isnan(voMeasured) || isnan(calcVoltage) || isnan(dustDensity) ) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
 
  if (client.connect(server,80)) {  //   "184.106.153.149" or api.thingspeak.com
    String postStr = apiKey;
           postStr +="&field1=";
           postStr += String(t);     //field1
           postStr +="&field2=";
           postStr += String(h);    //field2
           postStr +="&field3=";
           postStr += String(voMeasured);   //field3
           postStr +="&field4=";
           postStr += String(dustDensity);      //field4      
           postStr += "\r\n\r\n";
           
 
     client.print("POST /update HTTP/1.1\n"); 
     client.print("Host: api.thingspeak.com\n"); 
     client.print("Connection: close\n"); 
     client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n"); 
     client.print("Content-Type: application/x-www-form-urlencoded\n"); 
     client.print("Content-Length: "); 
     client.print(postStr.length()); 
     client.print("\n\n"); 
     client.print(postStr);
  
     Serial.print("Temperature: ");
     Serial.print(t);
     Serial.print(" degrees Celcius  Humidity: "); 
     Serial.print(h);
     Serial.println("% send to Thingspeak");  

    Serial.print("Raw Signal Value (0-1023): ");
    Serial.print(voMeasured);
    Serial.print(" - Voltage: ");
    Serial.print(calcVoltage);
    Serial.print(" - Dust Density: ");
    Serial.print(dustDensity ); // 這裡將數值呈現改成較常用的單位( ug/m3 )
    Serial.println(" ug/m3 ");
  
   }
 
  client.stop();
  Serial.println("Waiting...");    
  // thingspeak needs minimum 15 sec delay between updates
  delay(20000);  
}


沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...