Created
January 9, 2020 16:41
-
-
Save motform/aaa57aa29ad001b7780ce37bc32caab5 to your computer and use it in GitHub Desktop.
Techno-neolithic Syncro-ritualisations of Interdependent Bodies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
Actuator part of CO2-assemblage. | |
© Love Lagerkvist, 2020-01-07 | |
Looks for a BLE Peripheral advertising @ 19b10000-e8f2-537e-4f6c-d104768a1214 | |
Dims the ring based on the BLE Char at the same UUID, understood to be CO2 | |
Hardware setup: | |
Neopixel Ring LED 24: | |
5V to 5V pin | |
GND to GND pin | |
IN to D9 | |
Licensed under GPL-3. https://www.gnu.org/licenses/gpl-3.0.en.html | |
******************************************************************************/ | |
#include <ArduinoBLE.h> | |
#include <Adafruit_NeoPixel.h> | |
byte lastCO2 = 0; | |
Adafruit_NeoPixel pixels(25, // № LEDs | |
9, // Pin | |
NEO_GRB + NEO_KHZ800); | |
void setup() { | |
Serial.begin(9600); | |
pixels.begin(); | |
/* pixels.setBrightness(0); */ | |
BLE.begin(); | |
Serial.println("Scanning for BLE...\n"); | |
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); | |
} | |
void loop() { | |
BLEDevice peripheral = BLE.available(); | |
if (peripheral) { | |
Serial.print("Found "); | |
Serial.print(peripheral.address()); Serial.print(" '"); | |
Serial.print(peripheral.localName()); Serial.print("' "); | |
Serial.print(peripheral.advertisedServiceUuid()); Serial.println(); | |
BLE.stopScan(); | |
readPeripheral(peripheral); | |
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); | |
} | |
} | |
void readPeripheral(BLEDevice peripheral) { | |
/* Most of this function is BLE setup/troubleshooting | |
Note that characteristic UUID is hard coded */ | |
Serial.println("Connecting ..."); | |
if (peripheral.connect()) { | |
Serial.println("Connected"); | |
} else { | |
Serial.println("Failed to connect!"); | |
return; | |
} | |
Serial.println("Discovering attributes ..."); | |
if (peripheral.discoverAttributes()) { | |
Serial.println("Attributes discovered"); | |
} else { | |
Serial.println("Attribute discovery failed!"); | |
peripheral.disconnect(); | |
return; | |
} | |
BLECharacteristic CO2Characteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214"); | |
Serial.println("Discovering characteristics ..."); | |
if (!CO2Characteristic) { | |
Serial.println("Peripheral does not have CO2 characteristic!"); | |
peripheral.disconnect(); | |
return; | |
} | |
while (peripheral.connected()) { | |
byte CO2 = 0; // BLE packets are 1 byte big, which is all we need anyway | |
CO2Characteristic.readValue(CO2); | |
Serial.print("CO2: "); Serial.println(CO2); | |
actuateLED(CO2); | |
delay(100); | |
} | |
Serial.println("Peripheral disconnected"); | |
} | |
void actuateLED(byte CO2) { | |
CO2 = max(CO2, 2); // a brightness of 0 turns off the ring, which is not what we want! | |
// try to apply some easing to the shift in brightness | |
while (lastCO2 != CO2) { | |
pixels.setBrightness(lastCO2); | |
for (int i = 0; i < 25; i++) pixels.setPixelColor(i, pixels.Color(255, 255, 255)); // TODO superfluous? | |
pixels.show(); | |
(lastCO2 < CO2) ? lastCO2++ : lastCO2--; | |
delay(50); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
Sensing part of CO2-assemblage. | |
© Love Lagerkvist, 2020-01-07 | |
Acts as a BLE Peripheral advertising @ 19b10000-e8f2-537e-4f6c-d104768a1214 | |
Reads air quality CCS811 and delivers the current CO2 on char on ^^^ | |
Hardware setup: | |
CCS811: | |
3.3V to 3.3V pin | |
GND to GND pin | |
SDA to A4 | |
SCL to A5 | |
Licensed under GPL-3. https://www.gnu.org/licenses/gpl-3.0.en.html | |
******************************************************************************/ | |
#include <ArduinoBLE.h> | |
#include <Wire.h> | |
#include "SparkFunCCS811.h" | |
CCS811 CO2Sensor(0x5B); | |
BLEService CO2Service("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service | |
BLEByteCharacteristic CO2Characteristic("19B10001-E8F2-537E-4F6C-D104768A1214", | |
BLERead | BLENotify); | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("CO2 Sensor, waiting for Central to connect..."); | |
Wire.begin(); | |
setupCCS811(); | |
setupBLE(); | |
} | |
void loop() { | |
BLEDevice central = BLE.central(); | |
const int minCO2 = 400; | |
const int maxCO2 = 2000; | |
int CO2 = 0; | |
uint8_t mappedCO2 = 0; // 1-byte large to match a packet size of BLE | |
if (central) { | |
Serial.print("Connected to central: "); | |
Serial.println(central.address()); | |
while (central.connected()) { | |
if (CO2Sensor.dataAvailable()) { | |
CO2Sensor.readAlgorithmResults(); | |
CO2 = min(CO2Sensor.getCO2(), maxCO2); | |
mappedCO2 = map(CO2, minCO2, maxCO2, 0, 255); | |
CO2Characteristic.writeValue(mappedCO2); | |
Serial.print("CO2: "); Serial.print(CO2); Serial.print("/"); Serial.print(mappedCO2); Serial.println(); | |
} | |
delay(50); | |
} | |
} | |
} | |
void setupCCS811() { | |
CCS811Core::status returnCode = CO2Sensor.begin(); | |
if (returnCode != CCS811Core::SENSOR_SUCCESS) { | |
Serial.println(".begin() returned with an error."); | |
while(1); | |
} | |
} | |
void setupBLE() { | |
if (!BLE.begin()) { | |
Serial.println("starting BLE failed!"); | |
while(1); | |
} | |
BLE.setLocalName("CO2Monitor"); | |
BLE.setAdvertisedService(CO2Service); | |
CO2Service.addCharacteristic(CO2Characteristic); | |
BLE.addService(CO2Service); | |
CO2Characteristic.writeValue(0); | |
BLE.advertise(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment