Skip to content

Instantly share code, notes, and snippets.

@Neurogami
Created September 29, 2015 04:38
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 Neurogami/598648dfb0f6372ad376 to your computer and use it in GitHub Desktop.
Save Neurogami/598648dfb0f6372ad376 to your computer and use it in GitHub Desktop.
Simple sketch for the ESP8266 using the Arduino IDE
/*
This sketch sends data via HTTP GET requests to an IP address.
*/
#include <ESP8266WiFi.h>
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* host = "192.168.0.99"; // The IP address of the Web page you will hit
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
const char* ver = "0.3.0";
void loop() {
delay(1000);
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 8090;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/esp?ver=";
url += ver;
url += "&millis=";
url += millis();
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment