Chris 247
Chris 247

Reputation: 1

ESP32 with MPU9250 doesn't publish sensor readings to MQTT(mosquitto) broker & to node-red

#include <MPU9250_asukiaaa.h>
#include <WiFi.h>             //for wifi connection
#include <PubSubClient.h>     //pubsub model
#define mqtt_server "127.0.0.1"
#define mqttaX  "val/aX"
#define mqttaY  "val/aY"
#define mqttaZ  "val/aZ"
#define mqttaSqrt  "val/Sqrt"
#define mqttgX  "val/gX"
#define mqttgY "val/gY"
#define mqttgZ  "val/gZ"
#define mqttmDirection "val/Dir"
#define mqttmX "val/mX"
#define mqttmY "val/mY"
#define mqttmZ "val/mZ"

const char *ssid ="";             //network sample
const char *password ="";

WiFiClient espClient;
PubSubClient client(espClient);



#ifdef _ESP32_HAL_I2C_H_
#define SDA_PIN 22
#define SCL_PIN 21
#endif

MPU9250_asukiaaa mySensor;
float aX, aY, aZ, aSqrt, gX, gY, gZ, mDirection, mX, mY, mZ;      //mpu9250 readings



void setup() {

  Serial.begin(115200);
  while (!Serial);
  Serial.println("started");

  //begin wifi connection
  Serial.print("Connecting to : ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(2000);
  WiFi.begin(ssid, password);

  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("#");
  }

  Serial.println("");
  Serial.println("==Wifi connected==");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  client.setServer(mqtt_server,1883);
 

  // end of wifi connection
  
  

#ifdef _ESP32_HAL_I2C_H_ // For ESP32
  Wire.begin(SDA_PIN, SCL_PIN);
  mySensor.setWire(&Wire);
#endif

  mySensor.beginAccel();
  mySensor.beginGyro();
  mySensor.beginMag();

  // You can set your own offset for mag values
  // mySensor.magXOffset = -50;
  // mySensor.magYOffset = -55;
  // mySensor.magZOffset = -10;
  
  
 
}

/*
void reconnect() {                                //RECONNECT TO WIFI-FUNCTION
  // Loop until we're reconnected
  int counter = 0;
  while (!client.connected()) {
    if (counter==5){
      ESP.restart();
    }
    counter+=1;
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
   
    if (client.connect("mpu9250")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
 */


void loop() {
  
  /*if(!client.connected())
  {
    reconnect(); 
  }*/
  uint8_t sensorId;
  if (mySensor.readId(&sensorId) == 0) {
    Serial.println("sensorId: " + String(sensorId));
  } else {
    Serial.println("Cannot read sensorId");
  }

  if (mySensor.accelUpdate() == 0) {
    aX = mySensor.accelX();
    aY = mySensor.accelY();
    aZ = mySensor.accelZ();
    aSqrt = mySensor.accelSqrt();
    Serial.println("accelX: " + String(aX));
    Serial.println("accelY: " + String(aY));
    Serial.println("accelZ: " + String(aZ));
    Serial.println("accelSqrt: " + String(aSqrt));
  } else {
    Serial.println("Cannot read accel values");
  }

  if (mySensor.gyroUpdate() == 0) {
    gX = mySensor.gyroX();
    gY = mySensor.gyroY();
    gZ = mySensor.gyroZ();
    Serial.println("gyroX: " + String(gX));
    Serial.println("gyroY: " + String(gY));
    Serial.println("gyroZ: " + String(gZ));
  } else {
    Serial.println("Cannot read gyro values");
  }

  if (mySensor.magUpdate() == 0) {
    mX = mySensor.magX();
    mY = mySensor.magY();
    mZ = mySensor.magZ();
    mDirection = mySensor.magHorizDirection();
    Serial.println("magX: " + String(mX));
    Serial.println("maxY: " + String(mY));
    Serial.println("magZ: " + String(mZ));
    Serial.println("horizontal direction: " + String(mDirection));
  } else {
    Serial.println("Cannot read mag values");
  }

  Serial.println("at " + String(millis()) + "ms");
  Serial.println(""); // Add an empty line
  delay(1000);
  
  client.publish(mqttaX,String(aX).c_str(),true);     //VALUES TO STRING 
  client.publish(mqttaY,String(aY).c_str(),true);
  client.publish(mqttaZ,String(aZ).c_str(),true);
  client.publish(mqttaSqrt,String(aSqrt).c_str(),true);
  client.publish(mqttgX,String(gX).c_str(),true);
  client.publish(mqttgY,String(gY).c_str(),true);
  client.publish(mqttgZ,String(gZ).c_str(),true);
  client.publish(mqttmX,String(mX).c_str(),true);
  client.publish(mqttmY,String(mY).c_str(),true);
  client.publish(mqttmZ,String(mZ).c_str(),true);  
}

Even though code seems to be working fine, i've had issues with the mqtt mosquito broker for a while.I haven't used esp32 with mpu9250 for iot projects in the past , ive also never used mqtt broker and node-red so these tools are new to me.

node-red json :

[{"id":"aeb0197d.7d1fd8","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"aeb99911.35b6c8","type":"debug","z":"aeb0197d.7d1fd8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":280,"wires":[]},{"id":"c040a897.3ffcb8","type":"mqtt in","z":"aeb0197d.7d1fd8","name":"","topic":"val/aY","qos":"2","datatype":"auto","broker":"3d5e34f4.9a452c","x":210,"y":320,"wires":[["aeb99911.35b6c8"]]},{"id":"e55bea67.26a468","type":"mqtt in","z":"aeb0197d.7d1fd8","name":"","topic":"val/aZ","qos":"2","datatype":"auto","broker":"3d5e34f4.9a452c","x":210,"y":360,"wires":[["aeb99911.35b6c8"]]},{"id":"73f7e2ae.656dec","type":"mqtt in","z":"aeb0197d.7d1fd8","name":"","topic":"val/Sqrt","qos":"2","datatype":"auto","broker":"3d5e34f4.9a452c","x":210,"y":400,"wires":[["aeb99911.35b6c8"]]},{"id":"1e0a6d12.2e52b3","type":"mqtt in","z":"aeb0197d.7d1fd8","name":"","topic":"val/gX","qos":"2","datatype":"auto","broker":"3d5e34f4.9a452c","x":210,"y":440,"wires":[["aeb99911.35b6c8"]]},{"id":"7ddbd3f1.96af2c","type":"mqtt in","z":"aeb0197d.7d1fd8","name":"","topic":"val/gY","qos":"2","datatype":"auto","broker":"3d5e34f4.9a452c","x":210,"y":480,"wires":[["aeb99911.35b6c8"]]},{"id":"da0d3584.28d648","type":"mqtt in","z":"aeb0197d.7d1fd8","name":"","topic":"val/gZ","qos":"2","datatype":"auto","broker":"3d5e34f4.9a452c","x":210,"y":520,"wires":[["aeb99911.35b6c8"]]},{"id":"194c2ff8.99737","type":"mqtt in","z":"aeb0197d.7d1fd8","name":"","topic":"val/Dir","qos":"2","datatype":"auto","broker":"3d5e34f4.9a452c","x":210,"y":560,"wires":[["aeb99911.35b6c8"]]},{"id":"18feeaeb.442e15","type":"mqtt in","z":"aeb0197d.7d1fd8","name":"","topic":"val/mX","qos":"2","datatype":"auto","broker":"3d5e34f4.9a452c","x":210,"y":600,"wires":[["aeb99911.35b6c8"]]},{"id":"43022acf.d59cb4","type":"mqtt in","z":"aeb0197d.7d1fd8","name":"","topic":"val/mY","qos":"2","datatype":"auto","broker":"3d5e34f4.9a452c","x":210,"y":640,"wires":[["aeb99911.35b6c8"]]},{"id":"d4ebb01f.70055","type":"mqtt in","z":"aeb0197d.7d1fd8","name":"","topic":"val/mZ","qos":"2","datatype":"auto","broker":"3d5e34f4.9a452c","x":210,"y":680,"wires":[["aeb99911.35b6c8"]]},{"id":"ba8c7b79.8cc218","type":"mqtt in","z":"aeb0197d.7d1fd8","name":"","topic":"val/aX","qos":"2","datatype":"auto","broker":"3d5e34f4.9a452c","x":210,"y":280,"wires":[["aeb99911.35b6c8"]]},{"id":"3d5e34f4.9a452c","type":"mqtt-broker","name":"MQTT BROKER","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Upvotes: 0

Views: 272

Answers (1)

hardillb
hardillb

Reputation: 59618

You never call the PubSub client loop() function.

From the docs:

boolean loop ()

This should be called regularly to allow the client to process incoming messages and maintain its connection to the server.

Returns

  • false - the client is no longer connected
  • true - the client is still connected

Upvotes: 1

Related Questions