0
:D嗨!我正處在一個不錯的項目中。關於鎮水道的水泵自動化。所以我選擇了Arduino來完成這項任務。我打算通過互聯網與Arduino交流Android應用程序。我已經開發了API和Web服務器部分,並且還開發了應用程序(現在我可以在應用程序中查看我的數據庫數據)。現在,我在Arduino-Ethernet屏蔽的一側。我測試了GET方法,它工作得很好,也是POST方法。但是,當我嘗試混合他們時,他們停止了工作。我在不同的教程和論壇上進行了一些(廣泛的)搜索,並閱讀「爲GET創建客戶端,爲PUT創建另一個客戶端」,並讓PUT方法再次運行(現在我可以看到我的webServer響應),但GET方法是。?!不工作:(你能不能給我說說如何將它們混合一些建議或任何想法,感謝 這是混合代碼:在同一個草圖中製作POST和GET
/*
Web client sketch for IDE v1.0.1 and w5100/w5200
Uses POST method.
Posted November 2012 by SurferTim
*/
#include <SPI.h>
#include <Ethernet2.h>
double lecturasensor1 =20.40;
double lecturasensor2 =20.60;
char var1[6];
char var2[6];
String a ;
String b ;
int id = 1;
byte mac[] = {
0x90, 0xA2, 0xDA, 0x10, 0x5B, 0x2B };
//Change to your server domain
char serverName[] = "asada-florencia.herokuapp.com";
// change to your server's port
int serverPort = 80;
// change to the page on that server
char pageName[] = "/lecturas";
EthernetClient clientGET;
EthernetClient clientPOST;
IPAddress ip(172, 24, 46, 94);
int totalCount = 0;
// insure params is big enough to hold your variables
char params[100];
// set this to the number of milliseconds delay
// this is 30 seconds
#define delayMillis 8000UL
unsigned long thisMillis = 0;
unsigned long lastMillis = 0;
void setup() {
Serial.begin(9600);
// disable SD SPI no vamos a usar SD
//pinMode(4,OUTPUT);
//digitalWrite(4,HIGH);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
}
void loop()
{
//Pirmero el GET (por prioridad)
if (clientGET.connect(serverName, 80)) {
Serial.println("connected");
// Make a HTTP request:
clientGET.println("GET /lecturas HTTP/1.1");
clientGET.println("Host: asada-florencia.herokuapp.com");
clientGET.println("Connection: close");
clientGET.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
if (clientGET.available()) {
char c = clientGET.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!clientGET.connected()) {
Serial.println();
Serial.println("disconnecting.");
clientGET.stop();
// do nothing forevermore:
while (true);
}
/*
* final GET
*
------------------------------------------
*
* Ahora el POST después de un tiempo
*/
delay(3000);
thisMillis = millis();
if(thisMillis - lastMillis > delayMillis)
{
lastMillis = thisMillis;
a = dtostrf(lecturasensor1, 4 , 4 , var1);
b = dtostrf(lecturasensor2, 4 , 4 , var2);
String jsonString = "{\"valor\" : ";
jsonString += a;
jsonString +=" , \"valor2\" : ";
jsonString += b;
jsonString +="\"}";
clientPOST.print(jsonString);
Serial.print(jsonString);
if(!postPage(serverName,serverPort,pageName,params)) Serial.print(F("Fail "));
else Serial.print(F("Pass "));
totalCount++;
Serial.println(totalCount,DEC);
}
}
byte postPage(char* domainBuffer,int thisPort,char* page,char* thisData)
{
int inChar;
char outBuf[64];
Serial.print(F("connecting..."));
if(clientPOST.connect(domainBuffer,thisPort) == 1)
{
Serial.println(F("connected"));
// send the header
sprintf(outBuf,"POST %s HTTP/1.1",page);
clientPOST.println(outBuf);
sprintf(outBuf,"Host: %s",domainBuffer);
clientPOST.println(outBuf);
clientPOST.println(F("Connection: close\r\nContent-Type: application/json"));
sprintf(outBuf,"Content-Length: %u\r\n",strlen(thisData)); //thisData solo toma en cuenta la medida del header por que el server lo necesita
clientPOST.println(outBuf);
// buscar cómo realizar el POST HTTP (sintaxis)-->thisData esta como puntero que contiene las variables a mostrar en el "body"
//esto que sigue es el envio de datos thisData
clientPOST.print(thisData);
Serial.println("Datos enviados");
Serial.println(thisData);
}
else
{
Serial.println(F("failed"));
return 0;
}
int connectLoop = 0;
while(clientPOST.connected())
{
while(clientPOST.available())
{
inChar = clientPOST.read();
Serial.write(inChar);
connectLoop = 0;
}
delay(1);
connectLoop++;
if(connectLoop > 10000)
{
Serial.println();
Serial.println(F("Timeout"));
clientPOST.stop();
}
}
Serial.println();
Serial.println(F("disconnecting."));
clientPOST.stop();
return 1;
}
'if(clientGET.connect(serverName,80))'。所以Arduino正在向某個網絡服務器發送GET或POST請求? Android在哪裏發揮作用?您嘗試在GET和POST中混用什麼?你談到它就好像你正在接受這些請求。請將所有信息添加到您的帖子中,而不是發表評論。 – greenapps