last version

Dependencies:   MQTT

Committer:
fatihkilic
Date:
Fri Nov 22 17:55:41 2019 +0000
Revision:
3:e75fe1b54623
Parent:
2:5b7d055dbc91
last version;

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"
fatihkilic 3:e75fe1b54623 11 #include "mbed.h"
jensdehoog 0:fd29cd85e75e 12
jensdehoog 0:fd29cd85e75e 13 char* topic;
jensdehoog 0:fd29cd85e75e 14
jensdehoog 0:fd29cd85e75e 15 /**
jensdehoog 0:fd29cd85e75e 16 TODO
jensdehoog 0:fd29cd85e75e 17 ----
jensdehoog 0:fd29cd85e75e 18 - Check if the button has been pressed. If so, print the amount of clicks to a serial terminal.
jensdehoog 0:fd29cd85e75e 19 - Make an MQTT-service which:
jensdehoog 0:fd29cd85e75e 20 - starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP.
jensdehoog 0:fd29cd85e75e 21 - makes a client and connects it to the broker using a client ID and credentials (username & password).
jensdehoog 0:fd29cd85e75e 22 - sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service
jensdehoog 0:fd29cd85e75e 23 you are going to use. Make a separate function which handles the sending procedure. Therefore, this function
jensdehoog 0:fd29cd85e75e 24 can be called each time we want to send a certain message.
jensdehoog 0:fd29cd85e75e 25 - When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times,
jensdehoog 0:fd29cd85e75e 26 the program disconnects from the broker and terminates.
jensdehoog 0:fd29cd85e75e 27
jensdehoog 0:fd29cd85e75e 28 Extra
jensdehoog 0:fd29cd85e75e 29 -----
jensdehoog 0:fd29cd85e75e 30 - Subscribe to the topic on which the song data is published. Display this received message on the serial terminal.
jensdehoog 0:fd29cd85e75e 31 - Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones.
jensdehoog 0:fd29cd85e75e 32
jensdehoog 0:fd29cd85e75e 33 Tips & tricks
jensdehoog 0:fd29cd85e75e 34 -------------
jensdehoog 0:fd29cd85e75e 35 - To generate an interrupt on the press of a button, use:
jensdehoog 0:fd29cd85e75e 36 InterruptIn button(USER_BUTTON);
jensdehoog 0:fd29cd85e75e 37 ...
jensdehoog 0:fd29cd85e75e 38 button.fall(callback(someFunction));
jensdehoog 0:fd29cd85e75e 39 - Before implementing MQTT, test the multiclick feature first.
jensdehoog 0:fd29cd85e75e 40 - Have a look at the MQTT-library for Mbed and the HelloMQTT-example.
jensdehoog 0:fd29cd85e75e 41 - To have a uniform message sending procedure, use the following function usage:
jensdehoog 0:fd29cd85e75e 42 sendMessage(&client, topic, buf, qos, retained, duplicate)
jensdehoog 0:fd29cd85e75e 43 */
jensdehoog 0:fd29cd85e75e 44
fatihkilic 3:e75fe1b54623 45
fatihkilic 3:e75fe1b54623 46 EthInterface *ethernet;
fatihkilic 3:e75fe1b54623 47 Serial pc(USBTX,USBRX);
fatihkilic 3:e75fe1b54623 48
fatihkilic 3:e75fe1b54623 49 void messageArrived(MQTT::MessageData& md) {
fatihkilic 3:e75fe1b54623 50 MQTT::Message &message = md.message;
fatihkilic 3:e75fe1b54623 51 char msg[300];
fatihkilic 3:e75fe1b54623 52 sprintf(msg, "Message arrived: QoS%d, retained %d, dup %d, packetID %d\r\n", message.qos, message.retained, message.dup, message.id);
fatihkilic 3:e75fe1b54623 53 printf(msg);
fatihkilic 3:e75fe1b54623 54 wait_ms(1000);
fatihkilic 3:e75fe1b54623 55 char payload[300];
fatihkilic 3:e75fe1b54623 56 sprintf(payload, "Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
fatihkilic 3:e75fe1b54623 57 printf(payload);
fatihkilic 3:e75fe1b54623 58 }
fatihkilic 3:e75fe1b54623 59
jensdehoog 0:fd29cd85e75e 60 int main(int argc, char* argv[])
jensdehoog 0:fd29cd85e75e 61 {
fatihkilic 3:e75fe1b54623 62 pc.baud(9600);
fatihkilic 3:e75fe1b54623 63 ethernet = EthInterface::get_default_instance();
fatihkilic 3:e75fe1b54623 64 if (!ethernet) {
fatihkilic 3:e75fe1b54623 65 printf("ERROR: No ethernet found.\r\n");
fatihkilic 3:e75fe1b54623 66 return -1;
fatihkilic 3:e75fe1b54623 67 }
jensdehoog 0:fd29cd85e75e 68
fatihkilic 3:e75fe1b54623 69 NetworkInterface* net = ethernet;
fatihkilic 3:e75fe1b54623 70 MQTTNetwork mqttNetwork(net);
fatihkilic 3:e75fe1b54623 71 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
fatihkilic 3:e75fe1b54623 72
fatihkilic 3:e75fe1b54623 73 const char* host = "broker.hivemq.com";
fatihkilic 3:e75fe1b54623 74 const char* topic = "fat";
fatihkilic 3:e75fe1b54623 75 pc.printf("Connecting to TCP network...\r\n");
fatihkilic 3:e75fe1b54623 76 int rc = mqttNetwork.connect(host, 1883);
fatihkilic 3:e75fe1b54623 77 if (rc != 0) {
fatihkilic 3:e75fe1b54623 78 pc.printf("Connection error.");
fatihkilic 3:e75fe1b54623 79 return -1;
fatihkilic 3:e75fe1b54623 80 }
fatihkilic 3:e75fe1b54623 81 pc.printf("Successfully connected!\r\n");
fatihkilic 3:e75fe1b54623 82
fatihkilic 3:e75fe1b54623 83 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
fatihkilic 3:e75fe1b54623 84 data.MQTTVersion = 3;
fatihkilic 3:e75fe1b54623 85 data.clientID.cstring = "fat";
fatihkilic 3:e75fe1b54623 86
fatihkilic 3:e75fe1b54623 87 if ((rc = client.connect(data)) != 0){
fatihkilic 3:e75fe1b54623 88 pc.printf("Fail to connect MQTT\r\n");
fatihkilic 3:e75fe1b54623 89 }
fatihkilic 3:e75fe1b54623 90 if (client.subscribe(topic, MQTT::QOS0, messageArrived) != 0){
fatihkilic 3:e75fe1b54623 91 pc.printf("Fail to subscribe\r\n");
fatihkilic 3:e75fe1b54623 92 }
fatihkilic 3:e75fe1b54623 93
fatihkilic 3:e75fe1b54623 94 button1_enabled_cb();
fatihkilic 3:e75fe1b54623 95
fatihkilic 3:e75fe1b54623 96 while(true){
fatihkilic 3:e75fe1b54623 97 if(button1_enabled ){
fatihkilic 3:e75fe1b54623 98 // pc.printf("button enabled");
fatihkilic 3:e75fe1b54623 99 }
fatihkilic 3:e75fe1b54623 100 if(button1_busy && !button1_enabled){
fatihkilic 3:e75fe1b54623 101
fatihkilic 3:e75fe1b54623 102
fatihkilic 3:e75fe1b54623 103 }
fatihkilic 3:e75fe1b54623 104 else if(!button1_busy && !button1_enabled){
fatihkilic 3:e75fe1b54623 105 pc.printf("total: %d /n", total);
fatihkilic 3:e75fe1b54623 106 }
fatihkilic 3:e75fe1b54623 107 button1_onpressed_cb();
fatihkilic 3:e75fe1b54623 108 }
fatihkilic 3:e75fe1b54623 109
jensdehoog 0:fd29cd85e75e 110 return 0;
jensdehoog 0:fd29cd85e75e 111 }
fatihkilic 3:e75fe1b54623 112
fatihkilic 3:e75fe1b54623 113