Hi everyone,
I am trying to make a arduino uno with a wifi shield to connect to my mqtt server, but I am having no success. I have another one with ethernet shield that works great, can someone help me out?
this is the code:
#include <WiFi.h>
#include <PubSubClient.h>
char ssid[] = "V@Z Wifi"; // your network SSID (name)
char pass[] = "mypass"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
const char* MQTTserver = "192.168.7.3";
const char* myMQQTClientID = "ArduinoClient02";
WiFiClient wfClient;
PubSubClient mqttClient(wfClient);
void setup() {
// initialize serial:
Serial.begin(9600);
// Set MQTT Client
Serial.println(F("MQQT arduino client by Luis Vaz"));
Serial.print(F("Hello! My MQTT ClientID is "));
Serial.println(myMQQTClientID);
// attempt to connect using WPA2 encryption:
Serial.print(F("Attempting to connect to Wifi network with SSID "));
Serial.print(ssid);
Serial.println(F("..."));
status = WiFi.begin(ssid, pass);
// if you're not connected, stop here:
if ( status != WL_CONNECTED) {
Serial.println(F("Couldn't get a wifi connection. Is your wifi in range?"));
while(true);
}
// if you are connected, print out info about the connection:
else
{
Serial.println(F("Successfully connected to Wifi network!"));
printIPAddress();
}
mqttClient.setServer(MQTTserver, 1883);
mqttClient.setCallback(callback);
delay(1500);
}
void loop()
{
if (!mqttClient.connected())
{
reconnect();
}
mqttClient.loop();
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print(F("MQTT message arrived ["));
Serial.print(topic);
Serial.print(F("] "));
for (int i=0;i<length;i++)
{
char inChar = (char)payload[i];
//inMessage = inMessage + inChar;
Serial.print(inChar);
}
Serial.println(F(""));
}
void reconnect()
{
// Loop until we're reconnected
while (!mqttClient.connected()) {
Serial.print(F("Attempting MQTT connection on IP "));
Serial.print(MQTTserver);
Serial.println(F("..."));
// Attempt to connect
if (mqttClient.connect(myMQQTClientID)) {
Serial.println(F("Successfully connected to MQTT server!"));
// Once connected, publish an announcement...
mqttClient.publish("ArduinoClient01/Messages","Hi there! I'm up and running!");
// ... and resubscribe
mqttClient.subscribe("ArduinoClient02/Messages");
}
else
{
Serial.print(F("Failed to connect to MQTT server, rc="));
Serial.println(mqttClient.state());
Serial.println(F("Preparing to try again in 5 seconds..."));
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.print(bssid[5], HEX);
Serial.print(":");
Serial.print(bssid[4], HEX);
Serial.print(":");
Serial.print(bssid[3], HEX);
Serial.print(":");
Serial.print(bssid[2], HEX);
Serial.print(":");
Serial.print(bssid[1], HEX);
Serial.print(":");
Serial.println(bssid[0], HEX);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void printIPAddress()
{
Serial.print(F("My IP address: "));
for (byte thisByte = 0; thisByte < 4; thisByte++)
{
//print the value of each byte of the IP address:
Serial.print(WiFi.localIP()[thisByte], DEC);
if(thisByte <3)
Serial.print(F("."));
}
Serial.println(F(""));
}
on the output serial, this is what i get:
MQQT arduino client by Luis Vaz
Hello! My MQTT ClientID is ArduinoClient02
Attempting to connect to Wifi network with SSID V@Z Wifi...
Successfully connected to Wifi network!
My IP address: 192.168.7.178
Attempting MQTT connection on IP 192.168.7.3...
Failed to connect to MQTT server, rc=-2
Preparing to try again in 5 seconds...
Attempting MQTT connection on IP 192.168.7.3...
Failed to connect to MQTT server, rc=-2
Preparing to try again in 5 seconds...
Attempting MQTT connection on IP 192.168.7.3...
Failed to connect to MQTT server, rc=-2
Preparing to try again in 5 seconds...
Attempting MQTT connection on IP 192.168.7.3...
Failed to connect to MQTT server, rc=-2
Preparing to try again in 5 seconds...
Attempting MQTT connection on IP 192.168.7.3...
No Socket available
On the arduino shield it works perfectly, except that using wifi client i use ethernet client. Am I missing something ?