我正在开发一个由5个传感器组成的IAQ监控系统。它读得很好,但我已经卡住了将近一个星期。我一直在尝试所有可用的解决方案,但我把它们全部用尽了。
我注意到我的数据被发送到PHP代码(其中包含向MySQL提交数据的代码 - 经过测试并运行完美),无法通过。主机没问题(我正在使用XAMPP),客户端也可以,但是在发送数据时,它失败了。
我尝试使用POST方法,但也失败了。
我的代码:
//Libraries
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include "MQ135.h"
#include <CytronWiFiShield.h>
#include <CytronWiFiClient.h>
#include <CytronWiFiServer.h>
#include <SoftwareSerial.h>
//WiFi Setting Constant
const char *ssid = "Itik Tungging";
const char *pass = "mc053002";
IPAddress ip(192, 168, 1 ,242);
ESP8266Server server(80);
const char htmlHeader[] = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n\r\n"
"<!DOCTYPE HTML>\r\n"
"<html>\r\n";
//Send to mysql constants
IPAddress host(192, 168, 1, 83); //get from pc ipconfig http://192.168.1.83
//const char* host = "192.168.1.1"; (server ip)
const char* passcode = "mc053002";
const int httpPort = 80;
//MQ135 Constants
#define RZERO 879.13
#define RLOAD 22.0
MQ135 gasSensor = MQ135(A1);
//DHT22 Constants
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define DHTPIN11 3 //pin for DHT11
#define DHTTYPE11 DHT11 // DHT11
DHT dht22(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
DHT_Unified dht11(DHTPIN11, DHTTYPE11);
uint32_t delayMS;
//MQ7 CO Gas Sensor Constants
const int MQ7AOUTpin=0;//the AOUT pin of the CO sensor goes into analog pin A0 of the arduino
const int MQ7DOUTpin=8;//the DOUT pin of the CO sensor goes into digital pin D8 of the arduino
//MQ135 Variables
int val;
int sensorPin = A1;
int sensorValue = 0;
float avgzero, zerob=0, avgval, valb=0, avgppm, ppmb=0;
//DHT Variables
int chk22;
float hum22; //Stores humidity value
float temp22; //Stores temperature value
long randNumber;
//MQ7 Variables
int MQ7limit;
int MQ7value;
int count=0;
float hum11b=58.00, temp11b=22.00, hum22b=58.00, temp22b=22.00;
int MQ7valueb=54;
//Dust Sensor Variables
int measurePin = A2;
int ledPower = 12;
unsigned int samplingTime = 280;
unsigned int deltaTime = 40;
unsigned int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
//Data to mySQL variable
ESP8266Client client;
void setup()
{
Serial.begin(9600);
dht22.begin(); //begin temperature and humidity sensor
dht11.begin();
//MQ7 setup
pinMode(MQ7DOUTpin, INPUT);//sets the pin as an input to the arduino
//MQ135 setup
pinMode(sensorPin, INPUT);
//Dust sensor setup
pinMode(ledPower,OUTPUT);
//WiFi Setup
if(!wifi.begin(2, 3))
{
Serial.println(F("Error talking to shield"));
while(1);
}
Serial.println(wifi.firmwareVersion());
Serial.print(F("Mode: "));Serial.println(wifi.getMode());// 1- station mode, 2- softap mode, 3- both
Serial.println(F("Start wifi connection"));
if(!wifi.connectAP(ssid, pass))
{
Serial.println(F("Error connecting to WiFi"));
while(1);
}
Serial.print(F("Connected to "));Serial.println(wifi.SSID());
Serial.println(F("IP address: "));
Serial.println(wifi.localIP());
wifi.updateStatus();
Serial.println(wifi.status()); //2- wifi connected with ip, 3- got connection with servers or clients, 4- disconnect with clients or servers, 5- no wifi
server.begin();
//MySQL server setup
Serial.print("Connecting to ");
Serial.println(host);
if (!client.connect(host, httpPort))
{
Serial.println(F("Failed to connect to server."));
client.stop();
while(true);
}
}
void loop()
{
delay(2000);
count++;
Serial.print(" No ");
Serial.print(count);
Serial.println(" readings. ");
//Read DHT11 Sensor
// Get temperature event and print its value.
sensors_event_t event;
dht11.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.print (" [DHT11] Humidity: ");
Serial.print (hum11b);
}
else {
Serial.print (" [DHT11] Humidity: ");
Serial.print(event.relative_humidity);
hum11b=event.relative_humidity;
}
Serial.print(" %, Temp: ");
dht11.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.print(temp11b);
}
else {Serial.print(event.temperature);
temp11b=event.temperature;
}
Serial.println(" *C");
//Read DHT22 Sensor
//Read data and store it to variables hum and temp
hum22 = dht22.readHumidity();
temp22 = dht22.readTemperature();
//Print temp and humidity values to serial monitor
if (isnan(dht22.readHumidity())) {
Serial.print (" [DHT22] Humidity: ");
randNumber=random(1,99);
Serial.print (hum22b+randNumber/100);
}
else {
Serial.print (" [DHT22] Humidity: ");
Serial.print (hum22);
hum22b=hum22;
}
Serial.print(" %, Temp: ");
if (isnan(dht22.readTemperature())) {
randNumber=random(1,99);
Serial.print(temp22b+randNumber/100);
}
else {
Serial.print(temp22);
temp22b=temp22;
}
Serial.println(" *C");
//Read MQ7 Sensor
MQ7value= analogRead(MQ7AOUTpin);//reads the analog value from the CO sensor's AOUT pin
MQ7limit= digitalRead(MQ7DOUTpin);//reads the digital value from the CO sensor's DOUT pin
if (isnan(MQ7value)) {
Serial.print (" [MQ7] CO value: ");
Serial.print (MQ7valueb);
}
else {
Serial.print(" [MQ7] CO value: ");
Serial.print(MQ7value);
MQ7valueb = MQ7value;
}
Serial.print(" ppm ");
Serial.print(" Limit: ");
Serial.println(MQ7limit);//prints the limit reached as either LOW or HIGH (above or underneath)
//MQ135 works
Serial.print(" [MQ135] ");
val = analogRead(A1);
Serial.print (" raw = ");
Serial.print (val);
if (valb==0) {
valb=val;
avgval=val;
}
else {
avgval=(val+valb)/2;
valb=val;
}
Serial.print (" Average raw = ");
Serial.print (avgval);
float zero = gasSensor.getRZero();
Serial.print (" rzero: ");
Serial.print (zero);
if (zerob==0) {
zerob=zero;
avgzero=zero;
}
else {
avgzero=(zero+zerob)/2;
zerob=zero;
}
Serial.print (" Average rzero = ");
Serial.print (avgzero);
float ppm = gasSensor.getPPM();
Serial.print (" ppm: ");
Serial.print (ppm);
if (ppmb==0) {
ppmb=ppm;
avgppm=ppm;
}
else {
avgppm=(ppm+ppmb)/2;
ppmb=ppm;
}
Serial.print (" Average ppm = ");
Serial.println (avgppm);
//Read Dust Sensor
digitalWrite(ledPower,LOW);
delayMicroseconds(samplingTime);
voMeasured = analogRead(measurePin);
delayMicroseconds(deltaTime);
digitalWrite(ledPower,HIGH);
delayMicroseconds(sleepTime);
calcVoltage = voMeasured*(5.0/1024);
dustDensity = 0.17*calcVoltage-0.1;
if ( dustDensity < 0)
{
dustDensity = 0.00;
}
Serial.print(" [Dust Sensor] ");
Serial.print(" Raw Signal Value (0-1023): ");
Serial.print(voMeasured);
Serial.print(" Voltage: ");
Serial.print(calcVoltage);
Serial.print(" Dust Density:");
Serial.println(dustDensity);
// Create a URL for the request. Modify YOUR_HOST_DIRECTORY so that you're pointing to the PHP file.
String url = "/iotiaq/index.php?s1=";
url += hum11b;
url += "&s2=";
url += temp11b;
url += "&s3=";
url += hum22b;
url += "&s4=";
url += temp22b;
url += "&s5=";
url += MQ7valueb;
url += "&s6=";
url += MQ7limit;
url += "&s7=";
url += val;
url += "&s8=";
url += avgval;
url += "&s9=";
url += zero;
url += "&s10=";
url += avgzero;
url += "&s11=";
url += ppm;
url += "&s12=";
url += avgppm;
url += "&s13=";
url += voMeasured;
url += "&s14=";
url += calcVoltage;
url += "&s15=";
url += dustDensity;
url += "&pass=";
url += passcode;
// This will send the request to the server
Serial.print("Requesting URL: ");
Serial.print(host);
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + String(host) + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 10000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
Serial.println("");
delay(10000); //Delay 2 sec.
}
输出:
AT version:0.52.0.0(Jan 7 2016 18:44:24)
SDK version:1.5.1(e67da894)
compile time:Jan 7 2016 19:03:11
Mode: 1
Start wifi connection
Connected to Itik Tungging
IP address:
192.168.1.205
2
Connecting to 192.168.1.83
No 1 readings.
[DHT11] Humidity: 66.00 %, Temp: 29.00 *C
[DHT22] Humidity: 80.80 %, Temp: 27.40 *C
[MQ7] CO value: 711 ppm Limit: 1
[MQ135] raw = 709 Average raw = 709.00 rzero: 97.06 Average rzero = 97.06 ppm: 209.25 Average ppm = 209.25
[Dust Sensor] Raw Signal Value (0-1023): 0.00 Voltage: 0.00 Dust Density:0.00
Requesting URL: 192.168.1.83/iotiaq/index.php?s1=66.00&s2=29.00&s3=80.80&s4=27.40&s5=711&s6=1&s7=709&s8=709.00&s9=97.06&s10=97.06&s11=209.25&s12=209.25&s13=0.00&s14=0.00&s15=0.00&pass=mc053002
>>> Client Timeout !
No 2 readings.
[DHT11] Humidity: 65.00 %, Temp: 30.00 *C
[DHT22] Humidity: 80.80 %, Temp: 27.50 *C
[MQ7] CO value: 710 ppm Limit: 1
[MQ135] raw = 708 Average raw = 708.50 rzero: 96.58 Average rzero = 96.82 ppm: 209.25 Average ppm = 209.25
[Dust Sensor] Raw Signal Value (0-1023): 0.00 Voltage: 0.00 Dust Density:0.00
Requesting URL: 192.168.1.83/iotiaq/index.php?s1=65.00&s2=30.00&s3=80.80&s4=27.50&s5=710&s6=1&s7=708&s8=708.50&s9=96.58&s10=96.82&s11=209.25&s12=209.25&s13=0.00&s14=0.00&s15=0.00&pass=mc053002
>>> Client Timeout !
No 3 readings.
[DHT11] Humidity: 65.00 %, Temp: 29.00 *C
[DHT22] Humidity: 80.60 %, Temp: 27.50 *C
[MQ7] CO value: 710 ppm Limit: 1
[MQ135] raw = 711 Average raw = 709.50 rzero: 96.58 Average rzero = 96.58 ppm: 210.20 Average ppm = 209.72
[Dust Sensor] Raw Signal Value (0-1023): 0.00 Voltage: 0.00 Dust Density:0.00
Requesting URL: 192.168.1.83/iotiaq/index.php?s1=65.00&s2=29.00&s3=80.60&s4=27.50&s5=710&s6=1&s7=711&s8=709.50&s9=96.58&s10=96.58&s11=210.20&s12=209.72&s13=0.00&s14=0.00&s15=0.00&pass=mc053002
>>> Client Timeout !
No 4 readings.
[DHT11] Humidity: 67.00 %, Temp: 29.00 *C
[DHT22] Humidity: 80.70 %, Temp: 27.50 *C
[MQ7] CO value: 711 ppm Limit: 1
[MQ135] raw = 712 Average raw = 711.50 rzero: 96.90 Average rzero = 96.74 ppm: 214.99 Average ppm = 212.59
[Dust Sensor] Raw Signal Value (0-1023): 0.00 Voltage: 0.00 Dust Density:0.00
Requesting URL: 192.168.1.83/iotiaq/index.php?s1=67.00&s2=29.00&s3=80.70&s4=27.50&s5=711&s6=1&s7=712&s8=711.50&s9=96.90&s10=96.74&s11=214.99&s12=212.59&s13=0.00&s14=0.00&s15=0.00&pass=mc053002
>>> Client Timeout !
如果您需要,请向我询问更多信息。