Initial commit

Dependencies:   C12832 MQTT_MbedOS mbed

Files at this revision

API Documentation at this revision

Comitter:
co657_es445
Date:
Mon Dec 05 15:08:55 2016 +0000
Commit message:
Initial commit

Changed in this revision

C12832.lib Show annotated file Show diff for this revision Revisions of this file
MQTT.lib 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
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 6f752a7b935a C12832.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C12832.lib	Mon Dec 05 15:08:55 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/co657_frmb/code/C12832/#7accdf0bfc18
diff -r 000000000000 -r 6f752a7b935a MQTT.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTT.lib	Mon Dec 05 15:08:55 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/azazeal88/code/MQTT_MbedOS/#a84464ad0b6f
diff -r 000000000000 -r 6f752a7b935a main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Dec 05 15:08:55 2016 +0000
@@ -0,0 +1,169 @@
+#include "mbed.h"
+#include "C12832.h"
+#include "MQTTEthernet.h"
+#include "MQTTClient.h"
+ 
+// Using Arduino pin notation
+C12832 lcd(D11, D13, D12, D7, D10);
+DigitalIn fire(D4);
+PwmOut spkr (D6);
+
+DigitalOut r_l(D5);
+DigitalOut g_l(D9);
+
+static Serial host (USBTX, USBRX);   
+
+/* speed we talk up the [USB] serial-port at */   
+#define HOST_SPEED (38400)
+ 
+ 
+ /* connection stuff for MQTT */
+typedef struct S_mqtt_params {
+    char *hostname;
+    int port;
+    char *topic;
+    char *clientid;
+} mqtt_params_t;
+
+
+static mqtt_params_t mqtt_config = {
+    hostname: "co657-mqtt.kent.ac.uk",
+    port: 1883,
+    topic: "unikent/users/es445/iot/register",
+    clientid: "es445kent"
+};
+
+
+/*
+ *  generates status stuff on LCD and serial
+ */
+static int show_status (const char *fmt, ...)
+{
+    va_list ap;
+    int r;
+    
+    va_start (ap, fmt);
+    r = host.vprintf (fmt, ap);
+    va_end (ap);
+    host.printf ("\r\n");
+
+    lcd.fill (0, 20, 128, 12, 0);    
+    lcd.locate (0, 20);
+    va_start (ap, fmt);
+    lcd.vprintf (fmt, ap);
+    va_end (ap);
+    
+    return r;
+}
+
+
+/*
+ *  does various initialisation things;  returns 0 on success, non-zero on error.
+ */
+static int do_setup (void)
+{
+    host.baud (HOST_SPEED);
+    host.printf ("\r\n\r\nEthernet MQTT client from K64F\r\n");
+    
+    lcd.cls ();
+    lcd.locate (0, 0);
+    lcd.printf ("Example MQTT client\n");
+    
+    return 0;
+}
+
+int main()
+{
+    const char *ip;
+    int r;
+    MQTT::Message message;
+    char buf[100];
+    
+    do_setup ();
+    show_status ("Connecting to network..");
+
+    /* Brings up the network interface */
+    MQTTEthernet eth = MQTTEthernet ();
+    
+    ip = eth.get_ip_address();
+
+    show_status ("IP addr is: %s", ip ?: "No address");
+    
+    /* Create Mbed Client Interface */
+    MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown> (eth);
+
+    /* Create TCP connection */
+    eth.open (eth.getEth());
+    r = eth.connect (mqtt_config.hostname, mqtt_config.port);
+    
+    if (!r) {
+        show_status ("Connected okay, start MQTT client");
+        
+        /* Wait for a short length of time to allow user to see output messages. */
+        Thread::wait(1500);
+        
+        MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+        
+        data.MQTTVersion = 3;
+        data.clientID.cstring = mqtt_config.clientid;
+        r = client.connect (data);
+        
+        if (!r) {
+            show_status ("MQTT connected!");
+
+     while (!r) {   
+        lcd.cls();
+        lcd.locate(0,3);
+        lcd.printf("Scan To Register");
+        const char* st = "Accepted";
+        while(fire) {   // this is the third thread
+        show_status("Tap : %s\n",st);
+        r_l = !g_l;
+        wait(1.0);
+        for (float i=2000.0f; i<10000.0f; i+=100) {
+            spkr.period(1.0f/i);
+            spkr=0.5;
+            wait(0.02);
+        }
+        r_l = 0;
+        lcd.cls();
+        lcd.locate(0,3);
+        lcd.printf("Scan To Register");
+        show_status("Tap : \n");
+        
+        spkr=0.0;
+        sprintf (buf, "%s\n", st);                
+                // QoS 0
+                
+                message.qos = MQTT::QOS0;
+                message.retained = false;
+                message.dup = false;
+                message.payload = (void*)buf;
+                message.payloadlen = strlen(buf)+1;
+                
+        r = client.publish (mqtt_config.topic, message);
+        while(!fire) {}
+    }
+
+             
+                if (r) {
+                    show_status ("MQTT publish failed with %d", r);
+                } else {
+                    Thread::wait(1500);
+                }
+            }
+        } else {
+            show_status ("Failed to connect to MQTT");
+        }
+    } else {
+        show_status ("Failed to connect (TCP)");
+    }
+    
+    // It is good practice to close the socket
+    eth.disconnect();
+
+ 
+}
+
+
+
diff -r 000000000000 -r 6f752a7b935a mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Mon Dec 05 15:08:55 2016 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#e2617cc0e17f5c3fc2bae6a589c9bcfd3d1a717b
diff -r 000000000000 -r 6f752a7b935a mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Dec 05 15:08:55 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/0ab6a29f35bf
\ No newline at end of file