It can be run only with mbed LPC1768, LAN socket and DHT11 sensor. mbed application board is not needed.
Dependencies: DHT11 EthernetInterface MQTT mbed-rtos mbed
Fork of IBMIoTClientEthernetExample by
Example Photo
- 水魚堂さんの回路図エディタ BSch3V を利用して作成しました。回路図データ /media/uploads/strysd/simpleiot.ce3
- DHT11 は、masahiro's electronic lab さんで配布されている部品ライブラリを利用しました。http://www.eleclabo.com/denshi/doc/bschlib.html
Diff: main.cpp
- Revision:
- 20:d8387eff818d
- Parent:
- 18:94da9de96d54
--- a/main.cpp Mon May 23 13:48:48 2016 +0000 +++ b/main.cpp Sat Oct 15 05:55:27 2016 +0000 @@ -17,28 +17,25 @@ * Chris Styles - Added additional menu screen for software revision * James Sutton - Mac fix and extra debug * Ian Craggs - add not authorized messages - * - * To do : - * Add magnetometer sensor output to IoT data stream - * + ******************************************************************************* + * Forked by Satoru Yoshida 2016/Oct + * Removed support for mbed application board, this program can run only + * set of mbed LPC1768, LAN socket and DHT11 sensor *******************************************************************************/ -#include "LM75B.h" -#include "MMA7660.h" + #include "MQTTClient.h" #include "MQTTEthernet.h" -#include "C12832.h" -#include "Arial12x12.h" #include "rtos.h" // Update this to the next number *before* a commit -#define __APP_SW_REVISION__ "18" +#define __APP_SW_REVISION__ "1" // Configuration values needed to connect to IBM IoT Cloud -#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 ORG "" // For a registered connection, replace with your org +#define ID "" // For a registered connection, replace with your device id, not MAC address +#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 MQTT_PORT 1883 #define MQTT_TLS_PORT 8883 @@ -46,22 +43,13 @@ #define MQTT_MAX_PACKET_SIZE 250 -#if defined(TARGET_UBLOX_C027) -#warning "Compiling for mbed C027" -#include "C027.h" -#elif defined(TARGET_LPC1768) -#warning "Compiling for mbed LPC1768" #include "LPC1768.h" -#elif defined(TARGET_K64F) -#warning "Compiling for mbed K64F" -#include "K64F.h" -#endif bool quickstartMode = true; char org[11] = ORG; char type[30] = TYPE; -char id[30] = ID; // mac without colons +char id[30] = ID; // device id char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode bool connected = false; @@ -71,9 +59,7 @@ bool ethernetInitialising = true; int connack_rc = 0; // MQTT connack return code int retryAttempt = 0; -int menuItem = 0; -char* joystickPos = "CENTRE"; int blink_interval = 0; char* ip_addr = ""; @@ -103,198 +89,9 @@ } */ - -void off() -{ - r = g = b = 1.0; // 1 is off, 0 is full brightness -} - -void red() -{ - r = 0.7; g = 1.0; b = 1.0; // 1 is off, 0 is full brightness -} - -void yellow() -{ - r = 0.7; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness -} - -void green() -{ - r = 1.0; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness -} - - -void flashing_yellow(void const *args) -{ - bool on = false; - while (!connected && connack_rc != MQTT_NOT_AUTHORIZED && connack_rc != MQTT_BAD_USERNAME_OR_PASSWORD) // flashing yellow only while connecting - { - on = !on; - if (on) - yellow(); - else - off(); - wait(0.5); - } -} - - -void flashing_red(void const *args) // to be used when the connection is lost -{ - bool on = false; - while (!connected) - { - on = !on; - if (on) - red(); - else - off(); - wait(2.0); - } -} - - -void printMenu(int menuItem) -{ - static char last_line1[30] = "", last_line2[30] = ""; - char line1[30] = "", line2[30] = ""; - - switch (menuItem) - { - case 0: - sprintf(line1, "IBM IoT Cloud"); - sprintf(line2, "Scroll with joystick"); - break; - case 1: - sprintf(line1, "Go to:"); - sprintf(line2, "http://ibm.biz/iotqstart"); - break; - case 2: - sprintf(line1, "Device Identity:"); - sprintf(line2, "%s", id); - break; - case 3: - sprintf(line1, "MQTT Status:"); - if (mqttConnecting) - sprintf(line2, "Connecting... %d/5", retryAttempt); - else - { - if (connected) - sprintf(line2, "Connected"); - else - { - switch (connack_rc) - { - case MQTT_CLIENTID_REJECTED: - sprintf(line2, "Clientid rejected"); - break; - case MQTT_BAD_USERNAME_OR_PASSWORD: - sprintf(line2, "Invalid username or password"); - break; - case MQTT_NOT_AUTHORIZED: - sprintf(line2, "Not authorized"); - break; - default: - sprintf(line2, "Disconnected"); - } - } - } - break; - case 4: - sprintf(line1, "Ethernet State:"); - sprintf(line2, ethernetInitialising ? "Initializing..." : "Initialized"); - break; - case 5: - sprintf(line1, "Socket State:"); - if (netConnecting) - sprintf(line2, "Connecting... %d/5", retryAttempt); - else - sprintf(line2, netConnected ? "Connected" : "Disconnected"); - break; - case 6: - sprintf(line1, "IP Address:"); - sprintf(line2, "%s", ip_addr); - break; - case 7: - sprintf(line1, "Gateway:"); - sprintf(line2, "%s", gateway_addr); - break; - case 8: - sprintf(line1, "App version:"); - sprintf(line2, "%s", __APP_SW_REVISION__); - break; - case 9: - sprintf(line1, "Current Timeout:"); - sprintf(line2, "%d ms", connectTimeout); - break; - } - - if (strcmp(line1, last_line1) != 0 || strcmp(line2, last_line2) != 0) - { - lcd.cls(); - lcd.locate(0, 0); - lcd.printf(line1); - strncpy(last_line1, line1, sizeof(last_line1)); - - lcd.locate(0,16); - lcd.printf(line2); - strncpy(last_line2, line2, sizeof(last_line2)); - } -} - - -void setMenu() -{ - - if (Down) - { - joystickPos = "DOWN"; - if (menuItem >= 0 && menuItem < 9) - printMenu(++menuItem); - } - else if (Left) - joystickPos = "LEFT"; - else if (Click) - joystickPos = "CLICK"; - else if (Up) - { - joystickPos = "UP"; - if (menuItem <= 9 && menuItem > 0) - printMenu(--menuItem); - } - else if (Right) - joystickPos = "RIGHT"; - else - joystickPos = "CENTRE"; -} - -void menu_loop(void const *args) -{ - int count = 0; - while(true) - { - setMenu(); - if (++count % 10 == 0) - printMenu(menuItem); - Thread::wait(100); - } -} - - /** * Display a message on the LCD screen prefixed with IBM IoT Cloud */ -void displayMessage(char* message) -{ - lcd.cls(); - lcd.locate(0,0); - lcd.printf("IBM IoT Cloud"); - lcd.locate(0,16); - lcd.printf(message); -} - - int connect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) { const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com"; @@ -345,13 +142,13 @@ if ((rc = client->connect(data)) == 0) { connected = true; - green(); - displayMessage("Connected"); + led2 = LED2_ON; + LOG("Connected\n"); wait(1); - displayMessage("Scroll with joystick"); } else WARN("MQTT connect returned %d\n", rc); + led2 = LED2_OFF; if (rc >= 0) connack_rc = rc; mqttConnecting = false; @@ -382,8 +179,6 @@ if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) return; // don't reattempt to connect if credentials are wrong - Thread red_thread(flashing_red); - int timeout = getConnTimeout(++retryAttempt); WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); @@ -398,7 +193,6 @@ } } - int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) { MQTT::Message message; @@ -406,8 +200,7 @@ 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}}", - MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read()); + "{\"d\":{\"temp\":%d,\"humidity\":%d}}", dht.readTemperature(), dht.readHumidity()); message.qos = MQTT::QOS0; message.retained = false; message.dup = false; @@ -442,7 +235,9 @@ // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/ char* start = strstr(topic, "/cmd/") + 5; int len = strstr(topic, "/fmt/") - start; - + +//Sample for blink command. +/* if (memcmp(start, "blink", len) == 0) { char payload[message.payloadlen + 1]; @@ -461,20 +256,18 @@ } else WARN("Unsupported command: %.*s\n", len, start); +*/ + } int main() -{ +{ quickstartMode = (strcmp(org, "quickstart") == 0); - lcd.set_font((unsigned char*) Arial12x12); // Set a nice font for the LCD screen - - led2 = LED2_OFF; // K64F: turn off the main board LED + led2 = LED2_OFF; - displayMessage("Connecting"); - Thread yellow_thread(flashing_yellow); - Thread menu_thread(menu_loop); + LOG("Connecting\n"); LOG("***** IBM IoT Client Ethernet Example *****\n"); MQTTEthernet ipstack; @@ -489,9 +282,9 @@ if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) { - red(); + while (true) - wait(1.0); // Permanent failures - don't retry + wait(1); // Permanent failures - don't retry } if (!quickstartMode) @@ -501,21 +294,27 @@ WARN("rc from MQTT subscribe is %d\n", rc); } - blink_interval = 0; int count = 0; + int state = 0; while (true) { - if (++count == 100) - { // Publish a message every second - if (publish(&client, &ipstack) != 0) - attemptConnect(&client, &ipstack); // if we have lost the connection + if (++count == 600) + { // Publish a message every 6 seconds = 600 * 10ms by yield + + state = dht.readData(); + + if (state != DHT11::OK) { + LOG("DHT11 Error: %d\n", state); + LOG("{\"d\":{\"temp\":%d,\"humidity\":%d}}", dht.readTemperature(), dht.readHumidity()); + } else { + if (publish(&client, &ipstack) != 0) { + attemptConnect(&client, &ipstack); // if we have lost the connection + } + } count = 0; + led2 = !led2; } - if (blink_interval == 0) - led2 = LED2_OFF; - else if (count % blink_interval == 0) - led2 = !led2; - client.yield(10); // allow the MQTT client to receive messages + client.yield(10); // allow the MQTT client to receive messages within 10 msec. } -} +} \ No newline at end of file