HTTP request at the push of a button

Hi!

I have no trouble making a http GET request to a php file on my server, using the sample code from arduino.cc.

But I would like to send a request when firing an event, like when pushing a button or when receiving serial data, and this is where i'm stuck. I want to be able to send multiple requests, not just a single one.

Does anyone have some sample code for this, or would be kind to push me in the right direction? =)

Thanks!

I have no trouble making a http GET request to a php file on my server, using the sample code from arduino.cc.

Then, make the request only inside an if block.

if(digitalRead(somePin) == LOW) // Assumes pullup resistor
{
   // Make a GET request
}

Yeah, but the problem is I don't really know what makes the client connect. When I run the sample code the client disconnects right after the first request, and I cant make a second one..

Yeah, but the problem is I don't really know what makes the client connect.

That's a real problem, then.

You seem to have a fundamental misunderstanding of what the Arduino is doing.

It is either a server, that clients, like web browsers, connect to to get a web page (containing useful information) or it is a client that connects to a server and asks for information.

If your Arduino is a server, making it serve data only when a switch is pressed doesn't make any sense.

If the Arduino is a client, making it send a request only when a switch is pressed makes sense.

What is your Arduino? A client or a server?

It is impossible to tell from the code you posted.

Sorry for being unclear =)

The Arduino acts as a client and makes a http GET request to my server. What can't seem to understand is how I can put the http request into a function, that I can call whenever I want and as many times as I want..

Thanks for your answers!

What can't seem to understand is how I can put the http request into a function, that I can call whenever I want and as many times as I want..

Without seeing your code, we can't help you with that.

Simple GET test code you can try. The example code in the IDE has a line that throws the arduino into an infinate loop after the first GET request, probably to ensure a runaway loop making GET request is not created.

//zoomkat 4-04-12
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 
//the arduino lan IP address { 192, 168, 1, 102 } may need 
//to be modified modified to work with your router.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan assigned to arduino
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, gateway, gateway, subnet);
  Serial.begin(9600); 
  Serial.println("Better client test 4/04/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

I’m doing something similar and I also need advice on getting the Arduino code to work. I only want the HTTP Call to occur when the switch is closed (buttonState == HIGH). I have this Arduino code currently:

void setup() {
Connect();
startup();
}
void loop() {
loopConnect();
}
#include <SPI.h>
#include <WiFi.h>

char ssid = “Verizon-SM-G935V-5E4D”; // your network SSID (name)
char pass = “TaintedLove”; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
// if you don’t want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(192,168,43,236); // numeric IP for Google (no DNS)
//char server = “https://.com”; // name address for Google (using DNS)

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;
const int buttonPin = 4;
int buttonState = 0;

void startup() {
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(“WiFi shield not present”);
// don’t continue:
while(true);
}

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}
Serial.println(“Connected to wifi”);
printWifiStatus();

}

void Connect() {
//Initialize serial and wait for port to open:
Serial.begin(9600);

Serial.println("\nStarting connection to server…");
// if you get a connection, report back via serial:
if (client.connect(server, 3000)) {
Serial.println(“connected to server”);
// Make a HTTP request:
client.println(“POST /Page1 HTTP/1.1”);
//client.println("Host: ");
client.println(“Connection: close”);
client.println();
}
}

void loopConnect() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
Connect();
}

// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
}

// if the server’s disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println(“disconnecting from server.”);
client.stop();

// do nothing forevermore:
while(true);
}
}

void printWifiStatus() {
// print the SSID of the network you’re attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.print(rssi);
Serial.println(" dBm");
}

I only want the HTTP Call to occur when the switch is closed

Then, wire your switch correctly. In the absence of any details about how the switch is wired, I can only assume "incorrectly".

The code does something. You failed to explain what it does, or how that differs from what you want (or how the switch is wired).

PaulS:
Then, wire your switch correctly. In the absence of any details about how the switch is wired, I can only assume “incorrectly”.

The code does something. You failed to explain what it does, or how that differs from what you want (or how the switch is wired).

I apologize for the missing information. When the current sketch is uploaded it connects to internet and connects to the server. It does nothing when the tilt sensor is closed. It does everything inside of the Connect function except the HTTP Call. However, the parts of Connect that run do so regardless of whether the switch is closed or not. (See attached file for Serial output)

What do you mean by “wire your switch correctly”?
I am currently using a tilt switch that has been soldered to some wires. I have a LED on the board just to make sure that the tilt sensor is oriented correctly when it needs to be closed. More importantly, I have a jumper wire that is setup and connected to Digital input 4 so that the state of the tilt sensor is monitored.

Serial.println("\nStarting connection to server...");
 // if you get a connection, report back via serial:
 if (client.connect(server, 3000)) {
   Serial.println("connected to server");

Your picture of text (why?) shows the Starting message but not the connected, so you are not actually connecting to the server. Doesn’t that matter?

void loop() {
 loopConnect();
}

It makes no sense to put all the code in one function that loop() then has to call.

How do you know that the switch is being read correctly? If the pin that the switch is connected to is not reading HIGH, then Connect() will not be called again (after the initial call in setup()). The picture of the text does not indicate that Connect() got called again, so I infer that the pin is not reading HIGH, from which I infer that the switch is not wired correctly.

You have not described completely how the switch is wired, so I’m still assuming “incorrectly” best describes how it is wired.

The switch is a simple tilt switch (https://www.adafruit.com/product/173) that has a wire soldered to each end (https://learn.adafruit.com/assets/497). Essential I wish to run code that is checking for the tilt switch's change in orientation (state change) (Example code for state change on a tilt switch https://learn.adafruit.com/tilt-sensor/using-a-tilt-sensor). In the event of that change, I would like the code to send an HTTP post as shown in my previous code.

Essential I wish to run code that is checking for the tilt switch's change in orientation (state change)

Feel free, then, to do just that. Start with a dirt simple sketch, based on the state change detection example, to determine if you have wired the switch correctly, and can determine that it is indeed changing state.

Saying that the switch has two wires soldered to it, and posting a picture of a switch with two wires soldered to it, is useless information. We figured you hadn't just duct taped the switch to the Arduino. But, you have NOT said how the switch IS connected. So, I'm STILL going with "incorrectly". If you ever get around to showing a schematic, or even a photo, then perhaps I'll be willing to concede that the switch is indeed wired correctly. Or, be able to state exactly why the switch is not wired correctly.

I ran the example code from https://learn.adafruit.com/tilt-sensor/using-a-tilt-sensor and it works fine with the setup that I have. My switch is wired correctly. I'm more concerned with why the code for sending an HTTP POST request doesn't work when called inside the if statement. I can get both the wifi connection with HTTP POST and state change code to work separately. But not together with one another.

are you using an ethernet shield to connect to the internet?

But not together with one another.

Add some proof that the switch is being read correctly (Serial.println("Ouch! Your hands are cold!");) in the block where you think the statement belongs. Post the code and the serial output.