Skip to content

Instantly share code, notes, and snippets.

@alexstyl
Created August 29, 2015 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexstyl/e0ae3c9c3081762d919b to your computer and use it in GitHub Desktop.
Save alexstyl/e0ae3c9c3081762d919b to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "---"; // your network SSID (name)
char pass[] = "---"; // your network password
char server[] = "www.google.com";
//IPAddress server(64,131,82,241);
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
// Initialize the Wifi client library
WiFiClient client;
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 500L; // changed the day to 500ms
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial); //wait for serial
// 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);
if (status != WL_CONNECTED) {
// wait 5 seconds for connection, if we couldn't connect
delay(5000);
}
}
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
while (client.available()) {
char c = client.read();
Serial.write(c);
}
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop(); // <--- If removed,
// if there's a successful connection:
if (client.connect(server, port)) {
Serial.println("Sending...");
// send the HTTP PUT request:
client.println("GET /latest.txt HTTP/1.1");
client.println("Host: www.arduino.cc");
client.println("User-Agent: ArduinoWiFi/1.1");
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("connection failed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment