This is the code that has been used. The IP that is used is on the same WiFi network to which my laptop is connected. Only the last digit is different.
#include <Ethernet.h>
#include<SPI.h>
#include <PubSubClient.h>
byte mac[] = { 0x12, 0xED, 0xBA, 0xFE, 0x2E, 0xED };
String macstr="12edbafe2eed";
byte ip[] = {192,16,1,1};
EthernetClient ethClient;
char servername[]="99elnd.messaging.internetofthings.ibmcloud.com";
PubSubClient client(servername, 1883,ethClient);
void setup()
{
Serial.begin(9600);
Serial.println("Arduino MQTT v.1.2");
Ethernet.begin(mac,ip);
}
void loop()
{
char clientStr[33];
String clientName = String("d:99elnd:arduno_mitul:12edbafe2ee2");
clientName.toCharArray(clientStr,33);
char token[] = "mituliot7450";
while (!client.connected()) {
Serial.println("Reconnecting client … ");
client.connect(clientStr, "use-token-auth", token);
}
String data = "{\"d\": {\"TEST\":";
data+=random(10);
data+="} }";
char jsonStr[33];
data.toCharArray(jsonStr,33);
char topicStr[33];
String topicName = String("iot-2/evt/status/fmt/json");
topicName.toCharArray(topicStr,33);
Serial.print("attempt to send");
Serial.print(jsonStr);
Serial.print("to");
Serial.println(topicStr);
if (client.publish(topicStr,jsonStr))
Serial.println("successfully sent");
else
Serial.println("unsuccessfully sent");
Serial.println("Disconnecting client … ");
client.disconnect();
delay(5000);
}
The serial monitor prints:
Arduino MQTT v.1.2
Reconnecting client …
Reconnecting client …
Reconnecting client …
Reconnecting client …
Reconnecting client …
Its slightly strange you are using 192,16,1,1 as your IP. Typically x.x.x.1 is the default gateway for a given network. If that is the case in your network, then there will be an IP conflict and your device will not have Internet connectivity. This appears to be the case as the credentials from your sketch do work, but there is no record of that device ever even attempting to connect prior to my testing.
You could use DHCP instead which is probably more portable as in the hello world client example here:
http://www.tweaking4all.com/hardware/arduino/arduino-ethernet-data-pull/
or try using the IP address of your laptop (while that is disconnected of course) if indeed your laptop is also using static IP assignment. I've found that, if the gateway and the DHCP server are one and the same device, it will not route traffic for an IP it has not supplied via DHCP, even if it is a valid and unused IP address. You have to limit the range of addresses the DHCP server can dish out in order to free up some to be used in static assignment.