The use of the ESP8266 in the world of IoT

User avatar
By sp500
#86232 Hi, I'm trying to connect my NodeMCU to IBM Bluemix. However, everytime I upload the code from Arduino IDE. The program stalls at trying to connect to IBM Cloud and just prints out periods. Any help appreciated

Here's the example code I used. I tried with both quick start any by plugging in the Device and Org info, but no luck either way:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

//-------- Customise these values -----------
const char* ssid = "<yourSSID>";
const char* password = "<yourPassword>";

#define ORG "quickstart" // "quickstart" or use your organisation
#define DEVICE_ID "Test5"
#define DEVICE_TYPE "ESP8266" // your device type or not used for "quickstart"
#define TOKEN "xxxxxxxxxxxxx" // your device token or not used for "quickstart"
//-------- Customise the above values --------

char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char topic[] = "iot-2/evt/status/fmt/json";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;

WiFiClientSecure wifiClient;
PubSubClient client(server, 8883, wifiClient);

void setup() {
Serial.begin(115200); Serial.println();

Serial.print("Connecting to "); Serial.print(ssid);
if (strcmp (WiFi.SSID().c_str(), ssid) != 0) {
WiFi.begin(ssid, password);
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected, IP address: "); Serial.println(WiFi.localIP());

Serial.println("View the published data on Watson at: ");
if (ORG == "quickstart") {
Serial.println("https://quickstart.internetofthings.ibmcloud.com/#/device/" DEVICE_ID "/sensor/");
} else {
Serial.println("https://" ORG ".internetofthings.ibmcloud.com/dashboard/#/devices/browse/drilldown/" DEVICE_TYPE "/" DEVICE_ID);
}
}

void loop() {
if (!!!client.connected()) {
Serial.print("Reconnecting client to "); Serial.println(server);
while ( ! (ORG == "quickstart" ? client.connect(clientId) : client.connect(clientId, authMethod, token))) {
Serial.print(".");
delay(500);
}
Serial.println();
}

String payload = "{\"d\":{\"myName\":\"ESP8266.Test5\",\"counter\":";
payload += millis() / 1000;
payload += "}}";

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

if (client.publish(topic, (char*) payload.c_str())) {
Serial.println("Publish ok");
} else {
Serial.println("Publish failed");
}

delay(5000);
}
User avatar
By sp500
#86246 Thanks for the help davydnorris but still not working. I also tried changing DEVICEID to empty string but no luck. Also I don't really want to use quickstart, I just copied the original code.