Connect Arduino Uno Wifi to IBM Bluemix

Hello guys, i have a something problem how to connected arduino wifi uno to ibm bluemix via mqtt?can you help me, please write the arduino code. Thank you

an example of the code i write below:

I am confused here to change the header of the include <esp8266.h> into <<UnoWiFiDevEd.h> for arduino uno wifi

#include <ArduinoJson.h>

#include <PubSubClient.h>

#include <ESP8266WiFi.h>

 

/**

 * IBM IoT Foundation managed Device

 *

 * Author: Ant Elder

 * License: Apache License v2

 */

//#include <ESP8266WiFi.h>

//#include <PubSubClient.h> // https://github.com/knolleary/pubsubclient/releases/tag/v2.3

//#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson/releases/tag/v5.0.7

 

//-------- Customise these values -----------

const char* ssid = "CUSTOMISE";

const char* password = "CUSTOMISE";

 

#define ORG "CUSTOMISE"

#define DEVICE_TYPE "ESP8266"

#define DEVICE_ID "test1"

#define TOKEN "CUSTOMISE"

//-------- Customise the above values --------

 

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";

 

WiFiClient wifiClient;

void callback(char* topic, byte* payload, unsigned int payloadLength);

PubSubClient client(server, 1883, callback, wifiClient);

void handleUpdate(byte* payload);

 

int publishInterval = 30000; // 30 seconds

long lastPublishMillis;

 

 

void wifiConnect() {

 Serial.print("Connecting to "); Serial.print(ssid);

 WiFi.begin(ssid, password);

 while (WiFi.status() != WL_CONNECTED) {

 delay(500);

 Serial.print(".");

 }

 Serial.print("nWiFi connected, IP address: "); Serial.println(WiFi.localIP());

}

 

void mqttConnect() {

 if (!!!client.connected()) {

 Serial.print("Reconnecting MQTT client to "); Serial.println(server);

 while (!!!client.connect(clientId, authMethod, token)) {

 Serial.print(".");

 delay(500);

 }

 Serial.println();

 }

}

 

void initManagedDevice() {

 if (client.subscribe("iotdm-1/response")) {

 Serial.println("subscribe to responses OK");

 } else {

 Serial.println("subscribe to responses FAILED");

 }

 

 if (client.subscribe(rebootTopic)) {

 Serial.println("subscribe to reboot OK");

 } else {

 Serial.println("subscribe to reboot FAILED");

 }

 

 if (client.subscribe("iotdm-1/device/update")) {

 Serial.println("subscribe to update OK");

 } else {

 Serial.println("subscribe to update FAILED");

 }

 

 StaticJsonBuffer<300> jsonBuffer;

 JsonObject& root = jsonBuffer.createObject();

 JsonObject& d = root.createNestedObject("d");

 JsonObject& metadata = d.createNestedObject("metadata");

 metadata["publishInterval"] = publishInterval;

 JsonObject& supports = d.createNestedObject("supports");

 supports["deviceActions"] = true;

 

 char buff[300];

 root.printTo(buff, sizeof(buff));

 Serial.println("publishing device metadata:"); Serial.println(buff);

 if (client.publish(manageTopic, buff)) {

 Serial.println("device Publish ok");

 } else {

 Serial.print("device Publish failed:");

 }

}

 

void publishData() {

 String payload = "{\"d\":{\"counter\":";

 payload += millis() / 1000;

 payload += "}}";

 

 Serial.print("Sending payload: "); Serial.println(payload);

 

 if (client.publish(publishTopic, (char*) payload.c_str())) {

 Serial.println("Publish OK");

 } else {

 Serial.println("Publish FAILED");

 }

}

 

void callback(char* topic, byte* payload, unsigned int payloadLength) {

 Serial.print("callback invoked for topic: "); Serial.println(topic);

 

 if (strcmp (responseTopic, topic) == 0) {

 return; // just print of response for now

 }

 

 if (strcmp (rebootTopic, topic) == 0) {

 Serial.println("Rebooting...");

 ESP.restart();

 }

 

 if (strcmp (updateTopic, topic) == 0) {

 handleUpdate(payload);

 }

}

 

void handleUpdate(byte* payload) {

 StaticJsonBuffer<300> jsonBuffer;

 JsonObject& root = jsonBuffer.parseObject((char*)payload);

 if (!root.success()) {

 Serial.println("handleUpdate: payload parse FAILED");

 return;

 }

 Serial.println("handleUpdate payload:"); root.prettyPrintTo(Serial); Serial.println();

 

 JsonObject& d = root["d"];

 JsonArray& fields = d["fields"];

 for(JsonArray::iterator it=fields.begin(); it!=fields.end(); ++it) {

 JsonObject& field = *it;

 const char* fieldName = field["field"];

 if (strcmp (fieldName, "metadata") == 0) {

 JsonObject& fieldValue = field["value"];

 if (fieldValue.containsKey("publishInterval")) {

 publishInterval = fieldValue["publishInterval"];

 Serial.print("publishInterval:"); Serial.println(publishInterval);

 }

 }

 }

}

 

void setup() {

 Serial.begin(115200); Serial.println();

 

 wifiConnect();

 mqttConnect();

 initManagedDevice();

}

 

void loop() {

 if (millis() - lastPublishMillis > publishInterval) {

 publishData();

 lastPublishMillis = millis();

 }

 

 if (!client.loop()) {

 mqttConnect();

 }

}
[/quote]

What have you tried? Share your code (IBM has some basic info here for example, possibly somewhat obsolete as the platform is changing but concepts should still apply)

ok I attached an example coding from arduino uno + esp8266, how does it apply if the arduino uno wifi?

please help me.

This script works for me – I have an Uno with an ATWINC1500 WiFi board, a TMP36 temperature sensor, and an LED. The sketch as it stands writes to the IBM “quickstart” service which requires no registration, so I can see the data at IBM Watson IoT Platform . The commented-out lines work with the signup-required trial account that lets you do a lot more with the data.

#include <SPI.h>
#include <WiFi101.h>
#include <PubSubClient.h>

#define TMP36_PIN          0 // Analog pin
#define RED_LED_PIN        2 // Digital pin

#define MY_SSID            "XXXXXXXXXX"
#define MY_WEP_PASSWORD    "XXXXXXXXXX"
#define MY_WEP_KEY_INDEX   0

/*
#define BROKER             "XXXXX.messaging.internetofthings.ibmcloud.com"
#define ID                 "use-token-auth"
#define MY_AUTH_TOKEN      "XXXXXXXX"
#define TOPIC              "iot-2/evt/greeting/fmt/txt"
#define MY_CLIENT_ID       "d:XXXXXX:ArduinoUnoWiFi:PDH-AU1"
*/

#define BROKER             "quickstart.messaging.internetofthings.ibmcloud.com"
#define ID                 ""
#define MY_AUTH_TOKEN      ""
#define TOPIC              "iot-2/evt/status/fmt/json"
#define MY_CLIENT_ID       "d:quickstart:ArduinoUnoWiFi:PDH-AU1"

char message[128];
#define DELAY_BT_READINGS  60 // Delay time in seconds between messages

WiFiClient client; int port = 1883;
PubSubClient clientPS(client);

int connects = 0;
long startTime, lastConnectTime;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // Wait for serial port to connect. Needed for native USB port only
  }
  
  WiFi.setPins(8,7,4);
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("No WiFi shield");
    while (true);
  }

  pinMode (RED_LED_PIN, OUTPUT);
  
  clientPS.setServer(BROKER, port);

  startTime = millis();
}

void loop() {
  digitalWrite(RED_LED_PIN, HIGH);
  
  while (!clientPS.connected()) {
    connect_to_MQTT();
  }

  int t = getTempInF();
  int rssi = WiFi.RSSI();
  long uptimeMinutes = (millis() - startTime) / (1000L * 60L);
  long connectUptime = (millis() - lastConnectTime) / (1000L * 60L);

  buildMessage (t, rssi, connects, uptimeMinutes, connectUptime);
  
  Serial.print ("Publish: "); Serial.println (message);
  Serial.print ("  Len: "); Serial.println (strlen(message));
  clientPS.publish(TOPIC, message);
  Serial.println("  Success");

  delay(500);
  digitalWrite(RED_LED_PIN, LOW);

  Serial.println ("Delay");
  for (int i = 0; i < DELAY_BT_READINGS; ++i) {
    delay (1000);
    if (i%5 == 0) {
      boolean st = clientPS.loop();
      if (st != 1) { // Something is wrong with the connection
        Serial.print("  Status: "); Serial.println(st);
        break;
      }
    }
  }
}

void buildMessage(int temp, int rssi, int connects, long uptime, long connectUptime) {
  char tempBuf[5];
  
  strcpy (message, "{\"d\":{ \"temperature\":");
   itoa(temp, tempBuf, 10);
   strcat (message, tempBuf);
  
  strcat (message, ", \"RSSI\":");
   itoa(rssi, tempBuf, 10);
   strcat (message, tempBuf);
  
  strcat (message, ", \"connects\":");
   itoa (connects, tempBuf, 10);
   strcat (message, tempBuf);
  
  strcat (message, ", \"uptimeMinutes\":");
   ltoa (uptime, tempBuf, 10);
   strcat (message, tempBuf);
   
  strcat (message, ", \"connectUptime\":");
   ltoa(connectUptime, tempBuf, 10);
   strcat (message, tempBuf);

  strcat (message, "}}");
}

void connect_to_MQTT() {
  ++connects;

  connect_to_WiFi();
  
  if (!clientPS.connected()) {
    Serial.println("Connect to broker");
    boolean connected = (strlen(ID) > 0)? clientPS.connect(MY_CLIENT_ID, ID, MY_AUTH_TOKEN) :
                                          clientPS.connect(MY_CLIENT_ID); 
    if (connected) {
      Serial.println("  Success");
    } else {
      Serial.print("  Failed, rc="); Serial.print(clientPS.state());
        Serial.println (" ; delay 5 seconds");
      delay(5000);
    }
  }

  lastConnectTime = millis();
}

void connect_to_WiFi () {
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connect to WiFi");
    WiFi.begin(MY_SSID, MY_WEP_KEY_INDEX, MY_WEP_PASSWORD);
    Serial.println("  Delay");
    delay(5000);
  }
}

int getTempInF () {
  float aRef = 5.0;
  float voltage = analogRead(TMP36_PIN) * aRef / 1024.;
  float degreesC = (voltage - 0.5) * 100.0;
  float degreesF = degreesC * (9.0/5.0) + 32.0;
  int t = (int)(degreesF + .5);
  return (t);
}