2016年12月22日 星期四

pm2.5空氣品質感測器






/*
=================================================================
Connect the USB-TTL to pc and upload sketch to ESP-12
Note: GPIO0 should be LOW
      CH_PD and RST should be HIGH for first time flashing the board

GPIO0 should be LOW when uploading sketches
Remember use a 3.3V USB-TTL
connect RX of board to TX of usb and TX of board to RX
SKETCH:
edit auth code, ssid and pass
=================================================================
*/
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
SimpleTimer timer; //create a Timer object

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 ledPower1 = 16;   //Connect 3 led driver pins of dust sensor to Arduino D2

int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
long updateInterval = 250;               //傳送資料時間間隔,測試完請設定1800000(30分鐘)
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "605989b5204247989352d86301a4c4e9";    //Blynk Auth Token : 605989b5204247989352d86301a4c4e9
char ssid[] = "74170287";
char pass[] = "24063173";

void setup() {
  Serial.begin(9600); //Set console baud rate
  pinMode(ledPower,OUTPUT);
  pinMode(ledPower1,OUTPUT);
  Blynk.begin(auth, ssid, pass);   // auth, ssid, pass ;
  }

void loop() {
  Blynk.run();

  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
  // recover voltage
  calcVoltage = voMeasured * (3.3 / 1024.0);      //5V change into 3.3V
  // 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;
         }
   Serial.print("Raw Signal Value (0-1023): ");
   Serial.print(voMeasured);
   Serial.print(" - Voltage: ");
   Serial.print(calcVoltage);
   Serial.print(" - Dust Density: ");
   Serial.print(dustDensity * 1000); // 這裡將數值呈現改成較常用的單位( ug/m3 )
   Serial.println(" ug/m3 ");
   float T=dustDensity * 1000 ;

  Blynk.virtualWrite(V0, voMeasured);
  Blynk.virtualWrite(V1, calcVoltage);
  Blynk.virtualWrite(V2, T);
  for (byte i=0;i<=9;i++)
   {
     digitalWrite(ledPower1,LOW); // turn the LED off
     delay(updateInterval);   // 設定傳送時間間隔
     digitalWrite(ledPower1,HIGH); // turn the LED off
     delay(updateInterval);   // 設定傳送時間間隔
   }
   
}

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

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