-
-
Save modlfo/533a8bcd3815ae500478eecfcdf02b59 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 log_datadrop(int locked) { | |
// 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 +="&open="; | |
if(locked) | |
postStr += "False"; | |
else | |
postStr += "True"; | |
Serial.println("sending "+postStr); | |
client.println("GET /api/v1.0/Add?"+postStr+" HTTP/1.1"); | |
client.println("Host: datadrop.wolframcloud.com"); | |
client.println("Connection: close"); | |
client.println(""); | |
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(); | |
} | |
void log_thingspeak(int locked) { | |
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(locked); | |
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(); | |
} | |
void log(int locked) { | |
log_datadrop(locked); | |
log_thingspeak(locked); | |
} | |
int pre_locked; | |
int timeout; | |
void loop() { | |
int locked = digitalRead(SENSORPIN); | |
// Check if the status changed | |
if(pre_locked != locked) { | |
log(locked); | |
Serial.println(locked == HIGH ? "Locked" : "Unlocked"); | |
} | |
pre_locked = locked; | |
timeout = (timeout + 1) % 60; | |
if(timeout == 0) { | |
log(locked); | |
Serial.println(locked == HIGH ? "Locked" : "Unlocked"); | |
} | |
Serial.print("."); | |
// wait for 1 second | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment