confirm yun is connected to pc mqtt server

Hello
I have a Yun with wifi to my router and the pc is also connected to the router. I can upload sketches to the Yun and see data being generated via the Serial Monitor which all indicates that the PC and Yun are communicating. I have the mosquito server installed and running on the pc.
I can't see any data being generated from the code below so I am thinking I would need to ensure the PubSubClient is connecting correctly. Is there a log generated somewhere or is there some configuration that needs to be done to pubsubclient. Or is there a process to step through the code to view errors.

#include <YunClient.h>
#include <PubSubClient.h>
#include <Servo.h>

#define ADC_ref 5.0
#define analog_resolution 1024.0

#define zero_x 1.69434
#define zero_y 1.96777
#define zero_z 1.52344

#define sensitivity_x 0.05
#define sensitivity_y 0.33
#define sensitivity_z 0.31

//my pc ip address on the router
#define MQTT_SERVER "192.168.0.5"
//Yuns IP address on router or should it be "YUN-Sensor"
#define MQTT_CLIENTID "192.168.0.8"

int pos = 0;
unsigned int value_x;
unsigned int value_y;
unsigned int value_z;
unsigned long time;

float xv;
float yv;
float zv;
float angle_x;
float angle_y;
float angle_z;

char* reading;
char message_buffer[100];

Servo myservo;

void callback(char* topic, byte* payload, unsigned int length) {
}

YunClient yun;
PubSubClient mqtt(MQTT_SERVER, 1883, callback, yun);

void setup()
{
  myservo.attach(9);
  // initialize the serial communications:
  Serial.begin(9600);
  Bridge.begin();
  myservo.write(90); //limit servo to middle position
  delay(10);
}

void loop()
{
  if (!mqtt.connected())
  {
    mqtt.connect(MQTT_CLIENTID);
    mqtt.publish("mqttkb/test","yun connected");
  }
  
  value_x = analogRead(0);
  
  xv = (value_x/analog_resolution*ADC_ref-zero_x)/sensitivity_x;
  
  reading = dtostrf(xv, 5, 2, message_buffer);
  //code to limit servo movement
  if(xv < -8) {xv = -8;}
  else if(xv > 8) {xv = 8;}
  pos = map(xv, -8, 8, 0, 165);  //map G-Force to servo range
  myservo.write(pos);
  delay(10);

  if (millis() > (time + 1000)) {
    time = millis();
    //send values in "reading" to mqtt server
    mqtt.publish("mqttkb/test",reading);
  }
  mqtt.loop();
}

Thanks

binway:
I can upload sketches to the Yun and see data being generated via the Serial Monitor which all indicates that the PC and Yun are communicating.

Are you uploading and looking at the serial monitor using USB or over the network? If using USB, that is NOT proof that they are talking over the network.

Using a web browser on your PC, are you able to access the Yun's configuration web pages?

Or is there a process to step through the code to view errors.

No, there isn't, an unfortunate limitation of the Arduino environment - it could really use a debugger. Your best bet is to add Serial.print() calls at strategic places in your code so that you can monitor the code's progress and see what is happening.

MQTT Broker on Arduino YUN

http://forum.arduino.cc/index.php?topic=290098.msg2043615#msg2043615

uploading and looking at the serial monitor using USB or over the network? If using USB, that is NOT proof that they are talking over the network.

Definitely uploading via the network - no usb is connected. From my laptop I can ping the ip of the yun and putty into the yun using that ip address. When I putty into the yun I can ping the ip of the laptop.

I read somewhere else that when you install mosquitto on windows you should NOT install the services as well so I uninstalled and reinstalled and now have to start the mqtt with mosquitto -v, this gives a few more details on what it is doing as shown below. Hopefully this will help someone else.

C:\Program Files (x86)\mosquitto>mosquitto -v
1421659012: mosquitto version 1.3.5 (build date 08/10/2014 22:54:48.60) starting

1421659012: Using default config.
1421659012: Opening ipv6 listen socket on port 1883.
1421659012: Opening ipv4 listen socket on port 1883.
1421659154: New connection from ::1 on port 1883.
1421659154: New client connected from ::1 as paho/33F1197C90D0D748F8 (c2, k60).
1421659154: Sending CONNACK to paho/33F1197C90D0D748F8 (0)
1421659154: Received SUBSCRIBE from paho/33F1197C90D0D748F8
1421659154:     mqttkb/+ (QoS 0)
1421659154: paho/33F1197C90D0D748F8 0 mqttkb/+
1421659154: Sending SUBACK to paho/33F1197C90D0D748F8
1421659214: Received PINGREQ from paho/33F1197C90D0D748F8

I followed the steps in sonnyyu's post and installed the client on the yun but unfortunately I can still not get the data moving. I'll see if I can get a very simple piece of data moving to with some simple code in the Yun.

First test Yun MQTT Broker setup:

 /etc/init.d/mosquitto restart

Subscribing to a Topic:

mosquitto_sub -d -t hello/world
Received CONNACK
Received SUBACK
Subscribed (mid: 1): 0

Publishing to a Topic, open second terminal windows:

mosquitto_pub -d -t hello/world -m "Hello World"
Received CONNACK
Sending PUBLISH (d0, q0, r0, m1, 'hello/world', ... (11 bytes))

at first windows you should see:

Received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (11 bytes))
Hello World

Use -h YOUR_PC_IP_ADDRESS to communicate with PC.

 mosquitto_sub -h YOUR_PC_IP_ADDRESS -d -t hello/world

Python code:

opkg update
opkg install libmosquitto
opkg update
opkg install python-openssl #adds ssl support to python
opkg install distribute #it contains the easy_install command line tool (this can take some time)
easy_install pip #installs pip  (this can take some time)
pip install mosquitto
nano /mnt/sda1/temperature.py
#!/usr/bin/python
import mosquitto
import sys
mqttc = mosquitto.Mosquitto("python_pub")
mqttc.connect("127.0.0.1", 1883, 60, True)
mqttc.publish("hello/temperature", sys.argv[1] )
chmod 755  /mnt/sda1/temperature.py
/mnt/sda1/temperature.py 34

open second terminal windows

mosquitto_sub -d -t hello/temperature

to confirm temperature sent.

Arduino code:

#include <Process.h>
void setup() {
  // Initialize Bridge
  Bridge.begin();
  // Initialize Serial
  Serial.begin(9600);
  // Wait until a Serial Monitor is connected.
  while (!Serial);
}
void loop() {
  int temperature = random(0, 100);
  Process p;        // Create a process and call it "p"
  p.begin("/mnt/sda1/temperature.py");   // Process that launch the  command
  p.addParameter(temperature); // pass  parameter 
  p.run();      // Run the process and wait for its termination
  delay(5000);
}

The key concept:

  • Loose couple programing, Python could run or debug independently
  • Scale up programing, move as much processing to faster CPU

Thanks so much for all your work.
I got to this step where I used a putty window connected to the yun

Use -h YOUR_PC_IP_ADDRESS to communicate with PC.

Code:
mosquitto_sub -h YOUR_PC_IP_ADDRESS -d -t hello/world

When I ran this it just hung. But when I ping my laptop ip from the Yun I get a reply.
I have stopped at this point and will run the rest tomorrow night.

Thanks.

binway:
Thanks so much for all your work.
I got to this step where I used a putty window connected to the yun

Use -h YOUR_PC_IP_ADDRESS to communicate with PC.

Code:
mosquitto_sub -h YOUR_PC_IP_ADDRESS -d -t hello/world

When I ran this it just hung. But when I ping my laptop ip from the Yun I get a reply.
I have stopped at this point and will run the rest tomorrow night.

Thanks.

Check firewall of PC, turn off it to test. if works then leave TCP/IP port 1883 open.

Thanks - Yep that is the problem.
Thanks for that.

One more thought:

Use MQTT Broker Bridge between Yun Broker with PC Broker.

Now we could have loose couple network, the network could be unreliable. System is still working fine.

BTW. MQTT is Loose coupling system between mosquitto_sub and mosquitto_pub.

hi this is my arduino code to publish temperature sensor data using mqtt and mosquitto broker

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

// Pins
// Analog 0 is the input pin

// Variables
int value;
float temp;
String pubstring;
unsigned long time;
char message_buffer[100];

// Network Settings
// MAC address of ethernet shield
// Look for it on a sticket at the bottom of the shield.
// Old Arduino Ethernet Shields or clones may not have a dedicated MAC address. Set any hex values here.
byte MAC_ADDRESS[] = { 0xFE, 0xED, 0xDE, 0xAD, 0xBE, 0xEF };

// IP address of MQTT server
byte MQTT_SERVER[] = {192,168,1,115};
EthernetClient ethClient;
PubSubClient client;

void setup()
{
// Initilize serial link for debugging
Serial.begin(9600);

if (Ethernet.begin(MAC_ADDRESS) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
return;
}
Serial.print("Local IP=");
Serial.println(Ethernet.localIP());
client = PubSubClient(MQTT_SERVER, 1883, callback, ethClient);
}

void loop()
{
if (!client.connected())
{
//client.connect("clientID", "mqtt_username", "mqtt_password");
client.connect("sfo-arduino");
client.publish("sfo/arduino/alive", "I'm alive!");
}
else{
client.connect("arduino");
Serial.println("here i am connected");}

//tempC = dtostrf(tempF,5,2,message_buffer);
// Publish sensor reading every X milliseconds

if (millis() > (time + 5000)) {
time = millis();
value = analogRead(0);
temp = log((10240000.0/value)-10000.0);
temp = 1.0/(0.001129148 + (0.000234125 + (0.000000087641 * temp * temp)) * temp);
temp =temp - 273.15;
pubstring = String(temp);
pubstring.toCharArray(message_buffer, pubstring.length() + 1);
client.publish("arduino/temperature",message_buffer);
Serial.println("published!");
}

// MQTT client loop processing
client.loop();
}

// Handles messages arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
}

and when i run in the mosquitto terminal it just goes on like this without publishing..may i know what i am doing wrong pls??

C:\mosquitto>mosquitto_sub -h localhost -d -t 'arduino/temperature'
Client mosqsub/6904-chandini-P sending CONNECT
Client mosqsub/6904-chandini-P received CONNACK
Client mosqsub/6904-chandini-P sending SUBSCRIBE (Mid: 1, Topic: 'arduino/temper
ature', QoS: 0)
Client mosqsub/6904-chandini-P received SUBACK
Subscribed (mid: 1): 0
Client mosqsub/6904-chandini-P sending PINGREQ
Client mosqsub/6904-chandini-P received PINGRESP
Client mosqsub/6904-chandini-P sending PINGREQ
Client mosqsub/6904-chandini-P received PINGRESP

components: arduino ethernet shield, arduino uno and lm35 temperature sensor

chandini:
...

components: arduino ethernet shield, arduino uno and lm35 temperature sensor

This is Arduino Yun forum.

I know..I am just a newbie and I just thought you guys could help me with this too..anyways thanks

Hi sonnyyu

I like your python code with sketch that achieve pub ---> sub function.
And i can get the random temperature value from mosquitto_sub.

mosquitto_sub -t /hello/temperature

But here is a question
How to design a code that receive sub temperature value to control pins?
For example, if the temperature is over 25 Celsius degree, turn on the air conditioner.