+++ /dev/null
-// ------------------------------------------------
-// Controlling HC-SR501 through Particle Cloud
-// @author Rahmadi Trimananda - UC Irvine
-// ------------------------------------------------
-// HC-SR501 Motion Detector
-// Basically, this simple code detects motion and send the occurrences to the cloud
-// We use the WKP port (port A7) to wake up the device when there is motion
-//
-// Based on tutorial: http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/arduino-hc-sr501-motion-sensor-tutorial/
-
-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 -> motion-detected", Time.timeStr().c_str());
- Particle.publish(keyBuffer, dataBuffer);
- delay(1000);
-
- // TODO: Profiling
- //String strTime(micros());
- //Particle.publish("Time motion sensor", strTime.c_str());
- //while(true);
-
- System.sleep(SLEEP_MODE_DEEP, 0);
-}
+++ /dev/null
-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
-
-flash8:
- particle flash IoT-8 photon*.bin
-
-flash9:
- particle flash IoT-9 photon*.bin
-
-flash10:
- particle flash IoT-10 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)
+++ /dev/null
-# 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
+++ /dev/null
-// ------------------------------------------------
-// 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);
-
- // TODO: Profiling
- //String strTime(micros());
- //Particle.publish("Time motion sensor", strTime.c_str());
- //while(true);
-
- System.sleep(SLEEP_MODE_DEEP, 0);
-}
+++ /dev/null
-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
-
-flash14:
- particle flash IoT-14 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)
+++ /dev/null
-# 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
+++ /dev/null
-/* DHT library
-
-MIT license
-written by Adafruit Industries
-*/
-
-#include "DHT.h"
-
-DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
- _pin = pin;
- _type = type;
- _count = count;
- firstreading = true;
-}
-
-void DHT::begin(void) {
- // set up the pins!
- pinMode(_pin, INPUT);
- digitalWrite(_pin, HIGH);
- _lastreadtime = 0;
-}
-
-//boolean S == Scale. True == Farenheit; False == Celcius
-float DHT::readTemperature(bool S) {
- float f;
-
- if (read()) {
- switch (_type) {
- case DHT11:
- f = data[2];
- if(S)
- f = convertCtoF(f);
-
- return f;
- case DHT22:
- case DHT21:
- f = data[2] & 0x7F;
- f *= 256;
- f += data[3];
- f /= 10;
- if (data[2] & 0x80)
- f *= -1;
- if(S)
- f = convertCtoF(f);
-
- return f;
- }
- }
- return NAN;
-}
-
-float DHT::convertCtoF(float c) {
- return c * 9 / 5 + 32;
-}
-
-float DHT::convertFtoC(float f) {
- return (f - 32) * 5 / 9;
-}
-
-float DHT::readHumidity(void) {
- float f;
- if (read()) {
- switch (_type) {
- case DHT11:
- f = data[0];
- return f;
- case DHT22:
- case DHT21:
- f = data[0];
- f *= 256;
- f += data[1];
- f /= 10;
- return f;
- }
- }
- return NAN;
-}
-
-float DHT::computeHeatIndex(float tempFahrenheit, float percentHumidity) {
- // Adapted from equation at: https://github.com/adafruit/DHT-sensor-library/issues/9 and
- // Wikipedia: http://en.wikipedia.org/wiki/Heat_index
- return -42.379 +
- 2.04901523 * tempFahrenheit +
- 10.14333127 * percentHumidity +
- -0.22475541 * tempFahrenheit*percentHumidity +
- -0.00683783 * pow(tempFahrenheit, 2) +
- -0.05481717 * pow(percentHumidity, 2) +
- 0.00122874 * pow(tempFahrenheit, 2) * percentHumidity +
- 0.00085282 * tempFahrenheit*pow(percentHumidity, 2) +
- -0.00000199 * pow(tempFahrenheit, 2) * pow(percentHumidity, 2);
-}
-
-
-boolean DHT::read(void) {
- uint8_t laststate = HIGH;
- uint8_t counter = 0;
- uint8_t j = 0, i;
- unsigned long currenttime;
-
- // Check if sensor was read less than two seconds ago and return early
- // to use last reading.
- currenttime = millis();
- if (currenttime < _lastreadtime) {
- // ie there was a rollover
- _lastreadtime = 0;
- }
- if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
- return true; // return last correct measurement
- //delay(2000 - (currenttime - _lastreadtime));
- }
- firstreading = false;
- /*
- Serial.print("Currtime: "); Serial.print(currenttime);
- Serial.print(" Lasttime: "); Serial.print(_lastreadtime);
- */
- _lastreadtime = millis();
-
- data[0] = data[1] = data[2] = data[3] = data[4] = 0;
-
- // pull the pin high and wait 250 milliseconds
- digitalWrite(_pin, HIGH);
- delay(250);
-
- // now pull it low for ~20 milliseconds
- pinMode(_pin, OUTPUT);
- digitalWrite(_pin, LOW);
- delay(20);
- noInterrupts();
- digitalWrite(_pin, HIGH);
- delayMicroseconds(40);
- pinMode(_pin, INPUT);
-
- // read in timings
- for ( i=0; i< MAXTIMINGS; i++) {
- counter = 0;
- while (digitalRead(_pin) == laststate) {
- counter++;
- delayMicroseconds(1);
- if (counter == 255) {
- break;
- }
- }
- laststate = digitalRead(_pin);
-
- if (counter == 255) break;
-
- // ignore first 3 transitions
- if ((i >= 4) && (i%2 == 0)) {
- // shove each bit into the storage bytes
- data[j/8] <<= 1;
- if (counter > _count)
- data[j/8] |= 1;
- j++;
- }
-
- }
-
- interrupts();
-
- /*
- Serial.println(j, DEC);
- Serial.print(data[0], HEX); Serial.print(", ");
- Serial.print(data[1], HEX); Serial.print(", ");
- Serial.print(data[2], HEX); Serial.print(", ");
- Serial.print(data[3], HEX); Serial.print(", ");
- Serial.print(data[4], HEX); Serial.print(" =? ");
- Serial.println(data[0] + data[1] + data[2] + data[3], HEX);
- */
-
- // check we read 40 bits and that the checksum matches
- if ((j >= 40) &&
- (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
- return true;
- }
-
-
- return false;
-
-}
+++ /dev/null
-#ifndef DHT_H\r
-#define DHT_H\r
-#if ARDUINO >= 100\r
- #include "Arduino.h"\r
-#else\r
- #include "WProgram.h"\r
-#endif\r
-\r
-/* DHT library \r
-\r
-MIT license\r
-written by Adafruit Industries\r
-*/\r
-\r
-// how many timing transitions we need to keep track of. 2 * number bits + extra\r
-#define MAXTIMINGS 85\r
-\r
-#define DHT11 11\r
-#define DHT22 22\r
-#define DHT21 21\r
-#define AM2301 21\r
-\r
-class DHT {\r
- private:\r
- uint8_t data[6];\r
- uint8_t _pin, _type, _count;\r
- unsigned long _lastreadtime;\r
- boolean firstreading;\r
-\r
- public:\r
- DHT(uint8_t pin, uint8_t type, uint8_t count=6);\r
- void begin(void);\r
- float readTemperature(bool S=false);\r
- float convertCtoF(float);\r
- float convertFtoC(float);\r
- float computeHeatIndex(float tempFahrenheit, float percentHumidity);\r
- float readHumidity(void);\r
- boolean read(void);\r
-\r
-};\r
-#endif\r
+++ /dev/null
-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
-
-flash3:
- particle flash IoT-3 photon*.bin
-
-flash4:
- particle flash IoT-4 photon*.bin
-
-flash5:
- particle flash IoT-5 photon*.bin
-
-flash6:
- particle flash IoT-6 photon*.bin
-
-flash7:
- particle flash IoT-7 photon*.bin
-
-flash11:
- particle flash IoT-11 photon*.bin
-
-flash12:
- particle flash IoT-12 photon*.bin
-
-flash13:
- particle flash IoT-13 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)
+++ /dev/null
-#include "application.h"
-#include "DHT.h"
-
-// System defines
-// Arduino
-#define DHTPIN 2 // Data pin
-#define PWRPIN D5 // Power pin
-#define ERRPIN D7 // Error pin
-#define DHTTYPE DHT22 // DHT 22 (AM2302)
-// IoTCloud
-#define SLEEP_TIME 15 // Sleep time in seconds
-#define RETRY_SLEEP_TIME 5 // Sleep time in seconds
-#define RETRY_TIME 10000 // stop trying after 10 seconds
-#define CONNECTION_DELAY 2100000 // Need to delay after connecting WiFi to wait for sensor
-
-// Initialize DHT sensor for normal 16mhz Arduino
-DHT dht(DHTPIN, DHTTYPE);
-
-// Globals
-// IoTCloud
-char keyBuffer[80];
-char dataBuffer[80];
-int64_t machineId;
-
-void setup() {
- // TODO:
- // 1) This code reads from the sensor and publishes
- // the results on the cloud.
- // 2) If you want to use the Serial library,
- // please install "screen" on your machine and run
- // it on the serial port right after flashing the
- // firmware onto the Particle board.
- // e.g. sudo screen /dev/ttyACM0
-
- // We use one I/O pin to power the sensor so
- // that we can make it go to sleep when we go into
- // deep sleep.
-
- // TODO: Profiling!
- //Serial.begin();
- //Serial.print("Time begin setup: ");
- //Serial.println(micros());
- // Turn on sensor
- pinMode(PWRPIN, OUTPUT);
- digitalWrite(PWRPIN, HIGH);
- // Arduino DHT
- dht.begin();
-
- // Prepare device key from MAC (just last 2 of 6 digits)
- byte mac[6];
- WiFi.macAddress(mac);
-
- // TODO: Uncomment the following block to print MAC
- //for (int i=0; i<6; i++) {
- // Serial.printf("%02x%s", mac[i], i != 5 ? ":" : "");
- //}
- //Serial.println();
- // Prepare machine ID
- int64_t mac4 = (int64_t) mac[4];
- int64_t mac5 = (int64_t) mac[5];
- machineId = (mac4 * 256) + mac5;
- //machineId = (int64_t) mac[5]; // Shorter version of machine ID
-
- // TODO: Profiling
- //Serial.print("Time end setup: ");
- //Serial.println(micros());
-}
-
-void loop() {
- // Wait until sensor is ready
- delay(1500);
- // TODO: Profiling
- //Serial.print("Time begin loop: ");
- //Serial.println(micros());
- // Read humidity
- float humid = dht.readHumidity();
- // Read temperature as Fahrenheit
- float tempF = dht.readTemperature(true);
- // Check if any reads failed and exit early (to try again).
- if (isnan(humid) || isnan(tempF))
- return;
-
- // Machine ID
- sprintf(keyBuffer, "%04x", machineId);
- // Humidity + Temperature
- sprintf(dataBuffer, "%0.2f-%0.2f", humid, tempF);
-
- // Publish on PHOTON's cloud
- Particle.publish(keyBuffer, dataBuffer);
- delay(1000);
-
- // Turn off sensor
- digitalWrite(PWRPIN, LOW);
-
- // TODO: Profiling
- //Serial.print("Time end loop: ");
- //Serial.println(micros());
- //while(true) { }
- System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);
-}
-
+++ /dev/null
-# 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
--- /dev/null
+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
+
+flash8:
+ particle flash IoT-8 photon*.bin
+
+flash9:
+ particle flash IoT-9 photon*.bin
+
+flash10:
+ particle flash IoT-10 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)
--- /dev/null
+# 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
--- /dev/null
+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
+
+flash14:
+ particle flash IoT-14 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)
--- /dev/null
+# 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
--- /dev/null
+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
+
+flash3:
+ particle flash IoT-3 photon*.bin
+
+flash4:
+ particle flash IoT-4 photon*.bin
+
+flash5:
+ particle flash IoT-5 photon*.bin
+
+flash6:
+ particle flash IoT-6 photon*.bin
+
+flash7:
+ particle flash IoT-7 photon*.bin
+
+flash11:
+ particle flash IoT-11 photon*.bin
+
+flash12:
+ particle flash IoT-12 photon*.bin
+
+flash13:
+ particle flash IoT-13 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)
--- /dev/null
+# 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