/ Crickit_AdafruitIO / crickit_io / crickit_io.ino
crickit_io.ino
  1  // SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
  2  //
  3  // SPDX-License-Identifier: MIT
  4  
  5  // Crickit + Adafruit IO Publish & Subscribe 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  /************************** Configuration ***********************************/
 18  
 19  // edit the config.h tab and enter your Adafruit IO credentials
 20  // and any additional configuration needed for WiFi, cellular,
 21  // or ethernet clients.
 22  #include "config.h"
 23  #include <Adafruit_Crickit.h>
 24  #include <seesaw_servo.h>
 25  #include <seesaw_neopixel.h>
 26  
 27  #define NEOPIX_PIN (20)                  /* Neopixel pin */
 28  #define NEOPIX_NUMBER_OF_PIXELS (7)
 29  
 30  #define CAPTOUCH_THRESH 500
 31  
 32  #define IO_LOOP_DELAY (1000)
 33  unsigned long lastUpdate = 0;
 34  
 35  // set up the feeds
 36  AdafruitIO_Feed *servo1_control;
 37  AdafruitIO_Feed *neopixel_control;
 38  AdafruitIO_Feed *light;
 39  uint16_t last_reported_light = 0;
 40  
 41  AdafruitIO_Feed *touch;
 42  boolean last_touch = false;
 43  
 44  // set up the Crickit
 45  
 46  Adafruit_Crickit crickit;
 47  seesaw_Servo servo_1(&crickit);  // create servo object to control a servo
 48  
 49  // Parameter 1 = number of pixels in strip
 50  // Parameter 2 = Arduino pin number (most are valid)
 51  // Parameter 3 = pixel type flags, add together as needed:
 52  //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
 53  //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
 54  //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
 55  //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
 56  //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
 57  seesaw_NeoPixel strip = seesaw_NeoPixel(NEOPIX_NUMBER_OF_PIXELS, NEOPIX_PIN, NEO_GRB + NEO_KHZ800);
 58  
 59  void setup_feeds()
 60  {
 61    servo1_control = io.feed("crickit.servo1-control");
 62    neopixel_control = io.feed("crickit.neopixel-control");
 63    light = io.feed("crickit.light");
 64    touch = io.feed("crickit.touch-0");
 65  }
 66  
 67  
 68  void setup()
 69  {
 70    setup_feeds();
 71    Serial.println("Feeds set up");
 72  
 73    // start the serial connection
 74    Serial.begin(115200);
 75  
 76    // wait for serial monitor to open
 77    while(! Serial);
 78  
 79    Serial.println("Connecting to Adafruit IO");
 80  
 81    // connect to io.adafruit.com
 82    io.connect();
 83  
 84    // set up a message handler for the count feed.
 85    // the handleMessage function (defined below)
 86    // will be called whenever a message is
 87    // received from adafruit io.
 88    // setup handlers
 89    servo1_control->onMessage(handle_servo_message);
 90    neopixel_control->onMessage(handle_neopixel_message);
 91  
 92    // wait for a connection
 93    while(io.status() < AIO_CONNECTED) {
 94      Serial.println(io.statusText());
 95      delay(500);
 96    }
 97  
 98    // we are connected
 99    Serial.println();
100    Serial.println(io.statusText());
101  
102    if (!crickit.begin()) {
103      Serial.println("Error starting Crickit!");
104      while(1);
105    } else {
106      Serial.println("Crickit started");
107    }
108  
109    if(!strip.begin()){
110      Serial.println("Error starting Neopixels!");
111      while(1);
112    } else {
113      Serial.println("Neopixels started");
114    }
115  
116    servo1_control->get();
117    servo_1.attach(CRICKIT_SERVO1);
118  
119    Serial.println("setup complete");
120  }
121  
122  
123  void loop()
124  {
125  
126    // io.run(); is required for all sketches.
127    // it should always be present at the top of your loop
128    // function. it keeps the client connected to
129    // io.adafruit.com, and processes any incoming data.
130    io.run();
131  
132    if (millis() > (lastUpdate + IO_LOOP_DELAY)) {
133  
134      uint16_t light_level = crickit.analogRead(CRICKIT_SIGNAL1);
135      uint16_t light_delta = abs(light_level - last_reported_light);
136  
137      if (light_delta > 10) {
138        light->save(light_level);
139        last_reported_light = light_level;
140        Serial.print("Sending ");
141      }
142      Serial.print("Light: ");
143      Serial.println(light_level);
144  
145      uint16_t val = crickit.touchRead(0);
146  
147      if (val >= CAPTOUCH_THRESH && !last_touch) {
148        touch->save(1);
149        last_touch = true;
150        Serial.println("CT 0 touched.");
151      } else if (val < CAPTOUCH_THRESH && last_touch) {
152        touch->save(0);
153        last_touch = false;
154        Serial.println("CT 0 released.");
155      }
156  
157      // after publishing, store the current time
158      lastUpdate = millis();
159    }
160  
161  }
162  
163  void handle_servo_message(AdafruitIO_Data *data)
164  {
165    Serial.print("received servo control <- ");
166    Serial.println(data->value());
167    int angle = data->toInt();
168    if(angle < 0) {
169      angle = 0;
170    } else if(angle > 180) {
171      angle = 180;
172    }
173    servo_1.write(angle);
174  
175  }
176  
177  
178  void handle_neopixel_message(AdafruitIO_Data *data)
179  {
180    Serial.print("received neopixel control <- ");
181    Serial.println(data->value());
182    long color = data->toNeoPixel();
183    for (int pixel = 0; pixel < NEOPIX_NUMBER_OF_PIXELS; pixel++) {
184      strip.setPixelColor(pixel, color);
185    }
186    strip.show();
187  }