yet another post , this time for a different problem .
I am connecting my phone app with my Arduino which works as a server and transmits sensor values to the phone.
I want to get the integers from arduino to phone and upload them from phone to ioT platform.
Everything is fine so far and i can upload values but they are all 0 .
I checked that BLE reads these values from arduino as bytes , and sends them to phone as 4 bytes.
My problem is i need to convert these 4 bytes to an integer and then send it to iot platform.
in c++ you would need something like
int buffToInteger(char * buffer)
{
int a = int((unsigned char)(buffer[0]) << 24 |
(unsigned char)(buffer[1]) << 16 |
(unsigned char)(buffer[2]) << 8 |
(unsigned char)(buffer[3]));
return a;
}
but i have no clue how to convert it Here
Ps. unsigned because i know there are positive values
Transforming in a way like , Firstly i was sending int to BLE and BLe was giving me Hex values .
Could char array be converted to something in ble ? thats what i meant
Post your Arduino Sketch as a .txt file for me to take a quick look.
Looking at your 'StringsReceived' Block, if the Thingspeak_List is initialized as an actual Blocks List, the data packet values need to be split into this List. You also will not see a List correctly in a Label by just plugging it in, you have to feed or assemble the data in value-by-value.
Is it a top secret military document or something? We prefer everything to be in the open so that others can learn too. That's one of the expediences when the Software and the Tech Support are free.
heres the sketch , i tried with dtostrf but i think it needs a lib i dont have in order to happen , cuz i get compiler error so i tried itoa for convertsion
/*
*/
#include <ArduinoBLE.h>
#define SERVICE_UUID
#define CHARACTERISTIC_UUID_TX
#define SENSOR_PIN A0
#define LO_PLUS_PIN 10
#define LO_PLUS_MIN 11
BLEService ECGservice(SERVICE_UUID );
BLEIntCharacteristic ECGchar( CHARACTERISTIC_UUID_TX, BLERead | BLENotify );
int EcgValue = 0;
static unsigned long previousMillis = 0;
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(LO_PLUS_PIN, INPUT); // Setup for leads off detection LO +
pinMode(LO_PLUS_MIN, INPUT); // Setup for leads off detection LO -
while (!Serial);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
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("Ecg Rose");
BLE.setAdvertisedService(ECGservice); // add the service UUID
ECGservice.addCharacteristic(ECGchar); // add the ecg characteristic
BLE.addService(ECGservice); // Add the battery service
ECGchar.writeValue( 0 ); // set initial value for this characteristic
/* Start advertising BLE. It will start continuously transmitting BLE
advertising packets and will be visible to remote BLE 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 BLE 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:
// while the central is connected:
while (central.connected()) {
EcgValue = analogRead(SENSOR_PIN);
long currentMillis = millis();
// Let's convert the value to a char array:
char txString[4]; // make sure this is big enuffz
// dtostrf(EcgValue, 1, 2, txString); // int_val, min_width, digits_after_decimal, char_buffer it needs Library , doesnt read it
itoa(EcgValue, txString, 10);
if (currentMillis - previousMillis >= 200) {
previousMillis = currentMillis;
ECGchar.writeValue((int)txString);
Serial.println(txString);
}
}
// when the central disconnects, turn off the LED:
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}
Its the ble example and i added characteristic for Read notify and the Ecg value to read A0 and the conversion
it doesnt go higher than 1200 when i tested and the loop needs an auto format and delete the extra spaces i did to make it more clear when i was writing