使用 POST 和 Arduino 通过 HTTP 发送数据
Sending data over HTTP using POST with Arduino
您好,我正在尝试使用带有 WiFi 屏蔽的 Arduino 将从一系列传感器收集的数据发送到服务器。
我的代码是
无效读取数据()
{
字符 dataBuff [300];
logfile.seek(pos);
pos = logfile.position();
while(logfile.available()){
while(logfile.position() < logfile.size()){
String dataBuff = logfile.readStringUntil('\n');
Serial.println(F("DataBuff is"));
Serial.println(dataBuff);
//Variables for WiFi Shield
char ssid[] = "xxx"; //your network SSID (name)
char pass[] = "xxx";
char server[] = "http://example.com";
int dataSize = sizeof(dataBuff);
while (status != WL_CONNECTED) {
Serial.print(F("Attempting to connect to SSID: "));
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 1 second for connection:
delay(1000);
}
Serial.println(F("Connected to wifi"));
// #endif
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(F("WiFi shield not present"));
// don't continue:
while (true);
}
int fileSize = 0;
fileSize = sizeof(dataBuff);
uint32_t fileLength = 0;
fileLength = 137 + fileSize + 40;
Serial.println (fileLength);
Serial.println(F("\nStarting connection to server..."));
Serial.println(server);
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println(F("connected to server"));
// Make a HTTP request:
client.print(F("POST /uploadf HTTP/1.1\r\n"));
client.print(F("Host: example.com\r\n"));
client.print(F("User-Agent: Frank/1.0\r\n"));
client.print(F("Accept-Encoding: gzip, deflate\r\n"));
client.print(F("Accept: */*\r\n"));
client.print(F("Connection: keep-alive\r\n"));
client.print(F("Content-Length: "));
client.print(fileLength);
client.print(F("\r\n"));
client.print(F("Content-Type: multipart/form-data; boundary=710ff0c6cf2d4c73b12db64cab12e58c\r\n"));
client.print(F("\r\n"));
client.print(F("--710ff0c6cf2d4c73b12db64cab12e58c\r\nContent-Disposition: form-data; name=\"file\"; filename=\""));
client.print(dataSent);
client.print(F("\""));
client.print(F("\r\n"));
client.print(F("Content-Type: text/plain\r\n\r\n"));
client.print(dataBuff);
client.print(F("\r\n--710ff0c6cf2d4c73b12db64cab12e58c--\r\n"));
}
while(client.connected()){
while (client.available()&& status==WL_CONNECTED) {
char c = client.read();
Serial.write(c);
}
}
client.stop();
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.print(F("\r\n"));
Serial.println(F("disconnecting from server."));
client.flush();
client.stop();
}
}
}
logfile.close();
while (true);
}
当我 运行 代码时,数据被正确读取,但在连接到服务器后,我得到以下信息
已连接到服务器
正在断开与服务器的连接。
解决这个问题:
对于遇到类似问题的任何人,附加代码允许您打开一个文件并一次读取一行数据并上传到服务器。
void readdata()
{
//Variables for WiFi Shield
char ssid[] = "example"; //your network SSID (name)
char pass[] = "example";
char server[] = "http://sleeppapi.pythonanywhere.com";
while (status != WL_CONNECTED) {
Serial.print(F("Attempting to connect to SSID: "));
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 1 second for connection:
delay(1000);
}
Serial.println(F("Connected to wifi"));
// #endif
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(F("WiFi shield not present"));
// don't continue:
while (true);
}
int fileLength = 0;
fileLength = 137 + filesize + 40;
Serial.println(filesize);
Serial.println(F("\nStarting connection to server..."));
Serial.println(server);
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println(F("connected to server"));
// Make a HTTP request:
client.print(F("POST /uploadf HTTP/1.1\r\n"));
client.print(F("Host: example.com\r\n"));
client.print(F("User-Agent: Frank/1.0\r\n"));
client.print(F("Accept-Encoding: gzip, deflate\r\n"));
client.print(F("Accept: */*\r\n"));
client.print(F("Connection: keep-alive\r\n"));
client.print(F("Content-Length: "));
client.print(fileLength);
client.print(F("\r\n"));
client.print(F("Content-Type: multipart/form-data; boundary=710ff0c6cf2d4c73b12db64cab12e58c\r\n"));
client.print(F("\r\n"));
client.print(F("--710ff0c6cf2d4c73b12db64cab12e58c\r\nContent-Disposition: form-data; name=\"file\"; filename=\""));
client.print(dataSent);
client.print(F("\""));
client.print(F("\r\n"));
client.print(F("Content-Type: text/plain\r\n\r\n"));
logfile=SD.open(nameFile);
while(logfile.available()){
while(logfile.position() < logfile.size()){
String dataBuff = logfile.readStringUntil('\n');
client.print(dataBuff);
client.print("\n");
}
}
client.print(F("\r\n--710ff0c6cf2d4c73b12db64cab12e58c--\r\n"));
//client.flush();
}
while(client.connected()){
while (client.available()&& status==WL_CONNECTED) {
char c = client.read();
Serial.write(c);
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.print(F("\r\n"));
Serial.println(F("disconnecting from server."));
client.stop();
}
logfile.close();
while (true);
}
您好,我正在尝试使用带有 WiFi 屏蔽的 Arduino 将从一系列传感器收集的数据发送到服务器。
我的代码是 无效读取数据() { 字符 dataBuff [300];
logfile.seek(pos);
pos = logfile.position();
while(logfile.available()){
while(logfile.position() < logfile.size()){
String dataBuff = logfile.readStringUntil('\n');
Serial.println(F("DataBuff is"));
Serial.println(dataBuff);
//Variables for WiFi Shield
char ssid[] = "xxx"; //your network SSID (name)
char pass[] = "xxx";
char server[] = "http://example.com";
int dataSize = sizeof(dataBuff);
while (status != WL_CONNECTED) {
Serial.print(F("Attempting to connect to SSID: "));
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 1 second for connection:
delay(1000);
}
Serial.println(F("Connected to wifi"));
// #endif
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(F("WiFi shield not present"));
// don't continue:
while (true);
}
int fileSize = 0;
fileSize = sizeof(dataBuff);
uint32_t fileLength = 0;
fileLength = 137 + fileSize + 40;
Serial.println (fileLength);
Serial.println(F("\nStarting connection to server..."));
Serial.println(server);
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println(F("connected to server"));
// Make a HTTP request:
client.print(F("POST /uploadf HTTP/1.1\r\n"));
client.print(F("Host: example.com\r\n"));
client.print(F("User-Agent: Frank/1.0\r\n"));
client.print(F("Accept-Encoding: gzip, deflate\r\n"));
client.print(F("Accept: */*\r\n"));
client.print(F("Connection: keep-alive\r\n"));
client.print(F("Content-Length: "));
client.print(fileLength);
client.print(F("\r\n"));
client.print(F("Content-Type: multipart/form-data; boundary=710ff0c6cf2d4c73b12db64cab12e58c\r\n"));
client.print(F("\r\n"));
client.print(F("--710ff0c6cf2d4c73b12db64cab12e58c\r\nContent-Disposition: form-data; name=\"file\"; filename=\""));
client.print(dataSent);
client.print(F("\""));
client.print(F("\r\n"));
client.print(F("Content-Type: text/plain\r\n\r\n"));
client.print(dataBuff);
client.print(F("\r\n--710ff0c6cf2d4c73b12db64cab12e58c--\r\n"));
}
while(client.connected()){
while (client.available()&& status==WL_CONNECTED) {
char c = client.read();
Serial.write(c);
}
}
client.stop();
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.print(F("\r\n"));
Serial.println(F("disconnecting from server."));
client.flush();
client.stop();
}
}
}
logfile.close();
while (true);
}
当我 运行 代码时,数据被正确读取,但在连接到服务器后,我得到以下信息
已连接到服务器
正在断开与服务器的连接。
解决这个问题: 对于遇到类似问题的任何人,附加代码允许您打开一个文件并一次读取一行数据并上传到服务器。
void readdata()
{
//Variables for WiFi Shield
char ssid[] = "example"; //your network SSID (name)
char pass[] = "example";
char server[] = "http://sleeppapi.pythonanywhere.com";
while (status != WL_CONNECTED) {
Serial.print(F("Attempting to connect to SSID: "));
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 1 second for connection:
delay(1000);
}
Serial.println(F("Connected to wifi"));
// #endif
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(F("WiFi shield not present"));
// don't continue:
while (true);
}
int fileLength = 0;
fileLength = 137 + filesize + 40;
Serial.println(filesize);
Serial.println(F("\nStarting connection to server..."));
Serial.println(server);
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println(F("connected to server"));
// Make a HTTP request:
client.print(F("POST /uploadf HTTP/1.1\r\n"));
client.print(F("Host: example.com\r\n"));
client.print(F("User-Agent: Frank/1.0\r\n"));
client.print(F("Accept-Encoding: gzip, deflate\r\n"));
client.print(F("Accept: */*\r\n"));
client.print(F("Connection: keep-alive\r\n"));
client.print(F("Content-Length: "));
client.print(fileLength);
client.print(F("\r\n"));
client.print(F("Content-Type: multipart/form-data; boundary=710ff0c6cf2d4c73b12db64cab12e58c\r\n"));
client.print(F("\r\n"));
client.print(F("--710ff0c6cf2d4c73b12db64cab12e58c\r\nContent-Disposition: form-data; name=\"file\"; filename=\""));
client.print(dataSent);
client.print(F("\""));
client.print(F("\r\n"));
client.print(F("Content-Type: text/plain\r\n\r\n"));
logfile=SD.open(nameFile);
while(logfile.available()){
while(logfile.position() < logfile.size()){
String dataBuff = logfile.readStringUntil('\n');
client.print(dataBuff);
client.print("\n");
}
}
client.print(F("\r\n--710ff0c6cf2d4c73b12db64cab12e58c--\r\n"));
//client.flush();
}
while(client.connected()){
while (client.available()&& status==WL_CONNECTED) {
char c = client.read();
Serial.write(c);
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.print(F("\r\n"));
Serial.println(F("disconnecting from server."));
client.stop();
}
logfile.close();
while (true);
}