• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » How to Connect Arduino to IBM Cloud (To Send Sensor Data)

By Abhishek Ghosh December 18, 2018 10:15 pm Updated on December 18, 2018

How to Connect Arduino to IBM Cloud (To Send Sensor Data)

Advertisement

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 :

Vim
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

How to Connect Arduino to IBM Cloud To Send Sensor Data

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 :

Vim
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 :

Advertisement

---

Vim
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:

Vim
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) :

Vim
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 :

Vim
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.

Tagged With projecct on storing the arduino generated data int cloud , MQTT in arduino iot cloud , how we can connect ardunio to a cloud data , how to connect to IBM cloud , how to connect arduino and cloud data using mems sensor , get mqtt from ibm cloud arduino , connect to ibm iot cloud arduino , connect arduino to cloud , connect aduino with cloud , clientName toCharArray(clientStr 34);

This Article Has Been Shared 178 Times!

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How to Connect Arduino to IBM Cloud (To Send Sensor Data)

  • Cloud Computing in the Future

    Cloud Computing in the future is like the electrical power thought a century ago, computing power, storage would be available from companies with guarantee.

  • Price War in the cloud : The Actual War is Rackspace Versus Amazon

    Price War in the cloud is in practical World is Rackspace Versus Amazon as infrastructure provider or rather Open Source versus closed source. Let us look deep.

  • Linux : Basics About Unix-like OS

    Linux referred to the usually free, unix-like Operating systems based on the Linux kernel and is GNU GPL based software. Licensing of the Linux kernel is under GNU GPL.

  • Cloud computing is Not Equal to the Future of Computing

    Cloud computing is Not Equal to the Future of Computing. Cloud Computing is an marketing term which is closer to the technology of grid computing since decades.

  • How to Keep Massive Photo Archive Tidy and Secure

    How to Keep Massive Photo Archive Tidy and Secure ? How many pictures do you have on the hard drive of your computer ? How and When to Take Backups ?

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Proxy Server: Design Pattern in Programming January 30, 2023
  • Cyberpunk Aesthetics: What’s in it Special January 27, 2023
  • How to Do Electrical Layout Plan for Adding Smart Switches January 26, 2023
  • What is a Data Mesh? January 25, 2023
  • What is Vehicular Ad-Hoc Network? January 24, 2023

About This Article

Cite this article as: Abhishek Ghosh, "How to Connect Arduino to IBM Cloud (To Send Sensor Data)," in The Customize Windows, December 18, 2018, January 31, 2023, https://thecustomizewindows.com/2018/12/how-to-connect-arduino-to-ibm-cloud-to-send-sensor-data/.

Source:The Customize Windows, JiMA.in

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT