1

I am new to Arduino and IoT. I got my hands on a Yun and loaded a simple sketch to subscribe to a topic. I am receiving messages (I have set up a PHP script and broker on a server, which takes care of the messages) and can turn ON and OFF LED, L13. But when I disconnect the Yun from my PC and connect it again (to power it up), it stops receiving messages. I have to load the sketch again to make it work. Here is my code:

#include <spi.h>
#include <pubsubclient.h>
#include <yunclient.h>

byte server[] = { 192, 168, 1, 196 };
int port = 1883;
char* pubTopic = "fromYun";
char* subTopic = "sensor/light";

void callback(char* topic, byte* payload, unsigned int length) {
  //convert byte to char
  payload[length] = '\0';
  String strPayload = String((char*)payload);
  // handle message arrived
  if(strPayload == "ON") {
    digitalWrite(13, HIGH);
  } else if(strPayload == "OFF") {
    digitalWrite(13, LOW);
  }
}

YunClient yun;
PubSubClient mqtt(server, port, callback, yun);

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Bridge.begin();
  if (mqtt.connect("arduinoClient")) {
    mqtt.publish(pubTopic,"hello world!");
    mqtt.subscribe(subTopic);
  }
}

void loop() {
  mqtt.loop();
}

Am I missing something?

I referred to this post and PubSubClient -> mqtt_basic.ino for making this ketch.

I don't understand what mqtt.connect("arduinoClient") is. I mean, what is this string "arduinoClient"? In one sketch on GitHub I saw it as "yun"...

Any help will be appreciated. Thanks in advance!

EDIT: I am using Mosquitto MQTT.

  • The "arduinoClient" part is just an arbitrary name. It's useful when you look at the log in your server. It serve to separate requests from different clients. – user31481 Nov 1 '17 at 8:22
0

Please explain which messages? Serial port messages or MQTT messages?

The mqtt.connect name "arduinoClient" is the ID name. It depends on the server what the parameters should be. For some servers it can be an empty string and the second parameter should be the account name, and a third parameter for the password/key.

When you upload a sketch, the OpenWrt part is already running. Perhaps there is a problem with Bridge.begin. Try to wait 90 seconds before calling Bridge.begin. If a 90-second delay makes it work, lower it to 5 seconds, and keep it at 5 seconds.

Perhaps you can use (blinking) LEDs to indicate that the code has finished the Bridge.begin or to indicate an error.

When you solve this problem, there will be other problems. Have you updated to the newest firmware for the OpenWrt part? The Arduino Yún might only work with a very simple and small sketch. Don't try to add too many things that use the Bridge. The OpenWrt/linux part is reliable, you can run linux scripts there. The ATmega32U4 part is just like an Arduino Leonardo and is also reliable. It is the combination and the Bridge that causes a never ending chain of problems.

What about the Raspberry Pi? That is reliable. Have you thought about the ESP8266 or ESP32 in Arduino compatible mode? Adafruit has an MQTT server for users, and the Adafruit Feather M0 WiFi seems a very nice board. Anything but the Yún.

My best advice for you is to grab a hammer and destroy the Yún and forget about it. It will make your life so much easier.

(I just noticed that this is a very old question, sorry, but my answer is still valid).

  • It's dubious to call raspberry pi's reliable when they depend on a fragile SD card for their root filesystem. Waiting for the Linux side of things to fully start up does sound like a good idea, unless part of the Linux startup is commanding a reset of the ATmega. Ultimately, to get reliable communication through a bridge likely requires doing a full handshake between the software on both sides. A better system design would probably run most of the logic on the Linux side (as you observed, "you can run scripts there") and use the ATmega only as a delegate for I/O or simple realtime sequencing. – Chris Stratton Jul 3 '17 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.