|
各位arduino的前輩,
我想請教有關WIFI模組的程式融入於PM2.5 SENSOR+DHT11,其WIFI程式碼如下:#include <SoftwareSerial.h>
#define _baudrate 9600
#define _rxpin 9
#define _txpin 10
SoftwareSerial debug( _rxpin, _txpin ); // RX, TX
//*-- IoT Information
#define SSID "aerosol"
#define PASS "power725"
#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( _baudrate );
debug.begin( _baudrate );
sendDebug("AT");
Loding("sent AT");
connectWiFi();
}
void loop() {
delay(5000); // 60 second
SentOnCloud( String(5), String(9) );
}
boolean connectWiFi()
{
debug.println("AT+CWMODE=1");
Wifi_connect();
}
void SentOnCloud( String T, String H )
{
// 設定 ESP8266 作為 Client 端
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
if( debug.find( "Error" ) )
{
Serial.print( "RECEIVED: Error\nExit1" );
return;
}
cmd = GET + "&field1=" + T + "&field2=" + H +"\r\n";
debug.print( "AT+CIPSEND=" );
debug.println( cmd.length() );
if(debug.find( ">" ) )
{
Serial.print(">");
Serial.print(cmd);
debug.print(cmd);
}
else
{
debug.print( "AT+CIPCLOSE" );
}
if( debug.find("OK") )
{
Serial.println( "RECEIVED: OK" );
}
else
{
Serial.println( "RECEIVED: Error\nExit2" );
}
}
void Wifi_connect()
{
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
Loding("Wifi_connect");
}
void Loding(String state){
for (int timeout=0 ; timeout<10 ; timeout++)
{
if(debug.find("OK"))
{
Serial.println("RECEIVED: OK");
break;
}
else if(timeout==9){
Serial.print( state );
Serial.println(" fail...\nExit2");
}
else
{
Serial.print("Wifi Loading...");
delay(500);
}
}
}
void sendDebug(String cmd)
{
Serial.print("SEND: ");
Serial.println(cmd);
debug.println(cmd);
}
想融入先前寫好的PM2.5 SENSOR+DHT11,程式碼如下:
#include <DHT.h>
#include <stdio.h>
#include <DS1302.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include<SD.h>
#define DHTPIN 8
#define DHTTYPE DHT11
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
File myFile;
DHT dht(DHTPIN, DHTTYPE);
int measurePin = 0;
int ledPower = 3;
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
char buf[50];
char fn[12];
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
int mean_pm, mean_v0;
char pm[16];
char pm_SD[22];
char HT[7];
int m, d, H, M, S;
namespace {
const int kCePin = 5;
const int kIoPin = 6;
const int kSclkPin = 7;
DS1302 rtc(kCePin, kIoPin, kSclkPin);
};
void setup() {
Serial.begin(9600);
Serial.println("");
dht.begin();
pinMode(ledPower, OUTPUT);
lcd.begin(16, 2);
for (int i = 0; i < 2; i++) {
lcd.backlight();
delay(100);
lcd.noBacklight();
delay(100);
}
lcd.backlight();
rtc.writeProtect(false);
Serial.println("Initial SD card...");
pinMode(10, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("Initial SD card...");
if (!SD.begin(4)) {
Serial.println("Check SD and Reset!");
lcd.setCursor(0, 1);
lcd.print("Check SD and Reset!");
return;
}
else {
Serial.println("SD card is ready.");
lcd.setCursor(0, 1);
lcd.print("SD card is ready.");
}
Time t = rtc.time();
m=t.mon;
while (m>12) {
t=rtc.time();
m=t.mon;
delay(50);
}
d=t.date;
H=t.hr;
M=t.min;
S=t.sec;
delay(2000);
lcd.clear();
}
void loop() {
Time t = rtc.time();
if (t.mon>12) {
S=S+1;
}
else {
m=t.mon;
d=t.date;
H=t.hr;
M=t.min;
S=t.sec;
}
snprintf(fn, sizeof(fn), "t%02d%02d%02d.csv",
m, d, H);
myFile = SD.open(fn, FILE_WRITE);
snprintf(buf, sizeof(buf), "%02d/%02d %02d:%02d:%02d ",
m, d,
H, M, S);
Serial.print(fn);
Serial.print(" ");
Serial.print(buf);
Serial.print(" ");
lcd.setCursor(0, 0);
lcd.print(buf);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float tt = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(tt)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("RH:");
Serial.print(h);
Serial.print("% ");
Serial.print("T:");
Serial.print(tt);
Serial.print("C ");
snprintf(HT, sizeof(HT), "%2d, %d", int(h), int(tt));
//Serial.print(HT);
dustDensity = 0;
mean_v0=0;
float vs=170;
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)*1000;
// Serial.println(voMeasured);
mean_v0=mean_v0+voMeasured;
if (h>50) {
dustDensity += 0.6*(1-0.01467)*(h-50)*(voMeasured-vs);
}
else {
dustDensity += 0.6*(voMeasured-vs);
}
delay(18);
};
mean_v0=mean_v0/30;
mean_pm = dustDensity / 30.0;
snprintf(pm, sizeof(pm), "%04d", mean_pm);
snprintf(pm_SD, sizeof(pm_SD), "%05d", int(mean_v0));
lcd.setCursor(0, 1);
lcd.print(HT);
lcd.print(',');
lcd.print(pm);
lcd.print("ug/m3");
Serial.print(' ');
Serial.print(pm);
Serial.print(" ug/m3");
Serial.print(' ');
Serial.println(pm_SD);
myFile.print(buf);
myFile.print(',');
myFile.print(HT);
myFile.print(", ");
myFile.print(", ");
myFile.print(pm);
myFile.print(", ");
myFile.print(pm_SD);
myFile.println("");
myFile.close();
};
分開時都可以正常運行,但合在一起卻卡在WIFI 不斷LOADING,
請各位前輩不吝指點,感謝 |
|