|
本帖最后由 gakki123 于 2021-4-11 14:31 编辑
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <ESP8266.h>
#include <dht11.h>
#define SSID "6666"
#define PASSWORD "1234567890"
#define HOST_NAME "api.heclouds.com"
#define DEVICE_ID "655613493"
#define HOST_PORT (80)
String APIKey = "oIbLqvflv2HqrRKqEtXwSd1n4GI=";
#define INTERVAL_SENSOR 5000
dht11 DHT11;
#define DHT11PIN 4
SoftwareSerial mySerial(15, 14);
ESP8266 wifi(mySerial);
void setup()
{
mySerial.begin(9600);
Serial.begin(9600);
Serial.print("setup begin\r\n");
Serial.print("FW Version: ");
Serial.println(wifi.getVersion().c_str());
if (wifi.setOprToStation()) {
Serial.print("to station ok\r\n");
} else {
Serial.print("to station err\r\n");
}
if (wifi.joinAP(SSID, PASSWORD)) {
Serial.print("Join AP success\r\n");
Serial.print("IP: ");
Serial.println(wifi.getLocalIP().c_str());
} else {
Serial.print("Join AP failure\r\n");
}
Serial.println("");
Serial.print("DHT11 LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
mySerial.println("AT+UART_CUR=9600,8,1,0,0");
mySerial.begin(9600);
Serial.println("setup end\r\n");
}
unsigned long net_time1 = millis();
void loop(){
if (net_time1 > millis())
net_time1 = millis();
if (millis() - net_time1 > INTERVAL_SENSOR)
{
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk) {
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
float sensor_hum = (float)DHT11.humidity;
float sensor_tem = (float)DHT11.temperature;
Serial.print("Humidity (%): ");
Serial.println(sensor_hum, 2);
Serial.print("Temperature (oC): ");
Serial.println(sensor_tem, 2);
Serial.println("");
if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
Serial.print("create tcp ok\r\n");
char buf[10];
String jsonToSend = "{\"Temperature\":";
dtostrf(sensor_tem, 1, 2, buf);
jsonToSend += "\"" + String(buf) + "\"";
jsonToSend += ",\"Humidity\":";
dtostrf(sensor_hum, 1, 2, buf);
jsonToSend += "\"" + String(buf) + "\"";
jsonToSend += "}";
String postString = "POST /devices/";
postString += DEVICE_ID;
postString += "/datapoints?type=3 HTTP/1.1";
postString += "\r\n";
postString += "api-key:";
postString += APIKey;
postString += "\r\n";
postString += "Host:api.heclouds.com\r\n";
postString += "Connection:close\r\n";
postString += "Content-Length:";
postString += jsonToSend.length();
postString += "\r\n";
postString += "\r\n";
postString += jsonToSend;
postString += "\r\n";
postString += "\r\n";
postString += "\r\n";
const char *postArray = postString.c_str();
Serial.println(postArray);
wifi.send((const uint8_t *)postArray, strlen(postArray));
Serial.println("send success");
if (wifi.releaseTCP()) {
Serial.print("release tcp ok\r\n");
} else {
Serial.print("release tcp err\r\n");
}
postArray = NULL;
} else {
Serial.print("create tcp err\r\n");
}
Serial.println("");
net_time1 = millis();
}
}
错误信息:ResolveLibrary(queue.h)In file included from D:\Documents\Arduino\libraries\ESP8266WiFi\src/ESP8266WiFi.h:33:0,
from D:\Documents\Arduino\testonenet\testonenet.ino:2:
-> candidates: []D:\Documents\Arduino\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:26:10: fatal error: queue.h: No such file or directory
#include <queue.h>
^~~~~~~~~
compilation terminated.
"SoftwareSerial.h" 对应多个库
已使用: D:\Arduino\hardware\arduino\avr\libraries\SoftwareSerial
未使用:D:\Documents\Arduino\libraries\SoftwareSerial
使用 1.0 版本的库 SoftwareSerial 在文件夹: D:\Arduino\hardware\arduino\avr\libraries\SoftwareSerial
使用 1.0 版本的库 ESP8266WiFi 在文件夹: D:\Documents\Arduino\libraries\ESP8266WiFi
exit status 1
为开发板 Arduino Mega or Mega 2560 编译时出错。
|
|