IBM Cloud’s IoT platform for this kind of usage is free. This gives the capabillity of sending data from anywhere like from a car at zero cost. Here is How to Connect Arduino to IBM Cloud (IoT Platform). We are taking it granted that the reader will use Arduino UNO (not Yun, steps will be slightly different). For this project you need to get used with using ESP8266 with Arduino UNO. ESP8266 is a cheap option for Wi-Fi and has some difficulties, issues etc. Node MCU slightly different topic than talking around just Arduino UNO (NodeMCU will not suffer from packet loss). We can send collected data to IBM Cloud in 2 ways, with HTTP and MQTT. HTTP integration is easier. MQTT is good for use in embedded devices because it is asynchronous, usable where Internet connections are unreliable, less need of much software to implement a client with limited memory. There are many guides on this topic :
1 2 3 | https://github.com/ChrisBarsolai/arduino-mqtt-tutorial http://henrywill4.blogspot.com/2015/09/cabin-fever-part-5-mqtt-and-ibm.html http://www.zombieprototypes.com/?p=92 |
You need to login to IBM Cloud (formerly Bluemix), Find IoT from Catalogue of services and create a service (use boilerplate). Regardless of method (HTTP or MQTT), you will need to signup from this sub-domain of IBM Cloud :
1 | https://internetofthings.ibmcloud.com/ |
Generate API key, get the authentication token, create a device of device type with that authentication token generated. If you want to go with MQTT, then Arduino MQTT client has dedicated GitHub project :
---
1 | https://pubsubclient.knolleary.net/ |
In case you want to use MQTT, you need to install mosquitto
(install as service for MS Windows) to locally test:
1 | http://mosquitto.org/ |
For that kind of setup, you will need this kind of code for using with DHT 11 sensor (you must adjust code for newer DHT part, see this example) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | /* MQTT IOT Example - continuously obtains values from the Virtuabotix DHT11 temperature/humidity sensor - formats the results as a JSON string for the IBM IOT example - connects to an MQTT server (either local or at the IBM IOT Cloud) - and publishes the JSON String to the topic named quickstart:MY_MAC_ADDRESS */ #include <SPI.h> #include <Ethernet.h> #include <PubSubClient.h> #include <dht11.h> // Update this to either the MAC address found on the sticker on your ethernet shield (newer shields) // or a different random hexadecimal value (change at least the last four bytes) byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; char macstr[] = "deedbafefeed"; // Note this next value is only used if you intend to test against a local MQTT server byte localserver[] = {192, 168, 1, 98 }; // Update this value to an appropriate open IP on your local network byte ip[] = {192, 168, 1, 20 }; char servername[]="quickstart.messaging.internetofthings.ibmcloud.com"; String clientName = String("d:quickstart:arduino:") + macstr; String topicName = String("iot-2/evt/status/fmt/json"); dht11 DHT11; float tempF = 0.0; float tempC = 0.0; float humidity = 0.0; EthernetClient ethClient; // Uncomment this next line and comment out the line after it to test against a local MQTT server //PubSubClient client(localserver, 1883, 0, ethClient); PubSubClient client(servername, 1883, 0, ethClient); void setup() { // Start the ethernet client, open up serial port for debugging, and attach the DHT11 sensor Ethernet.begin(mac, ip); Serial.begin(9600); DHT11.attach(3); } void loop() { char clientStr[34]; clientName.toCharArray(clientStr,34); char topicStr[26]; topicName.toCharArray(topicStr,26); getData(); if (!client.connected()) { Serial.print("Trying to connect to: "); Serial.println(clientStr); client.connect(clientStr); } if (client.connected() ) { String json = buildJson(); char jsonStr[200]; json.toCharArray(jsonStr,200); boolean pubresult = client.publish(topicStr,jsonStr); Serial.print("attempt to send "); Serial.println(jsonStr); Serial.print("to "); Serial.println(topicStr); if (pubresult) Serial.println("successfully sent"); else Serial.println("unsuccessfully sent"); } delay(5000); } String buildJson() { String data = "{"; data+="\n"; data+= "\"d\": {"; data+="\n"; data+="\"myName\": \"Arduino DHT11\","; data+="\n"; data+="\"temperature (F)\": "; data+=(int)tempF; data+= ","; data+="\n"; data+="\"temperature (C)\": "; data+=(int)tempC; data+= ","; data+="\n"; data+="\"humidity\": "; data+=(int)humidity; data+="\n"; data+="}"; data+="\n"; data+="}"; return data; } void getData() { int chk = DHT11.read(); switch (chk) { case 0: Serial.println("Read OK"); humidity = (float)DHT11.humidity; tempF = DHT11.fahrenheit(); tempC = DHT11.temperature; break; case -1: Serial.println("Checksum error"); break; case -2: Serial.println("Time out error"); break; default: Serial.println("Unknown error"); break; } } |
Alternative Test Code to Connect Arduino to IBM Cloud
Alternative code is shared on IBM Developers network :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #include <PubSubClient.h> //#include<WiFi.h> #include <WiFiEspClient.h> #include <WiFiEspServer.h> #include <WiFiEsp.h> #include <WiFiEspUdp.h> #include <ArduinoJson.h> #include “SoftwareSerial.h” #define ORG “***” #define DEVICE_TYPE “****” #define DEVICE_ID “****” #define TOKEN “**************” #define WIFI_AP “*******” #define WIFI_PASSWORD “*******” WiFiEspClient espClient; //PubSubClient client(espClient); SoftwareSerial soft(2, 3); // RX, TX int status = WL_IDLE_STATUS; char server[] = ORG “.messaging.internetofthings.ibmcloud.com”; char authMethod[] = “use-token-auth”; char token[] = TOKEN; char clientId[] = “d:” ORG “:” DEVICE_TYPE “:” DEVICE_ID; const char publishTopic[] = “iot-2/evt/status/fmt/json”; const char responseTopic[] = “iotdm-1/response”; const char manageTopic[] = “iotdevice-1/mgmt/manage”; const char updateTopic[] = “iotdm-1/device/update”; const char rebootTopic[] = “iotdm-1/mgmt/initiate/device/reboot”; void callback(char* publishTopic, char* payload, unsigned int payloadLength); //WiFiClient wifiClient; PubSubClient client(server, 1883, callback, espClient); //PubSubClient client(espClient); int publishInterval = 30000; // 30 seconds long lastPublishMillis; void setup() { // initialize serial for debugging // client.setServer( server, 1883 ); Serial.begin(115200); InitWiFi(); Serial.print(WiFi.localIP()); if (!!!client.connected()) { Serial.print(“Reconnecting client to “); Serial.println(server); while (!!!client.connect(clientId, authMethod, token)) { Serial.print(“.”); //delay(500); } Serial.println(); } } int counter = 0; void loop() { // if (millis() – lastPublishMillis > publishInterval) { String payload = “{\”d\”:”; payload += counter++; payload += “}”; /*const size_t bufferSize = 2*JSON_OBJECT_SIZE(1) + 20; DynamicJsonBuffer jsonBuffer(bufferSize); const char* payload = “a”;*/ Serial.print(“Sending payload: “); Serial.println(payload); // client.publish(publishTopic, payload); //if(client.connected()) //{ // client.publish(publishTopic, (char *)payload.c_str()); if (client.publish(publishTopic, (char *)payload.c_str())) { Serial.println(“Publish ok”); if (!!!client.connected()) { Serial.print(“Reconnecting client to “); Serial.println(server); while (!!!client.connect(clientId, authMethod, token)) { Serial.print(“.”); //delay(500); } Serial.println(); } } else { Serial.println(“Publish failed”); if (!!!client.connected()) { Serial.print(“Reconnecting client to “); Serial.println(server); while (!!!client.connect(clientId, authMethod, token)) { Serial.print(“.”); //delay(500); } Serial.println(); } } } void InitWiFi() { // initialize serial for ESP module soft.begin(115200); // initialize ESP module WiFi.init(&soft); Serial.println(“Connecting to AP …”); // attempt to connect to WiFi network while ( status != WL_CONNECTED) { Serial.print(“Attempting to connect to WPA SSID: “); Serial.println(WIFI_AP); // Connect to WPA/WPA2 network status = WiFi.begin(WIFI_AP, WIFI_PASSWORD); delay(500); } Serial.println(“Connected to AP”); } void callback(char* publishTopic, char* payload, unsigned int length) { Serial.println(“callback invoked”); } |
You’ll see output on serial monitor. On your allocated subdomain from internetofthings.ibmcloud.com
, you can open the window to check device data, logs. You can create graph too.