Created
July 24, 2014 10:38
-
-
Save itsananderson/d8483fce033ad4a9624f to your computer and use it in GitHub Desktop.
Arduino client to send requests in a loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Repeating Web client | |
This sketch connects to a a web server and makes a request | |
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or | |
the Adafruit Ethernet shield, either one will work, as long as it's got | |
a Wiznet Ethernet module on board. | |
This example uses DNS, by assigning the Ethernet client with a MAC address, | |
IP address, and DNS address. | |
Circuit: | |
* Ethernet shield attached to pins 10, 11, 12, 13 | |
created 19 Apr 2012 | |
by Tom Igoe | |
http://arduino.cc/en/Tutorial/WebClientRepeating | |
This code is in the public domain. | |
*/ | |
#include <SPI.h> | |
#include <Ethernet.h> | |
// assign a MAC address for the ethernet controller. | |
// fill in your address here: | |
byte mac[] = { | |
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; | |
// fill in an available IP address on your network here, | |
// for manual configuration: | |
IPAddress ip(10,0,0,90); | |
// fill in your Domain Name Server address here: | |
IPAddress myDns(8,8,4,4); | |
// initialize the library instance: | |
EthernetClient client; | |
char server[] = "adal-token.azurewebsites.net"; | |
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds | |
boolean lastConnected = false; // state of the connection last time through the main loop | |
const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds | |
char lastChar; | |
boolean sendChars = false; | |
void setup() { | |
// start serial port: | |
Serial.begin(9600); | |
// give the ethernet module time to boot up: | |
delay(1000); | |
// start the Ethernet connection using a fixed IP address and DNS server: | |
Ethernet.begin(mac); | |
// print the Ethernet board/shield's IP address: | |
Serial.print("My IP address: "); | |
Serial.println(Ethernet.localIP()); | |
httpRequest(); | |
} | |
void loop() { | |
// if there's incoming data from the net connection. | |
// send it out the serial port. This is for debugging | |
// purposes only: | |
if (client.available()) { | |
char c = client.read(); | |
if (sendChars) { | |
Serial.print(c); | |
} | |
if (c == '\n' && lastChar == '\n') { | |
sendChars = true; | |
} | |
if (c != '\r') { | |
lastChar = c; | |
} | |
} | |
// if you're not connected, and ten seconds have passed since | |
// your last connection, then connect again and send data: | |
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { | |
httpRequest(); | |
} else { | |
//Serial.println(millis() - lastConnectionTime); | |
} | |
// store the state of the connection for next time through | |
// the loop: | |
lastConnected = client.connected(); | |
} | |
// this method makes a HTTP connection to the server: | |
void httpRequest() { | |
Serial.println(); | |
lastChar = '\0'; | |
sendChars = false; | |
// if there's a successful connection: | |
if (!client.connected()) { | |
client.stop(); | |
if (client.connect(server, 80)) { | |
// send the HTTP PUT request: | |
client.println("GET / HTTP/1.1"); | |
client.println("Host: adal-token.azurewebsites.net"); | |
client.println("User-Agent: arduino-ethernet"); | |
client.println("Connection: close"); | |
client.println(); | |
// note the time that the connection was made: | |
lastConnectionTime = millis(); | |
} | |
else { | |
// if you couldn't make a connection: | |
Serial.println("stopping"); | |
client.stop(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment