Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
Basic MQTT example with Authentication
How to send MQTT data to IBM Bluemix from Intel Edison via Arduino.
by Rex St. John
*/
#include <SPI.h>
#include <WiFi.h>
#include <PubSubClient.h>
// WiFi Login
char ssid[] = "YOUR_SSID"; // your network SSID (name)
char pass[] = "YOUR_PASSWORD"; // your network password
// IBM Bluemix Info
char orgName[] = "YOUR_ORG"; // Your org name, found under the "Credentials" area in your Bluemix dashboard.
char macstr[] = "YOUR_MAC"; // The MAC address of your Arduino gadget, update this to match your own.
char server[] = "[YOUR_ORG].messaging.internetofthings.ibmcloud.com"; // MQTT Host (taken from Bluemix dashboard)
char type[] = "DEVICE_TYPE"; // Type of device you have registered in the IBM Bluemix Devices area.
char token[] = "DEVICE_AUTH_TOKEN"; // Token issued when you first register your IoT device (only appears once)
int port = 1883;
String clientName = buildClientName();
String topicName = String("iot-2/evt/status/fmt/json"); // Topic
// WiFi connection
WiFiClient wifiClient;
int status = WL_IDLE_STATUS; // the Wifi radio's status
// PubSub Client.
PubSubClient client(server, port, 0 , wifiClient);
void setup()
{
Serial.begin(9600);
status = WiFi.begin(ssid, pass);
if(status == WL_CONNECTED){
Serial.println("WiFi Connected!");
} else {
Serial.println("WiFi Failed!");
}
}
void loop()
{
char clientStr[34];
clientName.toCharArray(clientStr,34);
char topicStr[26];
topicName.toCharArray(topicStr,26);
if (!client.connected()) {
Serial.print("Trying to connect to: ");
Serial.println(clientStr);
client.connect(clientStr, "use-token-auth", token);
}
if(client.connected()) {
Serial.println("Success getting online...Begin transmit...");
// Build the JSON data to publish.
String json = buildJson();
char jsonStr[200];
json.toCharArray(jsonStr,200);
// Publish the data.
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);
}
// Builds the clientName
String buildClientName (){
String data = "";
data+="d:";
data+=orgName;
data+=":";
data+=type;
data+=":";
data+=macstr;
return data;
}
// Sends the string: {d:{"Voltage" : "5"}} to Bluemix
String buildJson() {
String data = "{";
data+="\n";
data+= "\"d\": {";
data+="\n";
data+="\"Volts\": 5"; // Note: wrap like \"5\" for a string, this is a number we are sending.
data+="\n";
data+="}";
data+="\n";
data+="}";
return data;
}
@saoron

This comment has been minimized.

saoron commented Jul 23, 2016

Great job man!
You might want to put the buildClientName() in the header to avoid compilation error.

@JojoMojoe

This comment has been minimized.

JojoMojoe commented Jan 27, 2017

Hello, I have an error with the "buildClientName()". What should I do?
@saoron: What do you mean put it in the header? What is the header?

@DaliAyoub

This comment has been minimized.

DaliAyoub commented Mar 24, 2018

hello , i have the same issue as @JojoMojoe , is there a solution ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment