Created
September 11, 2016 17:41
-
-
Save groupsky/4aa515efbe892635a28507fe38ae146d to your computer and use it in GitHub Desktop.
Lock sensor
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
#include <ESP8266WiFi.h> | |
#define SENSORPIN 4 // what pin we’re connected to | |
// replace with your channel’s thingspeak API key, | |
String tsApiKey = "ENTER API KEY HERE"; | |
String ddId = "ENTER ID HERE"; | |
const char* ssid = "hackafe.org"; | |
const char* password = ""; | |
const char* tsServer = "api.thingspeak.com"; | |
const char* ddServer = "datadrop.wolframcloud.com"; | |
WiFiClient client; | |
int val = 0; | |
int cnt; | |
String str; | |
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println(); | |
WiFi.begin(ssid, password); | |
Serial.print("Connecting"); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(); | |
Serial.print("Connected, IP address: "); | |
Serial.println(WiFi.localIP()); | |
pinMode(SENSORPIN, INPUT); | |
} | |
void loop() { | |
val = digitalRead(SENSORPIN); | |
Serial.println(val == HIGH ? "Locked" : "Unlocked"); | |
// datadrop | |
// Serial.print("Connecting to "); | |
// Serial.print(ddServer); | |
// Serial.print("..."); | |
// if (client.connect(ddServer,80)) { // datadrop.wolframcloud.com | |
// Serial.println("connected"); | |
// String postStr = "bin="+ddId; | |
// postStr +="&lock="; | |
// postStr += String(val); | |
// Serial.println("sending "+postStr); | |
// | |
// client.print("GET /api/v1.0/Add?"+postStr+" HTTP/1.1\n"); | |
// client.print("Host: datadrop.wolframcloud.com\n"); | |
// client.print("Connection: close\n"); | |
// client.print("\n"); | |
// | |
// cnt = 0; | |
// while (client.connected() && !client.available() && cnt < 5000) { | |
// delay(1); //waits for data | |
// cnt++; | |
// } | |
// Serial.print("< "); | |
// str = ""; | |
// while (client.available()) { | |
// char c = client.read(); | |
// str += c; | |
// } | |
// Serial.println(str); | |
// } | |
// client.stop(); | |
// thingspeak | |
Serial.print("Connecting to "); | |
Serial.print(tsServer); | |
Serial.print("..."); | |
if (client.connect(tsServer,80)) { // "184.106.153.149" or api.thingspeak.com | |
Serial.println("connected"); | |
String postStr = tsApiKey; | |
postStr +="&field1="; | |
postStr += String(val); | |
postStr += "\r\n\r\n"; | |
Serial.println("sending "+postStr); | |
client.print("POST /update HTTP/1.1\n"); | |
client.print("Host: api.thingspeak.com\n"); | |
client.print("Connection: close\n"); | |
client.print("X-THINGSPEAKAPIKEY: "+tsApiKey+"\n"); | |
client.print("Content-Type: application/x-www-form-urlencoded\n"); | |
client.print("Content-Length: "); | |
client.print(postStr.length()); | |
client.print("\n\n"); | |
client.print(postStr); | |
cnt = 0; | |
while (client.connected() && !client.available() && cnt < 10000) { | |
delay(1); //waits for data | |
cnt++; | |
} | |
Serial.print("< "); | |
str = ""; | |
while (client.available()) { | |
char c = client.read(); | |
str += c; | |
} | |
Serial.println(str); | |
} | |
client.stop(); | |
Serial.println("Waiting..."); | |
// wait for 60 secs until next report | |
delay(30000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an updated version that fixes wolfram data drop and logs when the status changes (in windows of 1 sec)
https://gist.github.com/modlfo/533a8bcd3815ae500478eecfcdf02b59