Skip to content

Instantly share code, notes, and snippets.

@UNSWmir
Created May 3, 2018 12:39
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 UNSWmir/627f9e78746ee8b6f4623aa6a9c357bf to your computer and use it in GitHub Desktop.
Save UNSWmir/627f9e78746ee8b6f4623aa6a9c357bf to your computer and use it in GitHub Desktop.
#include <readiness_io.h>
#include "config.h"
const int LED_PIN = 4; // The pin connecting the LED (D3)
const int INTERRUPT_PIN = 14; // The pin connect the test button (D5)
volatile byte interrupt = 0;
static unsigned long last_interrupt_time = 0;
readiness_io client(CHANNEL_ID, TOPIC, SENSOR_ID, VERSION, FORMAT);
/* Interrupt for assessing a change in the hall effect sensor (both high and low) */
void halleffectInterrupt() {
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > DEBOUNCE_TIME)
{
interrupt++;
}
last_interrupt_time = interrupt_time;
}
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, HIGH); // internal LED is switched on when low - so we have to switch it off/
Serial.begin(115200);
Serial.setTimeout(2000);
while(!Serial) { } // Wait for serial to initialize.
Serial.println("Device Started");
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
client.wifiConnection(WIFI_SSID, WIFI_PASS);
pinMode(INTERRUPT_PIN, INPUT_PULLUP); // Set the interrupt pin for the reed/hall effect
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), halleffectInterrupt, CHANGE); // Attach the interrupt.
client.testConnection();
}
void loop() {
if(interrupt>0){
interrupt=0;
// GATE_STATE 0=open 1= closed
bool GATE_STATE = !digitalRead(INTERRUPT_PIN);
digitalWrite(LED_PIN,GATE_STATE);
client.publishData(GATE_STATE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment