Port to C027 (using AppShield and Ethernet)

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samdanbury 6:37b6d0d56190 1 #include "MQTTPacket.h"
samdanbury 6:37b6d0d56190 2
samdanbury 6:37b6d0d56190 3 #include <errno.h>
samdanbury 6:37b6d0d56190 4 #include <stdlib.h>
samdanbury 6:37b6d0d56190 5 #include <string.h>
samdanbury 6:37b6d0d56190 6
samdanbury 6:37b6d0d56190 7 #include "EthernetInterface.h"
samdanbury 6:37b6d0d56190 8
samdanbury 6:37b6d0d56190 9
samdanbury 6:37b6d0d56190 10 TCPSocketConnection mysock;
samdanbury 6:37b6d0d56190 11
samdanbury 6:37b6d0d56190 12 int getdata(char* buf, int count)
samdanbury 6:37b6d0d56190 13 {
samdanbury 6:37b6d0d56190 14 return mysock.receive(buf, (size_t)count);
samdanbury 6:37b6d0d56190 15 }
samdanbury 6:37b6d0d56190 16
samdanbury 6:37b6d0d56190 17 int toStop = 0;
samdanbury 6:37b6d0d56190 18
samdanbury 6:37b6d0d56190 19
samdanbury 6:37b6d0d56190 20 int main()
samdanbury 6:37b6d0d56190 21 {
samdanbury 6:37b6d0d56190 22 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
samdanbury 6:37b6d0d56190 23 int rc = 0;
samdanbury 6:37b6d0d56190 24 char buf[200];
samdanbury 6:37b6d0d56190 25 int buflen = sizeof(buf);
samdanbury 6:37b6d0d56190 26 int msgid = 1;
samdanbury 6:37b6d0d56190 27 MQTTString topicString = MQTTString_initializer;
samdanbury 6:37b6d0d56190 28 int req_qos = 0;
samdanbury 6:37b6d0d56190 29 char* payload = "mypayload";
samdanbury 6:37b6d0d56190 30 int payloadlen = strlen(payload);
samdanbury 6:37b6d0d56190 31 int len = 0;
samdanbury 6:37b6d0d56190 32 EthernetInterface eth;
samdanbury 6:37b6d0d56190 33
samdanbury 6:37b6d0d56190 34 eth.init(); //Use DHCP
samdanbury 6:37b6d0d56190 35 eth.connect();
samdanbury 6:37b6d0d56190 36
samdanbury 6:37b6d0d56190 37 rc = mysock.connect("m2m.eclipse.org", 1883);
samdanbury 6:37b6d0d56190 38
samdanbury 6:37b6d0d56190 39 data.clientID.cstring = "SendReceive mbed MQTT ";
samdanbury 6:37b6d0d56190 40 data.keepAliveInterval = 20;
samdanbury 6:37b6d0d56190 41 data.cleansession = 1;
samdanbury 6:37b6d0d56190 42
samdanbury 6:37b6d0d56190 43 mysock.set_blocking(true, 1000); /* 1 second Timeout */
samdanbury 6:37b6d0d56190 44
samdanbury 6:37b6d0d56190 45 len = MQTTSerialize_connect(buf, buflen, &data);
samdanbury 6:37b6d0d56190 46 rc = mysock.send(buf, len);
samdanbury 6:37b6d0d56190 47
samdanbury 6:37b6d0d56190 48 /* wait for connack */
samdanbury 6:37b6d0d56190 49 if (MQTTPacket_read(buf, buflen, getdata) == CONNACK)
samdanbury 6:37b6d0d56190 50 {
samdanbury 6:37b6d0d56190 51 int connack_rc;
samdanbury 6:37b6d0d56190 52
samdanbury 6:37b6d0d56190 53 if (MQTTDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
samdanbury 6:37b6d0d56190 54 {
samdanbury 6:37b6d0d56190 55 printf("Unable to connect, return code %d\n", connack_rc);
samdanbury 6:37b6d0d56190 56 goto exit;
samdanbury 6:37b6d0d56190 57 }
samdanbury 6:37b6d0d56190 58 }
samdanbury 6:37b6d0d56190 59 else
samdanbury 6:37b6d0d56190 60 goto exit;
samdanbury 6:37b6d0d56190 61
samdanbury 6:37b6d0d56190 62 /* subscribe */
samdanbury 6:37b6d0d56190 63 topicString.cstring = "substopic";
samdanbury 6:37b6d0d56190 64 len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos);
samdanbury 6:37b6d0d56190 65
samdanbury 6:37b6d0d56190 66 rc = mysock.send(buf, len);
samdanbury 6:37b6d0d56190 67 if (MQTTPacket_read(buf, buflen, getdata) == SUBACK) /* wait for suback */
samdanbury 6:37b6d0d56190 68 {
samdanbury 6:37b6d0d56190 69 int submsgid;
samdanbury 6:37b6d0d56190 70 int subcount;
samdanbury 6:37b6d0d56190 71 int granted_qos;
samdanbury 6:37b6d0d56190 72
samdanbury 6:37b6d0d56190 73 rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen);
samdanbury 6:37b6d0d56190 74 if (granted_qos != 0)
samdanbury 6:37b6d0d56190 75 {
samdanbury 6:37b6d0d56190 76 printf("granted qos != 0, %d\n", granted_qos);
samdanbury 6:37b6d0d56190 77 goto exit;
samdanbury 6:37b6d0d56190 78 }
samdanbury 6:37b6d0d56190 79 }
samdanbury 6:37b6d0d56190 80 else
samdanbury 6:37b6d0d56190 81 goto exit;
samdanbury 6:37b6d0d56190 82
samdanbury 6:37b6d0d56190 83 topicString.cstring = "pubtopic";
samdanbury 6:37b6d0d56190 84 while (!toStop)
samdanbury 6:37b6d0d56190 85 {
samdanbury 6:37b6d0d56190 86 if (MQTTPacket_read(buf, buflen, getdata) == PUBLISH)
samdanbury 6:37b6d0d56190 87 {
samdanbury 6:37b6d0d56190 88 int dup;
samdanbury 6:37b6d0d56190 89 int qos;
samdanbury 6:37b6d0d56190 90 int retained;
samdanbury 6:37b6d0d56190 91 int msgid;
samdanbury 6:37b6d0d56190 92 int payloadlen_in;
samdanbury 6:37b6d0d56190 93 char* payload_in;
samdanbury 6:37b6d0d56190 94 int rc;
samdanbury 6:37b6d0d56190 95 MQTTString receivedTopic;
samdanbury 6:37b6d0d56190 96
samdanbury 6:37b6d0d56190 97 rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic,
samdanbury 6:37b6d0d56190 98 &payload_in, &payloadlen_in, buf, buflen);
samdanbury 6:37b6d0d56190 99 printf("message arrived %.*s\n", payloadlen_in, payload_in);
samdanbury 6:37b6d0d56190 100 }
samdanbury 6:37b6d0d56190 101
samdanbury 6:37b6d0d56190 102 printf("publishing reading\n");
samdanbury 6:37b6d0d56190 103 len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, payload, payloadlen);
samdanbury 6:37b6d0d56190 104 rc = mysock.send(buf, len);
samdanbury 6:37b6d0d56190 105 }
samdanbury 6:37b6d0d56190 106
samdanbury 6:37b6d0d56190 107 printf("disconnecting\n");
samdanbury 6:37b6d0d56190 108 len = MQTTSerialize_disconnect(buf, buflen);
samdanbury 6:37b6d0d56190 109 rc = mysock.send(buf, len);
samdanbury 6:37b6d0d56190 110
samdanbury 6:37b6d0d56190 111 exit:
samdanbury 6:37b6d0d56190 112 eth.disconnect();
samdanbury 6:37b6d0d56190 113
samdanbury 6:37b6d0d56190 114 return 0;
samdanbury 6:37b6d0d56190 115 }