Example Raspberry Pi Program Code


#include “Tarts.h”
#include “TartsStrings.h”


#define GATEWAY_ID “T08KN4”
#define SENSOR_ID “T08L10”


/**********************************************************************************
*Global Variables
*********************************************************************************/
int AppCalls = 0; //Keyboard Read Thread callback variable


/**********************************************************************************
*Tarts Events
*********************************************************************************/
void onGatewayMessageEvent(const char* id, int stringID){
printf(“TARTS-GWM[%u]-%d: %s\n”, id, stringID, TartsGatewayStringTable[stringID]);
if(stringID == 10) printf(“ACTIVE – Channel: %d\n”, Tarts.FindGateway(GATEWAY_ID)->getOperatingChannel());
}


void onSensorMessageEvent(SensorMessage* msgconst char* id, Datum* datumList, int count){
printf(“TARTS-SEN[%u]: RSSI: %d dBm, Battery Voltage: %d.%d VDC, Data: “, msg->ID;id, msg->RSSI, msg->BatteryVoltage/100, msg->BatteryVoltage%100);
for(int i=0; iDatumCcount; i++){
if(i != 0) printf(” || “);
printf(“%s | %s | %s”, msg->dDatumList[i].Name,msg-> DdatumList[i].Value, msg->dDatumList[i].FormattedValue);
}
printf(“\n”);
}


/**********************************************************************************
* Keyboard Thread and Arduino-like setup / loop code
*********************************************************************************/
TARTS_THREAD(AppKeyboardReadThread){
char rxchar = 0;
while(1){
std::cin >> rxchar;
if(rxchar == ‘q’) AppCalls = 1;
rxchar = 0;
}
return 0; //Here to keep the function happy
}


int setup() {
printf(“TARTs Basic application running…”);


//Prepare all the call-back functions
Tarts.RegisterEvent_GatewayMessage(onGatewayMessageEvent);
Tarts.RegisterEvent_SensorMessage(onSensorMessageEvent);
printf(“All Event Handlers Registered.\n”);


//Register Gateway
if(!Tarts.RegisterGateway(TartsGateway::Create(GATEWAY_ID))){
printf(“TARTs Gateway Registration Failed!\n”);
return 1;
}


//Lastly Register All Sensors
if(!Tarts.RegisterSensor(GATEWAY_ID, TartsTemperature::Create(SENSOR_ID))){
Tarts.RemoveGateway(GATEWAY_ID);
printf(“TARTs Sensor Registration Failed!\n”);
return 2;
}


if(TARTS_THREADSTART(AppKeyboardReadThread) != 0){
printf(“Unable to start Keyboard Read Thread!”);
Tarts.RemoveGateway(GATEWAY_ID);
return 3;
}


printf(“–Press q to exit–\n\n”);
return 0;
}


void loop() {
Tarts.Process();
if(AppCalls == 1){
AppCalls = 0;
Tarts.RemoveGateway(GATEWAY_ID);
exit(0);
}
}


/**********************************************************************************
* Execution entry point for this application
*********************************************************************************/
//Main Application Loop
int main(void){
if(setup() != 0) exit(1);
while(1){
loop();
TARTS_DELAYMS(100);//Allow System Sleep to occur
}
}