working
Dependencies: C027_Support IBMIoTClientCellularExample MQTT mbed
Fork of MQTT-GSM-GPS-CAN-SMS by
Diff: main.cpp
- Revision:
- 12:d6b94e8051b0
- Parent:
- 11:7a6df9a2dcdc
- Child:
- 13:6a0af5deb2db
--- a/main.cpp Tue Mar 10 08:24:52 2015 +0000 +++ b/main.cpp Thu Mar 12 12:31:06 2015 +0000 @@ -21,10 +21,11 @@ * *******************************************************************************/ +#define USE_CELLULAR // Enable this switch on the C027 to use cellular + #include "LM75B.h" #include "MMA7660.h" #include "MQTTClient.h" -#include "MQTTEthernet.h" #include "C12832.h" #include "Arial12x12.h" #include "rtos.h" @@ -36,7 +37,7 @@ #define ORG "quickstart" // For a registered connection, replace with your org #define ID "" // For a registered connection, replace with your id #define AUTH_TOKEN "" // For a registered connection, replace with your auth-token -#define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type +#define TYPE_NAME DEFAULT_TYPE_NAME // For a registered connection, replace with your type #define MQTT_PORT 1883 #define MQTT_TLS_PORT 8883 @@ -45,19 +46,27 @@ #define MQTT_MAX_PACKET_SIZE 250 #if defined(TARGET_UBLOX_C027) -#warning "Compiling for mbed C027" -#include "C027.h" +# warning "Compiling for mbed C027" +# include "C027.h" #elif defined(TARGET_LPC1768) -#warning "Compiling for mbed LPC1768" -#include "LPC1768.h" +# warning "Compiling for mbed LPC1768" +# include "LPC1768.h" #elif defined(TARGET_K64F) -#warning "Compiling for mbed K64F" -#include "K64F.h" +# warning "Compiling for mbed K64F" +# include "K64F.h" +#endif + +#ifdef USE_CELLULAR +# define MQTT_CLIENT_TYPE MQTTSocket +# include "MQTTSocket.h" +#else +# define MQTT_CLIENT_TYPE MQTTEthernet +# include "MQTTEthernet.h" #endif bool quickstartMode = true; char org[11] = ORG; -char type[30] = TYPE; +char type[30] = TYPE_NAME; char id[30] = ID; // mac without colons char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode @@ -191,7 +200,7 @@ } -int connect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) +int connect(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack) { const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com"; @@ -237,7 +246,7 @@ } -void attemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) +void attemptConnect(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack) { int retryAttempt = 0; connected = false; @@ -245,7 +254,7 @@ // make sure a cable is connected before starting to connect while (!linkStatus()) { wait(1.0f); - WARN("Ethernet link not present. Check cable connection\n"); + WARN("Internet link not present. Check cable connection\n"); } while (connect(client, ipstack) != 0) @@ -270,7 +279,7 @@ } -int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) +int publish(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack) { MQTT::Message message; char* pubTopic = "iot-2/evt/status/fmt/json"; @@ -312,7 +321,7 @@ return rc; } -#else +#elif !defined(USE_CELLULAR) char* getMac(EthernetInterface& eth, char* buf, int buflen) // Obtain MAC address { strncpy(buf, eth.getMACAddress(), buflen); @@ -322,6 +331,7 @@ memmove(pos, pos + 1, strlen(pos) + 1); return buf; } +#else #endif @@ -374,15 +384,27 @@ Thread yellow_thread(flashing_yellow); #endif - MQTTEthernet ipstack; - MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); - +#ifdef USE_CELLULAR + MDMSerial mdm; + //mdm.setDebug(4); // enable this for debugging issues + if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) + return -1; +#endif + MQTT_CLIENT_TYPE ipstack; + MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); if (quickstartMode) { #if defined(TARGET_K64F) getUUID48(id, sizeof(id)); // getMac doesn't work on the K64F +#elif !defined(USE_CELLULAR) + getMac(ipstack.getEth(), id, sizeof(id)); #else - getMac(ipstack.getEth(), id, sizeof(id)); + MDMParser::NetStatus status; + if (mdm.checkNetStatus(&status) && *status.num) { + int l = strlen(status.num); + strcpy(id, "000000000000"); + strcpy(id+12-l, status.num); + } #endif }