UoK Shed IOT Crossing

Dependencies:   EthernetInterface MQTT mbed-dsp mbed-rtos mbed

Fork of KL25Z_FFT_Demo_tony by Leicester Hackspace

Committer:
Condo2k4
Date:
Thu Jan 14 16:20:28 2016 +0000
Revision:
6:78106c78e577
Parent:
5:4152530c0cf5
Fixes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tony1tf 0:b8c9dffbbe7e 1 // Audio Spectrum Display
tony1tf 0:b8c9dffbbe7e 2 // Copyright 2013 Tony DiCola (tony@tonydicola.com)
tony1tf 0:b8c9dffbbe7e 3 // Code ported from the guide at http://learn.adafruit.com/fft-fun-with-fourier-transforms?view=all
tony1tf 0:b8c9dffbbe7e 4 // mods by Tony Abbey to simplify code to drive tri-colour LED as a "colour organ"
tony1tf 0:b8c9dffbbe7e 5
tony1tf 0:b8c9dffbbe7e 6 #include "mbed.h"
tony1tf 0:b8c9dffbbe7e 7
Condo2k4 3:54760764be66 8 #include "EthernetInterface.h"
Condo2k4 3:54760764be66 9 #include "MQTTEthernet.h"
Condo2k4 3:54760764be66 10 #include "MQTTClient.h"
tony1tf 0:b8c9dffbbe7e 11
Condo2k4 3:54760764be66 12 #define IP_RETRIES 2
Condo2k4 3:54760764be66 13 #define HOSTNAME "192.168.0.1"
Condo2k4 3:54760764be66 14 //#define HOSTNAME "doughnut.kent.ac.uk"
Condo2k4 3:54760764be66 15 #define PORT 1883
tony1tf 0:b8c9dffbbe7e 16
Condo2k4 6:78106c78e577 17 enum Mode {RESPONSIVE, OVERRIDE};
tony1tf 0:b8c9dffbbe7e 18
Condo2k4 5:4152530c0cf5 19 //timer
Condo2k4 5:4152530c0cf5 20 Ticker timer;
Condo2k4 5:4152530c0cf5 21
Condo2k4 3:54760764be66 22 //buffers
Condo2k4 3:54760764be66 23 char mqtt_buffer[100];
Condo2k4 3:54760764be66 24 MQTT::Message message;
tony1tf 0:b8c9dffbbe7e 25
Condo2k4 3:54760764be66 26 //topics
Condo2k4 3:54760764be66 27 char personTopic[] = "shed/masterclass/iot/crossing/person";
Condo2k4 3:54760764be66 28 char modeTopic[] = "shed/masterclass/iot/crossing/mode";
tony1tf 0:b8c9dffbbe7e 29
Condo2k4 3:54760764be66 30 //io
Condo2k4 3:54760764be66 31 AnalogIn sensor(A0);
Condo2k4 3:54760764be66 32 DigitalOut GND(A2);
Condo2k4 3:54760764be66 33 DigitalOut pelican(D0);
Condo2k4 3:54760764be66 34 Ticker presenceTimer;
tony1tf 0:b8c9dffbbe7e 35
Condo2k4 3:54760764be66 36 //state
Condo2k4 3:54760764be66 37 bool personPresent = false;
Condo2k4 6:78106c78e577 38 volatile Mode mode = RESPONSIVE;
Condo2k4 3:54760764be66 39 volatile bool checkPresence = false;
tony1tf 0:b8c9dffbbe7e 40
Condo2k4 6:78106c78e577 41 volatile bool turnLightsOn = false;
Condo2k4 6:78106c78e577 42 volatile bool turnLightsOff = false;
Condo2k4 6:78106c78e577 43
Condo2k4 3:54760764be66 44 void presenceTick() {
Condo2k4 3:54760764be66 45 checkPresence = true;
tony1tf 0:b8c9dffbbe7e 46 }
tony1tf 0:b8c9dffbbe7e 47
Condo2k4 5:4152530c0cf5 48 void timeout() {
Condo2k4 6:78106c78e577 49 if(mode==RESPONSIVE) {
Condo2k4 6:78106c78e577 50 turnLightsOff = true;
Condo2k4 6:78106c78e577 51 }
Condo2k4 5:4152530c0cf5 52 timer.detach();
Condo2k4 5:4152530c0cf5 53 }
Condo2k4 5:4152530c0cf5 54
Condo2k4 3:54760764be66 55 void messageArrived(MQTT::MessageData& md) {
Condo2k4 3:54760764be66 56 MQTT::Message &msg = md.message;
Condo2k4 6:78106c78e577 57
Condo2k4 6:78106c78e577 58 char mode;
Condo2k4 6:78106c78e577 59 if(sscanf((char*)msg.payload, "{\"mode\":\"%c\"}", &mode)) {
Condo2k4 6:78106c78e577 60 switch(mode) {
Condo2k4 6:78106c78e577 61 case 'r': case 'R': mode = RESPONSIVE; if(!personPresent) timer.attach(&timeout,3.0f); break;
Condo2k4 6:78106c78e577 62 case 'o': case 'O': mode = OVERRIDE; turnLightsOn=true; break;
tony1tf 0:b8c9dffbbe7e 63 }
tony1tf 0:b8c9dffbbe7e 64 }
tony1tf 0:b8c9dffbbe7e 65 }
tony1tf 0:b8c9dffbbe7e 66
Condo2k4 3:54760764be66 67 int main() {
tony1tf 0:b8c9dffbbe7e 68
Condo2k4 3:54760764be66 69 MQTTEthernet ipstack;
Condo2k4 3:54760764be66 70 MQTT::Client<MQTTEthernet, Countdown> m_client(ipstack);
Condo2k4 3:54760764be66 71
Condo2k4 3:54760764be66 72 int ip_result;
Condo2k4 3:54760764be66 73 for(int i=0; i<IP_RETRIES; i++) {
Condo2k4 3:54760764be66 74 int ip_result = ipstack.connect(HOSTNAME, PORT);
Condo2k4 3:54760764be66 75 if(ip_result==0) break;
Condo2k4 3:54760764be66 76 }
Condo2k4 3:54760764be66 77 if (ip_result != 0) {
Condo2k4 3:54760764be66 78 error("IP stack failed");
Condo2k4 3:54760764be66 79 }
Condo2k4 3:54760764be66 80 if(m_client.connect()!=MQTT::SUCCESS) {
Condo2k4 3:54760764be66 81 error("MQTT connection failed");
Condo2k4 3:54760764be66 82 }
Condo2k4 3:54760764be66 83 if(m_client.subscribe(modeTopic, MQTT::QOS0, messageArrived)!=MQTT::SUCCESS) {
Condo2k4 3:54760764be66 84 error("MQTT subscribe failed");
tony1tf 0:b8c9dffbbe7e 85 }
Condo2k4 3:54760764be66 86
Condo2k4 3:54760764be66 87 GND = 0; //provide sensor with ground
Condo2k4 3:54760764be66 88
Condo2k4 3:54760764be66 89 presenceTimer.attach_us(&presenceTick,50000);
Condo2k4 3:54760764be66 90
tony1tf 0:b8c9dffbbe7e 91 while(1) {
Condo2k4 3:54760764be66 92
Condo2k4 3:54760764be66 93 sleep();
Condo2k4 3:54760764be66 94
Condo2k4 3:54760764be66 95 if(checkPresence) {
Condo2k4 3:54760764be66 96 float s = sensor;
Condo2k4 3:54760764be66 97 if((s<0.5f)!=personPresent) { //state changed
Condo2k4 6:78106c78e577 98 personPresent = s<0.5f;
Condo2k4 3:54760764be66 99
Condo2k4 6:78106c78e577 100 if(mode==RESPONSIVE && personPresent) {
Condo2k4 6:78106c78e577 101 sprintf(mqtt_buffer, "{\"personPresent\":1, \"lightState\":1}");
Condo2k4 6:78106c78e577 102 } else {
Condo2k4 6:78106c78e577 103 sprintf(mqtt_buffer, "{\"personPresent\":%d}", personPresent);
Condo2k4 6:78106c78e577 104 }
Condo2k4 3:54760764be66 105 message.qos = MQTT::QOS0; // Send at least once
Condo2k4 3:54760764be66 106 // Do not null terminate -- we have a length field, and it will piss off the JS front end
Condo2k4 3:54760764be66 107 message.payloadlen = strlen(mqtt_buffer);
Condo2k4 3:54760764be66 108 message.payload = (void*)mqtt_buffer;
Condo2k4 3:54760764be66 109 m_client.publish(personTopic, message);
Condo2k4 3:54760764be66 110
Condo2k4 5:4152530c0cf5 111 if(personPresent) {
Condo2k4 5:4152530c0cf5 112 timer.attach(&timeout,3.0f);
Condo2k4 5:4152530c0cf5 113 pelican = true;
tony1tf 1:7c7539fba82b 114 }
tony1tf 1:7c7539fba82b 115 }
Condo2k4 3:54760764be66 116 checkPresence = false;
tony1tf 0:b8c9dffbbe7e 117 }
Condo2k4 6:78106c78e577 118 if(turnLightsOn) {
Condo2k4 6:78106c78e577 119 pelican = true;
Condo2k4 6:78106c78e577 120 sprintf(mqtt_buffer, "{\"lightState\":1}");
Condo2k4 6:78106c78e577 121 message.qos = MQTT::QOS0; // Send at least once
Condo2k4 6:78106c78e577 122 // Do not null terminate -- we have a length field, and it will piss off the JS front end
Condo2k4 6:78106c78e577 123 message.payloadlen = strlen(mqtt_buffer);
Condo2k4 6:78106c78e577 124 message.payload = (void*)mqtt_buffer;
Condo2k4 6:78106c78e577 125 m_client.publish(personTopic, message);
Condo2k4 6:78106c78e577 126 turnLightsOn = false;
Condo2k4 6:78106c78e577 127 }
Condo2k4 6:78106c78e577 128 if(turnLightsOff) {
Condo2k4 6:78106c78e577 129 pelican = false;
Condo2k4 6:78106c78e577 130 sprintf(mqtt_buffer, "{\"lightState\":0}");
Condo2k4 6:78106c78e577 131 message.qos = MQTT::QOS0; // Send at least once
Condo2k4 6:78106c78e577 132 // Do not null terminate -- we have a length field, and it will piss off the JS front end
Condo2k4 6:78106c78e577 133 message.payloadlen = strlen(mqtt_buffer);
Condo2k4 6:78106c78e577 134 message.payload = (void*)mqtt_buffer;
Condo2k4 6:78106c78e577 135 m_client.publish(personTopic, message);
Condo2k4 6:78106c78e577 136 turnLightsOff = false;
Condo2k4 6:78106c78e577 137 }
Condo2k4 3:54760764be66 138
Condo2k4 3:54760764be66 139 m_client.yield(100);
tony1tf 0:b8c9dffbbe7e 140 }
Condo2k4 3:54760764be66 141
Condo2k4 3:54760764be66 142
Condo2k4 3:54760764be66 143 /*
Condo2k4 3:54760764be66 144 */
Condo2k4 3:54760764be66 145 }