#include <ArduinoBLE.h>
// #include <ArduinoLowPower.h>
#include <GxEPD2_BW.h>
#include <GxEPD2_3C.h>
#include <GxEPD2_7C.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include "GxEPD2_display_selection_new_style.h"
#include "GxEPD2_display_selection.h"
#include "GxEPD2_display_selection_added.h"
// // Bluetooth® Low Energy Battery Service
//BLEService sbService("180F");
//
//// Bluetooth® Low Energy Battery Level Characteristic
//BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
// BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
BLEService smartBinService("19B10010-E8F2-537E-4F6C-D1047652ABCD"); // create service
// create switch characteristic and allow remote device to read and write
BLEUnsignedCharCharacteristic weightCharacteristic("19B10010-E8F2-537E-4F6C-D1047652ABC1", BLERead | BLENotify);
// create button characteristic and allow remote device to get notifications
BLEUnsignedCharCharacteristic levelCharacteristic("19B10010-E8F2-537E-4F6C-D1047652ABC2", BLERead | BLENotify);
BLEStringCharacteristic itemNameCharacteristic( "19B10010-E8F2-537E-4F6C-D1047652ABC3", BLERead | BLEWrite, 20 );
BLEStringCharacteristic itemBinPositionCharacteristic( "19B10010-E8F2-537E-4F6C-D1047652ABC4", BLERead | BLEWrite, 5 );
BLEUnsignedCharCharacteristic itemMaxWeightCharacteristic( "19B10010-E8F2-537E-4F6C-D1047652ABC5", BLERead | BLEWrite);
int sensorPin = A1; // select the input pin for the potentiometer
int oldWeight = 0; // last battery level reading from analog input
int oldLevel = 0; // last battery level reading from analog input
int eink_weight_level = 0;
long previousMillis = 0; // last time the battery level was checked, in ms
int green = 2;
int red = 3;
int refresh_rate = 30000;
void setup() {
Serial.begin(9600); // initialize serial communication
//while (!Serial);
pinMode(red, OUTPUT); //define red pin
pinMode(green, OUTPUT); //define green pin
pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
einkDisplay_setup();
/* Set a local name for the Bluetooth® Low Energy device
This name will appear in advertising packets
and can be used by remote devices to identify this Bluetooth® Low Energy device
The name can be changed but maybe be truncated based on space left in advertisement packet
*/
BLE.setLocalName("WeightMonitor");
BLE.setDeviceName("WeightMonitor");
BLE.setAdvertisedService(smartBinService); // add the service UUID
smartBinService.addCharacteristic(weightCharacteristic); // add the weight characteristic
smartBinService.addCharacteristic(levelCharacteristic); // add the weight level characteristic
smartBinService.addCharacteristic(itemNameCharacteristic); // add the item name characteristic
smartBinService.addCharacteristic(itemBinPositionCharacteristic); // add the bin position characteristic
smartBinService.addCharacteristic(itemMaxWeightCharacteristic); // add the item max weight characteristic
BLE.addService(smartBinService); // Add the battery service
weightCharacteristic.writeValue(oldWeight); // set initial value for this characteristic
levelCharacteristic.writeValue(oldLevel); // set initial value for this characteristic
itemNameCharacteristic.writeValue("Not Set");
itemBinPositionCharacteristic.writeValue("NA");
itemMaxWeightCharacteristic.writeValue(50);
/* Start advertising Bluetooth® Low Energy. It will start continuously transmitting Bluetooth® Low Energy
advertising packets and will be visible to remote Bluetooth® Low Energy central devices
until it receives a new connection */
// start advertising
BLE.advertise();
Serial.println("Bluetooth® device active, waiting for connections...");
}
void loop() {
// wait for a Bluetooth® Low Energy central
BLEDevice central = BLE.central();
// if a central is connected to the peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's BT address:
Serial.println(central.address());
// turn on the LED to indicate the connection:
digitalWrite(LED_BUILTIN, HIGH);
// check the battery level every 200ms
// while the central is connected:
while (central.connected()) {
long currentMillis = millis();
// // if 200ms have passed, check the battery level:
if (currentMillis - previousMillis >= refresh_rate) {
previousMillis = currentMillis;
Serial.println("Central connected");
// Serial.println("going into deep sleep");
// LowPower.sleep(5000);
// Serial.println("ended deep sleep");
updateWeightLevel();
einkDisplay(1,eink_weight_level,itemNameCharacteristic.value(),itemBinPositionCharacteristic.value()); // void einkDisplay(int bleConn,int stock, char itemName[],char itemLocation[] )
}
}
// when the central disconnects, turn off the LED:
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
Serial.println("No Central connected ");
updateWeightLevel();
einkDisplay(0,eink_weight_level,itemNameCharacteristic.value(),itemBinPositionCharacteristic.value()); // void einkDisplay(int bleConn,int stock, char itemName[],char itemLocation[] )
delay(refresh_rate);
}
void updateWeightLevel() {
/* Read the current voltage level on the A0 analog input pin.
This is used here to simulate the charge level of a battery.
*/
int weightRead = analogRead(A1);
//int batteryLevel = map(battery, 0, 1023, 0, 100);
int weightLevel = 0.0088*weightRead*weightRead+0.2676*weightRead+0.3029;
int weightPercentage = weightLevel*100/itemMaxWeightCharacteristic.value();
Serial.println(itemNameCharacteristic.value());
Serial.println(itemBinPositionCharacteristic.value());
Serial.println(itemMaxWeightCharacteristic.value());
Serial.print("Voltage Level % is now: "); // print it
Serial.println(weightRead);
int weightPercentageInt = (int)weightPercentage; //typecast byte into int
//Serial.println(weightPercentageInt);
if (weightPercentageInt>75) {
Serial.println("Green");
digitalWrite(green,HIGH);
digitalWrite(red,LOW);
eink_weight_level = 3;
} else if (weightPercentageInt>25) {
Serial.println("Yellow");
digitalWrite(green,HIGH);
digitalWrite(red,HIGH);
eink_weight_level = 2;
} else {
Serial.println("Red");
digitalWrite(green,LOW);
digitalWrite(red,HIGH);
eink_weight_level = 1;
//delay(500);
}
if (weightLevel != oldLevel) { // if the battery level has changed
Serial.print("Weight Level % is now: "); // print it
Serial.println(weightRead);
weightCharacteristic.writeValue(weightLevel); // and update the weight characteristic
levelCharacteristic.writeValue(weightPercentage); // and update the weight characteristic
oldLevel = weightLevel; // save the level for next comparison
}
}
void einkDisplay_setup()
{
display.init(115200, true, 2, false); // USE THIS for Waveshare boards with "clever" reset circuit, 2ms reset pulse
display.setRotation(3);
display.setFont(&FreeMonoBold9pt7b);
display.setTextColor(GxEPD_BLACK);
display.setFullWindow();
display.firstPage();
}
void einkDisplay(int bleConn,int stock, String itemName,String itemLocation )
{
// display.setRotation(3);
// display.setFont(&FreeMonoBold9pt7b);
// display.setTextColor(GxEPD_BLACK);
// display.setFullWindow();
// display.firstPage();
do
{
//width is 212, height is 104
display.fillScreen(GxEPD_WHITE);
display.setCursor(0, 100); //Cursor is set to bottom left corner of text
if (bleConn==1){
display.print("BLE Connected");
}
else {
display.print("BLE Not Connected");
}
display.setCursor(0, 30);
display.print("Item: ");
display.print(itemName);
display.setCursor(0, 50);
display.print("Bin: ");
display.print(itemLocation);
display.setCursor(0, 70);
display.print("Stock: ");
if (stock == 1){
display.print("Low");
// display.fillRect(box_x, box_y, box_w, box_h, GxEPD_BLACK); box_x, box_y are top left corner of box
display.fillRect(0, 0, display.width(), 15, GxEPD_BLACK); //fill with black to show stock low
// display.fillRect(0, 0, display.width(), 15, GxEPD_RED); //fill with red to show stock low
}
else if(stock == 2) {
display.print("Mid");
// display.fillRect(box_x, box_y, box_w, box_h, GxEPD_BLACK); box_x, box_y are top left corner of box
display.fillRect(0, 0, display.width(), 15, GxEPD_WHITE); //fill with black to show stock low
}
else {
display.print("High");
// display.fillRect(box_x, box_y, box_w, box_h, GxEPD_BLACK); box_x, box_y are top left corner of box
display.fillRect(0, 0, display.width(), 15, GxEPD_WHITE); //fill with black to show stock low
}
}
while (display.nextPage());
display.hibernate();
}
I am using the GxEPD2 libary
The arduino nano 33 ble sense is not in the display selection of the GxEPD2_display_selection_new_style.h
It is able to compile on the MKR 1000 but not in the nano