vote controller implementatie van Jonas De Rynck

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Files at this revision

API Documentation at this revision

Comitter:
jonasdr
Date:
Mon Oct 30 16:43:58 2017 +0000
Parent:
1:34e76c0cbe5a
Commit message:
Jonas De Rynck - embedded vote controller

Changed in this revision

debounce_button.cpp Show annotated file Show diff for this revision Revisions of this file
debounce_button.h Show annotated file Show diff for this revision Revisions of this file
easy-connect.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 34e76c0cbe5a -r 5f2104244c27 debounce_button.cpp
--- a/debounce_button.cpp	Sun Oct 29 23:01:06 2017 +0000
+++ b/debounce_button.cpp	Mon Oct 30 16:43:58 2017 +0000
@@ -34,8 +34,56 @@
 /**
     This function is called when the button has been pressed by the user.
 */
+Timeout timeout;
+Timeout timeout2;
+DigitalOut busy_led(LED2);
+DigitalOut testled(LED1);
+volatile bool button1_enabled = true;  
+volatile int multiclick_state = 0;
+volatile bool button1_busy = false;      // Informs the mainloop that the user is clicking the button
+volatile int multiclick_state_mem = 0;
+volatile bool isready = false;
 
 void button1_onpressed_cb(void)
 {
+    if (button1_enabled)
+    {
+        if(multiclick_state == 0) //eerste druk binnen een seconde
+        {
+            button1_busy = true; //knop is busy voor 1 sec
+            busy_led = 1;
+            timeout2.attach(callback(button1_multiclick_reset_cb), 1.0);
+        }
+        multiclick_state += 1;
+        button1_enabled = false;
+        timeout.attach(callback(&button1_enabled_cb), 0.075);
+    }
+}
+
+void button1_multiclick_reset_cb(void)
+{
+    isready = true;
+    multiclick_state_mem = multiclick_state; // aantal kliks onthouden
+    multiclick_state = 0;
+    button1_busy = false;               //button niet meer busy
+    busy_led = 0;
+    for(int i = 0; i<multiclick_state_mem; i++)
+    {
+        testing();
+    }
     
+}
+
+
+void button1_enabled_cb(void)
+{
+    button1_enabled = true;
+}
+
+void testing(void)
+{
+    testled = 1;
+    wait(0.2);
+    testled = 0;
+    wait(0.2);
 }
\ No newline at end of file
diff -r 34e76c0cbe5a -r 5f2104244c27 debounce_button.h
--- a/debounce_button.h	Sun Oct 29 23:01:06 2017 +0000
+++ b/debounce_button.h	Mon Oct 30 16:43:58 2017 +0000
@@ -9,11 +9,14 @@
     
 */
 
-extern volatile bool button1_pressed;   // Used in the main loop
+//extern volatile bool button1_pressed = false;   // Used in the main loop
 extern volatile bool button1_enabled;   // Used for debouncing
 extern volatile int multiclick_state;   // Counts how many clicks occured in the time slot, used in main loop
-extern volatile bool button1_busy;      // Informs the mainloop that the user is clicking the button
+extern volatile bool button1_busy ;      // Informs the mainloop that the user is clicking the button
+extern volatile int multiclick_state_mem; // remembers how many times the button was pressed after the state reset to 0.
+extern volatile bool isready;
 
 void button1_multiclick_reset_cb(void); // Resets the amount of clicks, but stores this value for the usage in the main loop
 void button1_enabled_cb(void);          // Enables the button again after a timeout, used for debouncing the button 
-void button1_onpressed_cb(void);        // Callback which is called when the user presses the button
\ No newline at end of file
+void button1_onpressed_cb(void);        // Callback which is called when the user presses the button
+void testing(void);                     //function that uses LEDS for testing (couldn't use printf's because my computer couldn't find the right COM port).
\ No newline at end of file
diff -r 34e76c0cbe5a -r 5f2104244c27 easy-connect.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/easy-connect.h	Mon Oct 30 16:43:58 2017 +0000
@@ -0,0 +1,102 @@
+#ifndef __MAGIC_CONNECT_H__
+#define __MAGIC_CONNECT_H__
+
+#include "mbed.h"
+
+Serial output(USBTX, USBRX);
+
+#define ETHERNET        1
+#define WIFI_ESP8266    2
+#define MESH_LOWPAN_ND  3
+#define MESH_THREAD     4
+
+#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
+#include "ESP8266Interface.h"
+
+#ifdef MBED_CONF_APP_ESP8266_DEBUG
+ESP8266Interface esp(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX, MBED_CONF_APP_ESP8266_DEBUG);
+#else
+ESP8266Interface esp(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX);
+#endif
+
+#elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
+#include "EthernetInterface.h"
+EthernetInterface eth;
+#elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
+#define MESH
+#include "NanostackInterface.h"
+LoWPANNDInterface mesh;
+#elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
+#define MESH
+#include "NanostackInterface.h"
+ThreadInterface mesh;
+#else
+#error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)."
+#endif
+
+#if defined(MESH)
+#if MBED_CONF_APP_MESH_RADIO_TYPE == ATMEL
+#include "NanostackRfPhyAtmel.h"
+NanostackRfPhyAtmel rf_phy(ATMEL_SPI_MOSI, ATMEL_SPI_MISO, ATMEL_SPI_SCLK, ATMEL_SPI_CS,
+                           ATMEL_SPI_RST, ATMEL_SPI_SLP, ATMEL_SPI_IRQ, ATMEL_I2C_SDA, ATMEL_I2C_SCL);
+#elif MBED_CONF_APP_MESH_RADIO_TYPE == MCR20
+#include "NanostackRfPhyMcr20a.h"
+NanostackRfPhyMcr20a rf_phy(MCR20A_SPI_MOSI, MCR20A_SPI_MISO, MCR20A_SPI_SCLK, MCR20A_SPI_CS, MCR20A_SPI_RST, MCR20A_SPI_IRQ);
+#endif //MBED_CONF_APP_RADIO_TYPE
+#endif //MESH
+
+#ifndef MESH
+// This is address to mbed Device Connector
+#define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684"
+#else
+// This is address to mbed Device Connector
+#define MBED_SERVER_ADDRESS "coaps://[2607:f0d0:2601:52::20]:5684"
+#endif
+
+NetworkInterface* easy_connect(bool log_messages = false) {
+    NetworkInterface* network_interface = 0;
+    int connect_success = -1;
+#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
+    if (log_messages) {
+        output.printf("[EasyConnect] Using WiFi (ESP8266) \r\n");
+        output.printf("[EasyConnect] Connecting to WiFi..\r\n");
+    }
+    connect_success = esp.connect(MBED_CONF_APP_ESP8266_SSID, MBED_CONF_APP_ESP8266_PASSWORD);
+    network_interface = &esp;
+#elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
+    if (log_messages) {
+        output.printf("[EasyConnect] Using Ethernet\r\n");
+    }
+    connect_success = eth.connect();
+    network_interface = &eth;
+#endif
+#ifdef MESH
+    if (log_messages) {
+        output.printf("[EasyConnect] Using Mesh\r\n");
+        output.printf("[EasyConnect] Connecting to Mesh..\r\n");
+    }
+    connect_success = mesh.connect();
+    network_interface = &mesh;
+#endif
+    if(connect_success == 0) {
+        if (log_messages) {
+            output.printf("[EasyConnect] Connected to Network successfully\r\n");
+        }
+    } else {
+        if (log_messages) {
+            output.printf("[EasyConnect] Connection to Network Failed %d!\r\n", connect_success);
+        }
+        return NULL;
+    }
+    if (log_messages) {
+        const char *ip_addr = network_interface->get_ip_address();
+        if (ip_addr) {
+            output.printf("[EasyConnect] IP address %s\r\n", ip_addr);
+        } else {
+            output.printf("[EasyConnect] No IP address\r\n");
+        }
+    }
+    return network_interface;
+}
+
+#endif // __MAGIC_CONNECT_H__
diff -r 34e76c0cbe5a -r 5f2104244c27 main.cpp
--- a/main.cpp	Sun Oct 29 23:01:06 2017 +0000
+++ b/main.cpp	Mon Oct 30 16:43:58 2017 +0000
@@ -1,6 +1,6 @@
 #define APP_VERSION     0.6f
 #define MQTT_VERSION    3
-#define BROKER_NAME     "broker.hivemq.com"
+#define BROKER_NAME     "143.129.39.151"
 #define BROKER_PORT     1883
 
 #include "debounce_button.h"
@@ -9,8 +9,6 @@
 #include "MQTTmbed.h"
 #include "MQTTClient.h"
 
-char* topic;
-
 /**
     TODO
     ----
@@ -34,7 +32,7 @@
     -   To generate an interrupt on the press of a button, use:
             InterruptIn button(USER_BUTTON);
             ...
-            button.fall(callback(someFunction));
+            button.fall(callback(&someFunction));
         The function someFunction(void) is called when this interrupt occurs.
     -   Before implementing MQTT, test the multiclick feature first.
         You can simply use 'printf()' to print to a serial terminal. The baud rate is 9600.
@@ -43,8 +41,93 @@
             sendMessage(&client, topic, buf, qos, retained, duplicate)
 */
 
+InterruptIn button(USER_BUTTON);
+bool flag = false;  //set on interrupt
+DigitalOut connected_led(LED3);
+char* topic = "clubIOT/feedback"; //publish topic
+char* songTopic="clubIOT/songmeta"; //subscribe topic
+
+
+void buttonpress()
+{
+    flag = true;
+}
+
+void sendMessage(MQTT::Client<MQTTNetwork, Countdown> *client, int qos, char* mqtt_message)
+{
+    MQTT::Message message;
+    char buf[100];
+    sprintf(buf, mqtt_message);
+    if(qos==0)
+        message.qos = MQTT::QOS0;
+    else if(qos==1)
+        message.qos = MQTT::QOS1;
+    else
+        message.qos = MQTT::QOS2;
+    message.retained = false;
+    message.dup = false;
+    message.payload = (void*)buf;
+    message.payloadlen = strlen(buf)+1;
+    client->publish(topic, message);
+}
+
+void newSong(MQTT::MessageData &data)
+{
+    MQTT::Message &message = data.message;
+    printf("new song: %s \n", (char*)message.payload);
+}
+    
+
 int main(int argc, char* argv[])
 {
-
+    EthernetInterface eth_interface;      //maak ethernet interface
+    MQTTNetwork mqttNetwork(&eth_interface);                      //maak mqtt network
+    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);   //maak een client
+    eth_interface.connect();                                    // connect de ethernet interface
+    int rc = mqttNetwork.connect(BROKER_NAME, BROKER_PORT);              // connect to the MQTTbroker
+    if(rc!=0)
+        printf("'tis kapot %d \n", rc);
+    client.subscribe(songTopic, MQTT::QOS1, newSong); //subscribe op de songmeta
+    
+    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+    data.MQTTVersion = 3;
+    data.clientID.cstring = "jonas";
+    data.username.cstring = "smartcity";
+    data.password.cstring = "smartcity";
+    client.connect(data);
+    
+    char* mqtt_message = "";
+    
+    button.fall(callback(&buttonpress ));
+    while(1)
+    {
+        if(flag)
+        {
+            button1_onpressed_cb(); //roep debouncer op
+            flag = false;           //reset interrupt flag
+        }  
+        
+        if(!button1_busy && isready)
+        {
+                if (multiclick_state_mem == 1)
+                {
+                    mqtt_message = "like";
+                    sendMessage(&client, 0, mqtt_message);
+                }
+                else if(multiclick_state_mem == 2)
+                {
+                    mqtt_message = "dislike";
+                    sendMessage(&client, 0, mqtt_message);
+                }
+                else if(multiclick_state_mem == 4)
+                {
+                    client.unsubscribe(topic);
+                    client.disconnect();
+                    eth_interface.disconnect();
+                }
+                isready = false;
+        }
+    }
     return 0;
 }
+