Broadcast IO pin readings over BLE

Hi all,

I would like to use BLE on an Arduino RP2040 Nano Connect to broadcast readings of 3 IO pins. This data will be picked up by a Raspberry Pi running TheengsGateway.

The code below is not broadcasting any of the added characteristics. From the Raspberry Pi this is the output home/TheengsGateway/BTtoMQTT/30C6F7018E72 {"name": "RP2040", "id": "30:C6:F7:01:8E:72", "rssi": -60}

Am I missing something in my code for this to work?

Thank you.

#include <ArduinoBLE.h>

const int PSON = A3;
const int SSR = 8;
const int FAN = 6;

unsigned long ssrTimer = 0;  // SSR timer
unsigned long fanTimer = 0;  // Fan timer

int psState = 0;   // PSON status
int ssState = 0;   // SSR status
int fnState = 0;   // FAN status

BLEService ControlService("7b4e5c33-e8db-44e1-a329-919a56016229");

BLEIntCharacteristic PSonCharacteristic("3b2984e2-2997-4c75-b927-09691d056836", BLERead | BLENotify | BLEBroadcast);
BLEIntCharacteristic SSRCharacteristic("3b2984e2-2997-4c75-b927-09691d056837", BLERead | BLENotify | BLEBroadcast);
BLEIntCharacteristic FANCharacteristic("3b2984e2-2997-4c75-b927-09691d056838", BLERead | BLENotify | BLEBroadcast);

void setup() {
  pinMode(PSON, INPUT);  // analog pin A3 as input
  pinMode(SSR, OUTPUT);  // digital pin 8 as output
  pinMode(FAN, OUTPUT);  // digital pin 6 as output

  digitalWrite(SSR, LOW);
  digitalWrite(FAN, LOW);

  if (!BLE.begin()) {
  }

  // set the local name peripheral advertises
  BLE.setLocalName("RP2040");

  // set the UUID for the service this peripheral advertises:
  BLE.setAdvertisedService(ControlService);

  // add the characteristics to the service
  ControlService.addCharacteristic(PSonCharacteristic);
  ControlService.addCharacteristic(SSRCharacteristic);
  ControlService.addCharacteristic(FANCharacteristic);

  // add the service
  BLE.addService(ControlService);

  PSonCharacteristic.writeValue(psState);
  SSRCharacteristic.writeValue(ssState);
  FANCharacteristic.writeValue(fnState);

  BLE.setAdvertisingInterval(8000);

  // start advertising
  BLE.advertise();
}

void loop() {

  psState = analogRead(PSON);  // read analog input status
  ssState = digitalRead(SSR);  // read digital output status
  fnState = digitalRead(FAN);  // read digital output status

  // call poll() regularly to allow the library to send MQTT and prevent disconnection by the broker
  BLE.poll();

  if (psState > 50) {
    ssrTimer = 100;  // delay SSR for 10 seconds
  }

  psOFF();  // run psOFF

  if (ssState == 1) {
    fanTimer = 100;  // delay Fan for 10 seconds
  }

  fnOFF();

  tele();
}

void psOFF() {
  static unsigned long sspreviousTime;
  unsigned long sscurrentTime = millis();  // return time since board active

  // move forward if 100 ms has elapsed
  if (sscurrentTime - sspreviousTime < 100) {
    return;
  }

  sspreviousTime = sscurrentTime;

  if (!ssrTimer) {
    digitalWrite(SSR, LOW);  // if timer is done or still 0, set digital output LOW
  } else {
    digitalWrite(SSR, HIGH);  // set digital output HIGH
    digitalWrite(FAN, HIGH);  // set ditigal output HIGH
    ssrTimer--;
  }
}

void fnOFF() {
  static unsigned long fnpreviousTime;
  unsigned long fncurrentTime = millis();  // return time since board active

  // move forward if 100 ms has elapsed
  if (fncurrentTime - fnpreviousTime < 100) {
    return;
  }

  fnpreviousTime = fncurrentTime;

  if (!fanTimer) {
    digitalWrite(FAN, LOW);  // if timer is done or still 0, set digital output LOW
  } else {
    digitalWrite(FAN, HIGH);  // set digital output HIGH
    fanTimer--;
  }
}

void tele() {
  PSonCharacteristic.writeValue(psState);
  SSRCharacteristic.writeValue(ssState);
  FANCharacteristic.writeValue(fnState);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.