Unable to read messages from the subscribed topic after arduino board restart

Hi,

I am using Arduino Yun board for development. I have a DHT22 sensor and a mosquitto server running remotely. What I actually want to implement is to publish the temperature read by DHT22 on a topic and read response from another topic. When I upload my code it works perfectly fine without any errors. BUT when I only restart my Yun board it stops receiving messages from the subscribed topic. I don't want to re-upload my code every time I start the Yun board. I am attaching my code if someone can point out where I am going wrong.

// include process library
#include <Process.h>
#include "MQTTclient.h"
#include "DHT.h"

#define MQTT_HOST "xxx.xxx.xxx.xxx"


//LED pins
#define LOWTEMP 9
#define NORMTEMP 6
#define HIGHTEMP 11

//DHT pin
#define DHTPIN 8
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
    
  // start serial
  Serial.begin(9600);

  Bridge.begin();
  // begin the client library (initialize host)
  mqtt.begin(MQTT_HOST, 1883);
  // make some subscriptions
  mqtt.subscribe("light_LED", lightLED);
  
  pinMode(LOWTEMP, OUTPUT);
  pinMode(NORMTEMP, OUTPUT);
  pinMode(HIGHTEMP, OUTPUT);
  
  dht.begin();
  
}

void loop() {
  
  // check for incoming events
  mqtt.monitor();
  
  float temperature = dht.readTemperature();
  Serial.println(temperature);
  mqtt.publish("Temperature", temperature);

  delay(2000);

}

// use callback function to work with your messages
void lightLED(const String& topic, const String& subtopic, const String& message) {
  
  // print the topic and message
  Serial.print("topic: ");
  Serial.println(topic);
  Serial.print("message: "); 
  Serial.println(message); 
  
  if(message == "LOW"){
    digitalWrite(LOWTEMP, HIGH);
    digitalWrite(NORMTEMP, LOW);
    digitalWrite(HIGHTEMP, LOW);
  }
  else if(message == "NORM"){
    digitalWrite(LOWTEMP, LOW);
    digitalWrite(NORMTEMP, HIGH);
    digitalWrite(HIGHTEMP, LOW);
  }
  else{
    digitalWrite(LOWTEMP, LOW);
    digitalWrite(NORMTEMP, LOW);
    digitalWrite(HIGHTEMP, HIGH);
  }
  
  
}

Yun/Yun Shield MQTT Broker

You use Arduino code, not Arduino Yun code.

sonnyyu:
Yun/Yun Shield MQTT Broker

You use Arduino code, not Arduino Yun code.

sorry i didn't quite get what you meant as I am newbie to Arduino and MQTT can you please ellaborate a little more ?

At your code move from setup

void setup() {
...
mqtt.subscribe("light_LED", lightLED);
...
}

to loop.

void loop() {
...
mqtt.subscribe("light_LED", lightLED);
...
}

The Arduino Yun has both Linux and Arduino, your code only utilize Arduino. Move to Linux will give you a lot of benefit. local MQTT broker, MQTT Bridge, fail over, less code at Arduino, use less Arduino memory...

http://forum.arduino.cc/index.php?topic=349996.msg2415000#msg2415000

Get IP address info

Get Datetime preserve the timezone information

MQTT Broker

nano /root/mqtt.py
#!/usr/bin/python
import mosquitto
import sys
import json
import requests
import datetime
import pytz

def getipinfo():
        result = requests.get('http://ipinfo.io/json/')
        json_result = json.loads(result.text)
        tmpstr=''
  	for key, value in json_result.items():
                tmpstr=tmpstr+'"'+value+'",'
        return  tmpstr[:-1]

def getdatetime_with_timezone():
        fmt = '%Y-%m-%d %H:%M:%S %Z'
        d = datetime.datetime.now(pytz.timezone("America/New_York"))
        d_string = d.strftime(fmt)
        return d_string

sendstr='"'+sys.argv[1]+'","' +getdatetime_with_timezone()+'",' +getipinfo()
#print sendstr
mqttc = mosquitto.Mosquitto("python_pub")
mqttc.connect("127.0.0.1", 1883, 60, True)
mqttc.publish("hello/temperature", str(sendstr) )
chmnod 755 /root/mqtt.py

Test publish:

/root/mqtt.py  34

open second terminal windows:

mosquitto_sub -d -t hello/temperature
"34","2015-09-30 17:01:25 EDT","39.0437,-77.4875","Ashburn","US","Virginia","ec2-54-211-19-103.compute-1.amazonaws.com","54.211.19.103","AS14618 Amazon.com, Inc.","20147"

ATmega32u4 code:

#include <Process.h>
void setup() {
  Bridge.begin();   // Initialize Bridge
}
void loop() {
  int temperature = random(0, 100);
  Process p;        // Create a process and call it "p"
  p.begin("/root/mqtt.py");   // Process that launch the  command
  p.addParameter(temperature); // pass  parameter 
  p.run();      // Run the process and wait for its termination
  delay(5000);
}