Skip to content

Instantly share code, notes, and snippets.

@IdrisCytron
Created June 14, 2017 03:18
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 IdrisCytron/c7284a70f48ad770c07b1b469a980ceb to your computer and use it in GitHub Desktop.
Save IdrisCytron/c7284a70f48ad770c07b1b469a980ceb to your computer and use it in GitHub Desktop.
This code is written by Gayathri Gunasekaran from #mydigitalmaker Holiday Programme 2017
#include <ESPert.h>
ESPert espert;
const char *host = "www.espert.io";
String smartphone_key = "YourPhoneKey";
const char *ssid = "YourWiFiSSID";
const char *password = "YourWiFiPassword";
const int led1 = 2;
const int ir = 14;
const int switch1 = 12;
int value = 0;
int x = 0;
int clearLCD = 0;
int prev_state = 0;
int calibrationTime = 10;
bool currentSwitch = true;
void setup()
{
WiFi.begin(ssid,password);
pinMode(ir, INPUT_PULLUP);
pinMode(switch1, INPUT_PULLUP);
pinMode(led1, OUTPUT);
espert.init();
espert.oled.init();
delay(2000);
espert.oled.clear();
espert.oled.println(espert.info.getId());
espert.oled.println();
int mode = espert.wifi.init();
if (mode == ESPERT_WIFI_MODE_CONNECT) {
espert.println(">>> WiFi mode: connected.");
espert.oled.println("WiFi: connected.");
espert.oled.print("IP..: ");
espert.oled.println(espert.wifi.getLocalIP());
}
else if (mode == ESPERT_WIFI_MODE_DISCONNECT) {
espert.println(">>> WiFi mode: disconnected.");
espert.oled.println("WiFi: not connected.");
}
else if (mode == ESPERT_WIFI_MODE_SMARTCONFIG) {
espert.println(">>> WiFi mode: smart config.");
}
else if (mode == ESPERT_WIFI_MODE_SETTINGAP) {
espert.println(">>> WiFi mode: access point.");
}
}
void loop()
{
espert.loop();
int state = digitalRead(switch1);
value = analogRead(ir);
Serial.println(value);
if (prev_state != state){clearLCD = 0;}
if (state == HIGH) {
if (x == 0) {
x = 1;
clearLCD = 0;
espert.oled.clear();
delay(2000);
espert.oled.println("calibrating sensor... ");
delay(10000);
espert.oled.println(" done");
espert.oled.println("SENSOR ACTIVE");
delay(2000);
}
if (value < 1023) {
value = analogRead(ir);
Serial.println(value);
clearLCD = 0;
String message = "INTRUDER!" ;
message.replace(String(" "), String("%20"));
String path = "/MySmartphone/send?key=" + smartphone_key + "&message=" + message;
espert.println(">>" + espert.wifi.getHTTP(host, path.c_str()) + "<<");
espert.oled.println("INTRUDER!");
digitalWrite(led1, HIGH);
delay (2000);
}
else {
if (clearLCD == 0) {
digitalWrite(led1, LOW);
espert.oled.clear();
espert.oled.println("Nobody");
clearLCD = 1;
}
}
}
else {
if (value > 1023) {
if (clearLCD == 0) {
digitalWrite(led1, LOW);
espert.oled.clear();
espert.oled.println("OFF");
clearLCD = 1;
}
}
else {
clearLCD = 0;
}
}
prev_state = state;
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment