last version

Dependencies:   MQTT

main.cpp

Committer:
fatihkilic
Date:
2019-11-22
Revision:
3:e75fe1b54623
Parent:
2:5b7d055dbc91

File content as of revision 3:e75fe1b54623:

#define APP_VERSION     0.6f
#define MQTT_VERSION    3
#define BROKER_NAME     "broker.hivemq.com"
#define BROKER_PORT     1883

#include "debounce_button.h"
#include "EthernetInterface.h"
#include "MQTTNetwork.h"
#include "MQTTmbed.h"
#include "MQTTClient.h"
#include "mbed.h"

char* topic;

/**
    TODO
    ----
    -   Check if the button has been pressed. If so, print the amount of clicks to a serial terminal.
    -   Make an MQTT-service which:
        -   starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP.
        -   makes a client and connects it to the broker using a client ID and credentials (username & password).
        -   sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service
            you are going to use. Make a separate function which handles the sending procedure. Therefore, this function
            can be called each time we want to send a certain message.
    -   When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times,
        the program disconnects from the broker and terminates.
        
    Extra
    -----
    -   Subscribe to the topic on which the song data is published. Display this received message on the serial terminal.
    -   Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones.
    
    Tips & tricks
    -------------
    -   To generate an interrupt on the press of a button, use:
            InterruptIn button(USER_BUTTON);
            ...
            button.fall(callback(someFunction));
    -   Before implementing MQTT, test the multiclick feature first.
    -   Have a look at the MQTT-library for Mbed and the HelloMQTT-example.
    -   To have a uniform message sending procedure, use the following function usage:
            sendMessage(&client, topic, buf, qos, retained, duplicate)
*/


EthInterface *ethernet;
 Serial pc(USBTX,USBRX);
 
void messageArrived(MQTT::MessageData& md) {
   MQTT::Message &message = md.message;
   char msg[300];
   sprintf(msg, "Message arrived: QoS%d, retained %d, dup %d, packetID %d\r\n", message.qos, message.retained, message.dup, message.id);
   printf(msg);
   wait_ms(1000);
   char payload[300];
   sprintf(payload, "Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
   printf(payload);
}

int main(int argc, char* argv[])
{
    pc.baud(9600);
    ethernet = EthInterface::get_default_instance();
    if (!ethernet) {
      printf("ERROR: No ethernet found.\r\n");
      return -1;
    }    

    NetworkInterface* net = ethernet;
    MQTTNetwork mqttNetwork(net);
    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
    
    const char* host = "broker.hivemq.com";
   const char* topic = "fat";
   pc.printf("Connecting to TCP network...\r\n");
   int rc = mqttNetwork.connect(host, 1883);
   if (rc != 0) {
      pc.printf("Connection error.");
      return -1;
   }
   pc.printf("Successfully connected!\r\n");

   MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
   data.MQTTVersion = 3;
   data.clientID.cstring = "fat";

   if ((rc = client.connect(data)) != 0){
      pc.printf("Fail to connect MQTT\r\n");
   }
   if (client.subscribe(topic, MQTT::QOS0, messageArrived) != 0){
      pc.printf("Fail to subscribe\r\n");
   }

    button1_enabled_cb();
      
    while(true){
        if(button1_enabled ){
           // pc.printf("button enabled");
        }
        if(button1_busy && !button1_enabled){
            
             
        }
        else if(!button1_busy && !button1_enabled){
            pc.printf("total: %d /n", total);
         }
         button1_onpressed_cb();
    }
    
    return 0;
}