Initial commit

Dependencies:   C12832 MQTT_MbedOS mbed

Committer:
co657_es445
Date:
Mon Dec 05 15:08:55 2016 +0000
Revision:
0:6f752a7b935a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co657_es445 0:6f752a7b935a 1 #include "mbed.h"
co657_es445 0:6f752a7b935a 2 #include "C12832.h"
co657_es445 0:6f752a7b935a 3 #include "MQTTEthernet.h"
co657_es445 0:6f752a7b935a 4 #include "MQTTClient.h"
co657_es445 0:6f752a7b935a 5
co657_es445 0:6f752a7b935a 6 // Using Arduino pin notation
co657_es445 0:6f752a7b935a 7 C12832 lcd(D11, D13, D12, D7, D10);
co657_es445 0:6f752a7b935a 8 DigitalIn fire(D4);
co657_es445 0:6f752a7b935a 9 PwmOut spkr (D6);
co657_es445 0:6f752a7b935a 10
co657_es445 0:6f752a7b935a 11 DigitalOut r_l(D5);
co657_es445 0:6f752a7b935a 12 DigitalOut g_l(D9);
co657_es445 0:6f752a7b935a 13
co657_es445 0:6f752a7b935a 14 static Serial host (USBTX, USBRX);
co657_es445 0:6f752a7b935a 15
co657_es445 0:6f752a7b935a 16 /* speed we talk up the [USB] serial-port at */
co657_es445 0:6f752a7b935a 17 #define HOST_SPEED (38400)
co657_es445 0:6f752a7b935a 18
co657_es445 0:6f752a7b935a 19
co657_es445 0:6f752a7b935a 20 /* connection stuff for MQTT */
co657_es445 0:6f752a7b935a 21 typedef struct S_mqtt_params {
co657_es445 0:6f752a7b935a 22 char *hostname;
co657_es445 0:6f752a7b935a 23 int port;
co657_es445 0:6f752a7b935a 24 char *topic;
co657_es445 0:6f752a7b935a 25 char *clientid;
co657_es445 0:6f752a7b935a 26 } mqtt_params_t;
co657_es445 0:6f752a7b935a 27
co657_es445 0:6f752a7b935a 28
co657_es445 0:6f752a7b935a 29 static mqtt_params_t mqtt_config = {
co657_es445 0:6f752a7b935a 30 hostname: "co657-mqtt.kent.ac.uk",
co657_es445 0:6f752a7b935a 31 port: 1883,
co657_es445 0:6f752a7b935a 32 topic: "unikent/users/es445/iot/register",
co657_es445 0:6f752a7b935a 33 clientid: "es445kent"
co657_es445 0:6f752a7b935a 34 };
co657_es445 0:6f752a7b935a 35
co657_es445 0:6f752a7b935a 36
co657_es445 0:6f752a7b935a 37 /*
co657_es445 0:6f752a7b935a 38 * generates status stuff on LCD and serial
co657_es445 0:6f752a7b935a 39 */
co657_es445 0:6f752a7b935a 40 static int show_status (const char *fmt, ...)
co657_es445 0:6f752a7b935a 41 {
co657_es445 0:6f752a7b935a 42 va_list ap;
co657_es445 0:6f752a7b935a 43 int r;
co657_es445 0:6f752a7b935a 44
co657_es445 0:6f752a7b935a 45 va_start (ap, fmt);
co657_es445 0:6f752a7b935a 46 r = host.vprintf (fmt, ap);
co657_es445 0:6f752a7b935a 47 va_end (ap);
co657_es445 0:6f752a7b935a 48 host.printf ("\r\n");
co657_es445 0:6f752a7b935a 49
co657_es445 0:6f752a7b935a 50 lcd.fill (0, 20, 128, 12, 0);
co657_es445 0:6f752a7b935a 51 lcd.locate (0, 20);
co657_es445 0:6f752a7b935a 52 va_start (ap, fmt);
co657_es445 0:6f752a7b935a 53 lcd.vprintf (fmt, ap);
co657_es445 0:6f752a7b935a 54 va_end (ap);
co657_es445 0:6f752a7b935a 55
co657_es445 0:6f752a7b935a 56 return r;
co657_es445 0:6f752a7b935a 57 }
co657_es445 0:6f752a7b935a 58
co657_es445 0:6f752a7b935a 59
co657_es445 0:6f752a7b935a 60 /*
co657_es445 0:6f752a7b935a 61 * does various initialisation things; returns 0 on success, non-zero on error.
co657_es445 0:6f752a7b935a 62 */
co657_es445 0:6f752a7b935a 63 static int do_setup (void)
co657_es445 0:6f752a7b935a 64 {
co657_es445 0:6f752a7b935a 65 host.baud (HOST_SPEED);
co657_es445 0:6f752a7b935a 66 host.printf ("\r\n\r\nEthernet MQTT client from K64F\r\n");
co657_es445 0:6f752a7b935a 67
co657_es445 0:6f752a7b935a 68 lcd.cls ();
co657_es445 0:6f752a7b935a 69 lcd.locate (0, 0);
co657_es445 0:6f752a7b935a 70 lcd.printf ("Example MQTT client\n");
co657_es445 0:6f752a7b935a 71
co657_es445 0:6f752a7b935a 72 return 0;
co657_es445 0:6f752a7b935a 73 }
co657_es445 0:6f752a7b935a 74
co657_es445 0:6f752a7b935a 75 int main()
co657_es445 0:6f752a7b935a 76 {
co657_es445 0:6f752a7b935a 77 const char *ip;
co657_es445 0:6f752a7b935a 78 int r;
co657_es445 0:6f752a7b935a 79 MQTT::Message message;
co657_es445 0:6f752a7b935a 80 char buf[100];
co657_es445 0:6f752a7b935a 81
co657_es445 0:6f752a7b935a 82 do_setup ();
co657_es445 0:6f752a7b935a 83 show_status ("Connecting to network..");
co657_es445 0:6f752a7b935a 84
co657_es445 0:6f752a7b935a 85 /* Brings up the network interface */
co657_es445 0:6f752a7b935a 86 MQTTEthernet eth = MQTTEthernet ();
co657_es445 0:6f752a7b935a 87
co657_es445 0:6f752a7b935a 88 ip = eth.get_ip_address();
co657_es445 0:6f752a7b935a 89
co657_es445 0:6f752a7b935a 90 show_status ("IP addr is: %s", ip ?: "No address");
co657_es445 0:6f752a7b935a 91
co657_es445 0:6f752a7b935a 92 /* Create Mbed Client Interface */
co657_es445 0:6f752a7b935a 93 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown> (eth);
co657_es445 0:6f752a7b935a 94
co657_es445 0:6f752a7b935a 95 /* Create TCP connection */
co657_es445 0:6f752a7b935a 96 eth.open (eth.getEth());
co657_es445 0:6f752a7b935a 97 r = eth.connect (mqtt_config.hostname, mqtt_config.port);
co657_es445 0:6f752a7b935a 98
co657_es445 0:6f752a7b935a 99 if (!r) {
co657_es445 0:6f752a7b935a 100 show_status ("Connected okay, start MQTT client");
co657_es445 0:6f752a7b935a 101
co657_es445 0:6f752a7b935a 102 /* Wait for a short length of time to allow user to see output messages. */
co657_es445 0:6f752a7b935a 103 Thread::wait(1500);
co657_es445 0:6f752a7b935a 104
co657_es445 0:6f752a7b935a 105 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
co657_es445 0:6f752a7b935a 106
co657_es445 0:6f752a7b935a 107 data.MQTTVersion = 3;
co657_es445 0:6f752a7b935a 108 data.clientID.cstring = mqtt_config.clientid;
co657_es445 0:6f752a7b935a 109 r = client.connect (data);
co657_es445 0:6f752a7b935a 110
co657_es445 0:6f752a7b935a 111 if (!r) {
co657_es445 0:6f752a7b935a 112 show_status ("MQTT connected!");
co657_es445 0:6f752a7b935a 113
co657_es445 0:6f752a7b935a 114 while (!r) {
co657_es445 0:6f752a7b935a 115 lcd.cls();
co657_es445 0:6f752a7b935a 116 lcd.locate(0,3);
co657_es445 0:6f752a7b935a 117 lcd.printf("Scan To Register");
co657_es445 0:6f752a7b935a 118 const char* st = "Accepted";
co657_es445 0:6f752a7b935a 119 while(fire) { // this is the third thread
co657_es445 0:6f752a7b935a 120 show_status("Tap : %s\n",st);
co657_es445 0:6f752a7b935a 121 r_l = !g_l;
co657_es445 0:6f752a7b935a 122 wait(1.0);
co657_es445 0:6f752a7b935a 123 for (float i=2000.0f; i<10000.0f; i+=100) {
co657_es445 0:6f752a7b935a 124 spkr.period(1.0f/i);
co657_es445 0:6f752a7b935a 125 spkr=0.5;
co657_es445 0:6f752a7b935a 126 wait(0.02);
co657_es445 0:6f752a7b935a 127 }
co657_es445 0:6f752a7b935a 128 r_l = 0;
co657_es445 0:6f752a7b935a 129 lcd.cls();
co657_es445 0:6f752a7b935a 130 lcd.locate(0,3);
co657_es445 0:6f752a7b935a 131 lcd.printf("Scan To Register");
co657_es445 0:6f752a7b935a 132 show_status("Tap : \n");
co657_es445 0:6f752a7b935a 133
co657_es445 0:6f752a7b935a 134 spkr=0.0;
co657_es445 0:6f752a7b935a 135 sprintf (buf, "%s\n", st);
co657_es445 0:6f752a7b935a 136 // QoS 0
co657_es445 0:6f752a7b935a 137
co657_es445 0:6f752a7b935a 138 message.qos = MQTT::QOS0;
co657_es445 0:6f752a7b935a 139 message.retained = false;
co657_es445 0:6f752a7b935a 140 message.dup = false;
co657_es445 0:6f752a7b935a 141 message.payload = (void*)buf;
co657_es445 0:6f752a7b935a 142 message.payloadlen = strlen(buf)+1;
co657_es445 0:6f752a7b935a 143
co657_es445 0:6f752a7b935a 144 r = client.publish (mqtt_config.topic, message);
co657_es445 0:6f752a7b935a 145 while(!fire) {}
co657_es445 0:6f752a7b935a 146 }
co657_es445 0:6f752a7b935a 147
co657_es445 0:6f752a7b935a 148
co657_es445 0:6f752a7b935a 149 if (r) {
co657_es445 0:6f752a7b935a 150 show_status ("MQTT publish failed with %d", r);
co657_es445 0:6f752a7b935a 151 } else {
co657_es445 0:6f752a7b935a 152 Thread::wait(1500);
co657_es445 0:6f752a7b935a 153 }
co657_es445 0:6f752a7b935a 154 }
co657_es445 0:6f752a7b935a 155 } else {
co657_es445 0:6f752a7b935a 156 show_status ("Failed to connect to MQTT");
co657_es445 0:6f752a7b935a 157 }
co657_es445 0:6f752a7b935a 158 } else {
co657_es445 0:6f752a7b935a 159 show_status ("Failed to connect (TCP)");
co657_es445 0:6f752a7b935a 160 }
co657_es445 0:6f752a7b935a 161
co657_es445 0:6f752a7b935a 162 // It is good practice to close the socket
co657_es445 0:6f752a7b935a 163 eth.disconnect();
co657_es445 0:6f752a7b935a 164
co657_es445 0:6f752a7b935a 165
co657_es445 0:6f752a7b935a 166 }
co657_es445 0:6f752a7b935a 167
co657_es445 0:6f752a7b935a 168
co657_es445 0:6f752a7b935a 169