#include
#include "DHT.h"
#define DEBUG
#define _ledpin 13
#define _rxpin 0
#define _txpin 1
SoftwareSerial debug( _rxpin, _txpin ); // RX, TX
#define _dhtpin 8
#define _dhttype DHT11
DHT dht11( _dhtpin, _dhttype );
uint8_t dhtbuf[2];
int measurePin = 0; //Connect dust sensor to Arduino A0 pin
int ledPower = 3; //Connect 3 led driver pins of dust sensor to Arduino D2
int samplingTime = 460;
int deltaTime = 40;
int sleepTime = 9500;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
int mean_pm, mean_v0;
char pm[16];
char HT[7];
char pm_SD[22];
#define SSID "889"
#define PASS "sf000093"
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149
// 使用 GET 傳送資料的格式
// GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&filed2=[data 2]...;
String GET = "GET /update?key=UR0WZRMIAUXQVUBI";
void setup() {
Serial.begin(9600);
debug.begin(9600);
sendDebug("AT");
delay(5000);
if(Serial.find("OK"))
{
debug.println("RECEIVED: OK\nData ready to sent!");
connectWiFi();
}
dht11.begin();
pinMode( _ledpin, OUTPUT );
digitalWrite( _ledpin, LOW );
}
void loop() {
float h = dht11.readHumidity();
float tt = dht11.readTemperature();
// 確認取回的溫溼度數據可用
if( isnan(dhtbuf[0]) || isnan(dhtbuf[1]) )
{
debug.println( "Failed to read form DHT11" );
}
else
{
digitalWrite( _ledpin, HIGH );
char buf[3];
String HH, TT;
buf[0] = 0x30 + dhtbuf[1] / 10;
buf[1] = 0x30 + dhtbuf[1] % 10;
TT = (String(buf)).substring( 0, 2 );
buf[0] = 0x30 + dhtbuf[0] / 10;
buf[1] = 0x30 + dhtbuf[0] % 10;
HH = (String(buf)).substring( 0, 2 );
updateDHT11( TT, HH );
#ifdef DEBUG
debug.print("Humidity: ");
debug.print( HH );
debug.print(" %\t");
debug.print("Temperature: ");
debug.print( TT );
debug.println(" *C\t");
#endif
digitalWrite( _ledpin, LOW );
}
delay(2000); // 60 second
dustDensity = 0;
mean_v0=0;
float vs=140;
for (int i = 0; i <= 29; i++) {
digitalWrite(ledPower, LOW);
delayMicroseconds(samplingTime);
voMeasured = analogRead(measurePin);
delayMicroseconds(deltaTime);
digitalWrite(ledPower, HIGH);
delayMicroseconds(sleepTime);
calcVoltage = voMeasured * (5.0 / 1024.0);
mean_v0=mean_v0+voMeasured;
if (h>50) {
dustDensity = 0.6*(1-0.01467*(h-50))*(voMeasured-vs);
}
else {
dustDensity = 0.6*(voMeasured-vs);
}
Serial.print("Raw input voltage: ");
Serial.print(calcVoltage);
Serial.print(" Raw Signal Valued: ");
Serial.print(voMeasured);
Serial.print(" PM2.5: ");
Serial.println(dustDensity);
Serial.println(" ug/m3 ");
Serial.print("RH:");
Serial.print(h);
Serial.print("% ");
Serial.print("T:");
Serial.print(tt);
Serial.print("C ");
delay(2000);
};
}
void updateDHT11( String T, String H, String PM2.5)
{
// 設定 ESP8266 作為 Client 端
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
delay(2000);
if( Serial.find( "Error" ) )
{
debug.print( "RECEIVED: Error\nExit1" );
return;
}
cmd = GET + "&field1=" + T + "&field2=" + H "&field3=" + PM2.5 +"\r\n";
Serial.print( "AT+CIPSEND=" );
Serial.println( cmd.length() );
if(Serial.find( ">" ) )
{
debug.print(">");
debug.print(cmd);
Serial.print(cmd);
}
else
{
sendDebug( "AT+CIPCLOSE" );
}
if( Serial.find("OK") )
{
debug.println( "RECEIVED: OK" );
}
else
{
debug.println( "RECEIVED: Error\nExit2" );
}
}
void sendDebug(String cmd)
{
debug.print("SEND: ");
debug.println(cmd);
Serial.println(cmd);
}
boolean connectWiFi()
{
Serial.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
delay(5000);
if(Serial.find("OK"))
{
debug.println("RECEIVED: OK");
return true;
}
else
{
debug.println("RECEIVED: Error");
return false;
}
cmd = "AT+CIPMUX=0";
sendDebug( cmd );
if( Serial.find( "Error") )
{
debug.print( "RECEIVED: Error" );
return false;
}
}
歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/) | Powered by Discuz! X3.2 |