Created
July 22, 2018 00:45
-
-
Save debsahu/bae29c78442a82fd4ae2de2296a1783c to your computer and use it in GitHub Desktop.
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 <SparkFun_APDS9960.h> // https://github.com/SteveQuinn1/SparkFun_APDS-9960_Sensor_Arduino_Library | |
#include <SSD1306.h> // https://github.com/ThingPulse/esp8266-oled-ssd1306 | |
#include <ESP8266WiFi.h> | |
#include <WiFiClientSecure.h> | |
#include <HARestAPI.h> // https://github.com/debsahu/HARestAPI | |
// Pins | |
#define SDA_PIN D2 | |
#define SCL_PIN D1 | |
#define APDS9960_INT D3 | |
WiFiClientSecure sclient; | |
HARestAPI ha(sclient); | |
// Constants | |
// Global Variables | |
SparkFun_APDS9960 apds = SparkFun_APDS9960(); | |
volatile bool isr_flag = false; | |
bool _debug = false; | |
SSD1306 display(0x3c, SDA_PIN, SCL_PIN); | |
#ifndef SECRET | |
const char* ssid = "WiFi SSID"; | |
const char* password = "WiFi Password"; | |
const char* ha_ip = "192.168.0.xxx"; | |
uint16_t ha_port = 8123; | |
const char* ha_pwd = "HA_PASSWORD"; | |
String fingerprint = "35 85 74 EF 67 35 A7 CE 40 69 50 F3 C0 F6 80 CF 80 3B 2E 19"; | |
#endif | |
void displayInit() { | |
display.init(); | |
display.flipScreenVertically(); | |
display.setFont(ArialMT_Plain_24); | |
} | |
void displayInt(int dispInt, int x, int y) { | |
display.setTextAlignment(TEXT_ALIGN_CENTER); | |
display.drawString(x, y, String(dispInt)); | |
display.setFont(ArialMT_Plain_24); | |
display.display(); | |
} | |
void displayString(String dispString, int x, int y) { | |
display.setTextAlignment(TEXT_ALIGN_CENTER); | |
display.drawString(x, y, dispString); | |
display.setFont(ArialMT_Plain_24); | |
display.display(); | |
} | |
void setup() { | |
//pinMode(SCL_PIN, INPUT_PULLUP); | |
//pinMode(SDA_PIN, INPUT_PULLUP); | |
Wire.begin(SDA_PIN, SCL_PIN); | |
// Set interrupt pin as input | |
pinMode(APDS9960_INT, INPUT); | |
// Initialize Serial port | |
Serial.begin(115200); | |
displayInit(); | |
displayString("Welcome", 64, 15); | |
delay(500); | |
WiFi.begin(ssid, password); | |
Serial.print(F("\nConnecting to ")); | |
Serial.print(ssid); | |
display.clear(); | |
displayString("Connecting\nWiFi", 64, 5); | |
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } | |
Serial.println(F("Connected!")); | |
Serial.print(F("IP address: ")); | |
Serial.println(WiFi.localIP()); | |
display.clear(); | |
displayString("Connected", 64, 15); | |
delay(500); | |
ha.setHAServer(ha_ip, ha_port); | |
ha.setHAPassword(ha_pwd); | |
ha.setFingerPrint(fingerprint); | |
display.clear(); | |
displayString("Starting\nSensor", 64, 5); | |
Serial.println(); | |
Serial.println(F("--------------------------------")); | |
Serial.println(F("SparkFun APDS-9960 - GestureTest")); | |
Serial.println(F("--------------------------------")); | |
// Initialize interrupt service routine | |
attachInterrupt(APDS9960_INT, interruptRoutine, FALLING); | |
// Initialize APDS-9960 (configure I2C and initial values) | |
if ( apds.init() ) { | |
Serial.println(F("APDS-9960 initialization complete")); | |
} else { | |
Serial.println(F("Something went wrong during APDS-9960 init!")); | |
} | |
// Start running the APDS-9960 gesture sensor engine | |
if ( apds.enableGestureSensor(true) ) { | |
Serial.println(F("Gesture sensor is now running")); | |
} else { | |
Serial.println(F("Something went wrong during gesture sensor init!")); | |
} | |
display.clear(); | |
displayString("Started\nSensor", 64, 5); | |
} | |
void loop() { | |
if( isr_flag == 1 ) { | |
detachInterrupt(APDS9960_INT); | |
handleGesture(); | |
isr_flag = 0; | |
attachInterrupt(APDS9960_INT, interruptRoutine, FALLING); | |
} | |
} | |
void interruptRoutine() { | |
isr_flag = 1; | |
} | |
void handleGesture() { | |
if ( apds.isGestureAvailable() ) { | |
switch ( apds.readGesture() ) { | |
case DIR_UP: | |
display.clear(); | |
displayString("UP", 64, 15); | |
Serial.println("UP"); | |
break; | |
case DIR_DOWN: | |
display.clear(); | |
displayString("DOWN", 64, 15); | |
Serial.println("DOWN"); | |
break; | |
case DIR_LEFT: | |
display.clear(); | |
displayString("LEFT", 64, 15); | |
ha.setComponent("light.bedroom_3_light"); | |
ha.sendHALight(true); | |
Serial.println("LEFT"); | |
break; | |
case DIR_RIGHT: | |
display.clear(); | |
displayString("RIGHT", 64, 15); | |
ha.setComponent("light.bedroom_3_light"); | |
ha.sendHALight(false); | |
Serial.println("RIGHT"); | |
break; | |
case DIR_NEAR: | |
display.clear(); | |
displayString("NEAR", 64, 15); | |
Serial.println("NEAR"); | |
break; | |
case DIR_FAR: | |
display.clear(); | |
displayString("FAR", 64, 15); | |
Serial.println("FAR"); | |
break; | |
default: | |
display.clear(); | |
displayString("----", 64, 15); | |
Serial.println("NONE"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment