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);
}
}
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...
#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);
}