Keith G / Mbed 2 deprecated IOTM-Crossing

Dependencies:   EthernetInterface MQTT mbed-dsp mbed-rtos mbed

Fork of KL25Z_FFT_Demo_tony by Leicester Hackspace

Committer:
Condo2k4
Date:
Mon Jan 11 11:03:24 2016 +0000
Revision:
5:4152530c0cf5
Parent:
3:54760764be66
Child:
6:78106c78e577
Added delay before lights turn of when pedestrian starts crossing

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 3:54760764be66 17 enum Mode {RESPONCIVE, 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 3:54760764be66 38 volatile Mode mode = RESPONCIVE;
Condo2k4 3:54760764be66 39 volatile bool checkPresence = false;
tony1tf 0:b8c9dffbbe7e 40
Condo2k4 3:54760764be66 41 void presenceTick() {
Condo2k4 3:54760764be66 42 checkPresence = true;
tony1tf 0:b8c9dffbbe7e 43 }
tony1tf 0:b8c9dffbbe7e 44
Condo2k4 5:4152530c0cf5 45 void timeout() {
Condo2k4 5:4152530c0cf5 46 if(mode==RESPONCIVE) pelican = false;
Condo2k4 5:4152530c0cf5 47 timer.detach();
Condo2k4 5:4152530c0cf5 48 }
Condo2k4 5:4152530c0cf5 49
Condo2k4 3:54760764be66 50 void messageArrived(MQTT::MessageData& md) {
Condo2k4 3:54760764be66 51 MQTT::Message &msg = md.message;
Condo2k4 3:54760764be66 52 if(msg.payloadlen>0) {
Condo2k4 3:54760764be66 53 switch(*(char*)msg.payload) {
Condo2k4 5:4152530c0cf5 54 case 'r': case 'R': mode = RESPONCIVE; if(!personPresent) timer.attach(&timeout,3.0f); break;
Condo2k4 3:54760764be66 55 case 'o': case 'O': mode = OVERRIDE; pelican=true; break;
tony1tf 0:b8c9dffbbe7e 56 }
tony1tf 0:b8c9dffbbe7e 57 }
tony1tf 0:b8c9dffbbe7e 58 }
tony1tf 0:b8c9dffbbe7e 59
Condo2k4 3:54760764be66 60 int main() {
tony1tf 0:b8c9dffbbe7e 61
Condo2k4 3:54760764be66 62 MQTTEthernet ipstack;
Condo2k4 3:54760764be66 63 MQTT::Client<MQTTEthernet, Countdown> m_client(ipstack);
Condo2k4 3:54760764be66 64
Condo2k4 3:54760764be66 65 int ip_result;
Condo2k4 3:54760764be66 66 for(int i=0; i<IP_RETRIES; i++) {
Condo2k4 3:54760764be66 67 int ip_result = ipstack.connect(HOSTNAME, PORT);
Condo2k4 3:54760764be66 68 if(ip_result==0) break;
Condo2k4 3:54760764be66 69 }
Condo2k4 3:54760764be66 70 if (ip_result != 0) {
Condo2k4 3:54760764be66 71 error("IP stack failed");
Condo2k4 3:54760764be66 72 }
Condo2k4 3:54760764be66 73 if(m_client.connect()!=MQTT::SUCCESS) {
Condo2k4 3:54760764be66 74 error("MQTT connection failed");
Condo2k4 3:54760764be66 75 }
Condo2k4 3:54760764be66 76 if(m_client.subscribe(modeTopic, MQTT::QOS0, messageArrived)!=MQTT::SUCCESS) {
Condo2k4 3:54760764be66 77 error("MQTT subscribe failed");
tony1tf 0:b8c9dffbbe7e 78 }
Condo2k4 3:54760764be66 79
Condo2k4 3:54760764be66 80 GND = 0; //provide sensor with ground
Condo2k4 3:54760764be66 81
Condo2k4 3:54760764be66 82 presenceTimer.attach_us(&presenceTick,50000);
Condo2k4 3:54760764be66 83
tony1tf 0:b8c9dffbbe7e 84 while(1) {
Condo2k4 3:54760764be66 85
Condo2k4 3:54760764be66 86 sleep();
Condo2k4 3:54760764be66 87
Condo2k4 3:54760764be66 88 if(checkPresence) {
Condo2k4 3:54760764be66 89 float s = sensor;
Condo2k4 3:54760764be66 90 if((s<0.5f)!=personPresent) { //state changed
Condo2k4 5:4152530c0cf5 91 personPresent = !personPresent;
Condo2k4 3:54760764be66 92
Condo2k4 3:54760764be66 93 sprintf(mqtt_buffer, "{\"personPresent\":%d}", personPresent);
Condo2k4 3:54760764be66 94 message.qos = MQTT::QOS0; // Send at least once
Condo2k4 3:54760764be66 95 // Do not null terminate -- we have a length field, and it will piss off the JS front end
Condo2k4 3:54760764be66 96 message.payloadlen = strlen(mqtt_buffer);
Condo2k4 3:54760764be66 97 message.payload = (void*)mqtt_buffer;
Condo2k4 3:54760764be66 98 m_client.publish(personTopic, message);
Condo2k4 3:54760764be66 99
Condo2k4 5:4152530c0cf5 100 if(personPresent) {
Condo2k4 5:4152530c0cf5 101 timer.attach(&timeout,3.0f);
Condo2k4 5:4152530c0cf5 102 pelican = true;
tony1tf 1:7c7539fba82b 103 }
tony1tf 1:7c7539fba82b 104 }
Condo2k4 3:54760764be66 105 checkPresence = false;
tony1tf 0:b8c9dffbbe7e 106 }
Condo2k4 3:54760764be66 107
Condo2k4 3:54760764be66 108 m_client.yield(100);
tony1tf 0:b8c9dffbbe7e 109 }
Condo2k4 3:54760764be66 110
Condo2k4 3:54760764be66 111
Condo2k4 3:54760764be66 112 /*
Condo2k4 3:54760764be66 113 */
Condo2k4 3:54760764be66 114 }