hellomqttt to thingspeak mqtt and ifttt

Dependencies:   Servo MQTTPacket FP

Committer:
icraggs
Date:
Tue Feb 04 22:38:15 2014 +0000
Revision:
0:0cae29831d01
Child:
1:a1d5c7a6acbc
Sample program for MQTT client

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 0:0cae29831d01 1 #include "mbed.h"
icraggs 0:0cae29831d01 2 #include "EthernetInterface.h"
icraggs 0:0cae29831d01 3 #include "C12832_lcd.h"
icraggs 0:0cae29831d01 4
icraggs 0:0cae29831d01 5 #include "MQTTPacket.h"
icraggs 0:0cae29831d01 6
icraggs 0:0cae29831d01 7
icraggs 0:0cae29831d01 8 DigitalOut myled(LED2);
icraggs 0:0cae29831d01 9 C12832_LCD lcd;
icraggs 0:0cae29831d01 10
icraggs 0:0cae29831d01 11 int publish()
icraggs 0:0cae29831d01 12 {
icraggs 0:0cae29831d01 13 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 0:0cae29831d01 14 int rc = 0;
icraggs 0:0cae29831d01 15 char buf[200];
icraggs 0:0cae29831d01 16 int buflen = sizeof(buf);
icraggs 0:0cae29831d01 17 TCPSocketConnection mysock;
icraggs 0:0cae29831d01 18 MQTTString topicString = MQTTString_initializer;
icraggs 0:0cae29831d01 19 char* payload = "I'm alive!";
icraggs 0:0cae29831d01 20 int payloadlen = strlen(payload);
icraggs 0:0cae29831d01 21 int len = 0;
icraggs 0:0cae29831d01 22
icraggs 0:0cae29831d01 23 mysock.connect("m2m.eclipse.org", 1883);
icraggs 0:0cae29831d01 24
icraggs 0:0cae29831d01 25 data.clientID.cstring = "mbed test client - Ian Craggs";
icraggs 0:0cae29831d01 26 data.keepAliveInterval = 20;
icraggs 0:0cae29831d01 27 data.cleansession = 1;
icraggs 0:0cae29831d01 28 data.MQTTVersion = 3;
icraggs 0:0cae29831d01 29
icraggs 0:0cae29831d01 30 len = MQTTSerialize_connect(buf, buflen, &data);
icraggs 0:0cae29831d01 31
icraggs 0:0cae29831d01 32 topicString.cstring = "mbed NXP LPC1768";
icraggs 0:0cae29831d01 33 len += MQTTSerialize_publish(buf + len, buflen - len, 0, 0, 0, 0, topicString, payload, payloadlen);
icraggs 0:0cae29831d01 34
icraggs 0:0cae29831d01 35 len += MQTTSerialize_disconnect(buf + len, buflen - len);
icraggs 0:0cae29831d01 36
icraggs 0:0cae29831d01 37 rc = 0;
icraggs 0:0cae29831d01 38 while (rc < len)
icraggs 0:0cae29831d01 39 {
icraggs 0:0cae29831d01 40 int rc1 = mysock.send(buf, len);
icraggs 0:0cae29831d01 41 if (rc1 == -1)
icraggs 0:0cae29831d01 42 {
icraggs 0:0cae29831d01 43 lcd.printf("Send failed\n");
icraggs 0:0cae29831d01 44 break;
icraggs 0:0cae29831d01 45 }
icraggs 0:0cae29831d01 46 else
icraggs 0:0cae29831d01 47 rc += rc1;
icraggs 0:0cae29831d01 48 }
icraggs 0:0cae29831d01 49 if (rc == len)
icraggs 0:0cae29831d01 50 lcd.printf("Send succeeded\n");
icraggs 0:0cae29831d01 51 wait(0.2);
icraggs 0:0cae29831d01 52
icraggs 0:0cae29831d01 53 return 0;
icraggs 0:0cae29831d01 54 }
icraggs 0:0cae29831d01 55
icraggs 0:0cae29831d01 56 int main() {
icraggs 0:0cae29831d01 57 EthernetInterface eth;
icraggs 0:0cae29831d01 58 eth.init(); //Use DHCP
icraggs 0:0cae29831d01 59 eth.connect();
icraggs 0:0cae29831d01 60 lcd.printf("IP Address is %s\n", eth.getIPAddress());
icraggs 0:0cae29831d01 61
icraggs 0:0cae29831d01 62 while(1) {
icraggs 0:0cae29831d01 63 myled = 1;
icraggs 0:0cae29831d01 64 publish();
icraggs 0:0cae29831d01 65 wait(0.2);
icraggs 0:0cae29831d01 66 myled = 0;
icraggs 0:0cae29831d01 67 publish();
icraggs 0:0cae29831d01 68 wait(0.2);
icraggs 0:0cae29831d01 69 }
icraggs 0:0cae29831d01 70
icraggs 0:0cae29831d01 71 eth.disconnect();
icraggs 0:0cae29831d01 72 }