Initial commit

Dependencies:   C12832 MQTT_MbedOS mbed

Committer:
co657_kn241
Date:
Mon Dec 05 15:07:11 2016 +0000
Revision:
0:fb05faf411e7
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co657_kn241 0:fb05faf411e7 1 /**
co657_kn241 0:fb05faf411e7 2 * Door lock Mechanism which work similar to locking and unlocking
co657_kn241 0:fb05faf411e7 3 * by using an RFID card to scan nfc tag which is publish onto MQTT
co657_kn241 0:fb05faf411e7 4 * server by saying in door is locked or unlucked.
co657_kn241 0:fb05faf411e7 5 * By Kwame Nyantakyi
co657_kn241 0:fb05faf411e7 6
co657_kn241 0:fb05faf411e7 7 * Use of MQTT client code created by Daniel Knox and Fred Barnes
co657_kn241 0:fb05faf411e7 8 **/
co657_kn241 0:fb05faf411e7 9
co657_kn241 0:fb05faf411e7 10 //
co657_kn241 0:fb05faf411e7 11 #include "mbed.h"
co657_kn241 0:fb05faf411e7 12 #include "C12832.h"
co657_kn241 0:fb05faf411e7 13 #include "MQTTEthernet.h"
co657_kn241 0:fb05faf411e7 14 #include "MQTTClient.h"
co657_kn241 0:fb05faf411e7 15
co657_kn241 0:fb05faf411e7 16 /* speed we talk up the [USB] serial-port at */
co657_kn241 0:fb05faf411e7 17 #define HOST_SPEED (38400)
co657_kn241 0:fb05faf411e7 18
co657_kn241 0:fb05faf411e7 19 // Using Arduino pin notation
co657_kn241 0:fb05faf411e7 20 C12832 lcd(D11, D13, D12, D7, D10);
co657_kn241 0:fb05faf411e7 21
co657_kn241 0:fb05faf411e7 22 static Serial host (USBTX, USBRX); /* connection to host */
co657_kn241 0:fb05faf411e7 23
co657_kn241 0:fb05faf411e7 24 // led display
co657_kn241 0:fb05faf411e7 25 DigitalOut red_led(D5);
co657_kn241 0:fb05faf411e7 26 DigitalOut green_led(D9);
co657_kn241 0:fb05faf411e7 27
co657_kn241 0:fb05faf411e7 28 // joystick left and right
co657_kn241 0:fb05faf411e7 29 DigitalIn left(A4);
co657_kn241 0:fb05faf411e7 30 AnalogIn right(A5);
co657_kn241 0:fb05faf411e7 31
co657_kn241 0:fb05faf411e7 32 // sw2 and sw3
co657_kn241 0:fb05faf411e7 33 InterruptIn sw2_int (PTC6); /* interrupts for the two on-board switches */
co657_kn241 0:fb05faf411e7 34 InterruptIn sw3_int (PTA4);
co657_kn241 0:fb05faf411e7 35
co657_kn241 0:fb05faf411e7 36 static volatile int sw2_trig; /* switches triggered? */
co657_kn241 0:fb05faf411e7 37 static volatile int sw3_trig;
co657_kn241 0:fb05faf411e7 38
co657_kn241 0:fb05faf411e7 39 PwmOut spkr(D6);
co657_kn241 0:fb05faf411e7 40
co657_kn241 0:fb05faf411e7 41 const char* def = "Unknown";
co657_kn241 0:fb05faf411e7 42 const char* stat = "Unknown";
co657_kn241 0:fb05faf411e7 43
co657_kn241 0:fb05faf411e7 44
co657_kn241 0:fb05faf411e7 45 /* connection stuff for MQTT */
co657_kn241 0:fb05faf411e7 46 typedef struct S_mqtt_params {
co657_kn241 0:fb05faf411e7 47 char *hostname;
co657_kn241 0:fb05faf411e7 48 int port;
co657_kn241 0:fb05faf411e7 49 char *topic;
co657_kn241 0:fb05faf411e7 50 char *clientid;
co657_kn241 0:fb05faf411e7 51 } mqtt_params_t;
co657_kn241 0:fb05faf411e7 52
co657_kn241 0:fb05faf411e7 53 /* configure MQTT with hostname, port, topic and clientid */
co657_kn241 0:fb05faf411e7 54 static mqtt_params_t mqtt_config = {
co657_kn241 0:fb05faf411e7 55 hostname: "129.12.44.120"
co657_kn241 0:fb05faf411e7 56 ,
co657_kn241 0:fb05faf411e7 57 port: 1883,
co657_kn241 0:fb05faf411e7 58 topic: "unikent/users/kn241/room/status"
co657_kn241 0:fb05faf411e7 59 ,
co657_kn241 0:fb05faf411e7 60 clientid: "kn241"
co657_kn241 0:fb05faf411e7 61 };
co657_kn241 0:fb05faf411e7 62
co657_kn241 0:fb05faf411e7 63 /*
co657_kn241 0:fb05faf411e7 64 * prints stuffs on the serial host and lcd
co657_kn241 0:fb05faf411e7 65 */
co657_kn241 0:fb05faf411e7 66 static int set_up(void)
co657_kn241 0:fb05faf411e7 67 {
co657_kn241 0:fb05faf411e7 68 host.baud (HOST_SPEED);
co657_kn241 0:fb05faf411e7 69 host.printf ("\r\n\r\nEthernet MQTT client from K64F\r\n");
co657_kn241 0:fb05faf411e7 70
co657_kn241 0:fb05faf411e7 71 lcd.cls ();
co657_kn241 0:fb05faf411e7 72 lcd.locate (0, 0);
co657_kn241 0:fb05faf411e7 73 lcd.printf ("Door Lock Mechanism\n");
co657_kn241 0:fb05faf411e7 74
co657_kn241 0:fb05faf411e7 75 return 0;
co657_kn241 0:fb05faf411e7 76 }
co657_kn241 0:fb05faf411e7 77
co657_kn241 0:fb05faf411e7 78
co657_kn241 0:fb05faf411e7 79 /*
co657_kn241 0:fb05faf411e7 80 * Display the status of the connection to MQTT aswell as
co657_kn241 0:fb05faf411e7 81 * the activity of the user.
co657_kn241 0:fb05faf411e7 82 *
co657_kn241 0:fb05faf411e7 83 * Roboust function used to print out format,arg of server
co657_kn241 0:fb05faf411e7 84 * server connection and user interaction on the serial host
co657_kn241 0:fb05faf411e7 85 * and lcd
co657_kn241 0:fb05faf411e7 86 */
co657_kn241 0:fb05faf411e7 87 static int display_info(const char *format, ...)
co657_kn241 0:fb05faf411e7 88 {
co657_kn241 0:fb05faf411e7 89 va_list arg;
co657_kn241 0:fb05faf411e7 90 int re;
co657_kn241 0:fb05faf411e7 91
co657_kn241 0:fb05faf411e7 92 va_start(arg, format);
co657_kn241 0:fb05faf411e7 93 re = host.vprintf (format, arg);
co657_kn241 0:fb05faf411e7 94 va_end (arg);
co657_kn241 0:fb05faf411e7 95 host.printf ("\r\n");
co657_kn241 0:fb05faf411e7 96
co657_kn241 0:fb05faf411e7 97 lcd.fill (0, 20, 128, 12, 0);
co657_kn241 0:fb05faf411e7 98 lcd.locate (0, 15);
co657_kn241 0:fb05faf411e7 99 va_start (arg, format);
co657_kn241 0:fb05faf411e7 100 lcd.vprintf (format, arg);
co657_kn241 0:fb05faf411e7 101 va_end (arg);
co657_kn241 0:fb05faf411e7 102
co657_kn241 0:fb05faf411e7 103 return re;
co657_kn241 0:fb05faf411e7 104 }
co657_kn241 0:fb05faf411e7 105
co657_kn241 0:fb05faf411e7 106 /* plays sounds and sets chars to accepted */
co657_kn241 0:fb05faf411e7 107 void accepted()
co657_kn241 0:fb05faf411e7 108 {
co657_kn241 0:fb05faf411e7 109 def = "Accepted";
co657_kn241 0:fb05faf411e7 110 stat= "Unlocked";
co657_kn241 0:fb05faf411e7 111 for (float i=2000.0f; i<10000.0f; i+=100) {
co657_kn241 0:fb05faf411e7 112 spkr.period(1.0f/i);
co657_kn241 0:fb05faf411e7 113 spkr=0.5;
co657_kn241 0:fb05faf411e7 114 wait(0.02);
co657_kn241 0:fb05faf411e7 115 spkr = 0.0;
co657_kn241 0:fb05faf411e7 116 }
co657_kn241 0:fb05faf411e7 117 }
co657_kn241 0:fb05faf411e7 118
co657_kn241 0:fb05faf411e7 119 /* function which checks when wrong card is scanned */
co657_kn241 0:fb05faf411e7 120 void declined()
co657_kn241 0:fb05faf411e7 121 {
co657_kn241 0:fb05faf411e7 122 def = "Rejected";
co657_kn241 0:fb05faf411e7 123 stat ="Locked";
co657_kn241 0:fb05faf411e7 124 red_led = !left;
co657_kn241 0:fb05faf411e7 125
co657_kn241 0:fb05faf411e7 126 for (float i=2000.0f; i<10000.0f; i+=100) {
co657_kn241 0:fb05faf411e7 127 spkr.period(1.0/150.0); // 500hz period
co657_kn241 0:fb05faf411e7 128 spkr =0.25;
co657_kn241 0:fb05faf411e7 129 wait(.02);
co657_kn241 0:fb05faf411e7 130 spkr=0.0;
co657_kn241 0:fb05faf411e7 131 }
co657_kn241 0:fb05faf411e7 132 }
co657_kn241 0:fb05faf411e7 133
co657_kn241 0:fb05faf411e7 134 //main code, where the program runs
co657_kn241 0:fb05faf411e7 135 int main()
co657_kn241 0:fb05faf411e7 136 {
co657_kn241 0:fb05faf411e7 137 const char *ip;
co657_kn241 0:fb05faf411e7 138 int re = 0;
co657_kn241 0:fb05faf411e7 139 MQTT::Message msg;
co657_kn241 0:fb05faf411e7 140 char buf[200];
co657_kn241 0:fb05faf411e7 141
co657_kn241 0:fb05faf411e7 142 set_up();
co657_kn241 0:fb05faf411e7 143 display_info("Establishing network...");
co657_kn241 0:fb05faf411e7 144
co657_kn241 0:fb05faf411e7 145 /* Brings up the network interface */
co657_kn241 0:fb05faf411e7 146 MQTTEthernet eth = MQTTEthernet ();
co657_kn241 0:fb05faf411e7 147 ip = eth.get_ip_address();
co657_kn241 0:fb05faf411e7 148
co657_kn241 0:fb05faf411e7 149 display_info("IP address is: %s", ip ?: "Not found.");
co657_kn241 0:fb05faf411e7 150
co657_kn241 0:fb05faf411e7 151 /* Create Mbed Client Interface */
co657_kn241 0:fb05faf411e7 152 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown> (eth);
co657_kn241 0:fb05faf411e7 153
co657_kn241 0:fb05faf411e7 154 /* Create TCP connection */
co657_kn241 0:fb05faf411e7 155 eth.open (eth.getEth());
co657_kn241 0:fb05faf411e7 156 re = eth.connect (mqtt_config.hostname, mqtt_config.port);
co657_kn241 0:fb05faf411e7 157
co657_kn241 0:fb05faf411e7 158
co657_kn241 0:fb05faf411e7 159 if (!re) {
co657_kn241 0:fb05faf411e7 160 display_info ("Connected Status: Good");
co657_kn241 0:fb05faf411e7 161
co657_kn241 0:fb05faf411e7 162 /* Wait for a short length of time to allow user to see output messages. */
co657_kn241 0:fb05faf411e7 163 Thread::wait(1500);
co657_kn241 0:fb05faf411e7 164
co657_kn241 0:fb05faf411e7 165 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
co657_kn241 0:fb05faf411e7 166 data.keepAliveInterval = 20;
co657_kn241 0:fb05faf411e7 167 data.cleansession = 1;
co657_kn241 0:fb05faf411e7 168 data.MQTTVersion = 3;
co657_kn241 0:fb05faf411e7 169 data.clientID.cstring = mqtt_config.clientid;
co657_kn241 0:fb05faf411e7 170 re = client.connect (data);
co657_kn241 0:fb05faf411e7 171
co657_kn241 0:fb05faf411e7 172 if (!re) {
co657_kn241 0:fb05faf411e7 173 display_info ("MQTT connected!");
co657_kn241 0:fb05faf411e7 174
co657_kn241 0:fb05faf411e7 175 while (!re) {
co657_kn241 0:fb05faf411e7 176 /* clear screen to setup - set up door status*/
co657_kn241 0:fb05faf411e7 177 lcd.cls ();
co657_kn241 0:fb05faf411e7 178 set_up();
co657_kn241 0:fb05faf411e7 179
co657_kn241 0:fb05faf411e7 180 while(1) {
co657_kn241 0:fb05faf411e7 181
co657_kn241 0:fb05faf411e7 182 //Depending on button pressed display a colour
co657_kn241 0:fb05faf411e7 183 red_led = !left;
co657_kn241 0:fb05faf411e7 184 green_led = !right;
co657_kn241 0:fb05faf411e7 185
co657_kn241 0:fb05faf411e7 186 if(right && !left) {
co657_kn241 0:fb05faf411e7 187 accepted();
co657_kn241 0:fb05faf411e7 188 } else if(left && !right) {
co657_kn241 0:fb05faf411e7 189 declined();
co657_kn241 0:fb05faf411e7 190 }
co657_kn241 0:fb05faf411e7 191 display_info("Scan :%s \n", def);
co657_kn241 0:fb05faf411e7 192 sprintf (buf, "%s\n", stat);
co657_kn241 0:fb05faf411e7 193 // QoS 0
co657_kn241 0:fb05faf411e7 194
co657_kn241 0:fb05faf411e7 195 msg.qos = MQTT::QOS0;
co657_kn241 0:fb05faf411e7 196 msg.retained = false;
co657_kn241 0:fb05faf411e7 197 msg.dup = false;
co657_kn241 0:fb05faf411e7 198 msg.payload = (void*)buf;
co657_kn241 0:fb05faf411e7 199 msg.payloadlen = strlen(buf)+1;
co657_kn241 0:fb05faf411e7 200
co657_kn241 0:fb05faf411e7 201 re = client.publish (mqtt_config.topic, msg);
co657_kn241 0:fb05faf411e7 202 wait(1.0);
co657_kn241 0:fb05faf411e7 203
co657_kn241 0:fb05faf411e7 204
co657_kn241 0:fb05faf411e7 205 if (re) {
co657_kn241 0:fb05faf411e7 206 //if data can't be publish
co657_kn241 0:fb05faf411e7 207 display_info ("MQTT publish failed with %d", re);
co657_kn241 0:fb05faf411e7 208 } else {
co657_kn241 0:fb05faf411e7 209 Thread::wait(1500);
co657_kn241 0:fb05faf411e7 210 }
co657_kn241 0:fb05faf411e7 211 }
co657_kn241 0:fb05faf411e7 212 }
co657_kn241 0:fb05faf411e7 213 } else {
co657_kn241 0:fb05faf411e7 214 //
co657_kn241 0:fb05faf411e7 215 display_info ("Failed to connect to MQTT");
co657_kn241 0:fb05faf411e7 216 }
co657_kn241 0:fb05faf411e7 217 } else {
co657_kn241 0:fb05faf411e7 218 display_info ("Failed to connect (TCP)");
co657_kn241 0:fb05faf411e7 219 }
co657_kn241 0:fb05faf411e7 220
co657_kn241 0:fb05faf411e7 221 // closed ethernet socket
co657_kn241 0:fb05faf411e7 222 eth.disconnect();
co657_kn241 0:fb05faf411e7 223 }