remote_monitor.ino
1 // SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 // Crickit + Adafruit IO Publish Example 6 // 7 // Adafruit invests time and resources providing this open source code. 8 // Please support Adafruit and open source hardware by purchasing 9 // products from Adafruit! 10 // 11 // Written by Dave Astels for Adafruit Industries 12 // Copyright (c) 2018 Adafruit Industries 13 // Licensed under the MIT license. 14 // 15 // All text above must be included in any redistribution. 16 17 #include "config.h" 18 #include <Adafruit_Crickit.h> 19 20 #define CAPTOUCH_THRESH 500 21 22 #define IO_LOOP_DELAY (1000) 23 unsigned long lastUpdate = 0; 24 25 // set up the feeds 26 AdafruitIO_Feed *light; 27 uint16_t last_reported_light = 0; 28 29 AdafruitIO_Feed *touch; 30 boolean last_touch = false; 31 32 // set up the Crickit 33 34 Adafruit_Crickit crickit; 35 36 void setup_feeds() 37 { 38 light = io.feed("crickit.light"); 39 touch = io.feed("crickit.touch-0"); 40 } 41 42 43 void setup() 44 { 45 setup_feeds(); 46 Serial.println("Feeds set up"); 47 48 // start the serial connection 49 Serial.begin(115200); 50 51 // wait for serial monitor to open 52 while(! Serial); 53 54 Serial.println("Connecting to Adafruit IO"); 55 56 // connect to io.adafruit.com 57 io.connect(); 58 59 // wait for a connection 60 while(io.status() < AIO_CONNECTED) { 61 Serial.print("."); 62 // Serial.println(io.statusText()); 63 delay(500); 64 } 65 66 // we are connected 67 Serial.println(); 68 Serial.println(io.statusText()); 69 70 if (!crickit.begin()) { 71 Serial.println("Error starting Crickit!"); 72 while(1); 73 } else { 74 Serial.println("Crickit started"); 75 } 76 77 Serial.println("setup complete"); 78 } 79 80 81 void loop() 82 { 83 84 // io.run(); is required for all sketches. 85 // it should always be present at the top of your loop 86 // function. it keeps the client connected to 87 // io.adafruit.com, and processes any incoming data. 88 io.run(); 89 90 if (millis() > (lastUpdate + IO_LOOP_DELAY)) { 91 92 uint16_t light_level = crickit.analogRead(CRICKIT_SIGNAL1); 93 uint16_t light_delta = abs(light_level - last_reported_light); 94 95 if (light_delta > 10) { 96 light->save(light_level); 97 last_reported_light = light_level; 98 Serial.print("Sending "); 99 } 100 Serial.print("Light: "); 101 Serial.println(light_level); 102 103 uint16_t val = crickit.touchRead(0); 104 105 if (val >= CAPTOUCH_THRESH && !last_touch) { 106 touch->save(1); 107 last_touch = true; 108 Serial.println("CT 0 touched."); 109 } else if (val < CAPTOUCH_THRESH && last_touch) { 110 touch->save(0); 111 last_touch = false; 112 Serial.println("CT 0 released."); 113 } 114 115 // after publishing, store the current time 116 lastUpdate = millis(); 117 } 118 119 }