Hi,
I was trying to publish some sensor data via MQTT in real-time. I was able to do that and captured. But I couldn't do the publish part simultaneously.
I mean if I comment the publish part, subscribe works. If I comment the subscribe, publish works. It never really worked all together. ie. publishing sensor data to MQTT server and if in between time I push something to that arduino from MQTT, but that does not change LED status.as far as I am guessing callback is not getting anything. Hoping for the solution.
Let me give code snippet :
void callback(char* topic, byte* payload, unsigned int length);
#define MQTT_SERVER_SUB "x.xx.xx.x"
PubSubClient client(MQTT_SERVER, 1883, callback, wfclient);
void setup()
{
** pinMode(LED, OUTPUT);**
** // init serial link for debugging**
** Serial.begin(115200);**
** while (status != WL_CONNECTED) {**
** Serial.print("Attempting to connect to SSID: ");**
** Serial.println(ssid);**
** // Connect to WPA/WPA2 network. Change this line if using open or WEP network:**
** //status = WiFi.begin(ssid, pass);**
** status = WiFi.begin(ssid,pass);**
** // wait 10 seconds for connection:**
** delay(3000);**
** }**
** Serial.println("Connected to wifi");**
** printWifiStatus();**
** delay(100);**
** Wire.begin(); // Start the I2C interface.**
** error = compass.setScale(1.3); // Set the scale of the compass.**
** if(error != 0) // If there is an error, print it out.**
** Serial.println(compass.getErrorText(error));**
** Serial.println("Setting measurement mode to continous.");**
** error = compass.setMeasurementMode(MEASUREMENT_CONTINUOUS);**
** if(error != 0) // If there is an error, print it out.**
** Serial.println(compass.getErrorText(error));**
edison_mqtt_conn();
}
void getAccel(char* accelpacket){
** // read the both analog in values:**
** sensorValue1 = analogRead(analogInPin1); **
** sensorValue2 = analogRead(analogInPin2); **
** sensorValue3 = analogRead(analogInPin3); **
** // map it to the range of the analog out:**
** outputValue1 = map(sensorValue1, 0, 1023, 0, 255); **
** outputValue2 = map(sensorValue2, 0, 1023, 0, 255);**
** outputValue3 = map(sensorValue3, 0, 1023, 0, 255);**
//Serial.println(outputValue1);
memset(accelpacket , '\0', 40);
int lena = 0;
sprintf(accelpacket, ""Ax": "%d","Ay": "%d","Az":"%d"",outputValue1, outputValue2, outputValue3);
lena = strlen(accelpacket);
//Serial.print("Accel packet length : ");
//Serial.println(lena);
}
void getGyro(char* gyropacket){
MagnetometerScaled scaled = compass.readScaledAxis();
float xaxis = scaled.XAxis;
float yaxis = scaled.YAxis;
float zaxis = scaled.ZAxis;
int leng = 0;
memset(gyropacket , '\0', 50);
//char* sensorid = "pirmotion";
sprintf(gyropacket, ""Gx": "%.2f", "Gy": "%.2f","Gz": "%.2f"",xaxis, yaxis, zaxis);
leng = strlen(gyropacket);
//Serial.print("gyro packet length : ");
//Serial.println(leng);
//Serial.println(gyropacket);
}
void payload(char* payload, char* gyro, char* accel){
__ char* sensorid = "AccNgyro";__
memset(payload , '\0', 130);
sprintf(payload, "{ "sensor": "%s", "record": {%s,%s} }", sensorid, gyro, accel);
}
void loop()
{
edison_mqtt_conn();
edison_mqtt_pub();
edison_mqtt_sub();
if(client.subscribe("mqtt")){
** Serial.println("Client subscribed");**
}
delay(1);
}
// handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
** int i = 0;**
** Serial.println("Message arrived: topic: " + String(topic));**
** Serial.println("Length: " + String(length,DEC));**
** // create character buffer with ending null terminator (string)**
** for(i=0; i<length; i++) {**
message_buff = payload*;*
* }*
message_buff = '\0';
* String msgString = String(message_buff);
_ Serial.println("Payload: " + msgString);
if (msgString == "ON")
digitalWrite(LED, HIGH);
else if (msgString == "OFF")
digitalWrite(LED, LOW);
else*
* Serial.println("not a valid command for LED");*_
}
void printWifiStatus() {
* // print the SSID of the network you're attached to:*
* Serial.print("SSID: ");*
* Serial.println(WiFi.SSID());*
* // print your WiFi shield's IP address:*
* IPAddress ip = WiFi.localIP();*
* Serial.print("IP Address: ");*
* Serial.println(ip);*
* // print the received signal strength:*
* long rssi = WiFi.RSSI();*
* Serial.print("signal strength (RSSI):");*
* Serial.print(rssi);*
* Serial.println(" dBm");*
}
//Connection to MQTT
void edison_mqtt_conn(){
if (!client.connected()) {
if (client.connect("edison_pub")) {
Serial.println("[PHYSICAL] Successfully connected with MQTT");
edison_mqtt_sub(); //MQTT subscription
* }*
* }*
* client.loop();*
}
/* MQTT subscribe */
void edison_mqtt_sub() {
* client.subscribe(TOPIC);*
}
/* MQTT publish */
void edison_mqtt_pub() {
* getGyro(gyropacket);*
* getAccel(accelpacket);*
* payload(payloadBuff , gyropacket, accelpacket);*
}[/b]