我正在从传感器收集数据,我想将它们保存到数据库中。所以我用Java API来保存数据库中的数据。我在邮递员中测试了该API,并且一切正常。 但是似乎我在部分arduino代码上存在一些问题,我在其中发送了POST请求。 有人可以帮我做那部分吗?
#include <SoftwareSerial.h>
#include "cactus_io_AM2302.h"
#include <BMP280.h>
#define AM2302_PIN 2
BMP280 bmp;
#define RX 10
#define TX 11
#define P0 1013.25
String AP = "xxxx"; // CHANGE ME
String PASS = "xxxxx"; // CHANGE ME
String API = "????"; // CHANGE ME
String HOST = "api.thingspeak.com";
String PORT = "80";
String Host = "192.168.0.87:8080" ;
String Url = "/e/api/environment_data/sendData" ;
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
SoftwareSerial esp8266(RX,TX);
AM2302 dht(AM2302_PIN);
double T = 0;
double P = 0;
char measure = 0;
float pritisak = 0;
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
//am2302(dht)
dht.begin();
//BMP280
if(!bmp.begin()) {
while(1);
}
bmp.setOversampling(4);
}
void loop() {
dht.readHumidity();
dht.readTemperature();
if (isnan(dht.humidity) || isnan(dht.temperature_C)) {
return;
}
float temperatura = dht.temperature_C;
float vlaznost = dht.humidity;
float kvalitet_zraka = analogRead(0);
pritisak = collectData();
Serial.println("Sending data: " + String(temperatura));
String urlfinal = "?temperature="+String(temperatura) + "&humidity=" + String(vlaznost) + "&airQuality=" + String(kvalitet_zraka) + "&pressure=" + String(pritisak);
String Post = "POST " + Url + " HTTP/1.1\r\n" +
"Host: " + Host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded" +
"Content-Length: " + urlfinal.length() + "\r\n" + urlfinal +
"\r\n" + ;
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ Host +"\","+ 8080,15,"OK");
sendCommand("AT+CIPSEND=0," + String(Post.length()+4),4,">");
esp8266.println(Post);
Serial.println(Post);
delay(30000);
countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}
void sendCommand(String command, int maxTime, char readReplay[]) {
while(countTimeCommand < (maxTime*1)) {
esp8266.println(command);
if(esp8266.find(readReplay)) {
found = true;
break;
}
countTimeCommand++;
}
if(found == true) {
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false) {
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
float collectData() {
measure = bmp.startMeasurment();
if(measure != 0) {
delay(measure);
measure = bmp.getTemperatureAndPressure(T, P);
if(measure != 0) {
P = P + 17;
T = T - 0.8;
return P;
delay(30000);
}
else
return 0;
}
else
return 0;
}
我在串行监视器中打印出Postln,这是它的样子:
POST /e/api/environment_data/sendData HTTP/1.1
Host: 192.168.0.27:8080
Content-Type: application/x-www-form-urlencodedContent-Length: 67
?temperature=27.60&humidity=59.70&airQuality=37.00&pressure=1002.39
我也检查了wifi模块的连接,也没关系,因为它可以将数据保存到Thingspeak,但是当我尝试将数据保存到localhost数据库时,则无法正常工作。
答案 0 :(得分:0)
几件事:
1:POST数据不是以“?”开头(获取)
2:标题和数据之间必须为空行
3:Content-Type之后必须换行
4:数据后没有新行
因此它将变为:
String urlfinal = "temperature="+String(temperatura) + "&humidity=" + String(vlaznost) + "&airQuality=" + String(kvalitet_zraka) + "&pressure=" + String(pritisak);
String Post = "POST " + Url + " HTTP/1.1\r\n" +
"Host: " + Host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n" +
"Content-Length: " + urlfinal.length() + "\r\n\r\n" + urlfinal";
我也将字符集添加到了Content-Type中,尽管这并不总是必需的。