
Freescale FRDM K64F with DeviceHub.net IoT platform demo code
Dependencies: EthernetInterface FXOS8700Q MQTTS MbedJSONValue mbed-rtos mbed wolfSSL
Fork of HelloMQTTS by
Revision 17:25584027fae0, committed 2015-07-26
- Comitter:
- wolfSSL
- Date:
- Sun Jul 26 09:55:46 2015 +0000
- Parent:
- 16:28d062c5522b
- Child:
- 18:8fd48d6389de
- Commit message:
- Added TLS, extended stack size, connect info from SD file;
Changed in this revision
--- a/C12832.lib Mon Oct 06 11:42:25 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://mbed.org/teams/components/code/C12832/#03069e3deaa4
--- a/EthernetInterface.lib Mon Oct 06 11:42:25 2014 +0000 +++ b/EthernetInterface.lib Sun Jul 26 09:55:46 2015 +0000 @@ -1,1 +1,1 @@ -https://mbed.org/users/mbed_official/code/EthernetInterface/#f69b81aa9eb1 +https://mbed.org/users/mbed_official/code/EthernetInterface/#2fc406e2553f
--- a/MQTT.lib Mon Oct 06 11:42:25 2014 +0000 +++ b/MQTT.lib Sun Jul 26 09:55:46 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/mqtt/code/MQTT/#c299463ae853 +http://mbed.org/teams/mqtt/code/MQTT/#d8968fcc21b8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NTPClient.lib Sun Jul 26 09:55:46 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/donatien/code/NTPClient/#881559865a93
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Sun Jul 26 09:55:46 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mbed_official/code/SDFileSystem/#7b35d1709458
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/getline.cpp Sun Jul 26 09:55:46 2015 +0000 @@ -0,0 +1,43 @@ + +#include <mbed.h> + +int getline(char *prompt, char *buff, int size) +{ + int sz ; + + printf("%s", prompt) ; + for(sz = 0 ; (sz < size) && ((*buff = getchar()) != '\r'); sz++, buff++) { + putchar(*buff) ; + if(*buff == '\\') { + if(++sz >= size)break ; + *buff = getchar() ; + putchar(*buff) ; + switch(*buff) { + case 'n' : + *buff = '\n' ; + break ; + case 'r' : + *buff = '\r' ; + break ; + case 't' : + *buff = '\t' ; + break ; + case '\\': + *buff = '\\' ; + break ; + default: + buff[1] = buff[0] ; + buff[0] = '\\' ; + buff++ ; + } + } else if(*buff == '\b') { + if(sz >= 2) { + buff-=2 ; + sz-=2; + } + } + } ; + putchar('\n') ; + *buff = '\0' ; + return sz ; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/getline.h Sun Jul 26 09:55:46 2015 +0000 @@ -0,0 +1,1 @@ +int getline(char *prompt, char *buff, int size) ; \ No newline at end of file
--- a/main.cpp Mon Oct 06 11:42:25 2014 +0000 +++ b/main.cpp Sun Jul 26 09:55:46 2015 +0000 @@ -13,121 +13,215 @@ * Contributors: * Ian Craggs - initial API and implementation and/or initial documentation *******************************************************************************/ - - /** - This is a sample program to illustrate the use of the MQTT Client library - on the mbed platform. The Client class requires two classes which mediate - access to system interfaces for networking and timing. As long as these two - classes provide the required public programming interfaces, it does not matter - what facilities they use underneath. In this program, they use the mbed - system libraries. - - */ +/** + This is a sample program to illustrate the use of the MQTT Client library + on the mbed platform. The Client class requires two classes which mediate + access to system interfaces for networking and timing. As long as these two + classes provide the required public programming interfaces, it does not matter + what facilities they use underneath. In this program, they use the mbed + system libraries. -#include "C12832.h" -C12832 lcd(p5, p7, p6, p8, p11); +*/ #include "MQTTEthernet.h" #include "MQTTClient.h" +#include "NTPClient.h" +#include "getline.h" +#include "SDFileSystem.h" +SDFileSystem sdCard(PTE3, PTE1, PTE2, PTE4, "sd"); /* pins for FRDM-K64F */ +const char* certFile = "/sd/mqtts-cert.pem"; + int arrivedcount = 0; - void messageArrived(MQTT::MessageData& md) { MQTT::Message &message = md.message; - lcd.cls(); - lcd.locate(0,3); printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); ++arrivedcount; - lcd.puts((char*)message.payload); + puts((char*)message.payload); +} + +static char * removeNL(char *str) +{ + for(int i=strlen(str)-1; (str[i]=='\n')||(str[i]=='\r'); i--)str[i]='\0' ; + return str ; } +static bool getConnectInfo(char **hn, char **un, char **pwd, char **cID, + char **tc, int *pt, char **ms, char **cert) +{ + static char hostname[100] ; + static char username[20] ; + static char password[20] ; + static char clientID[20] ; + static char topic[50] ; + static char port_s[10] ; + static int port ; + static char msg[100] ; + static char certName[30] ; + static char fullName[sizeof(certName)+10] ; -int main(int argc, char* argv[]) -{ + *hn = hostname ; + *un = username ; + *pwd= password ; + *cID= clientID ; + *tc = topic ; + *ms = msg ; + *cert=fullName ; + + FILE *fp = fopen("/sd/connectInfo.txt", "r"); + if (fp == NULL) { + printf("Cannot open \"connectInfo.txt\"\n") ; + return false ; + } + fgets(hostname, sizeof(hostname), fp) ; + fgets(username , sizeof(username), fp); + fgets(password, sizeof(password), fp) ; + fgets(clientID, sizeof(clientID), fp) ; + fgets(topic, sizeof(topic), fp) ; + getline("Port(1883/8883): ", port_s, sizeof(port_s)) ; + getline("Message: ", msg, sizeof(msg)) ; + removeNL(hostname) ; + removeNL(username) ; + removeNL(password) ; + removeNL(clientID) ; + removeNL(topic) ; + port = atoi(removeNL(port_s)) ; + *pt = port ; + printf("Connecting to %s:%d, %s\n", hostname, port, port>8000? "MQTT-TLS":"MQTT" ); + if(port>8000) { + getline("Cert File name: ", certName, sizeof(certName)) ; + removeNL(certName) ; + if(*certName != '\0') { + sprintf(fullName, "/sd/%s", certName) ; + FILE *fp = fopen(fullName, "r"); + if (fp != NULL) { + NTPClient ntp; /* set Realtime clock for cert verification */ + if(ntp.setTime("ntp.jst.mfeed.ad.jp") != 0) { + printf("NTP Error\n") ; + return false ; + } + printf("Verify Server: %s\n", certName) ; + } else { + printf("ERROR: file not found\n") ; + return false ; + } + } else { + fullName[0] = '\0' ; + printf("No verify Server\n") ; + } + } + return true ; +} + +void mqtt_main(void const *av) +{ MQTTEthernet ipstack = MQTTEthernet(); float version = 0.47; - char* topic = "mbed-sample"; - - lcd.printf("Version is %f\n", version); + printf("Version is %f\n", version); - + printf("Version is %f\n", version); + MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); - - char* hostname = "m2m.eclipse.org"; - int port = 1883; - lcd.printf("Connecting to %s:%d\n", hostname, port); - int rc = ipstack.connect(hostname, port); + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + + /* get connect info */ + char *hostname ; + char *username ; + char *password ; + char *clientID ; + char *topic ; + int port ; + char *msg ; + char *fullName ; + + if(!getConnectInfo(&hostname, &username, &password, &clientID, &topic, &port, &msg, &fullName)) + return ; + data.username.cstring = username ; + data.password.cstring = password ; + data.clientID.cstring = clientID ; + + int rc = ipstack.connect(hostname, port, port>8000 ? fullName : NULL); if (rc != 0) - lcd.printf("rc from TCP connect is %d\n", rc); - - MQTTPacket_connectData data = MQTTPacket_connectData_initializer; - data.MQTTVersion = 3; - data.clientID.cstring = "mbed-sample"; - data.username.cstring = "testuser"; - data.password.cstring = "testpassword"; + printf("rc from TCP connect is %d\n", rc); + if ((rc = client.connect(data)) != 0) - lcd.printf("rc from MQTT connect is %d\n", rc); - - if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0) - lcd.printf("rc from MQTT subscribe is %d\n", rc); - + printf("rc from MQTT connect is %d\n", rc); + printf("MQTT connected\n") ; + if ((rc = client.subscribe(topic, MQTT::QOS0, messageArrived)) != 0) + printf("rc from MQTT subscribe is %d\n", rc); + printf("Subscribed\n") ; MQTT::Message message; - // QoS 0 - char buf[100]; - sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version); +// QoS 0 + char buf[sizeof(message)+50] ; + sprintf(buf, "\"%s\", QoS 0 message from app version %f\n", msg, version); message.qos = MQTT::QOS0; message.retained = false; message.dup = false; message.payload = (void*)buf; message.payloadlen = strlen(buf)+1; rc = client.publish(topic, message); + + printf("QoS0: Published to %s\n\t%s\n", topic, message.payload) ; while (arrivedcount < 1) client.yield(100); - - // QoS 1 - sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version); + +// QoS 1 + sprintf(buf, "\"%s\", QoS 1 message from app version %f\n", msg, version); message.qos = MQTT::QOS1; + message.payloadlen = strlen(buf)+1; rc = client.publish(topic, message); + printf("QoS1: Published to %s\n\t%s\n", topic, message.payload) ; while (arrivedcount < 2) client.yield(100); - - // QoS 2 - sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version); + +#if 0 /* QoS 2 not tested */ +// QoS 2 + sprintf(buf, "\"%s\", QoS 2 message from app version %f\n", version); message.qos = MQTT::QOS2; message.payloadlen = strlen(buf)+1; rc = client.publish(topic, message); + printf("QoS2: Published to %s\n\t%s\n", topic, message.payload) ; while (arrivedcount < 3) client.yield(100); - - // n * QoS 2 - for (int i = 1; i <= 10; ++i) - { - sprintf(buf, "Hello World! QoS 2 message number %d from app version %f\n", i, version); + +// n * QoS 2 + for (int i = 1; i <= 10; ++i) { + sprintf(buf, "\"%s\", QoS 2 message number %d from app version %f\n", i, version); message.qos = MQTT::QOS2; message.payloadlen = strlen(buf)+1; rc = client.publish(topic, message); + printf("QoS2[%d]: Published to %s\n\t%s\n", i, topic, message.payload) ; while (arrivedcount < i + 3) client.yield(100); } - +#endif + + printf("unsubscribing\n") ; if ((rc = client.unsubscribe(topic)) != 0) printf("rc from unsubscribe was %d\n", rc); - + if ((rc = client.disconnect()) != 0) printf("rc from disconnect was %d\n", rc); - + ipstack.disconnect(); - - lcd.cls(); - lcd.locate(0,3); - lcd.printf("Version %.2f: finish %d msgs\n", version, arrivedcount); + printf("Version %.2f: finish %d msgs\n", version, arrivedcount); printf("Finishing with %d messages received\n", arrivedcount); - - return 0; + + return ; } + +main() +{ +#define STACK_SIZE 24000 + Thread t(mqtt_main, NULL, osPriorityNormal, STACK_SIZE); + while (true) { + Thread::wait(1000); + } +}
--- a/mbed-rtos.lib Mon Oct 06 11:42:25 2014 +0000 +++ b/mbed-rtos.lib Sun Jul 26 09:55:46 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#34e80e862021 +http://mbed.org/users/mbed_official/code/mbed-rtos/#5aed8bae1001
--- a/mbed.bld Mon Oct 06 11:42:25 2014 +0000 +++ b/mbed.bld Sun Jul 26 09:55:46 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/6213f644d804 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/bad568076d81 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wolfSSL.lib Sun Jul 26 09:55:46 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/wolfSSL/code/wolfSSL/#28278596c2a2