How would I get the nrFToolbox for BLE app to use Arduino code written using the ArduinoBLE library? - c++

For reference, I am using the Arduino Nano 33 BLE Sense board.
At the moment, I can get the nrFToolbox app to recognize the board as a Bluetooth device, but that's about it. I would like to able to use the app to get data about the temperature and display it.
#include <Arduino_LPS22HB.h> //Pressure sensor, documentation found on https://www.arduino.cc/en/Reference/ArduinoLPS22HB
#include <Arduino_HTS221.h> //Temperature sensor, documentation found on https://www.arduino.cc/en/Reference/ArduinoHTS221
#include <ArduinoBLE.h> //Bluetooth LE sensor, documentation found on https://www.arduino.cc/en/Reference/ArduinoBLE
// Service for the BLE module, using a 128-bit UUID.
BLEService TestService("32BC370F-88AF-4928-B8AB-9B56902FA670");
//TODO: BLE temperature characteristic
//TODO: BLE humidity characteristic
//TODO: BLE pressure characteristic
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //Initialize the serial connection
while(!Serial); //Will pause until a serial connection is established.
if (!BARO.begin() ) { //Initialize the barometer
Serial.println("Unable to initialize the barometer, stopping.");
while (1);
}
if (!HTS.begin() ) { //Initialize the Temperature & Humidity sensor
Serial.println("Unable to initialize the temp/humidity sensor, stopping.");
while (1);
}
if (!BLE.begin() ) { //Initialize the bluetooth module
Serial.println("Unable to initialize the bluetooth module, stopping");
while (1);
}
/* Set a local name for the BLE device
This name will appear in advertising packets
and can be used by remote devices to identify this BLE device
The name can be changed but maybe be truncated based on space left in advertisement packet
*/
BLE.setLocalName("TestDevice");
BLE.setAdvertisedService(TestService); // add the service UUID
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("Temperature: ");
Serial.print(HTS.readTemperature(FAHRENHEIT));
Serial.print(" -- Humidity: ");
Serial.print(HTS.readHumidity() );
Serial.print(" -- Pressure: ");
Serial.println(BARO.readPressure() );
delay(1000);
}

Related

Connect output pin from MCP23017 to Input pin on another MCP23017 (2 independent arduinos)

Im struggling with the following scenario: I have 2 ESP32 boards (Dev Kit 1) and each ESP32 has 1 MCP23017 I2C module connected to it. So my goal is to use all 16 pins on MCP 1 as Output and all other 16 pins on second MCP as inputs and be able to "READ" these values (HIGH OR LOW) I Already setup connection using common ground on both ESP and wiring SDA SCL accordingly. I Already tested using input and output on same MCP unit and works fine (displays on COM port a seral.println message) but when I want to do the same thing but connecting from MCP1 output pin 1 to MCP2 input pin 1, nothing happens on the COM port, but when I unplug the jumper wire (which I use to connect input pin to output pin on the protoboard) then it does detect the voltage change and displays the message. Any idea whats going on here? Why I cant bridge from 1 MCP output to another MCP input and be able to detect this on COM port? Im using following code:
#include "Adafruit_MCP23017.h"
Adafruit_MCP23017 mcp1; // Instantiate mcp module object 1, (MCP001)
int dly = 250; // 1/4 second delay
void setup() {
Wire.begin();
Serial.begin(19200);
Serial.println("MCP module 1 Ready");
mcp1.begin(); // "Start" the mcp object
mcp1.pinMode(0, OUTPUT); //set pin "0" on MCP1 as output
mcp1.pinMode(1, INPUT); //set next pin "1" on MCP1 as input
}
void loop() {
mcp1.digitalWrite(0, HIGH); // Set pin HIGH (on)
if (mcp1.digitalRead (1 == HIGH)) {
Serial.println("Continuity Detected at Pin 1 ");
}
mcp1.digitalWrite(0, LOW); // Set pin LOW (off)
delay(dly); // On for 1/4 second
}
I solved the problem by just removing or commenting out the line 27 (mcp1.digitalWrite(0, LOW);) Now works the way I want. Thanks #romkey for your time and help.

How to read LoRa Messages in Serial monitor via heltec esp32

I am new to the esp32 and LoRa messaging.
I am following the two examples in the heltect esp32 lora library.
The two modules I am using to send and receive data have an oled display... and I print their values to the serial monitor. The data values appear on the oled display but they are random characters in the serial monitor like this... "⸮⸮3⸮⸮JS⸮⸮⸮⸮⸮J)⸮⸮".
My question is how can I receive data from the sender in string/float/integer like form so I can perform logic on them. I am new to c++ and lora so any help is welcome.
I am pretty sure this(the first code block directly below this paragraph) is the piece of code responsible for printing the received message, but it is not printing in any type of format I can work with even when I change the "char" dtype to String.
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
Receiver Code
/*
Check the new incoming messages, and print via serialin 115200 baud rate.
by Aaron.Lee from HelTec AutoMation, ChengDu, China
成都惠利特自动化科技有限公司
www.heltec.cn
this project also realess in GitHub:
https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series
*/
#include "heltec.h"
#define BAND 915E6 //you can set band here directly,e.g. 868E6,915E6
void setup() {
//WIFI Kit series V1 not support Vext control
Heltec.begin(true /*DisplayEnable Enable*/, true /*Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
Sender Code
/*
Basic test program, send date at the BAND you seted.
by Aaron.Lee from HelTec AutoMation, ChengDu, China
成都惠利特自动化科技有限公司
www.heltec.cn
this project also realess in GitHub:
https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series
*/
#include "heltec.h"
#define BAND 915E6 //you can set band here directly,e.g. 868E6,915E6
int counter = 0;
void setup() {
//WIFI Kit series V1 not support Vext control
Heltec.begin(true /*DisplayEnable Enable*/, true /*Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
}
void loop() {
Serial.print("Sending packet: ");
Serial.println(counter);
// send packet
LoRa.beginPacket();
/*
* LoRa.setTxPower(txPower,RFOUT_pin);
* txPower -- 0 ~ 20
* RFOUT_pin could be RF_PACONFIG_PASELECT_PABOOST or RF_PACONFIG_PASELECT_RFO
* - RF_PACONFIG_PASELECT_PABOOST -- LoRa single output via PABOOST, maximum output 20dBm
* - RF_PACONFIG_PASELECT_RFO -- LoRa single output via RFO_HF / RFO_LF, maximum output 14dBm
*/
LoRa.setTxPower(14,RF_PACONFIG_PASELECT_PABOOST);
LoRa.print("hello ");
LoRa.print(counter);
LoRa.endPacket();
counter++;
digitalWrite(25, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(25, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Perhaps, the serial monitor is configured with the wrong Baud Rate? In my case, default in IDE was 9600, but ESP32 sent with 115200. You can change the Baud Rate in a combobox in the serial monitor.

Failed to get the correct sensor data at firebase using nodemcu esp32. The serial monitor constantly shows zero

I have been working on the project using the DfRobot heart rate monitor sensor (wiki_Dfrobot heartrate sensor) with nodemcu esp 32. I have tested it on the digital mode (code taken from the wiki page with slight modification) and it works perfectly(able to obtain heart beat bpm range from 60-100). However, when i try to use wifi to send the data to firebase, it turns out "0" BPM and "wait for valid data" instead of normal heart beat rate (BPM). Anyone can please help with this problem ?
DFrobot heart rate cpp and h file:
https://github.com/DFRobot/DFRobot_Heartrate
I am using esp32 with Arduino IDE 1.8.12 - using real time firebase
Code(intent to send data to firebase):
#define heartratePin 32
#include "DFRobot_Heartrate.h"
#include <WiFi.h> //1. Include WiFi.h
#include "FirebaseESP32.h" //2. Include Firebase ESP32 library (this library)
// Set these to run example.
#define FIREBASE_HOST "test.firebaseio.com"
#define FIREBASE_AUTH "hW8qlM48dGkxxxxxxxx1Pq5UD21YgPq2E0c"
#define WIFI_SSID "Gummya"
#define WIFI_PASSWORD "testtest1"
DFRobot_Heartrate heartrate(DIGITAL_MODE); ///< ANALOG_MODE or DIGITAL_MODE
FirebaseData firebaseData; //3. Declare the Firebase Data object in the global scope
void setup() {
Serial.begin(115200);
initWifi();
}
void loop() {
uint8_t rateValue;
heartrate.getValue(heartratePin); ///< A1 foot sampled values
rateValue = heartrate.getRate(); ///< Get heart rate value
if(Firebase.setInt(firebaseData,"HeartBeat/BPM_data", rateValue)){
//if (firebaseData.dataType()== "Int")
Serial.println(rateValue);
}
delay(20);
}
void initWifi(){
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
//Set database read timeout to 1 minute (max 15 minutes)
Firebase.setReadTimeout(firebaseData, 1000 * 60);
//tiny, small, medium, large and unlimited.
//Size and its write timeout e.g. tiny (1s), small (10s), medium (30s) and large (60s).
Firebase.setwriteSizeLimit(firebaseData, "tiny");
}
Serial Monitor output:
Got the FirebaseESP32.h library from --> https://github.com/mobizt/Firebase-ESP32
I've searched for quite awhile but ended up with nothing, Im fairly new to arduino and programming so please help me. I will be grateful to you.
Any help will be highly appreciated. Thanks !

ESP8266 I2C slave does not acknowledge data

I have a TM4C123 processor acting as a I2C master and a ESP8266 as a slave. For the ESP I am using the Arduino IDE with ESP8266 support installed at version 2.5.2, which should support the I2C slave mode. However, I can't get it to work. Even with the Arduino slave_receiver example, the slave does not acknowledge (ACK) the master's requests which I am displaying on a scope.
To make sure I am using the right address at least once, I implemented an address sweep on the master. And to make sure I am using the right pins on the ESP I implemented the master mode on the ESP first and did a pin sweep with an I2C slave device. So I am fairly certain neither of this can be the problem.
I am using the Olimex Mod-wifi board, with SDA Pin 12 and SCL Pin 13 (Schematic here)
Can someone help me on this? Here is my code:
// Wire Slave Receiver
// by devyte
// based on the example by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this
// This example code is in the public domain.
#include <Wire.h>
#define SDA_PIN 12
#define SCL_PIN 13
const int16_t I2C_SLAVE = 0x12;
void setup() {
Serial.begin(115200); // start serial for output
Wire.begin(SDA_PIN, SCL_PIN, I2C_SLAVE); // new syntax: join i2c bus (address required for slave)
Wire.onReceive(receiveEvent); // register event
}
void loop() {
delay(1000);
Serial.println("Loop");
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(size_t howMany) {
(void) howMany;
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
I had a compareable issue with the ESP8266. It was no problem sending data between the Arduino Nano (Slave) to the ESP8266 (Master) via I²C. But when I switch the modes (Arduino Nano = Master and ESP8266 = Slave) the Wire example doesn't work.
My workaround for this issue was to reduce the I²C working frequence from 100kHz to about 20kHz.

SD.remove() is not removing a file on Arduino C++

I have two sketches I am running on an Arduino Uno. The first one dumps a file to the serial if it exists. This is one of the examples that comes with the Arduino, but I've modified it as such:
/*
SD card file dump
This example shows how to read a file from the SD card using the
SD library and send it over the serial port.
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
Created 22 December 2010 by Limor Fried
Modified 9 Apr 2012 by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // Wait for serial port to connect. Needed for Leonardo only.
}
Serial.print("Initializing SD card...");
// Make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// See if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// Don't do anything more:
return;
}
Serial.println("card initialized.");
// Open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt");
// If the file is available, write to it:
if (dataFile) {
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close();
}
// If the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
void loop()
{
}
My other sketch is supposed to remove a file. When I run this remove sketch it says there is no file found. Yet I can continually run the above sketch and dump the contents to the serial. My remove sketch is as follows:
#include <SD.h>
const int chipSelect = 4;
void setup(){
Serial.begin(115200);
Serial.print("Initializing SD card...");
// Make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
if(SD.exists("datalog.txt"))
{
SD.remove("datalog.txt");
Serial.println("file removed");
}
else
{
Serial.println("no file to remove");
}
}
void loop(){
}
Am I missing something here?
Just after posting this I realized I was not making this remove sketch fault tolerant and added the following code after the pinmode line:
// See if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
Thus the new remove sketch was as follows:
#include <SD.h>
const int chipSelect = 4;
void setup(){
Serial.begin(115200);
Serial.print("Initializing SD card...");
// Make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// See if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// Don't do anything more:
return;
}
Serial.println("card initialized.");
if(SD.exists("datalog.txt"))
{
SD.remove("datalog.txt");
Serial.println("file removed");
}
else
{
Serial.println("no file to remove");
}
}
void loop(){
}
Upon running that sketch it now removes the files. Why is it that new version works but the old one did not?
Adding SD.begin() doesn't make it fault tolerant. It initializes the library. You need to call that before calling other functions. From the Reference:
begin() Initializes the SD library and card. This begins use of the
SPI bus (digital pins 11, 12, and 13 on most Arduino boards; 50, 51,
and 52 on the Mega) and the chip select pin, which defaults to the
hardware SS pin (pin 10 on most Arduino boards, 53 on the Mega).