http://microcontrollerkits.blogspot.co.id/2015/06/arduino-iot-sensor-ibm-bluemix.html
sketch quickstart:
/*
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 <dht.h>
dht DHT;
#define DHT11_PIN 7
// 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, 8, 1 };
// Update this value to an appropriate open IP on your local network
byte ip[] = {192, 168, 8, 201 };
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);
//DHT.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();
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case 0:
Serial.println("Read OK");
// humidity = (float)DHT11.humidity;
humidity = (float)DHT.humidity;
// tempF = DHT11.fahrenheit();
// tempF = DHT.fahrenheit();
// tempC = DHT11.temperature;
tempC = DHT.temperature;
tempF = tempC * 1.8 + 32.0;
break;
case -1:
Serial.println("Checksum error");
break;
case -2:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
}
hasil:
Kirim data suhu smartphone ke cloud?
cek url ini http://quickstart.internetofthings.ibmcloud.com/iotsensor
skrinsot di hape.
hasil di bluemix :
:: sketch register device:
/*
#include <SPI.h>
#include <PubSubClient.h>
#include <Ethernet.h>
String buildClientName();
// IBM Bluemix Info
char orgName[] = "fjvcb9"; // Your org name, found under the "Credentials" area in your Bluemix dashboard.
//byte mac[] = { 0x00, 0xFF, 0xBB, 0xCC, 0xDE, 0x02 };
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "fjvcb9.messaging.internetofthings.ibmcloud.com"; // MQTT Host (taken from Bluemix dashboard)
char type[] = "sensordev"; // Type of device you have registered in the IBM Bluemix Devices area.
char token[] = "Ov+4_TXm(Z8xixL5By"; // 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
EthernetClient ethernetClient;
int status = 0;
// PubSub Client.
PubSubClient client(server, port, 0 , ethernetClient);
void setup()
{
Serial.begin(9600);
status = Ethernet.begin(mac);
//To do : Check return code.
}
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+="device2";
return data;
}
// Sends the string: {d:{"temperature" : "27.8"}} to Bluemix
String buildJson() {
String data = "{";
data+="\n";
data+= "\"d\": {";
data+="\n";
data+="\"temperature\": 27.8";
data+="\n";
data+="}";
data+="\n";
data+="}";
return data;
}
:: responn serial,
Share This :




0 comments