I'm hoping this is something simple that I'm just not understanding. I'm trying to wrap my mind around both ethernet and mqtt on arduino. I've used them separately, but I can get a lot of raspberry pi's out of the way if I can publish mqtt messages directly from the arduino.
I have a nano with the nano arduino shield. I can publish messages to my local network (192.168.0.222) with no password on port 1883.
I can publish messages to my private server which is on the public internet from a raspberry pi (so I definitely know the password, ip addresses etc).
However, I can't figure out how to publish messages from the arduino when I need a username and password. Here's my local sketch...
#include <UIPEthernet.h>
#include "PubSubClient.h"
#define CLIENT_ID "BoilerRoom"
#define INTERVAL 15000 // 15 sec delay between publishing
uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
EthernetClient ethClient;
PubSubClient mqttClient;
long previousMillis;
void setup() {
// setup serial communication
Serial.begin(9600);
// setup ethernet communication using DHCP
if(Ethernet.begin(mac) == 0) {
Serial.println(F("Ethernet configuration using DHCP failed"));
for(;;);
}
// setup mqtt client
mqttClient.setClient(ethClient);
mqttClient.setServer("192.168.0.222",1883);
Serial.println(F("MQTT client configured"));
previousMillis = millis();
}
void loop() {
// check interval
if(millis() - previousMillis > INTERVAL) {
sendData();
previousMillis = millis();
}
mqttClient.loop();
}
void sendData() {
char msgBuffer[20];
if(mqttClient.connect(CLIENT_ID)) {
mqttClient.publish("boilerTemp", "5000");
Serial.println(F("Attempted to send temp"));
}else{
Serial.println(F("Failed"));
}
}
Looking at the documentation, I was assuming I could change port 1883 to 8883 and change this line...
if(mqttClient.connect(CLIENT_ID)) {
to
if(mqttClient.connect(CLIENT_ID, "myUsername", "myPassword")) {
but that doesn't seem to be working. Can anyone help?