在相同的草图中进行POST和GET

时间:2016-05-18 21:55:01

标签: android arduino

:D嗨!我正处于一个不错的项目中。关于城镇渡槽水泵的自动化。所以我选择了Arduino来完成任务。我打算通过互联网连接与Arduino交流Android应用程序。我已经创建了API和Web服务器部分,还有de App(现在应用程序中我可以看到我的数据库数据)。现在,我正在使用Arduino-Ethernet屏蔽。我测试了GET方法,它运行得很好,也是POST方法。但是,当我尝试混合它们时,它们就停止了工作。我在不同的教程和论坛上进行了一些(广泛的)搜索,并阅读“为GUT创建一个客户端,为PUT创建另一个客户端”,并让PUT方法再次工作(现在我可以看到我的webServer响应),但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;
}

1 个答案:

答案 0 :(得分:0)

解决它! :D全部在GET标题中,只是对标题的每个部分使用了clientPOST.println()。谢谢!!

&#13;
&#13;
// Header del GET
    clientPOST.println("GET /yourendpoint HTTP/1.1");
    clientPOST.println("Host: yourhost.com");
    clientPOST.println("Accept: text/html,application/xhtml+xml,application/xml");
    clientPOST.println("Upgrade-Insecure-Requests: 1");
    clientPOST.println("User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");
    clientPOST.println("Accept-Encoding: gzip, deflate, sdch");
    clientPOST.println("Accept-Language: en-US,en");
    clientPOST.println();
&#13;
&#13;
&#13;

谢谢!