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