I am building a weather bot and want to set the internal RTC of the 101.
The 101 does not have a battery for the RTC and every time it is powered up the clock resets.
I can set it in hard code, but this means reloading the program each time.
What I want to do is get the current time from my android (or desktop) using the standard BLE CurrentTime service, when it connects and set the RTC from this characteristic.
Here is my code so far, but it is throwing errors related to the CurrentTime service. I think it is telling me I have a variable type mismatch (I always struggle with these).
I have pared it down to mostly the CurrentTime Service and the Device Information service.
Note: the device information service code works and lets me see current revision date of my code on the android. You can also pass serial number, model number, etc. if you want.
Can someone tell me what I am doing wrong, or point me to an example where someone else has pulled the datetime from BLE (I haven't found any examples).
/* Arduino 101
-test for current time service */
#define BotName "WeatherBot"
#define Version "12/29/16 " //0x2A28 utf8s
//#define MfgName "JLD" //0x2A29 utf8s
//#define ModelNum "000001" //0x2A24 utf8s
//#define SerialNum "000001" //0x2A25 utf8s
//#define HardwareRev "0001" //0x2A27 utf8s
//#define FirmwareRev "0001" //0x2A26 utf8s
//#define SystemID "000001" //0x2A23 uint40
#include <CurieBLE.h>
// initialize Real time clock
#include <CurieTime.h>
char DateString[16];
char TimeString[16];
char date[16];
//initialize variables
unsigned long currentMillis = millis();
unsigned long prevSensor = 0;
const int sensorFreq = 1000;
bool serialFlag = 1;
char state = 'z';
char cmd, LCmd = ' ';
#define ledPin 13
#define pwrMOSFET 13
bool pwrFlag = 1;
BLEPeripheral blePeripheral;
bool BLEstatus = false;
//* create Device Info Service and Characteristic
BLEService DeviceInfoService("180A");
BLECharacteristic SoftwareRevision("2A28", BLERead, 10);
// change this it throws errors !!
//create Current Time Service and Characteristic
BLEService CurrentTimeService("1805");
BLECharacteristic CurrentTimeCharacteristic("2A2B", BLERead | BLEWrite | BLENotify); //exacttime 256
timezone, uint16 dstoffset
void setup()
{
Serial.begin(115200);
//* set advertised local name and service UUID:
blePeripheral.setLocalName(BotName); //make a unique name
// assign event handlers
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
//* change this to get time from BLE central
// set the current time to 14:27:00, December 14th, 2015
//setTime(14, 27, 00, 14, 12, 2016);
blePeripheral.setAdvertisedServiceUuid(CurrentTimeService.uuid());
blePeripheral.addAttribute(CurrentTimeService);
blePeripheral.addAttribute(CurrentTimeCharacteristic);
CurrentTimeCharacteristic.setEventHandler(BLEWritten, CurrentTimeCharacteristicWritten);
//*/
blePeripheral.setAdvertisedServiceUuid(DeviceInfoService.uuid());
blePeripheral.addAttribute(DeviceInfoService);
blePeripheral.addAttribute(SoftwareRevision);
static const uint8_t Version2[] = Version;
SoftwareRevision.setValue(Version2,10);
// begin advertising BLE service:
blePeripheral.begin();
if (serialFlag)
{ delay(5000); //while(!Serial);
Serial.println(F("initialization complete"));
Serial.println(F("Bluetooth device active, waiting for connections..."));
Serial.print(F("Starting! ")); Serial.print(BotName);
Serial.print(F(" ")); Serial.println(Version);
}
} // end of setup
void loop()
{
//* check to see if character received from serial port
if (!Serial.available() < 1)
{
cmd = Serial.read();
decode (cmd);
} // end of check serial */
//*
if (BLEstatus )
{
// only do this if BLE is connected
if(currentMillis - prevSensor > sensorFreq)
{
}
} // end if central connected actions */
} // end of void loop
void blePeripheralConnectHandler(BLECentral& central)
{
BLEstatus = true;
digitalWrite(ledPin, HIGH);
decode('x');
if (serialFlag)
{
Serial.print(F("Connected event, central: "));
Serial.println(central.address());
}
} // end of connect handler *****/
void blePeripheralDisconnectHandler(BLECentral& central)
{
BLEstatus = false;
digitalWrite(ledPin, LOW);
pwrFlag = 0;
decode('z');
if (serialFlag)
{
Serial.print(F("Disconnected event, central: "));
Serial.println(central.address());
}
delay(100);
} // end of disconnect handler
void CurrentTimeCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic)
{
if (characteristic.value()) // NULL pointer check
{
//date[] = *characteristic.value(); // This throws error for wrong type
Serial.print("time Characteristic event, written: ");
//Serial.println(date);
}
} // end of datetime written
void decode(char a)
{
LCmd = a;
if (serialFlag)
{Serial.println(a);}
switch(a)
{
case '=': // toggle serial output flag
serialFlag=!serialFlag;
break;
case 'z': // toggle power flag off
state = 'z';
pwrFlag = 0;
digitalWrite (pwrMOSFET, pwrFlag);
break;
case 'x': // toggle power flag on
state = 'x';
pwrFlag = 1;
digitalWrite (pwrMOSFET, pwrFlag);
break;
} // end of switch case
} // end of decode