我是这方面的菜鸟,所以希望是一个愚蠢的显而易见的人。
我正在尝试创建一个简单的温度/湿度传感器,它从 DHT22 获取读数并使用 ESP8266 将它们 ping 到 Thingspeak 的 API,然后绘制/存储等。
我在下面粘贴了代码——它在 Arduino Uno 上工作,我正试图将它缩小到 ESP8266 上,这样我就可以在房子周围生产很多小温度传感器。
症状
- 它可以很好地连接到 wifi
- 它正在生成正确的 API 字符串(我已经通过手动剪切并将其粘贴到浏览器中进行了测试)
- 温度传感器也在产生正确的读数
- 它正在返回“数据失败!” 在串行监视器中,表明这是代码中发生错误的位置
我不知道从 Arduino Uno 迁移到 ESP8266 是否会引起问题(即需要不同的库,TCP 命令不同等)
任何经验丰富的老手的帮助将不胜感激!
这是串行监视器输出和代码的片段(只是密码/api等)
22:16:50.266 -> **************
22:16:57.579 -> Wifi Connection Successful
22:16:57.579 -> The IP Address of the Sensor is:192.168.1.211
22:16:57.579 -> Humidity: 41.50
22:16:57.579 -> Temperature: 21.70
22:16:57.579 -> AT+CIPSTART="TCP","api.thingspeak.com",80
22:17:00.574 -> AT+CIPSEND=63
22:17:01.561 -> AT+CIPCLOSE
22:17:02.577 -> Data Fail!
22:17:02.577 -> GET /update?apikey=<REMOVED>&field1=21.70&field2=41.50
#include<stdlib.h>
#include "DHT.h"
#include <ESP8266WiFi.h>
#define SSID "<REMOVED>" //your network name
#define PASS "<REMOVED>" //your network password
#define API "<REMOVED>" //api string
#define IP "api.thingspeak.com" // thingspeak.com
#define DHTPIN 4 // what pin the DHT sensor is connected to
#define DHTTYPE DHT22 // Change to DHT22 if that's what you have
#define Baud_Rate 115200 //Another common value is 9600
#define DELAY_TIME 300000 //time in ms between posting data to ThingSpeak
//Can use a post also
String GET = String("GET /update?apikey=") + API + "&field1=";
String FIELD2 = "&field2=";
//if you want to add more fields this is how
//String FIELD3 = "&field3=";
bool updated;
DHT dht(DHTPIN, DHTTYPE);
//this runs once
void setup()
{
delay(5000);
Serial.begin(Baud_Rate);
// Connect to WIFI
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("*");
}
Serial.println("");
Serial.println("Wifi Connection Successful");
Serial.print("The IP Address of the Sensor is:");
Serial.println(WiFi.localIP()); //Print the IP Address
//initalize DHT sensor
dht.begin();
}
//this runs over and over
void loop() {
float h = dht.readHumidity();
Serial.print("Humidity: ");
Serial.println(h);
// Read temperature as Fahrenheit (isFahrenheit = true)
float c = dht.readTemperature();
Serial.print("Temperature: ");
Serial.println(c);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(c)) {
Serial.println("Reading DHT22 Failed, exiting");
return;
}
//update ThingSpeak channel with new values
updated = updateTemp(String(c), String(h));
//wait for delay time before attempting to post again
delay(DELAY_TIME);
}
bool updateTemp(String tempC, String humid) {
//initialize your AT command string
String cmd = "AT+CIPSTART=\"TCP\",\"";
//add IP address and port
cmd += IP;
cmd += "\",80";
//connect
Serial.println(cmd);
delay(2000);
if (Serial.find("Error")) {
return false;
}
//build GET command, ThingSpeak takes Post or Get commands for updates, I use a Get
cmd = GET;
cmd += tempC;
cmd += FIELD2;
cmd += humid;
cmd += "\r\n";
//continue to add data here if you have more fields such as a light sensor
//cmd += FIELD3;
//cmd += <field 3 value>
//Serial.println(cmd);
//Use AT commands to send data
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if (Serial.find(">")) {
//send through command to update values
Serial.print(cmd);
} else {
Serial.println("AT+CIPCLOSE");
}
if (Serial.find("OK")) {
//success! Your most recent values should be online.
Serial.println("Data Sent!");
return true;
} else {
Serial.println("Data Fail!");
Serial.println(cmd);
return false;
}
}
boolean connectWiFi() {
//set ESP8266 mode with AT commands
Serial.println("AT+CWMODE=1");
delay(2000);
//build connection command
String cmd = "AT+CWJAP=\"";
cmd += SSID;
cmd += "\",\"";
cmd += PASS;
cmd += "\"";
//connect to WiFi network and wait 5 seconds
Serial.println(cmd);
delay(5000);
//if connected return true, else false
if (Serial.find("OK")) {
Serial.println("WIFI connected");
return true;
} else {
Serial.println("WIFI not connected");
return false;
}
}