Skip to content

adding demo code for IoT button BFF #2382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions IoT_Button_BFF_Examples/CircuitPython_Basic_Example/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""Basic IoT Button with NeoPixel BFF Example"""
import time
import board
from digitalio import DigitalInOut, Direction, Pull
from rainbowio import colorwheel
import neopixel

# setup onboard NeoPixel
pixel_pin = board.A3
num_pixels = 1

pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)

# setup onboard button
switch = DigitalInOut(board.A2)
switch.direction = Direction.INPUT
switch.pull = Pull.UP

# rainbow cycle function
def rainbow_cycle(wait):
for j in range(255):
for i in range(num_pixels):
rc_index = (i * 256 // num_pixels) + j
pixels[i] = colorwheel(rc_index & 255)
pixels.show()
time.sleep(wait)

while True:
# run rainbow cycle animation
rainbow_cycle(0)

# if the button is not pressed..
if switch.value:
# neopixel brightness is zero and appears to be "off"
pixels.brightness = 0
# if the button is pressed..
else:
# neopixel brightness is 0.3 and rainbow animation is visible
pixels.brightness = 0.3
94 changes: 94 additions & 0 deletions IoT_Button_BFF_Examples/CircuitPython_Simple_IO_Example/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
# SPDX-License-Identifier: MIT

"""Simple Adafruit IO Example for IoT Button with NeoPixel BFF"""
import os
import time
import ssl
import wifi
import socketpool
import microcontroller
import board
from digitalio import DigitalInOut, Direction, Pull
import neopixel
import adafruit_requests
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError

# setup onboard button
switch = DigitalInOut(board.A2)
switch.direction = Direction.INPUT
switch.pull = Pull.UP

# setup onboard NeoPixel
pixel_pin = board.A3
num_pixels = 1

pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)

# neopixel status colors
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# red until connecting
pixels.fill(RED)
pixels.show()

wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))

aio_username = os.getenv('aio_username')
aio_key = os.getenv('aio_key')

pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# Initialize an Adafruit IO HTTP API object
io = IO_HTTP(aio_username, aio_key, requests)
print("connected to io")
# blue when talking to IO
pixels.fill(BLUE)
pixels.show()

try:
# get feed
button_feed = io.get_feed("buttonbff")
except AdafruitIO_RequestError:
# if no feed exists, create one
button_feed = io.create_new_feed("buttonbff")

# green once connected
pixels.fill(GREEN)
pixels.show()

# button press count sent to IO
count = 0

while True:
try:
# if the button is pressed..
if not switch.value:
# blue when talking to IO
pixels.fill(BLUE)
pixels.show()
# increase by 1 with press
count += 1
# send count to feed
io.send_data(button_feed["key"], count)
print("sent %d" % count)
print()
# delay
time.sleep(5)
else:
# green if connected
pixels.fill(GREEN)
pixels.show()

# pylint: disable=broad-except
# any errors, reset board
except Exception as e:
# neopixels red with an error
pixels.fill(RED)
pixels.show()
print("Error:\n", str(e))
print("Resetting microcontroller in 10 seconds")
time.sleep(10)
microcontroller.reset()
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// SPDX-FileCopyrightText: 2016 Todd Treece, Adapted 2023 Liz Clark for Adafruit Industries
//
// SPDX-License-Identifier: MIT
// Adafruit IO IoT Button with NeoPixel BFF Demo
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by Todd Treece for Adafruit Industries
// Copyright (c) 2016 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.

/************************** Configuration ***********************************/

// edit the config.h tab and enter your Adafruit IO credentials
// and any additional configuration needed for WiFi, cellular,
// or ethernet clients.
#include "config.h"
#include <Adafruit_NeoPixel.h>

/************************ Example Starts Here *******************************/

#define BUTTON_PIN A2
#define LED_PIN A3
#define LED_COUNT 1
Adafruit_NeoPixel pixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

// button state
bool current = false;
bool last = false;

// set up the 'digital' feed
AdafruitIO_Feed *digital = io.feed("digital");

void setup() {
pixel.begin();
pixel.show();
pixel.setBrightness(50);
pixel.setPixelColor(0, pixel.Color(150, 0, 0));
pixel.show();

// set button pin as an input
pinMode(BUTTON_PIN, INPUT);

// start the serial connection
Serial.begin(115200);

// wait for serial monitor to open
while(! Serial);

// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();

// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}

// we are connected
Serial.println();
Serial.println(io.statusText());
pixel.setPixelColor(0, pixel.Color(0, 150, 0));
pixel.show();

}

void loop() {

// io.run(); is required for all sketches.
// it should always be present at the top of your loop
// function. it keeps the client connected to
// io.adafruit.com, and processes any incoming data.
io.run();

// grab the current state of the button.
// we have to flip the logic because we are
// using a pullup resistor.
if(digitalRead(BUTTON_PIN) == LOW){
current = true;
pixel.setPixelColor(0, pixel.Color(0, 0, 150));
pixel.show();
}
else {
current = false;
pixel.setPixelColor(0, pixel.Color(0, 150, 0));
pixel.show();
}
// return if the value hasn't changed
if(current == last)
return;

// save the current state to the 'digital' feed on adafruit io
Serial.print("sending button -> ");
Serial.println(current);
digital->save(current);

// store last button state
last = current;

}
75 changes: 75 additions & 0 deletions IoT_Button_BFF_Examples/adafruitIO_iotButtonNeoPixelBFF/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-FileCopyrightText: 2016 Todd Treece, Adapted 2023 Liz Clark for Adafruit Industries
//
// SPDX-License-Identifier: MIT
/************************ Adafruit IO Config *******************************/

// visit io.adafruit.com if you need to create an account,
// or if you need your Adafruit IO key.
#define IO_USERNAME "your-username-here"
#define IO_KEY "your-key-here"

/******************************* WIFI **************************************/

// the AdafruitIO_WiFi client will work with the following boards:
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
// - Feather HUZZAH ESP32 -> https://www.adafruit.com/product/3405
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010
// - Feather WICED -> https://www.adafruit.com/products/3056
// - Adafruit PyPortal -> https://www.adafruit.com/product/4116
// - Adafruit Metro M4 Express AirLift Lite ->
// https://www.adafruit.com/product/4000
// - Adafruit AirLift Breakout -> https://www.adafruit.com/product/4201
// - Adafruit AirLift Shield -> https://www.adafruit.com/product/4285
// - Adafruit AirLift FeatherWing -> https://www.adafruit.com/product/4264

#define WIFI_SSID "your-ssid-here"
#define WIFI_PASS "your-ssid-password-here"

// uncomment the following line if you are using airlift
// #define USE_AIRLIFT

// uncomment the following line if you are using winc1500
// #define USE_WINC1500

// uncomment the following line if you are using mrk1010 or nano 33 iot
//#define ARDUINO_SAMD_MKR1010

// comment out the following lines if you are using fona or ethernet
#include "AdafruitIO_WiFi.h"

#if defined(USE_AIRLIFT) || defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || \
defined(ADAFRUIT_PYPORTAL)
// Configure the pins used for the ESP32 connection
#if !defined(SPIWIFI_SS) // if the wifi definition isnt in the board variant
// Don't change the names of these #define's! they match the variant ones
#define SPIWIFI SPI
#define SPIWIFI_SS 10 // Chip select pin
#define NINA_ACK 9 // a.k.a BUSY or READY pin
#define NINA_RESETN 6 // Reset pin
#define NINA_GPIO0 -1 // Not connected
#endif
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS, SPIWIFI_SS,
NINA_ACK, NINA_RESETN, NINA_GPIO0, &SPIWIFI);
#else
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
#endif
/******************************* FONA **************************************/

// the AdafruitIO_FONA client will work with the following boards:
// - Feather 32u4 FONA -> https://www.adafruit.com/product/3027

// uncomment the following two lines for 32u4 FONA,
// and comment out the AdafruitIO_WiFi client in the WIFI section
// #include "AdafruitIO_FONA.h"
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY);

/**************************** ETHERNET ************************************/

// the AdafruitIO_Ethernet client will work with the following boards:
// - Ethernet FeatherWing -> https://www.adafruit.com/products/3201

// uncomment the following two lines for ethernet,
// and comment out the AdafruitIO_WiFi client in the WIFI section
// #include "AdafruitIO_Ethernet.h"
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY);
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
//
// SPDX-License-Identifier: MIT

// Basic IoT Button with NeoPixel BFF Demo

#include <Adafruit_NeoPixel.h>

#define LED_PIN A3
#define BUTTON_PIN A2
#define LED_COUNT 1

int buttonState = 0;

Adafruit_NeoPixel pixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
pinMode(BUTTON_PIN, INPUT);
pixel.begin();
pixel.show();
pixel.setBrightness(50);
}

void loop() {
//pixel.clear();
buttonState = digitalRead(BUTTON_PIN);

if(buttonState == HIGH) {
pixel.setPixelColor(0, pixel.Color(150, 0, 0));
pixel.show();
}

if(buttonState == LOW) {
pixel.setPixelColor(0, pixel.Color(0, 0, 0));
pixel.show();
}

}