Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface MQTT mbed-dsp mbed-rtos mbed
Fork of KL25Z_FFT_Demo_tony by
main.cpp@3:54760764be66, 2016-01-08 (annotated)
- Committer:
- Condo2k4
- Date:
- Fri Jan 08 15:49:02 2016 +0000
- Revision:
- 3:54760764be66
- Parent:
- 2:aa24865dfef5
- Child:
- 5:4152530c0cf5
Initial import
Who changed what in which revision?
| User | Revision | Line number | New 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 | 3:54760764be66 | 19 | //buffers |
| Condo2k4 | 3:54760764be66 | 20 | char mqtt_buffer[100]; |
| Condo2k4 | 3:54760764be66 | 21 | MQTT::Message message; |
| tony1tf | 0:b8c9dffbbe7e | 22 | |
| Condo2k4 | 3:54760764be66 | 23 | //topics |
| Condo2k4 | 3:54760764be66 | 24 | char personTopic[] = "shed/masterclass/iot/crossing/person"; |
| Condo2k4 | 3:54760764be66 | 25 | char modeTopic[] = "shed/masterclass/iot/crossing/mode"; |
| tony1tf | 0:b8c9dffbbe7e | 26 | |
| Condo2k4 | 3:54760764be66 | 27 | //io |
| Condo2k4 | 3:54760764be66 | 28 | AnalogIn sensor(A0); |
| Condo2k4 | 3:54760764be66 | 29 | DigitalOut GND(A2); |
| Condo2k4 | 3:54760764be66 | 30 | DigitalOut pelican(D0); |
| Condo2k4 | 3:54760764be66 | 31 | Ticker presenceTimer; |
| tony1tf | 0:b8c9dffbbe7e | 32 | |
| Condo2k4 | 3:54760764be66 | 33 | //state |
| Condo2k4 | 3:54760764be66 | 34 | bool personPresent = false; |
| Condo2k4 | 3:54760764be66 | 35 | volatile Mode mode = RESPONCIVE; |
| Condo2k4 | 3:54760764be66 | 36 | volatile bool checkPresence = false; |
| tony1tf | 0:b8c9dffbbe7e | 37 | |
| Condo2k4 | 3:54760764be66 | 38 | void presenceTick() { |
| Condo2k4 | 3:54760764be66 | 39 | checkPresence = true; |
| tony1tf | 0:b8c9dffbbe7e | 40 | } |
| tony1tf | 0:b8c9dffbbe7e | 41 | |
| Condo2k4 | 3:54760764be66 | 42 | void messageArrived(MQTT::MessageData& md) { |
| Condo2k4 | 3:54760764be66 | 43 | MQTT::Message &msg = md.message; |
| Condo2k4 | 3:54760764be66 | 44 | if(msg.payloadlen>0) { |
| Condo2k4 | 3:54760764be66 | 45 | switch(*(char*)msg.payload) { |
| Condo2k4 | 3:54760764be66 | 46 | case 'r': case 'R': mode = RESPONCIVE; pelican=personPresent; break; |
| Condo2k4 | 3:54760764be66 | 47 | case 'o': case 'O': mode = OVERRIDE; pelican=true; break; |
| tony1tf | 0:b8c9dffbbe7e | 48 | } |
| tony1tf | 0:b8c9dffbbe7e | 49 | } |
| tony1tf | 0:b8c9dffbbe7e | 50 | } |
| tony1tf | 0:b8c9dffbbe7e | 51 | |
| Condo2k4 | 3:54760764be66 | 52 | int main() { |
| tony1tf | 0:b8c9dffbbe7e | 53 | |
| Condo2k4 | 3:54760764be66 | 54 | MQTTEthernet ipstack; |
| Condo2k4 | 3:54760764be66 | 55 | MQTT::Client<MQTTEthernet, Countdown> m_client(ipstack); |
| Condo2k4 | 3:54760764be66 | 56 | |
| Condo2k4 | 3:54760764be66 | 57 | int ip_result; |
| Condo2k4 | 3:54760764be66 | 58 | for(int i=0; i<IP_RETRIES; i++) { |
| Condo2k4 | 3:54760764be66 | 59 | int ip_result = ipstack.connect(HOSTNAME, PORT); |
| Condo2k4 | 3:54760764be66 | 60 | if(ip_result==0) break; |
| Condo2k4 | 3:54760764be66 | 61 | } |
| Condo2k4 | 3:54760764be66 | 62 | if (ip_result != 0) { |
| Condo2k4 | 3:54760764be66 | 63 | error("IP stack failed"); |
| Condo2k4 | 3:54760764be66 | 64 | } |
| Condo2k4 | 3:54760764be66 | 65 | if(m_client.connect()!=MQTT::SUCCESS) { |
| Condo2k4 | 3:54760764be66 | 66 | error("MQTT connection failed"); |
| Condo2k4 | 3:54760764be66 | 67 | } |
| Condo2k4 | 3:54760764be66 | 68 | if(m_client.subscribe(modeTopic, MQTT::QOS0, messageArrived)!=MQTT::SUCCESS) { |
| Condo2k4 | 3:54760764be66 | 69 | error("MQTT subscribe failed"); |
| tony1tf | 0:b8c9dffbbe7e | 70 | } |
| Condo2k4 | 3:54760764be66 | 71 | |
| Condo2k4 | 3:54760764be66 | 72 | GND = 0; //provide sensor with ground |
| Condo2k4 | 3:54760764be66 | 73 | |
| Condo2k4 | 3:54760764be66 | 74 | presenceTimer.attach_us(&presenceTick,50000); |
| Condo2k4 | 3:54760764be66 | 75 | |
| tony1tf | 0:b8c9dffbbe7e | 76 | while(1) { |
| Condo2k4 | 3:54760764be66 | 77 | |
| Condo2k4 | 3:54760764be66 | 78 | sleep(); |
| Condo2k4 | 3:54760764be66 | 79 | |
| Condo2k4 | 3:54760764be66 | 80 | if(checkPresence) { |
| Condo2k4 | 3:54760764be66 | 81 | float s = sensor; |
| Condo2k4 | 3:54760764be66 | 82 | if((s<0.5f)!=personPresent) { //state changed |
| Condo2k4 | 3:54760764be66 | 83 | if(personPresent) { |
| Condo2k4 | 3:54760764be66 | 84 | //last person left |
| Condo2k4 | 3:54760764be66 | 85 | personPresent = false; |
| Condo2k4 | 3:54760764be66 | 86 | } else { |
| Condo2k4 | 3:54760764be66 | 87 | //person arrived |
| Condo2k4 | 3:54760764be66 | 88 | personPresent = true; |
| Condo2k4 | 3:54760764be66 | 89 | } |
| Condo2k4 | 3:54760764be66 | 90 | |
| Condo2k4 | 3:54760764be66 | 91 | sprintf(mqtt_buffer, "{\"personPresent\":%d}", personPresent); |
| Condo2k4 | 3:54760764be66 | 92 | message.qos = MQTT::QOS0; // Send at least once |
| Condo2k4 | 3:54760764be66 | 93 | // Do not null terminate -- we have a length field, and it will piss off the JS front end |
| Condo2k4 | 3:54760764be66 | 94 | message.payloadlen = strlen(mqtt_buffer); |
| Condo2k4 | 3:54760764be66 | 95 | message.payload = (void*)mqtt_buffer; |
| Condo2k4 | 3:54760764be66 | 96 | m_client.publish(personTopic, message); |
| Condo2k4 | 3:54760764be66 | 97 | |
| Condo2k4 | 3:54760764be66 | 98 | if(mode==RESPONCIVE) { |
| Condo2k4 | 3:54760764be66 | 99 | pelican = personPresent; |
| tony1tf | 1:7c7539fba82b | 100 | } |
| tony1tf | 1:7c7539fba82b | 101 | } |
| Condo2k4 | 3:54760764be66 | 102 | checkPresence = false; |
| tony1tf | 0:b8c9dffbbe7e | 103 | } |
| Condo2k4 | 3:54760764be66 | 104 | |
| Condo2k4 | 3:54760764be66 | 105 | m_client.yield(100); |
| tony1tf | 0:b8c9dffbbe7e | 106 | } |
| Condo2k4 | 3:54760764be66 | 107 | |
| Condo2k4 | 3:54760764be66 | 108 | |
| Condo2k4 | 3:54760764be66 | 109 | /* |
| Condo2k4 | 3:54760764be66 | 110 | */ |
| Condo2k4 | 3:54760764be66 | 111 | } |

