From 393a618dfb3a2ddd91a81d57f0f6e297fc9b03f9 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Thu, 10 May 2018 17:40:08 -0700 Subject: [PATCH] Adding magnetic sensor controller for Particle cloud (PHOTON). --- .../Magnetic-Sensor_Particle-Cloud.ino | 41 +++++++++++++++++++ version2/src/others/Magnetic-Sensor/Makefile | 40 ++++++++++++++++++ version2/src/others/Magnetic-Sensor/common.mk | 16 ++++++++ 3 files changed, 97 insertions(+) create mode 100644 version2/src/others/Magnetic-Sensor/Magnetic-Sensor_Particle-Cloud.ino create mode 100644 version2/src/others/Magnetic-Sensor/Makefile create mode 100644 version2/src/others/Magnetic-Sensor/common.mk diff --git a/version2/src/others/Magnetic-Sensor/Magnetic-Sensor_Particle-Cloud.ino b/version2/src/others/Magnetic-Sensor/Magnetic-Sensor_Particle-Cloud.ino new file mode 100644 index 0000000..3f4c9c8 --- /dev/null +++ b/version2/src/others/Magnetic-Sensor/Magnetic-Sensor_Particle-Cloud.ino @@ -0,0 +1,41 @@ +// ------------------------------------------------ +// Controlling Arduino EK1656 through cloud +// @author Rahmadi Trimananda - UC Irvine +// ------------------------------------------------ +// Gikfun MC-38 - Arduino EK1656 (magnetic sensor) +// Basically, this simple code detects sensor opening and send the occurrences to the cloud +// We use the WKP port (port A7) to wake up the device when there is motion +// + +int64_t machineId; +// Key and data +char keyBuffer[80]; +char dataBuffer[80]; + +// Setting up pins for input +void setup() { + + // Get the MAC address + byte mac[6]; + WiFi.macAddress(mac); + // Prepare machine ID + int64_t mac4 = (int64_t) mac[4]; + int64_t mac5 = (int64_t) mac[5]; + machineId = (mac4 * 256) + mac5; +} + +void loop() { + + // Basically delay to give time to send updates to Particle cloud. + // If we do less than this then it would fail publishing. + delay(2000); + // Machine ID + sprintf(keyBuffer, "%04x", machineId); + // Motion detection + sprintf(dataBuffer, "%s -> closing-door-detected", Time.timeStr().c_str()); + Particle.publish(keyBuffer, dataBuffer); + + delay(1000); + + System.sleep(SLEEP_MODE_DEEP, 0); +} diff --git a/version2/src/others/Magnetic-Sensor/Makefile b/version2/src/others/Magnetic-Sensor/Makefile new file mode 100644 index 0000000..ffc9773 --- /dev/null +++ b/version2/src/others/Magnetic-Sensor/Makefile @@ -0,0 +1,40 @@ +include common.mk + +all: compile + +# To run Particle, we need to install Particle CLI +# Please see: +# https://docs.particle.io/guide/getting-started/connect/photon/#install-the-particle-cli +monitor: + sudo chmod 666 /dev/ttyACM0 + particle serial monitor + +compile: + rm -f *.bin + particle compile photon + +flash0: + particle flash IoT-0 photon*.bin + +flash1: + particle flash IoT-1 photon*.bin + +flash2: + particle flash IoT-2 photon*.bin + +PHONY += clean +clean: + rm -f *.bin + +PHONY += mrclean +mrclean: clean + rm -rf ../docs + +tabbing: + uncrustify -c C.cfg --no-backup *.cpp + uncrustify -c C.cfg --no-backup *.h + +wc: + wc *.cpp *.h + +.PHONY: $(PHONY) diff --git a/version2/src/others/Magnetic-Sensor/common.mk b/version2/src/others/Magnetic-Sensor/common.mk new file mode 100644 index 0000000..479a31c --- /dev/null +++ b/version2/src/others/Magnetic-Sensor/common.mk @@ -0,0 +1,16 @@ +# A few common Makefile items + +CC := gcc +CXX := g++ + +UNAME := $(shell uname) + +LIB_NAME := iotcloud +LIB_SO := lib_$(LIB_NAME).so + +CPPFLAGS += -Wall -g -O0 + +# Mac OSX options +ifeq ($(UNAME), Darwin) +CPPFLAGS += -D_XOPEN_SOURCE -DMAC +endif -- 2.34.1