Skip to content

Instantly share code, notes, and snippets.

@rexstjohn
Created August 11, 2015 02:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rexstjohn/e497aaff88ba183cce05 to your computer and use it in GitHub Desktop.
Save rexstjohn/e497aaff88ba183cce05 to your computer and use it in GitHub Desktop.
/*
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
Copy link

saoron commented Jul 23, 2016

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

@JojoMojoe
Copy link

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
Copy link

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