MQTT demo program for Nucleo F401RE and Seeedstudio W5200 Ethernet Shield which is based on Wiznet IBMIoTClientEthernetExample_W5500.
Dependencies: MQTT W5200Interface mbed DHT11
Fork of IBMIoTClientEthernetExample_W5500 by
I post description regarding this source code here.
http://developer.mbed.org/users/hillkim7/notebook/ibmiotclientethernetexample_w5200/
Diff: main.cpp
- Revision:
- 1:532d83b9f910
- Parent:
- 0:910036879da0
- Child:
- 2:87c816cd88e2
--- a/main.cpp Fri Sep 26 08:06:10 2014 +0000 +++ b/main.cpp Mon Sep 29 04:57:17 2014 +0000 @@ -3,17 +3,17 @@ #include "MQTTEthernet.h" // Configuration values needed to connect to IBM IoT Cloud -#define QUICKSTARTMODE 1 +#define QUICKSTARTMODE 0 #if (QUICKSTARTMODE) -#define ORG "quickstart" +#define ORG "uasfg" #define ID "" -#define AUTH_TOKEN "" -#define TYPE "iotsample-mbed-lpc1768" +#define AUTH_TOKEN "mhhWy4Qg)C*w3jL@(O" +#define TYPE "W5500" #else -#define ORG "Replace with your org" -#define ID "Replace with your id" -#define TYPE "Replace with your type" -#define AUTH_TOKEN "Replace with your auth-token" +#define ORG "uasfg" +#define ID "" +#define AUTH_TOKEN "mhhWy4Qg)C*w3jL@(O" +#define TYPE "W5500" #endif #define MQTT_PORT 1883 @@ -23,6 +23,8 @@ #define MQTT_MAX_PACKET_SIZE 250 +#define USING_HW_STACK_W5500 + bool quickstartMode = (QUICKSTARTMODE) ? true : false; char org[11] = ORG; char type[30] = TYPE; @@ -34,8 +36,7 @@ char* getMac(EthernetInterface& eth, char* buf, int buflen) // Obtain MAC address { - //strncpy(buf, eth.getMACAddress(), buflen); - strncpy(buf,"00:02:f7:f2:05:00",buflen); + strncpy(buf, eth.getMACAddress(), buflen); char* pos; // Remove colons from mac address while ((pos = strchr(buf, ':')) != NULL) @@ -51,6 +52,7 @@ char hostname[strlen(org) + strlen(iot_ibm) + 1]; sprintf(hostname, "%s%s", org, iot_ibm); int rc = ipstack->connect(hostname, IBM_IOT_PORT); + if (rc != 0) return rc; @@ -74,6 +76,7 @@ { connected = true; } + return rc; } @@ -111,8 +114,8 @@ char buf[250]; sprintf(buf, - "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}", - 12.4, 22.3, 12.2, 25.3, "up", 12.3, 22.1); + "{\"d\":{\"myName\":\"IoT mbed\",\"cputemp\":%0.4f,\"testvalue\":%s}}", + 12.4,"W5500"); message.qos = MQTT::QOS0; message.retained = false; @@ -156,33 +159,19 @@ WARN("Unsupported command: %.*s\r\n", len, start); } - - int main() { - Serial pc(USBTX, USBRX); - pc.baud(115200); - - SPI spi(D11, D12, D13); // mosi, miso, sclk - wait(1); - - MQTTEthernet ipstack(&spi, D10, D9); //scs(D10), nRESET(PTA20) - MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); +#if defined(TARGET_KL25Z) + SPI spi(D11, D12, D13); // mosi, miso, sclk + wait(1); + + MQTTEthernet ipstack(&spi, D10, D9); //scs(D10), nRESET(PTA20) + MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); +#endif - if (quickstartMode ) - { - getMac(ipstack.getEth(), id, sizeof(id)); - } - + getMac(ipstack.getEth(), id, sizeof(id)); attemptConnect(&client, &ipstack); - if (!quickstartMode) - { - int rc = 0; - if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0) - WARN("rc from MQTT subscribe is %d\r\n", rc); - } - int count = 0; while (true) {