How to make an HTTP GET request for a web service with Arduino

Asked

Viewed 5,292 times

4

With a request via GET to a web service with Arduino, using the following URL and passing a parameter http://192.168. 0.1:8080/automation/Sensor? value=###, where ### is variable and is updated every 30 seconds with the help of a card Shield.

#include <SPI.h>
#include <Ethernet.h>

// this must be unique
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// change to your network settings
IPAddress ip(192,168,2,2);
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);

// change to your server
IPAddress server(74,125,227,16); // Google

//Change to your domain name for virtual servers
char serverName[] = "www.meuservidor.com.br";
// If no domain name, use the ip address above
// char serverName[] = "74.125.227.16";

// change to your server's port
int serverPort = 80;

EthernetClient client;
int totalCount = 0;
char pageAdd[64];

// set this to the number of milliseconds delay
// this is 30 seconds
#define delayMillis 30000UL

unsigned long thisMillis = 0;
unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // Start ethernet
  Serial.println(F("Starting ethernet..."));
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  // If using dhcp, comment out the line above 
  // and uncomment the next 2 lines plus the Ethernet.maintain call in loop

  // if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  // else Serial.println(F("ok"));

  Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println(F("Ready"));
}

void loop()
{
  // If using dhcp to get an IP, uncomment the next line
  // Ethernet.maintain();

  thisMillis = millis();

  if(thisMillis - lastMillis > delayMillis)
  {
    lastMillis = thisMillis;

    // Modify next line to load different page
    // or pass values to server
    sprintf(pageAdd,"/",totalCount);

    // sprintf(pageAdd,"/arduino.php?test=%u",totalCount);

    if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
    totalCount++;
    Serial.println(totalCount,DEC);
  }    
}

byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
  int inChar;
  char outBuf[128];

  Serial.print(F("connecting..."));

  if(client.connect(ipBuf,thisPort) == 1)
  {
    Serial.println(F("connected"));

    sprintf(outBuf,"GET %s HTTP/1.1",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",serverName);
    client.println(outBuf);
    client.println(F("Connection: close\r\n"));
  } 
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

  // connectLoop controls the hardware fail timeout
  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000)
    {
      // then close the connection from this end.
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println();

  Serial.println(F("disconnecting."));
  // close client end
  client.stop();

  return 1;
}
  • c++? Are you sure?

  • You have already made the connection of Arduino to the network via Ethernet?

  • Yes I already made the connection using the Ethernet Shield board.

  • 1

    Post the code you’ve already done. It’s important to know which Ethernet library you’re using, among other things. To be more complete: When I asked if you have already connected via Ethernet is, beyond the card and network physically, if you already have code that communicates... ie, your Arduino already has a MAC/ IP on the network?

  • I posted the code, my doubt is in relation to sending parameters to the web service through HTTP GET or POST requests, I even managed to make the request but the parameter is not sent.

  • @Brunorichart, your code is already well advanced. What problem are you facing?

  • I am unable to send the parameters with the value of the sensors to the web service.

  • Good afternoon, don’t duplicate questions unnecessarily. I recommend reading Help: http://answall.com/help - (http://answall.com/q/101531/3635)

  • @Brunorichart, which components you used to make HTTP?

Show 4 more comments

1 answer

3


Try playing the example described here: https://www.arduino.cc/en/Tutorial/WebClient

However, replace the following code snippet for your reality:

  if (client.connect(server, 80)) {
    Serial.println("connected");
    //Faz uma requisição HTTP
    client.println("GET /automacao/Sensor?valor=10 HTTP/1.1");
    client.println("Host: 192.168.0.1");
    client.println("Connection: close");
    client.println();
  } else {
    //Caso não seja possível obter uma conexao
    Serial.println("connection failed");
  }

Note that an HTTP request is being mounted for the server specified in the variable server and in the HTTP header (GET), the URL to which the request should be made. The value of the sensor is set to 10 (remember, it’s just an example).

I believe that this example is very simple and with it you will know if everything works for the so-called Web Service, in other words, it is a simpler code with the aim of isolating other parts.

  • 1

    Perfect @Cantoni, it worked thank you so much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.