UoK Shed IOT Crossing
Dependencies: EthernetInterface MQTT mbed-dsp mbed-rtos mbed
Fork of KL25Z_FFT_Demo_tony by
Revision 6:78106c78e577, committed 2016-01-14
- Comitter:
- Condo2k4
- Date:
- Thu Jan 14 16:20:28 2016 +0000
- Parent:
- 5:4152530c0cf5
- Commit message:
- Fixes
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 4152530c0cf5 -r 78106c78e577 main.cpp --- a/main.cpp Mon Jan 11 11:03:24 2016 +0000 +++ b/main.cpp Thu Jan 14 16:20:28 2016 +0000 @@ -14,7 +14,7 @@ //#define HOSTNAME "doughnut.kent.ac.uk" #define PORT 1883 -enum Mode {RESPONCIVE, OVERRIDE}; +enum Mode {RESPONSIVE, OVERRIDE}; //timer Ticker timer; @@ -35,24 +35,31 @@ //state bool personPresent = false; -volatile Mode mode = RESPONCIVE; +volatile Mode mode = RESPONSIVE; volatile bool checkPresence = false; +volatile bool turnLightsOn = false; +volatile bool turnLightsOff = false; + void presenceTick() { checkPresence = true; } void timeout() { - if(mode==RESPONCIVE) pelican = false; + if(mode==RESPONSIVE) { + turnLightsOff = true; + } timer.detach(); } void messageArrived(MQTT::MessageData& md) { MQTT::Message &msg = md.message; - if(msg.payloadlen>0) { - switch(*(char*)msg.payload) { - case 'r': case 'R': mode = RESPONCIVE; if(!personPresent) timer.attach(&timeout,3.0f); break; - case 'o': case 'O': mode = OVERRIDE; pelican=true; break; + + char mode; + if(sscanf((char*)msg.payload, "{\"mode\":\"%c\"}", &mode)) { + switch(mode) { + case 'r': case 'R': mode = RESPONSIVE; if(!personPresent) timer.attach(&timeout,3.0f); break; + case 'o': case 'O': mode = OVERRIDE; turnLightsOn=true; break; } } } @@ -88,9 +95,13 @@ if(checkPresence) { float s = sensor; if((s<0.5f)!=personPresent) { //state changed - personPresent = !personPresent; + personPresent = s<0.5f; - sprintf(mqtt_buffer, "{\"personPresent\":%d}", personPresent); + if(mode==RESPONSIVE && personPresent) { + sprintf(mqtt_buffer, "{\"personPresent\":1, \"lightState\":1}"); + } else { + sprintf(mqtt_buffer, "{\"personPresent\":%d}", personPresent); + } message.qos = MQTT::QOS0; // Send at least once // Do not null terminate -- we have a length field, and it will piss off the JS front end message.payloadlen = strlen(mqtt_buffer); @@ -104,6 +115,26 @@ } checkPresence = false; } + if(turnLightsOn) { + pelican = true; + sprintf(mqtt_buffer, "{\"lightState\":1}"); + message.qos = MQTT::QOS0; // Send at least once + // Do not null terminate -- we have a length field, and it will piss off the JS front end + message.payloadlen = strlen(mqtt_buffer); + message.payload = (void*)mqtt_buffer; + m_client.publish(personTopic, message); + turnLightsOn = false; + } + if(turnLightsOff) { + pelican = false; + sprintf(mqtt_buffer, "{\"lightState\":0}"); + message.qos = MQTT::QOS0; // Send at least once + // Do not null terminate -- we have a length field, and it will piss off the JS front end + message.payloadlen = strlen(mqtt_buffer); + message.payload = (void*)mqtt_buffer; + m_client.publish(personTopic, message); + turnLightsOff = false; + } m_client.yield(100); }