Kjell Kleyn
/
PGO6_VoteController_template
Vote Controller IOT
Fork of PGO6_VoteController_template by
main.cpp@3:b2c9de2f45c7, 2017-11-06 (annotated)
- Committer:
- kjellkleyn
- Date:
- Mon Nov 06 14:18:21 2017 +0000
- Revision:
- 3:b2c9de2f45c7
- Parent:
- 2:5b7d055dbc91
Vote Controller einde laatste les
Who changed what in which revision?
User | Revision | Line number | New 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 | |
kjellkleyn | 3:b2c9de2f45c7 | 12 | using namespace MQTT; |
kjellkleyn | 3:b2c9de2f45c7 | 13 | char* topic = "dansvloerVincent/votes"; |
kjellkleyn | 3:b2c9de2f45c7 | 14 | char buf[100]; |
kjellkleyn | 3:b2c9de2f45c7 | 15 | |
kjellkleyn | 3:b2c9de2f45c7 | 16 | |
kjellkleyn | 3:b2c9de2f45c7 | 17 | volatile bool button1_pressed; // Used in the main loop |
kjellkleyn | 3:b2c9de2f45c7 | 18 | volatile bool button1_enabled = true; // Used for debouncing |
kjellkleyn | 3:b2c9de2f45c7 | 19 | volatile int multiclick_state; // Counts how many clicks occured in the time slot, used in main loop |
kjellkleyn | 3:b2c9de2f45c7 | 20 | volatile bool button1_busy; // Informs the mainloop that the user is clicking the button |
kjellkleyn | 3:b2c9de2f45c7 | 21 | volatile bool countSecond = false; |
kjellkleyn | 3:b2c9de2f45c7 | 22 | volatile bool readyClick = false; |
kjellkleyn | 3:b2c9de2f45c7 | 23 | int arrivedcount = 0; |
jensdehoog | 0:fd29cd85e75e | 24 | |
jensdehoog | 0:fd29cd85e75e | 25 | /** |
jensdehoog | 0:fd29cd85e75e | 26 | TODO |
jensdehoog | 0:fd29cd85e75e | 27 | ---- |
jensdehoog | 0:fd29cd85e75e | 28 | - Check if the button has been pressed. If so, print the amount of clicks to a serial terminal. |
jensdehoog | 0:fd29cd85e75e | 29 | - Make an MQTT-service which: |
jensdehoog | 0:fd29cd85e75e | 30 | - starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP. |
jensdehoog | 0:fd29cd85e75e | 31 | - makes a client and connects it to the broker using a client ID and credentials (username & password). |
jensdehoog | 0:fd29cd85e75e | 32 | - sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service |
jensdehoog | 0:fd29cd85e75e | 33 | you are going to use. Make a separate function which handles the sending procedure. Therefore, this function |
jensdehoog | 0:fd29cd85e75e | 34 | can be called each time we want to send a certain message. |
jensdehoog | 0:fd29cd85e75e | 35 | - When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times, |
jensdehoog | 0:fd29cd85e75e | 36 | the program disconnects from the broker and terminates. |
jensdehoog | 0:fd29cd85e75e | 37 | |
jensdehoog | 0:fd29cd85e75e | 38 | Extra |
jensdehoog | 0:fd29cd85e75e | 39 | ----- |
jensdehoog | 0:fd29cd85e75e | 40 | - Subscribe to the topic on which the song data is published. Display this received message on the serial terminal. |
jensdehoog | 0:fd29cd85e75e | 41 | - Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones. |
jensdehoog | 0:fd29cd85e75e | 42 | |
jensdehoog | 0:fd29cd85e75e | 43 | Tips & tricks |
jensdehoog | 0:fd29cd85e75e | 44 | ------------- |
jensdehoog | 0:fd29cd85e75e | 45 | - To generate an interrupt on the press of a button, use: |
jensdehoog | 0:fd29cd85e75e | 46 | InterruptIn button(USER_BUTTON); |
jensdehoog | 0:fd29cd85e75e | 47 | ... |
jensdehoog | 0:fd29cd85e75e | 48 | button.fall(callback(someFunction)); |
jensdehoog | 0:fd29cd85e75e | 49 | - Before implementing MQTT, test the multiclick feature first. |
jensdehoog | 0:fd29cd85e75e | 50 | - Have a look at the MQTT-library for Mbed and the HelloMQTT-example. |
jensdehoog | 0:fd29cd85e75e | 51 | - To have a uniform message sending procedure, use the following function usage: |
jensdehoog | 0:fd29cd85e75e | 52 | sendMessage(&client, topic, buf, qos, retained, duplicate) |
jensdehoog | 0:fd29cd85e75e | 53 | */ |
jensdehoog | 0:fd29cd85e75e | 54 | |
kjellkleyn | 3:b2c9de2f45c7 | 55 | void messageArrived(MQTT::MessageData& md) |
kjellkleyn | 3:b2c9de2f45c7 | 56 | { |
kjellkleyn | 3:b2c9de2f45c7 | 57 | MQTT::Message &message = md.message; |
kjellkleyn | 3:b2c9de2f45c7 | 58 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
kjellkleyn | 3:b2c9de2f45c7 | 59 | printf("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); |
kjellkleyn | 3:b2c9de2f45c7 | 60 | ++arrivedcount; |
kjellkleyn | 3:b2c9de2f45c7 | 61 | } |
kjellkleyn | 3:b2c9de2f45c7 | 62 | |
jensdehoog | 0:fd29cd85e75e | 63 | int main(int argc, char* argv[]) |
jensdehoog | 0:fd29cd85e75e | 64 | { |
kjellkleyn | 3:b2c9de2f45c7 | 65 | printf("Starting program \r\nConnecting...\r\n"); |
kjellkleyn | 3:b2c9de2f45c7 | 66 | EthernetInterface eInterface; |
kjellkleyn | 3:b2c9de2f45c7 | 67 | //eInterface.set_network("192.168.0.95","255.255.255.0","192.168.0.1"); |
kjellkleyn | 3:b2c9de2f45c7 | 68 | eInterface.connect(); |
kjellkleyn | 3:b2c9de2f45c7 | 69 | |
kjellkleyn | 3:b2c9de2f45c7 | 70 | |
kjellkleyn | 3:b2c9de2f45c7 | 71 | //const char *ip = eInterface.get_ip_address(); |
kjellkleyn | 3:b2c9de2f45c7 | 72 | printf("IP address is: %s\r\n", eInterface.get_ip_address()); |
kjellkleyn | 3:b2c9de2f45c7 | 73 | |
kjellkleyn | 3:b2c9de2f45c7 | 74 | //________________________________________________________________ |
kjellkleyn | 3:b2c9de2f45c7 | 75 | NetworkInterface *network = &eInterface; |
kjellkleyn | 3:b2c9de2f45c7 | 76 | if (!network) { |
kjellkleyn | 3:b2c9de2f45c7 | 77 | return -1; |
kjellkleyn | 3:b2c9de2f45c7 | 78 | } |
kjellkleyn | 3:b2c9de2f45c7 | 79 | |
kjellkleyn | 3:b2c9de2f45c7 | 80 | MQTTNetwork mqttNetwork(network); |
kjellkleyn | 3:b2c9de2f45c7 | 81 | |
kjellkleyn | 3:b2c9de2f45c7 | 82 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); |
kjellkleyn | 3:b2c9de2f45c7 | 83 | |
kjellkleyn | 3:b2c9de2f45c7 | 84 | const char* hostname = "143.129.39.151"; |
kjellkleyn | 3:b2c9de2f45c7 | 85 | //const char* hostname = "broker.mqttdashboard.com"; |
kjellkleyn | 3:b2c9de2f45c7 | 86 | int port = 1883; |
kjellkleyn | 3:b2c9de2f45c7 | 87 | printf("Connecting to %s:%d\r\n", hostname, port); |
kjellkleyn | 3:b2c9de2f45c7 | 88 | int rc = mqttNetwork.connect(hostname, port); |
kjellkleyn | 3:b2c9de2f45c7 | 89 | if (rc != 0) |
kjellkleyn | 3:b2c9de2f45c7 | 90 | printf("rc from TCP connect is %d\r\n", rc); |
kjellkleyn | 3:b2c9de2f45c7 | 91 | |
kjellkleyn | 3:b2c9de2f45c7 | 92 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
kjellkleyn | 3:b2c9de2f45c7 | 93 | data.MQTTVersion = 3; |
kjellkleyn | 3:b2c9de2f45c7 | 94 | data.clientID.cstring = "kjell"; |
kjellkleyn | 3:b2c9de2f45c7 | 95 | data.username.cstring = "smartcity"; |
kjellkleyn | 3:b2c9de2f45c7 | 96 | data.password.cstring = "smartcity"; |
kjellkleyn | 3:b2c9de2f45c7 | 97 | if ((rc = client.connect(data)) != 0) |
kjellkleyn | 3:b2c9de2f45c7 | 98 | printf("rc from MQTT connect is %d\r\n", rc); |
kjellkleyn | 3:b2c9de2f45c7 | 99 | |
kjellkleyn | 3:b2c9de2f45c7 | 100 | MQTT::Message message; |
kjellkleyn | 3:b2c9de2f45c7 | 101 | message.qos = MQTT::QOS1; |
kjellkleyn | 3:b2c9de2f45c7 | 102 | InterruptIn button(USER_BUTTON); |
kjellkleyn | 3:b2c9de2f45c7 | 103 | button.fall(button1_onpressed_cb); |
kjellkleyn | 3:b2c9de2f45c7 | 104 | while(multiclick_state < 4){ |
kjellkleyn | 3:b2c9de2f45c7 | 105 | if(readyClick){ |
kjellkleyn | 3:b2c9de2f45c7 | 106 | printf("TimesClicked %d \r\n" , multiclick_state); |
kjellkleyn | 3:b2c9de2f45c7 | 107 | |
kjellkleyn | 3:b2c9de2f45c7 | 108 | if(multiclick_state == 1){ |
kjellkleyn | 3:b2c9de2f45c7 | 109 | sprintf(buf,"likeKjell\n"); |
kjellkleyn | 3:b2c9de2f45c7 | 110 | }else if(multiclick_state == 2 || multiclick_state == 3){ |
kjellkleyn | 3:b2c9de2f45c7 | 111 | sprintf(buf,"dislikeKjell\n"); |
kjellkleyn | 3:b2c9de2f45c7 | 112 | } |
kjellkleyn | 3:b2c9de2f45c7 | 113 | printf("buf : %s\r\n" ,buf); |
kjellkleyn | 3:b2c9de2f45c7 | 114 | message.payload = (void*)buf; |
kjellkleyn | 3:b2c9de2f45c7 | 115 | message.payloadlen = strlen(buf)+1; |
kjellkleyn | 3:b2c9de2f45c7 | 116 | rc = client.publish(topic, message); |
kjellkleyn | 3:b2c9de2f45c7 | 117 | |
kjellkleyn | 3:b2c9de2f45c7 | 118 | readyClick = false; |
kjellkleyn | 3:b2c9de2f45c7 | 119 | } |
kjellkleyn | 3:b2c9de2f45c7 | 120 | } |
kjellkleyn | 3:b2c9de2f45c7 | 121 | |
kjellkleyn | 3:b2c9de2f45c7 | 122 | if ((rc = client.disconnect()) != 0) |
kjellkleyn | 3:b2c9de2f45c7 | 123 | printf("rc from disconnect was %d\r\n", rc); |
kjellkleyn | 3:b2c9de2f45c7 | 124 | |
kjellkleyn | 3:b2c9de2f45c7 | 125 | mqttNetwork.disconnect(); |
kjellkleyn | 3:b2c9de2f45c7 | 126 | printf("exiting program...\r\n"); |
jensdehoog | 0:fd29cd85e75e | 127 | |
jensdehoog | 0:fd29cd85e75e | 128 | return 0; |
jensdehoog | 0:fd29cd85e75e | 129 | } |