Last active
January 5, 2017 04:08
-
-
Save JasonPellerin/318c37aabd5ea8bc4e77 to your computer and use it in GitHub Desktop.
ESP8266 TwitWire
This file contains 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
/* | |
* This sketch sends data via HTTP GET requests to api.thingspeak.com. | |
* | |
* You need to get api_Key at api.thingspeak.com and paste it | |
* below. | |
* | |
*/ | |
#include <ESP8266WiFi.h> | |
// WiFi Creds | |
const char* ssid = "YourSSID"; | |
const char* password = "YourPASSWD"; | |
// ThingSpeak Creds | |
const char* host = "api.thingspeak.com"; | |
const char* api_Key = "YourAPI_KEY"; // Your api_key obtained from ThingSpeak | |
const char* status = "ESP8266 and thingspeak FTW!"; | |
//////////////////////////////////////////////////////////////////// | |
const int BUTTON = 0;/////// the input pin where the/////////////// | |
const int LED = 2; /// the pin for the LED ///////////////// | |
////////////////////////// pushbutton is connected/////////////// | |
int val = 0; /// val will be used to store the state/// | |
//////////////////////// of the input pin////////////////////// | |
int old_val = 0; /// stores the previous value of "val"//// | |
int state = 0;//////// 0 = LED off while 1 = LED on////////// | |
unsigned long startTime = 0; // when did we begin pressing?/ | |
/////////////////////////////////////////////////////////// | |
void setup() { | |
// Set BUTTON(GPIO0) as an input | |
pinMode(BUTTON, INPUT); | |
// prepare GPIO2 as LED | |
pinMode(LED, OUTPUT); | |
digitalWrite(LED, 0); | |
Serial.begin(115200); | |
delay(10); | |
// 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("Aquired Teh WiFiz, time to Hack Planet!!"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
int value = 0; | |
void loop() { | |
val = digitalRead(BUTTON); // read input value and store it | |
// check if there was a state transition | |
if ((val == HIGH) && (old_val == LOW)){ | |
state = 1 - state; | |
delay(10); | |
} | |
old_val = val; // val is now old, let's store it | |
if (state == 1) { // Sensor has changed the state, time to make | |
// a GET request to the ThingSpeak API, | |
// which will POST a status update (Tweet) | |
//to your Twitter TimeLine. | |
blink_1(); | |
blink2(); | |
blink2(); | |
blink2(); | |
Serial.print("connecting to "); | |
Serial.println(host); | |
// Use WiFiClient class to create TCP connections | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(host, httpPort)) { | |
Serial.println("connection failed"); | |
delay(5000); | |
return; | |
} | |
// We now create a URI for the request | |
String url = "/apps/thinghttp/"; | |
url += "send_request?api_key="; | |
url += api_Key; | |
url += "&status="; | |
url += status; | |
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(5000); | |
// 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"); | |
state = 0; | |
} else { | |
allGood(); | |
} | |
deBug(); | |
delay(1000); | |
} | |
void blink_1() { | |
digitalWrite(LED, HIGH); // turn LED ON | |
delay(1000); | |
digitalWrite(LED, LOW); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
delay(100); | |
digitalWrite(LED, LOW); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
delay(100); | |
digitalWrite(LED, LOW); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
delay(100); | |
digitalWrite(LED, LOW); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
delay(100); | |
digitalWrite(LED, LOW); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
delay(100); | |
digitalWrite(LED, LOW); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
delay(100); | |
digitalWrite(LED, LOW); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
} | |
void blink2() { | |
delay(100); | |
digitalWrite(LED, LOW); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
delay(100); | |
digitalWrite(LED, LOW); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
delay(100); | |
digitalWrite(LED, LOW); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
delay(100); | |
digitalWrite(LED, LOW); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
delay(100); | |
digitalWrite(LED, LOW); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
delay(100); | |
digitalWrite(LED, LOW); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
} | |
void allGood() { | |
digitalWrite(LED, LOW); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println("All Is Good In Teh H00d!!!"); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
} | |
void deBug() { | |
Serial.print("VALUE IS: "); | |
Serial.println(val); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.print("OLD VALUE IS: "); | |
Serial.println(old_val); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.print("STATE IS: "); | |
Serial.println(state); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println(""); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment