Hi All,
I followed https://www.youtube.com/watch?v=RvbWl8rZOoQ to setup a baisc app that can receive basic data from ESP32 via BLE and send the slider value to ESP. Now i want to send the current time to ESP (Current Epoch will also work). Which blocks in MIT app Invertor should i use for the same and how can i convert the time to string as it gives me error due to string and type gnu.math.IntNum
to a java.lang.String
,
Any help is appreciated. Thank you
Show us the blocks you use to send time to esp that are causing the error.
ABG
January 27, 2024, 9:57pm
4
Look in the Sensors Drawer for the Clock component.
I have the clock component, conversion and sending is the problem.
error due to string and type gnu.math.IntNum
to a java.lang.String
,
ABG
January 28, 2024, 3:47am
6
Show us the ESP32 code, all of it.
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
// BLE Service and Characteristic UUIDs
#define SERVICE_UUID "00001805-0000-1000-8000-00805f9b34fb"
#define TIME_CHAR_UUID "00002a2b-0000-1000-8000-00805f9b34fb"
#define STEP_COUNT_CHAR_UUID "00002a53-0000-1000-8000-00805f9b34fb"
#define HEART_RATE_CHAR_UUID "00002a37-0000-1000-8000-00805f9b34fb"
BLEServer* pServer = NULL;
BLECharacteristic* pTimeCharacteristic = NULL;
BLECharacteristic* pStepCountCharacteristic = NULL;
BLECharacteristic* pHeartRateCharacteristic = NULL;
bool deviceConnected = false;
unsigned long previousMillis = 0;
const long interval = 1000; // Interval in milliseconds for sending step count data
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("Connected to client");
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("Disconnected from client");
}
};
void sendHRData();
void sendStepCountData();
int getStepCount();
int getHRData();
void setup() {
Serial.begin(115200);
BLEDevice::init("ESP32"); // Initialize BLE
// Create BLE server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create BLE service
BLEService* pService = pServer->createService(SERVICE_UUID);
// Create time characteristic
pTimeCharacteristic = pService->createCharacteristic( TIME_CHAR_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
pTimeCharacteristic->addDescriptor(new BLE2902());
// Create step count characteristic
pStepCountCharacteristic = pService->createCharacteristic(STEP_COUNT_CHAR_UUID, BLECharacteristic::PROPERTY_NOTIFY);
pStepCountCharacteristic->addDescriptor(new BLE2902());
// Create heart rate characteristic
pHeartRateCharacteristic = pService->createCharacteristic(HEART_RATE_CHAR_UUID, BLECharacteristic::PROPERTY_NOTIFY);
pHeartRateCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
BLEAdvertising* pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("Characteristic defined! Now you can read it in your phone!");
}
void loop() {
// Handle BLE server events
//pServer->handleClient();
// Send step count data periodically
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
if (deviceConnected){
sendStepCountData();
sendHRData();
previousMillis = currentMillis;
}
}
}
void sendHRData(){
int HRate = getHRData();
byte HRData[sizeof(int)];
memcpy(HRData, &HRate, sizeof(int));
// Update the characteristic value
pHeartRateCharacteristic->setValue(HRData, sizeof(int));
pHeartRateCharacteristic->notify();
}
void sendStepCountData() {
// Get the step count data
int stepCount = getStepCount(); // Replace with your own logic to obtain step count
// Convert step count to byte array
byte stepCountData[sizeof(int)];
memcpy(stepCountData, &stepCount, sizeof(int));
// Update the characteristic value
pStepCountCharacteristic->setValue(stepCountData, sizeof(int));
pStepCountCharacteristic->notify();
}
int getStepCount(){
int Step = random(-10,10);
return Step;
}
int getHRData(){
int HeartRate = random(50,100);
return HeartRate;
}```
ABG
January 28, 2024, 6:27pm
8
I don't see anything that could cause that specific error message on the AI2 side in the blocks you posted.
(Though there is an opportunity for a list select out of range if your arriving bytes list is unexpectedly short.)
(Canned Reply: ABG- Export & Upload .aia)
Export your .aia file and upload it here.
.
ABG
January 28, 2024, 6:40pm
9
I notice in your original post you wanted Epoch Time, but in your blocks you only send a single byte with Clock1.Seconds(Clock1.Now()), which according to its tool tip will be a single number in the range 0-59.
That's way short of an epoch.
The Clock component has extensive formatting options in the FormatDateTime block, which you can use for format a string that hopefully you can send using a UUID devoted to text (outside of my expertise.)