Skip to content

Commit 95cfa77

Browse files
authored
Merge pull request #2382 from adafruit/iotButtonBFF
adding demo code for IoT button BFF
2 parents 6ccfb33 + 4530771 commit 95cfa77

File tree

7 files changed

+355
-0
lines changed

7 files changed

+355
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""Basic IoT Button with NeoPixel BFF Example"""
6+
import time
7+
import board
8+
from digitalio import DigitalInOut, Direction, Pull
9+
from rainbowio import colorwheel
10+
import neopixel
11+
12+
# setup onboard NeoPixel
13+
pixel_pin = board.A3
14+
num_pixels = 1
15+
16+
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)
17+
18+
# setup onboard button
19+
switch = DigitalInOut(board.A2)
20+
switch.direction = Direction.INPUT
21+
switch.pull = Pull.UP
22+
23+
# rainbow cycle function
24+
def rainbow_cycle(wait):
25+
for j in range(255):
26+
for i in range(num_pixels):
27+
rc_index = (i * 256 // num_pixels) + j
28+
pixels[i] = colorwheel(rc_index & 255)
29+
pixels.show()
30+
time.sleep(wait)
31+
32+
while True:
33+
# run rainbow cycle animation
34+
rainbow_cycle(0)
35+
36+
# if the button is not pressed..
37+
if switch.value:
38+
# neopixel brightness is zero and appears to be "off"
39+
pixels.brightness = 0
40+
# if the button is pressed..
41+
else:
42+
# neopixel brightness is 0.3 and rainbow animation is visible
43+
pixels.brightness = 0.3
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""Simple Adafruit IO Example for IoT Button with NeoPixel BFF"""
5+
import os
6+
import time
7+
import ssl
8+
import wifi
9+
import socketpool
10+
import microcontroller
11+
import board
12+
from digitalio import DigitalInOut, Direction, Pull
13+
import neopixel
14+
import adafruit_requests
15+
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
16+
17+
# setup onboard button
18+
switch = DigitalInOut(board.A2)
19+
switch.direction = Direction.INPUT
20+
switch.pull = Pull.UP
21+
22+
# setup onboard NeoPixel
23+
pixel_pin = board.A3
24+
num_pixels = 1
25+
26+
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)
27+
28+
# neopixel status colors
29+
RED = (255, 0, 0)
30+
GREEN = (0, 255, 0)
31+
BLUE = (0, 0, 255)
32+
33+
# red until connecting
34+
pixels.fill(RED)
35+
pixels.show()
36+
37+
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
38+
39+
aio_username = os.getenv('aio_username')
40+
aio_key = os.getenv('aio_key')
41+
42+
pool = socketpool.SocketPool(wifi.radio)
43+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
44+
# Initialize an Adafruit IO HTTP API object
45+
io = IO_HTTP(aio_username, aio_key, requests)
46+
print("connected to io")
47+
# blue when talking to IO
48+
pixels.fill(BLUE)
49+
pixels.show()
50+
51+
try:
52+
# get feed
53+
button_feed = io.get_feed("buttonbff")
54+
except AdafruitIO_RequestError:
55+
# if no feed exists, create one
56+
button_feed = io.create_new_feed("buttonbff")
57+
58+
# green once connected
59+
pixels.fill(GREEN)
60+
pixels.show()
61+
62+
# button press count sent to IO
63+
count = 0
64+
65+
while True:
66+
try:
67+
# if the button is pressed..
68+
if not switch.value:
69+
# blue when talking to IO
70+
pixels.fill(BLUE)
71+
pixels.show()
72+
# increase by 1 with press
73+
count += 1
74+
# send count to feed
75+
io.send_data(button_feed["key"], count)
76+
print("sent %d" % count)
77+
print()
78+
# delay
79+
time.sleep(5)
80+
else:
81+
# green if connected
82+
pixels.fill(GREEN)
83+
pixels.show()
84+
85+
# pylint: disable=broad-except
86+
# any errors, reset board
87+
except Exception as e:
88+
# neopixels red with an error
89+
pixels.fill(RED)
90+
pixels.show()
91+
print("Error:\n", str(e))
92+
print("Resetting microcontroller in 10 seconds")
93+
time.sleep(10)
94+
microcontroller.reset()

IoT_Button_BFF_Examples/adafruitIO_iotButtonNeoPixelBFF/.qtpy_esp32s2.test.only

Whitespace-only changes.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// SPDX-FileCopyrightText: 2016 Todd Treece, Adapted 2023 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
// Adafruit IO IoT Button with NeoPixel BFF Demo
5+
//
6+
// Adafruit invests time and resources providing this open source code.
7+
// Please support Adafruit and open source hardware by purchasing
8+
// products from Adafruit!
9+
//
10+
// Written by Todd Treece for Adafruit Industries
11+
// Copyright (c) 2016 Adafruit Industries
12+
// Licensed under the MIT license.
13+
//
14+
// All text above must be included in any redistribution.
15+
16+
/************************** Configuration ***********************************/
17+
18+
// edit the config.h tab and enter your Adafruit IO credentials
19+
// and any additional configuration needed for WiFi, cellular,
20+
// or ethernet clients.
21+
#include "config.h"
22+
#include <Adafruit_NeoPixel.h>
23+
24+
/************************ Example Starts Here *******************************/
25+
26+
#define BUTTON_PIN A2
27+
#define LED_PIN A3
28+
#define LED_COUNT 1
29+
Adafruit_NeoPixel pixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
30+
31+
// button state
32+
bool current = false;
33+
bool last = false;
34+
35+
// set up the 'digital' feed
36+
AdafruitIO_Feed *digital = io.feed("digital");
37+
38+
void setup() {
39+
pixel.begin();
40+
pixel.show();
41+
pixel.setBrightness(50);
42+
pixel.setPixelColor(0, pixel.Color(150, 0, 0));
43+
pixel.show();
44+
45+
// set button pin as an input
46+
pinMode(BUTTON_PIN, INPUT);
47+
48+
// start the serial connection
49+
Serial.begin(115200);
50+
51+
// wait for serial monitor to open
52+
while(! Serial);
53+
54+
// connect to io.adafruit.com
55+
Serial.print("Connecting to Adafruit IO");
56+
io.connect();
57+
58+
// wait for a connection
59+
while(io.status() < AIO_CONNECTED) {
60+
Serial.print(".");
61+
delay(500);
62+
}
63+
64+
// we are connected
65+
Serial.println();
66+
Serial.println(io.statusText());
67+
pixel.setPixelColor(0, pixel.Color(0, 150, 0));
68+
pixel.show();
69+
70+
}
71+
72+
void loop() {
73+
74+
// io.run(); is required for all sketches.
75+
// it should always be present at the top of your loop
76+
// function. it keeps the client connected to
77+
// io.adafruit.com, and processes any incoming data.
78+
io.run();
79+
80+
// grab the current state of the button.
81+
// we have to flip the logic because we are
82+
// using a pullup resistor.
83+
if(digitalRead(BUTTON_PIN) == LOW){
84+
current = true;
85+
pixel.setPixelColor(0, pixel.Color(0, 0, 150));
86+
pixel.show();
87+
}
88+
else {
89+
current = false;
90+
pixel.setPixelColor(0, pixel.Color(0, 150, 0));
91+
pixel.show();
92+
}
93+
// return if the value hasn't changed
94+
if(current == last)
95+
return;
96+
97+
// save the current state to the 'digital' feed on adafruit io
98+
Serial.print("sending button -> ");
99+
Serial.println(current);
100+
digital->save(current);
101+
102+
// store last button state
103+
last = current;
104+
105+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// SPDX-FileCopyrightText: 2016 Todd Treece, Adapted 2023 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
/************************ Adafruit IO Config *******************************/
5+
6+
// visit io.adafruit.com if you need to create an account,
7+
// or if you need your Adafruit IO key.
8+
#define IO_USERNAME "your-username-here"
9+
#define IO_KEY "your-key-here"
10+
11+
/******************************* WIFI **************************************/
12+
13+
// the AdafruitIO_WiFi client will work with the following boards:
14+
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
15+
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
16+
// - Feather HUZZAH ESP32 -> https://www.adafruit.com/product/3405
17+
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010
18+
// - Feather WICED -> https://www.adafruit.com/products/3056
19+
// - Adafruit PyPortal -> https://www.adafruit.com/product/4116
20+
// - Adafruit Metro M4 Express AirLift Lite ->
21+
// https://www.adafruit.com/product/4000
22+
// - Adafruit AirLift Breakout -> https://www.adafruit.com/product/4201
23+
// - Adafruit AirLift Shield -> https://www.adafruit.com/product/4285
24+
// - Adafruit AirLift FeatherWing -> https://www.adafruit.com/product/4264
25+
26+
#define WIFI_SSID "your-ssid-here"
27+
#define WIFI_PASS "your-ssid-password-here"
28+
29+
// uncomment the following line if you are using airlift
30+
// #define USE_AIRLIFT
31+
32+
// uncomment the following line if you are using winc1500
33+
// #define USE_WINC1500
34+
35+
// uncomment the following line if you are using mrk1010 or nano 33 iot
36+
//#define ARDUINO_SAMD_MKR1010
37+
38+
// comment out the following lines if you are using fona or ethernet
39+
#include "AdafruitIO_WiFi.h"
40+
41+
#if defined(USE_AIRLIFT) || defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || \
42+
defined(ADAFRUIT_PYPORTAL)
43+
// Configure the pins used for the ESP32 connection
44+
#if !defined(SPIWIFI_SS) // if the wifi definition isnt in the board variant
45+
// Don't change the names of these #define's! they match the variant ones
46+
#define SPIWIFI SPI
47+
#define SPIWIFI_SS 10 // Chip select pin
48+
#define NINA_ACK 9 // a.k.a BUSY or READY pin
49+
#define NINA_RESETN 6 // Reset pin
50+
#define NINA_GPIO0 -1 // Not connected
51+
#endif
52+
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS, SPIWIFI_SS,
53+
NINA_ACK, NINA_RESETN, NINA_GPIO0, &SPIWIFI);
54+
#else
55+
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
56+
#endif
57+
/******************************* FONA **************************************/
58+
59+
// the AdafruitIO_FONA client will work with the following boards:
60+
// - Feather 32u4 FONA -> https://www.adafruit.com/product/3027
61+
62+
// uncomment the following two lines for 32u4 FONA,
63+
// and comment out the AdafruitIO_WiFi client in the WIFI section
64+
// #include "AdafruitIO_FONA.h"
65+
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY);
66+
67+
/**************************** ETHERNET ************************************/
68+
69+
// the AdafruitIO_Ethernet client will work with the following boards:
70+
// - Ethernet FeatherWing -> https://www.adafruit.com/products/3201
71+
72+
// uncomment the following two lines for ethernet,
73+
// and comment out the AdafruitIO_WiFi client in the WIFI section
74+
// #include "AdafruitIO_Ethernet.h"
75+
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY);

IoT_Button_BFF_Examples/basic_IoTButtonExample/.qtpy_esp32s2.test.only

Whitespace-only changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
// Basic IoT Button with NeoPixel BFF Demo
6+
7+
#include <Adafruit_NeoPixel.h>
8+
9+
#define LED_PIN A3
10+
#define BUTTON_PIN A2
11+
#define LED_COUNT 1
12+
13+
int buttonState = 0;
14+
15+
Adafruit_NeoPixel pixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
16+
17+
void setup() {
18+
pinMode(BUTTON_PIN, INPUT);
19+
pixel.begin();
20+
pixel.show();
21+
pixel.setBrightness(50);
22+
}
23+
24+
void loop() {
25+
//pixel.clear();
26+
buttonState = digitalRead(BUTTON_PIN);
27+
28+
if(buttonState == HIGH) {
29+
pixel.setPixelColor(0, pixel.Color(150, 0, 0));
30+
pixel.show();
31+
}
32+
33+
if(buttonState == LOW) {
34+
pixel.setPixelColor(0, pixel.Color(0, 0, 0));
35+
pixel.show();
36+
}
37+
38+
}

0 commit comments

Comments
 (0)