Hi,
I have installed Mosquitto Broker on my laptop and now I am trying to connect MKR1000 with this Broker to publish and subscribe.
MKR1000, as client, cannot connect to the Broker and mqttClient.state() returns -2.
Any hint?
Below is the code...
#include <SPI.h>
#include <WiFi101.h>
#include <PubSubClient.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
char topic[]= "news";
const char* deviceID = "myDevice";
int status = WL_IDLE_STATUS;
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient); // MQTT client
// MQTT Broker IP address, example:
//const char* mqtt_server = "192.168.1.144";
const IPAddress mqttBrokerIP(127, 168, 1, 121);
void mqttCallback(char* topic, byte* message, unsigned int length){
//is executed whenever a message is arrived
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message*);*
_ messageTemp += (char)message*;_
_ }_
_ Serial.println();*_
}
// default setup function
void setup() {
* //Initialize serial and wait for port to open:*
* Serial.begin(9600);*
* //connect to the*
// WiFi.mode(WIFI_STA);
* connectToInternet();*
* //connect to MQTT Broker*
* mqttSetup();*
}
// default loop function
void loop() {
* // Connect if we're not already connected.*
* if (!mqttClient.connected())*
* reconnect();*
* // process any events.*
* mqttClient.loop();*
}
// connect to the internet
void connectToInternet(){
* while (!Serial) {*
* ; // wait for serial port to connect. Needed for native USB port only*
* }*
* // check for the presence of the shield:*
* if (WiFi.status() == WL_NO_SHIELD) {
_ Serial.println("WiFi shield not present");_
_ // don't continue:_
_ while (true);_
_ }_
_ // attempt to connect to WiFi network:_
while (status != WL_CONNECTED) {
_ Serial.print("Attempting to connect to SSID: ");_
_ Serial.println(ssid);_
_ // Connect to WPA/WPA2 network. Change this line if using open or WEP network:_
_ status = WiFi.begin(ssid, pass);_
_ // wait 10 seconds for connection:_
_ delay(10000);_
_ }_
_ Serial.println("Connected to wifi");_
_ printWiFiStatus();_
_}_
_// print wifi status and information*_
void printWiFiStatus() {
* // print the SSID of the network you're attached to:*
* Serial.print("SSID: ");*
* Serial.println(WiFi.SSID());*
* // print your WiFi shield's IP address:*
* IPAddress ip = WiFi.localIP();*
* Serial.print("IP Address: ");*
* Serial.println(ip);*
* // print the received signal strength:*
* long rssi = WiFi.RSSI();*
* Serial.print("signal strength (RSSI):");*
* Serial.print(rssi);*
* Serial.println(" dBm");*
* Serial.println("------------------------------------");*
}
// MQTT setup
void mqttSetup(){
* //settings for mqttClient*
* mqttClient.setServer(mqttBrokerIP, 1883);*
* mqttClient.setCallback(mqttCallback);*
}
// Reconnecting MQTT
void reconnect(){
* //ensure there is a connection to the MQTT broker*
* while(!mqttClient.connected()){*
* //debug info*
* Serial.print("Attempting to connect to MQTT broker at ");*
* Serial.println(mqttBrokerIP);*
* //Attempt to connect*
* if (mqttClient.connect(deviceID)){*
* //debug info*
* Serial.println("Connected to MQTT broker");*
* //once connected, publish an announcement to the*
* mqttClient.publish(topic, "Hi");*
* //subscribe to receive messages*
* mqttClient.subscribe(topic);*
* }*
* else {*
* //dubug info, why connection was not made*
* Serial.print("Connection to MQTT broker failed, rc: ");*
* Serial.println(mqttClient.state());*
* Serial.println("trying again in 5 seconds...");*
* //wait for 5 seconds and try again*
* delay(5000);*
* } *
* }*
}