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.
Fork of PGO6_VoteController_template by
main.cpp
00001 #define APP_VERSION 0.6f 00002 #define MQTT_VERSION 3 00003 #define BROKER_NAME "143.129.39.151" 00004 #define BROKER_PORT 1883 00005 #define CLIENTID "Reinout" 00006 #define TOPIC "clubIOT/feedback" 00007 #include "debounce_button.h" 00008 #include "EthernetInterface.h" 00009 #include "MQTTNetwork.h" 00010 #include "MQTTmbed.h" 00011 #include "MQTTClient.h" 00012 00013 DigitalOut info(LED2); 00014 DigitalOut connected(LED3); 00015 EthernetInterface interface; 00016 Timer timer; 00017 MQTTNetwork network(&interface); 00018 MQTT::Client<MQTTNetwork, Countdown> client(network); 00019 MQTT::Message message; 00020 00021 /** 00022 TODO 00023 ---- 00024 - Check if the button has been pressed. If so, print the amount of clicks to a serial terminal. 00025 - Make an MQTT-service which: 00026 - starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP. 00027 - makes a client and connects it to the broker using a client ID and credentials (username & password). 00028 - sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service 00029 you are going to use. Make a separate function which handles the sending procedure. Therefore, this function 00030 can be called each time we want to send a certain message. 00031 - When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times, 00032 the program disconnects from the broker and terminates. 00033 00034 Extra 00035 ----- 00036 - Subscribe to the topic on which the song data is published. Display this received message on the serial terminal. 00037 - Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones. 00038 00039 Tips & tricks 00040 ------------- 00041 - To generate an interrupt on the press of a button, use: 00042 InterruptIn button(USER_BUTTON); 00043 ... 00044 button.fall(callback(someFunction)); 00045 - Before implementing MQTT, test the multiclick feature first. 00046 - Have a look at the MQTT-library for Mbed and the HelloMQTT-example. 00047 - To have a uniform message sending procedure, use the following function usage: 00048 sendMessage(&client, topic, buf, qos, retained, duplicate) 00049 */ 00050 00051 //Connects to Network & MQTT 00052 void connect(char* name){ 00053 interface.connect(); 00054 while(interface.get_ip_address()== NULL){ 00055 } 00056 00057 network.connect(BROKER_NAME, BROKER_PORT); 00058 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00059 data.MQTTVersion = 3; 00060 data.clientID.cstring = name; 00061 data.username.cstring = "smartcity"; 00062 data.password.cstring = "smartcity"; 00063 client.connect(data); 00064 connected=1; 00065 } 00066 00067 //Publishes MQTT data 00068 void send(char* feedback){ 00069 char buf[100]; 00070 sprintf(buf, feedback); 00071 sprintf(buf+ strlen(buf), CLIENTID); 00072 // QoS 0 00073 message.qos = MQTT::QOS0; 00074 message.retained = false; 00075 message.dup = false; 00076 message.payload = (void*)buf; 00077 message.payloadlen = strlen(buf)+1; 00078 client.publish(TOPIC, message); 00079 } 00080 00081 //Disconnects MQTT 00082 void disconnect(void){ 00083 client.unsubscribe(TOPIC); 00084 client.disconnect(); 00085 network.disconnect(); 00086 connected=0; 00087 } 00088 00089 //Processes message arrived 00090 void messageArrived(MQTT::MessageData& md) 00091 { 00092 MQTT::Message &message = md.message; 00093 info=1; 00094 //NOTE: Due to lack of working serial terminal, sends data to clubIOT/feedback for reading. This should be printf. 00095 char buf[100]; 00096 sprintf(buf,"Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); 00097 send(buf); 00098 sprintf(buf,"Payload %.*s\r\n", message.payloadlen, (char*)message.payload); 00099 send(buf); 00100 } 00101 00102 //Subscribe to MQTT topic 00103 void subscribe(char* topic){ 00104 client.subscribe(topic, MQTT::QOS2, messageArrived); 00105 } 00106 00107 00108 int main(int argc, char* argv[]) 00109 { 00110 connect("Reinout"); 00111 subscribe("clubIOT/songmeta"); 00112 //Connect button to interrupt 00113 InterruptIn button(USER_BUTTON); 00114 button.fall(callback(&button1_onpressed_cb)); 00115 00116 while(connected){ 00117 if(!button1_busy && multiclick_state!=0){ 00118 if(multiclick_state==1) 00119 send("like-"); 00120 else if(multiclick_state==2) 00121 send("dislike-"); 00122 else if(multiclick_state==4) 00123 // if(connected==1) 00124 disconnect(); 00125 // else connect("Jef"); 00126 multiclick_state=0; 00127 } 00128 } 00129 return 0; 00130 }
Generated on Wed Jul 20 2022 10:42:55 by
