PGO6_VoteController (Astrid Vanneste)

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Committer:
AstridVanneste
Date:
Mon Oct 08 15:39:22 2018 +0000
Revision:
5:ba94770ce1c7
Parent:
4:08da93eb6014
Child:
6:60b968c8f35c
mqtt

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
jensdehoog 0:fd29cd85e75e 3 #define BROKER_NAME "broker.hivemq.com"
jensdehoog 0:fd29cd85e75e 4 #define BROKER_PORT 1883
jensdehoog 0:fd29cd85e75e 5
jensdehoog 0:fd29cd85e75e 6 #include "debounce_button.h"
jensdehoog 0:fd29cd85e75e 7 #include "EthernetInterface.h"
jensdehoog 0:fd29cd85e75e 8 #include "MQTTNetwork.h"
jensdehoog 0:fd29cd85e75e 9 #include "MQTTmbed.h"
jensdehoog 0:fd29cd85e75e 10 #include "MQTTClient.h"
jensdehoog 0:fd29cd85e75e 11
jensdehoog 0:fd29cd85e75e 12 char* topic;
jensdehoog 0:fd29cd85e75e 13
jensdehoog 0:fd29cd85e75e 14 /**
jensdehoog 0:fd29cd85e75e 15 TODO
jensdehoog 0:fd29cd85e75e 16 ----
jensdehoog 0:fd29cd85e75e 17 - Check if the button has been pressed. If so, print the amount of clicks to a serial terminal.
jensdehoog 0:fd29cd85e75e 18 - Make an MQTT-service which:
jensdehoog 0:fd29cd85e75e 19 - starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP.
jensdehoog 0:fd29cd85e75e 20 - makes a client and connects it to the broker using a client ID and credentials (username & password).
jensdehoog 0:fd29cd85e75e 21 - sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service
jensdehoog 0:fd29cd85e75e 22 you are going to use. Make a separate function which handles the sending procedure. Therefore, this function
jensdehoog 0:fd29cd85e75e 23 can be called each time we want to send a certain message.
jensdehoog 0:fd29cd85e75e 24 - When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times,
jensdehoog 0:fd29cd85e75e 25 the program disconnects from the broker and terminates.
jensdehoog 0:fd29cd85e75e 26
jensdehoog 0:fd29cd85e75e 27 Extra
jensdehoog 0:fd29cd85e75e 28 -----
jensdehoog 0:fd29cd85e75e 29 - Subscribe to the topic on which the song data is published. Display this received message on the serial terminal.
jensdehoog 0:fd29cd85e75e 30 - Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones.
jensdehoog 0:fd29cd85e75e 31
jensdehoog 0:fd29cd85e75e 32 Tips & tricks
jensdehoog 0:fd29cd85e75e 33 -------------
jensdehoog 0:fd29cd85e75e 34 - To generate an interrupt on the press of a button, use:
jensdehoog 0:fd29cd85e75e 35 InterruptIn button(USER_BUTTON);
jensdehoog 0:fd29cd85e75e 36 ...
jensdehoog 0:fd29cd85e75e 37 button.fall(callback(someFunction));
jensdehoog 0:fd29cd85e75e 38 - Before implementing MQTT, test the multiclick feature first.
jensdehoog 0:fd29cd85e75e 39 - Have a look at the MQTT-library for Mbed and the HelloMQTT-example.
jensdehoog 0:fd29cd85e75e 40 - To have a uniform message sending procedure, use the following function usage:
jensdehoog 0:fd29cd85e75e 41 sendMessage(&client, topic, buf, qos, retained, duplicate)
jensdehoog 0:fd29cd85e75e 42 */
jensdehoog 0:fd29cd85e75e 43
jensdehoog 0:fd29cd85e75e 44 int main(int argc, char* argv[])
jensdehoog 0:fd29cd85e75e 45 {
AstridVanneste 5:ba94770ce1c7 46 printf("STARTED!\n");
AstridVanneste 3:376ac6744373 47 InterruptIn button(USER_BUTTON);
AstridVanneste 5:ba94770ce1c7 48 button.fall(callback(&button1_onpressed_cb));
AstridVanneste 3:376ac6744373 49 init_debouncer();
AstridVanneste 4:08da93eb6014 50
AstridVanneste 5:ba94770ce1c7 51 EthernetInterface network;
AstridVanneste 5:ba94770ce1c7 52 int error = network.connect();
AstridVanneste 5:ba94770ce1c7 53 if(error != 0)
AstridVanneste 5:ba94770ce1c7 54 {
AstridVanneste 5:ba94770ce1c7 55 printf("ERROR: ethernet.connect() = %d\n", error);
AstridVanneste 5:ba94770ce1c7 56 }
AstridVanneste 5:ba94770ce1c7 57 const char* ip_add = network.get_ip_address();
AstridVanneste 5:ba94770ce1c7 58
AstridVanneste 5:ba94770ce1c7 59 MQTTNetwork mqttNetwork(&network);
AstridVanneste 5:ba94770ce1c7 60
AstridVanneste 5:ba94770ce1c7 61 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
AstridVanneste 5:ba94770ce1c7 62
AstridVanneste 5:ba94770ce1c7 63 error = mqttNetwork.connect(BROKER_NAME, BROKER_PORT);
AstridVanneste 5:ba94770ce1c7 64 if(error != 0)
AstridVanneste 5:ba94770ce1c7 65 {
AstridVanneste 5:ba94770ce1c7 66 printf("ERROR: mqtt.connect() = %d\n", error);
AstridVanneste 5:ba94770ce1c7 67 }
AstridVanneste 5:ba94770ce1c7 68
AstridVanneste 5:ba94770ce1c7 69 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
AstridVanneste 5:ba94770ce1c7 70 data.MQTTVersion = MQTT_VERSION;
AstridVanneste 5:ba94770ce1c7 71
AstridVanneste 5:ba94770ce1c7 72 printf("INITIALIZED\n");
AstridVanneste 4:08da93eb6014 73
AstridVanneste 4:08da93eb6014 74 while(true)
AstridVanneste 4:08da93eb6014 75 {
AstridVanneste 5:ba94770ce1c7 76 if(button1_pressed)
AstridVanneste 5:ba94770ce1c7 77 {
AstridVanneste 5:ba94770ce1c7 78 printf("Clicks: %d\n", multiclick_state);
AstridVanneste 5:ba94770ce1c7 79 button1_pressed = false;
AstridVanneste 5:ba94770ce1c7 80 }
AstridVanneste 4:08da93eb6014 81 }
jensdehoog 0:fd29cd85e75e 82
jensdehoog 0:fd29cd85e75e 83 return 0;
jensdehoog 0:fd29cd85e75e 84 }