//Callback function that is called when a commend is receied void callback(const char *key, const char *value) { //pc.printf("CallBack %s : %s\n",key, value); if(strstr(value,"modeoff")!=NULL) { pc.printf("CallBack modeOFF\n\r"); modeManager.setMode(OFF); modeManager.setModeJustChanged(true); } else if(strstr(value,"modeauto")!=NULL) { pc.printf("CallBack modeAUTO\n\r"); modeManager.setMode(AUTO); modeManager.setModeJustChanged(true); } else if(strstr(value,"modeon")!=NULL) { pc.printf("CallBack modeON\n\r"); modeManager.setMode(ON); modeManager.setModeJustChanged(true); } }
int main() { //Declaration of the object LedCmd ledcommander; LightDetector lightDetector; FailureDetector failureDetector; PowerConsumption powerConsumption; PreviousState previousState=LIGHT_OFF; Mode previousMode=AUTO; bool isFirstTime=true; float power=0; float lightSensor=0; char str1[16]; char str2[16]; char* server="eu.airvantage.net"; char* apn="internet.maingate.se"; int port = 1883; char* password="******"; dout1=0; //Step 1 : Initialize in AUTO Mode modeManager.setMode(AUTO); //Step 2 Configure the Serial connection(used to communication with the Kompel card) pc.baud(115200); //Step 3 Start the Kompel card startKompel(); wait(2); //Step 4 : MQTTClient client(server, &callback, password, port, &pc, apn); while(1) { if(client.connected == true) { //Green Light On dout1 = 1; if(modeManager.getMode() == AUTO) { //Check first if the ambiant light is under the threshold if(lightDetector.isLightUnderTrigger()) { wait(0.5); //If under, check if there is a failure and publish just once if(failureDetector.isFailureDetected()) { if(previousState!=FAIL) { wait(0.1); ledcommander.setOff(); wait(0.5); sprintf(str1,"%f",0.0); lightSensor = lightDetector.getLightSensorValue(); sprintf(str2,"%f",lightSensor); client.pub("st.power",str1,"st.lightsensor",str2,"st.lightstatus","on","st.failuredetected","true"); blinkPublish(); previousState = FAIL; } } //If under and no failure, publish just once else { if(previousMode == OFF || previousState == LIGHT_OFF || previousState == FAIL || previousMode == ON) { wait(0.1); ledcommander.setOn(); wait(0.5); power = powerConsumption.getPower(); sprintf(str1,"%f",power); lightSensor=lightDetector.getLightSensorValue(); sprintf(str2,"%f",lightSensor); client.pub("st.power",str1,"st.lightsensor",str2,"st.lightstatus","on","st.failuredetected","false"); blinkPublish(); modeManager.setModeJustChanged(false); previousState = LIGHT_ON; } } } //if the ambiant light isn't under the threshold, the lamp switches off. else { ledcommander.setOff(); if(isFirstTime == true || previousMode == OFF || previousState == LIGHT_ON || previousState == FAIL || previousMode == ON) { wait(0.1); sprintf(str1,"%f",0.0); lightSensor = lightDetector.getLightSensorValue(); sprintf(str2,"%f",lightSensor); client.pub("st.power",str1,"st.lightsensor",str2,"st.lightstatus","off","st.failuredetected","false"); blinkPublish(); modeManager.setModeJustChanged(false); previousState = LIGHT_OFF; isFirstTime = false; } } previousMode = AUTO; } //When a command Mode OFF is received, the application switches in the OFF mode. The lamp will be always off. else if(modeManager.getMode() == OFF) { if(previousMode == AUTO || previousMode == ON) { ledcommander.setOff(); wait(0.1); sprintf(str1,"%f",0.0); lightSensor=lightDetector.getLightSensorValue(); sprintf(str2,"%f",lightSensor); client.pub("st.power",str1,"st.lightsensor",str2,"st.lightstatus","off","st.failuredetected","false"); blinkPublish(); modeManager.setModeJustChanged(false); } previousState = LIGHT_OFF; previousMode = OFF; } //When a command Mode ON is received, the application switches in the ON mode. The lamp will be always on. else if(modeManager.getMode() == ON) { if(previousMode == AUTO || previousMode == OFF) { ledcommander.setOn(); wait(0.1); power = powerConsumption.getPower(); sprintf(str1,"%f",power); lightSensor = lightDetector.getLightSensorValue(); sprintf(str2,"%f",lightSensor); client.pub("st.power",str1,"st.lightsensor",str2,"st.lightstatus","on","st.failuredetected","false"); blinkPublish(); modeManager.setModeJustChanged(false); } previousState = LIGHT_ON; previousMode = ON; } } //If no more connected, try to reconnect. else { pc.printf("Deconnected\n"); //Green Light OFF dout1=0; client.reconnect(); previousState=LIGHT_OFF; previousMode=AUTO; } //Call the background task (i.e. Ping task + Receiv task) client.loop(); }//while(1) }