1
#include "SPI.h"
#include “WiFiEsp.h”
#include  <WiFiEspClient.h>
#include “SoftwareSerial.h”
#include <PubSubClient.h>
#include <WiFiEspUdp.h>

float temp=0;
int tempPin = 0;
int isClientConnected = 0;

char data[80];
char ssid[] = “SSID”; // your network SSID (name)
char pass[] = “PASSWORD”; // your network password

int status = WL_IDLE_STATUS; // the Wifi radio’s status
char deviceName = “ArduinoClient1”;

IPAddress server(xxx,xxx,xxx,xxx); //MQTT server IP
IPAddress ip(192,168,43,200);

void callback(char* topic, byte* payload, unsigned int length) {
    Serial.print(“Message arrived [“);
    Serial.print(topic);
    Serial.print(“] “);
    for (int i=0;i<length;i++) {
        Serial.print((char)payload[i]);
    }
    Serial.println("-");
}

// Emulate Serial1 on pins 6/7 if not present
WiFiEspClient espClient;
PubSubClient client(espClient);

SoftwareSerial Serial1(6,7); // RX, TX

void setup(){
    Serial.begin(9600);
    Serial1.begin(9600);
    WiFi.init(&Serial1);
    WiFi.config(ip);
    if (WiFi.status() == WL_NO_SHIELD) {
       Serial.println("WiFi shield not present");
       while (true);
    }

    while ( status != WL_CONNECTED) {
       Serial.print("Attemptingonnect to WPA SSID: ");
       Serial.println(ssid);
       status = WiFi.begin(ssid, pass);
       Serial.print("WiFius : ");
       Serial.println(status);
    }

    //connect to MQTT server
    client.setServer(server, 1883);
    client.setCallback(callback);
    isClientConnected = client.connect(deviceName);
    Serial.println("+++++++");
    Serial.print("isClientConnected;
    Serial.println(isClientConnected);
    Serial.print("client.state");
    Serial.println(client.state());

    if (isClientConnected) {
        Serial.println("Connected…..");
        client.publish("status","Welcome to ISG");
        client.subscribe("isg/demoPublish/rpi/ardTempWarn"); 
        //Not able to recieve for this subscribed topic on Arduino Uno Only if I   
        //print it returns 1

    }
}

void loop() {
    temp = (5.0 * analogRead(tempPin) * 100.0) / 1024;
    Serial.print(" temp : " );
    Serial.println(temp);
    Serial.print("client.connected);
    Serial.println(client.connected());
    if (!client.connected()) {
            reconnect();
    }
    client.publish("isg/demoPublish/ard1/tempLM35",String(temp).c_str()); 
    // able to receive data at other         
    // clients like RPI,Web using Mosquitto broker

    client.loop();
    delay(5000);
}

void reconnect() {
    Serial.println("Device is trying to connect to server ");
    while (!client.connected()) {
        if (client.connect(deviceName)) {
        } else {
            delay(5000);
        }
    }
}

I am using Arduino Uno R3 and ESP8266-01 as wifi connector. I have to read temperature data and send to Mosquitto ,MongoDB and Raspberry Pi and receive a data on specific condition for that i have subscribed a topic in Arduino. I am able to receive data from Arduino to all other clients but I am not able to receive data on Subscribed topic in Arduino. But all other deviced like MongoDB able to receive data from Raspberry Pi. I have used Arduino Uno R3, ESP8266-01 devices and liberary for to connect and send/receive data WiFiEsp.h, WiFiEspClient.h, WiFiEspUdp.h, SoftwareSerial.h, PubSubClient.h client.subscribe("topic"); returns 1 Also callback function implemented but not able to get call.

So can any one help me why I am not getting subscribed topic message in Arduino?

I have follow https://sonyarouje.com/2016/03/15/mqtt-communication-with-arduino-using-esp8266-esp-01/#comment-111773

user3256309
  • 29
  • 10
  • what exactly do you mean by this? " I am able to receive data from Arduino to all other clients but I am not able to receive data on Subscribed topic in Arduino." Also please post your code along with your question so you can get help easier. – Rohin Gopalakrishnan Nov 15 '16 at 15:42
  • all the mqtt clients i've seen for the esp8266 need code on the esp, not just AT commands – dandavis Nov 15 '16 at 19:45
  • Hi, I have attached temperature sensor with Arduino and it reads temperature with 5 seconds delay and publish to Mosquitto broker and Mosquitto broker send that temperature data to MySQL,Raspberry Pi and web client which are subscribed for temperature data. If temperature increase above 60 than i have to keep alert on Raspberry Pi. this all working proper. – user3256309 Nov 15 '16 at 19:56
  • From Raspberry Pi i have to controll the Arduino so I have to publish message from RPI to Arduino using Mosquitto broker. So I have subscribed a topic for example "receivedRPIData". Now i am able to recieve data for "receivedRPIData" on MySQL client and web. But not able to receive message in Arduino. i have subscribed for "receivedRPIData" topic and it retuns 1 if i print on serial monito client.subscribe("receivedRPIData") – user3256309 Nov 15 '16 at 19:56
  • So what what is the problem i am not getting. As i have used code from this link https://sonyarouje.com/2016/03/15/mqtt-communication-with-arduino-using-esp8266-esp-01/#comment-111773 – user3256309 Nov 15 '16 at 19:56
  • Still need the code pieces. – cagdas Nov 16 '16 at 05:07
  • I have added code in my question please can you help me to solve probelm? – user3256309 Nov 18 '16 at 08:54
  • I have exactly the same issue. Which PubSubClient do you use? knolearry or lmroy fork? – igraczech Jul 03 '17 at 19:34

1 Answers1

0

The library that you are using has bug in callback hence it would be better that you use other library my preference would be https://github.com/vshymanskyy/TinyGSM this library include almost all variants of SIM800(A,C,L,H,808), variants of SIM900(A,D,908,968), ESP8266 (mounted on arduino), Ethernet shield etc. It worked for me, as same issue bugged me for almost 1-2 weeks but latter was able to receive all subscribed message irrespective of mode of communication(GSM,Ethernet,WiFi)

ankur
  • 2,039
  • 2
  • 10
  • 12
  • He's referring to PubSubClient library and TinyGSM is depending on knolearry's PubSubClient. There's also other version by lmroy available on GitHub. – igraczech Jul 03 '17 at 19:31