1

enter image description hereI have followed the steps outlined in the following link to install and run the Mosquitto broker on my laptop. https://sivatechworld.wordpress.com/2015/06/11/step-by-step-installing-and-configuring-mosquitto-with-windows-7/ I have tested the publishing and subscription events using command line i.e, the mosquitto_pub and mosquitto_sub commands and they worked fine!
I want to publish to this broker running on my laptop (listening to port 1883 - confirmed using the netstat-an command) using an Arduino which is using the functions from "PubSubClient" library.
For the server's IP address in the Arduino Publish sketch, I have given the IP address of my laptop itself which it gets when it connects to my home network. Following is the publisher code:

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>


#define CLIENTID "Arsen"
const char topic[] = "tem";
#define POLLINTERVAL 120000

void callback(char* topic, byte* payload, unsigned int length){
//Do nothing as we are publishing ONLY.
}

byte mac [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED} ;
IPAddress server(192, 168, 0, 10);
EthernetClient ethClient;
PubSubClient arduinoClient(server,1883, callback, ethClient) ;
unsigned long boardTime = 0 ;
float sensedTemperature = 0;
char charTemp [6];
int connRC = 0;


void setup(void) {
Serial.begin(9600);
//Connect to the MQTT server - test.mosquitto.org
beginConnection() ;
}

//Initialise MQTT connection
void beginConnection() {
Serial.begin(9600);
Ethernet.begin(mac) ;   //using the address assigned through DHCP
do{ 
int connRC = arduinoClient.connect(CLIENTID);
}while(connRC==1);
Serial.println("We are connected finally");
delay(5000);
Serial.println(arduinoClient.state());
}



void loop(void) {
boardTime = millis();
if ((boardTime % POLLINTERVAL) == 0) {
  getTemp();
  dtostrf(sensedTemperature,5,2,charTemp);
  boolean rec = arduinoClient.publish(topic, charTemp);
  Serial.println(rec);
  Serial.println("Successfully published");
}
arduinoClient.loop();
}

void getTemp() {
// Send the command to get temperatures
delay(100);
sensedTemperature = analogRead(2);   //temperature sensor at analog pin 2 on Arduino.
Serial.println(sensedTemperature);
delay(150);
}

The subscriber code is:

    #include <Ethernet.h>
#include <EthernetUdp.h>
#include <EthernetServer.h>
#include <EthernetClient.h>
#include <Dns.h>
#include <Dhcp.h>
#include <SPI.h>
#include <PubSubClient.h>

#define PIN 13

//MQTT Definition

#define CLIENTID "Aract"
#define TOPICNAME "tem"
byte mac [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED} ;
IPAddress server(192,168,0,5);     // Mosquitto Server
int connRC = 0;

//Handle message from mosquitto and set LEDs
void callback(char* topic, byte* payload, unsigned int length) {
int i=0; char buffer [length];
for(i=0;i<length;i++) {
  buffer [i] = char((payload[i]));
}
String payLoadData = buffer;
Serial.println(payLoadData);
setled(payLoadData);
}

//Our MQTT client
EthernetClient ethclient;
PubSubClient arduinoClient(server, 1883, callback, ethclient) ;
void setup() {
//Connect to the MQTT server - test.mosquitto.org
beginConnection() ;
}

//Initialise MQTT connection
void beginConnection() {
Serial.begin(9600);
Ethernet.begin(mac) ;
do{
connRC = arduinoClient.connect(CLIENTID) ;
delay(2000);
}while(connRC==1);
delay(3000);
Serial.println(arduinoClient.state());
if (connRC==1) {
boolean rec = arduinoClient.subscribe(TOPICNAME) ;
Serial.println(rec);
Serial.println("Successfully Subscribed");
} else {
Serial.println("connRC") ;
}
}

void loop()
{
  arduinoClient.loop() ;

}

void setled(String s)
{
  int temperature;
  temperature = s.toInt();
  Serial.println(temperature);
  if (temperature >= 200){
    digitalWrite(PIN, HIGH);
  }
  else{
    digitalWrite(PIN, LOW);
  }
}

The Ethernet shield is working well as it successfully executed the example programs. The state function returns "zero" which implies that the board is connecting to the network. However, the publish command returns a boolean value "rec" which is giving false. I want to know as to why this process is being unsuccessful all the time?
The verbose option (-v) along with Mosquitto on command line throws up an unknown error instead of showing the logged details. How to overcome this problem or what exactly is wrong in my technique?

enter image description here

  • Add the mosquitto console output to the question so we can see exactly what the error is as this will likely point to the cause of the problem – hardillb Apr 04 '16 at 15:01
  • As pointed out, the pub and sub commands are working fine. The verbose command is however not listing the jist of actions which run in the background. Also, I don't think that it is able to accept IPv4 addresses which it generally should! – Chetan Chavan Apr 06 '16 at 17:31
  • This has nothing to do with the Ardunio code, it's just mosquitto failing to start. How did you install it and did you install the pre-reqs mentioned in the readme-windows.txt? – hardillb Apr 06 '16 at 18:31
  • @ hardlib. Mosquitto failing to start? I have installed it following the steps mentioned here : https://sivatechworld.wordpress.com/2015/06/11/step-by-step-installing-and-configuring-mosquitto-with-windows-7/ – Chetan Chavan Apr 07 '16 at 06:14
  • @hardlib. If it is failing to start, why are the pub and sub commands working? Also, yesterday night, I was able to publish data onto the mosquitto broker running on my laptop and subscribed to the topic (under which the arduino was publishing) via command line (mosquitto_sub). I was able to receive the data successfully! Attached is the screen shot (second one in the question). The present problem is with subscriber running on arduino which is not able to receive the data from broker although the command line subscriber is able to. I have included the subscriber code too. – Chetan Chavan Apr 07 '16 at 06:18
  • I published through arduino and subscribed through a command line client and it worked fine! Please see the first photo. – Chetan Chavan Apr 07 '16 at 06:24

1 Answers1

0

I had faced the same problem and I got the solution as this... Just go to the services running in your PC and stop the mosquitto-broker, now open the cmd and change the directory to the place where mosuitto files were located (in my case it is "cd C:\Program Files (x86)\mosquitto") and then enter this command mosquitto -v, now just left that window like that (If u closed then it won't work)... It really worked and the reason why its not working in before case is, when we started moquitto-broker service then its not opening ports for IPv4 and here is the reference "Mosquitto Error"

Community
  • 1
  • 1
K.V.Lucky
  • 51
  • 6