Initial commit

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Committer:
Rennert
Date:
Thu Oct 26 14:16:10 2017 +0000
Revision:
1:e7a311bd038e
Parent:
0:fd29cd85e75e
Child:
2:f283e5274a0f
Original Commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jensdehoog 0:fd29cd85e75e 1 #define APP_VERSION 0.6f
jensdehoog 0:fd29cd85e75e 2 #define MQTT_VERSION 3
Rennert 1:e7a311bd038e 3 #define BROKER_NAME "143.129.39.151"
jensdehoog 0:fd29cd85e75e 4 #define BROKER_PORT 1883
Rennert 1:e7a311bd038e 5 #define CLIENTID "Reinout"
Rennert 1:e7a311bd038e 6 #define TOPIC "clubIOT/feedback"
jensdehoog 0:fd29cd85e75e 7 #include "debounce_button.h"
jensdehoog 0:fd29cd85e75e 8 #include "EthernetInterface.h"
jensdehoog 0:fd29cd85e75e 9 #include "MQTTNetwork.h"
jensdehoog 0:fd29cd85e75e 10 #include "MQTTmbed.h"
jensdehoog 0:fd29cd85e75e 11 #include "MQTTClient.h"
jensdehoog 0:fd29cd85e75e 12
Rennert 1:e7a311bd038e 13 DigitalOut info(LED2);
Rennert 1:e7a311bd038e 14 DigitalOut connected(LED3);
Rennert 1:e7a311bd038e 15 EthernetInterface interface;
Rennert 1:e7a311bd038e 16 Timer timer;
Rennert 1:e7a311bd038e 17 MQTTNetwork network(&interface);
Rennert 1:e7a311bd038e 18 MQTT::Client<MQTTNetwork, Countdown> client(network);
Rennert 1:e7a311bd038e 19 MQTT::Message message;
jensdehoog 0:fd29cd85e75e 20
jensdehoog 0:fd29cd85e75e 21 /**
jensdehoog 0:fd29cd85e75e 22 TODO
jensdehoog 0:fd29cd85e75e 23 ----
jensdehoog 0:fd29cd85e75e 24 - Check if the button has been pressed. If so, print the amount of clicks to a serial terminal.
jensdehoog 0:fd29cd85e75e 25 - Make an MQTT-service which:
jensdehoog 0:fd29cd85e75e 26 - starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP.
jensdehoog 0:fd29cd85e75e 27 - makes a client and connects it to the broker using a client ID and credentials (username & password).
jensdehoog 0:fd29cd85e75e 28 - sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service
jensdehoog 0:fd29cd85e75e 29 you are going to use. Make a separate function which handles the sending procedure. Therefore, this function
jensdehoog 0:fd29cd85e75e 30 can be called each time we want to send a certain message.
jensdehoog 0:fd29cd85e75e 31 - When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times,
jensdehoog 0:fd29cd85e75e 32 the program disconnects from the broker and terminates.
jensdehoog 0:fd29cd85e75e 33
jensdehoog 0:fd29cd85e75e 34 Extra
jensdehoog 0:fd29cd85e75e 35 -----
jensdehoog 0:fd29cd85e75e 36 - Subscribe to the topic on which the song data is published. Display this received message on the serial terminal.
jensdehoog 0:fd29cd85e75e 37 - Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones.
jensdehoog 0:fd29cd85e75e 38
jensdehoog 0:fd29cd85e75e 39 Tips & tricks
jensdehoog 0:fd29cd85e75e 40 -------------
jensdehoog 0:fd29cd85e75e 41 - To generate an interrupt on the press of a button, use:
jensdehoog 0:fd29cd85e75e 42 InterruptIn button(USER_BUTTON);
jensdehoog 0:fd29cd85e75e 43 ...
jensdehoog 0:fd29cd85e75e 44 button.fall(callback(someFunction));
jensdehoog 0:fd29cd85e75e 45 - Before implementing MQTT, test the multiclick feature first.
jensdehoog 0:fd29cd85e75e 46 - Have a look at the MQTT-library for Mbed and the HelloMQTT-example.
jensdehoog 0:fd29cd85e75e 47 - To have a uniform message sending procedure, use the following function usage:
jensdehoog 0:fd29cd85e75e 48 sendMessage(&client, topic, buf, qos, retained, duplicate)
jensdehoog 0:fd29cd85e75e 49 */
Rennert 1:e7a311bd038e 50 void connect(char* name){
Rennert 1:e7a311bd038e 51 interface.connect();
Rennert 1:e7a311bd038e 52 while(interface.get_ip_address()== NULL){
Rennert 1:e7a311bd038e 53 }
Rennert 1:e7a311bd038e 54
Rennert 1:e7a311bd038e 55 network.connect(BROKER_NAME, BROKER_PORT);
Rennert 1:e7a311bd038e 56 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
Rennert 1:e7a311bd038e 57 data.MQTTVersion = 3;
Rennert 1:e7a311bd038e 58 data.clientID.cstring = name;
Rennert 1:e7a311bd038e 59 data.username.cstring = "smartcity";
Rennert 1:e7a311bd038e 60 data.password.cstring = "smartcity";
Rennert 1:e7a311bd038e 61 client.connect(data);
Rennert 1:e7a311bd038e 62 connected=1;
Rennert 1:e7a311bd038e 63 }
Rennert 1:e7a311bd038e 64
Rennert 1:e7a311bd038e 65 void send(char* feedback){
Rennert 1:e7a311bd038e 66 char buf[100];
Rennert 1:e7a311bd038e 67 sprintf(buf, feedback);
Rennert 1:e7a311bd038e 68 sprintf(buf+ strlen(buf), CLIENTID);
Rennert 1:e7a311bd038e 69 // QoS 0
Rennert 1:e7a311bd038e 70 message.qos = MQTT::QOS0;
Rennert 1:e7a311bd038e 71 message.retained = false;
Rennert 1:e7a311bd038e 72 message.dup = false;
Rennert 1:e7a311bd038e 73 message.payload = (void*)buf;
Rennert 1:e7a311bd038e 74 message.payloadlen = strlen(buf)+1;
Rennert 1:e7a311bd038e 75 client.publish(TOPIC, message);
Rennert 1:e7a311bd038e 76 }
Rennert 1:e7a311bd038e 77
Rennert 1:e7a311bd038e 78 void disconnect(void){
Rennert 1:e7a311bd038e 79 client.unsubscribe(TOPIC);
Rennert 1:e7a311bd038e 80 client.disconnect();
Rennert 1:e7a311bd038e 81 network.disconnect();
Rennert 1:e7a311bd038e 82 connected=0;
Rennert 1:e7a311bd038e 83 }
Rennert 1:e7a311bd038e 84
Rennert 1:e7a311bd038e 85 void messageArrived(MQTT::MessageData& md)
Rennert 1:e7a311bd038e 86 {
Rennert 1:e7a311bd038e 87 MQTT::Message &message = md.message;
Rennert 1:e7a311bd038e 88 info=1;
Rennert 1:e7a311bd038e 89 char buf[100];
Rennert 1:e7a311bd038e 90 sprintf(buf,"Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
Rennert 1:e7a311bd038e 91 send(buf);
Rennert 1:e7a311bd038e 92 sprintf(buf,"Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
Rennert 1:e7a311bd038e 93 send(buf);
Rennert 1:e7a311bd038e 94 }
Rennert 1:e7a311bd038e 95
Rennert 1:e7a311bd038e 96 void subscribe(char* topic){
Rennert 1:e7a311bd038e 97 client.subscribe(topic, MQTT::QOS2, messageArrived);
Rennert 1:e7a311bd038e 98 }
jensdehoog 0:fd29cd85e75e 99
jensdehoog 0:fd29cd85e75e 100 int main(int argc, char* argv[])
jensdehoog 0:fd29cd85e75e 101 {
Rennert 1:e7a311bd038e 102 connect("Jef");
Rennert 1:e7a311bd038e 103 subscribe("clubIOT/songmeta");
Rennert 1:e7a311bd038e 104 InterruptIn button(USER_BUTTON);
Rennert 1:e7a311bd038e 105
Rennert 1:e7a311bd038e 106 button.fall(callback(&button1_onpressed_cb));
Rennert 1:e7a311bd038e 107 float x=0;
Rennert 1:e7a311bd038e 108 while(connected){
Rennert 1:e7a311bd038e 109 if(!button1_busy && multiclick_state!=0){
Rennert 1:e7a311bd038e 110 if(multiclick_state==1)
Rennert 1:e7a311bd038e 111 send("like-");
Rennert 1:e7a311bd038e 112 else if(multiclick_state==2)
Rennert 1:e7a311bd038e 113 send("dislike-");
Rennert 1:e7a311bd038e 114 else if(multiclick_state==4)
Rennert 1:e7a311bd038e 115 // if(connected==1)
Rennert 1:e7a311bd038e 116 disconnect();
Rennert 1:e7a311bd038e 117 // else connect("Jef");
Rennert 1:e7a311bd038e 118 multiclick_state=0;
Rennert 1:e7a311bd038e 119 }
Rennert 1:e7a311bd038e 120 }
jensdehoog 0:fd29cd85e75e 121 return 0;
jensdehoog 0:fd29cd85e75e 122 }