How to interface MPU9250 and NodeMcu? I am trying to send MPU sensor data to Pubnub IOT platform but not able to do it since there is some unknown problem. There is no error in code but following issue pops up in com port:-



It looks like sensor reading needs to be in JSON Format but no sure how to do it. I Just need it in raw imu sensor reading for now so how do i get it.

Here is the code:

#include &lt;ESP8266WiFi.h&gt;
#define PubNub_BASE_CLIENT WiFiClient
#include &lt;PubNub.h&gt;
#define PUBNUB_DEFINE_STRSPN_AND_STRNCASECMP
#include "MPU9250.h"

const char *ssid =  "mym404";     // replace with your wifi ssid and wpa2 key
const char *pass =  "786110786";

const char * pubkey = "my key";
const char * subkey = "my key";

const char * pubchannel = "demo_keyset";
const char * subchannel = "demo_keyset";

MPU9250 IMU(Wire,0x68);
int status;


void setup() {
    /* For debugging, set to speed of your choice */
    Serial.begin(115200);


       Serial.println("Connecting to ");
       Serial.println(ssid); 

       WiFi.begin(ssid, pass); 
       while (WiFi.status() != WL_CONNECTED) 
          {
            delay(500);
            Serial.print(".");
          }
      Serial.println("");
      Serial.println("WiFi connected");

    /* Start the Pubnub library by giving it a publish and subscribe
       keys */
    PubNub.begin(pubkey, subkey);

    while(!Serial) {}

  // start communication with IMU 
  status = IMU.begin();
  if (status &lt; 0) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    Serial.print("Status: ");
    Serial.println(status);
    while(1) {}
  }
}

void loop() {
    // read the sensor
  IMU.readSensor();
  // display the data
  Serial.print(IMU.getAccelX_mss(),6);
  Serial.print("\t");


    /* Publish message. You could use `auto` here... */
    PubNonSubClient *pclient = PubNub.publish(pubchannel, "String(IMU.getAccelX_mss())");
    if (!pclient) return;
    PublishCracker cheez;
    cheez.read_and_parse(pclient);
    /** You're mostly interested in `outcome()`, and,
        if it's "failed", then `description()`. 
    */
    Serial.print("Outcome: "); Serial.print(cheez.outcome());
    Serial.print(' '); Serial.println(cheez.to_str(cheez.outcome()));
    Serial.print("description: "); Serial.println(cheez.description());
    Serial.print("timestamp: "); Serial.println(cheez.timestamp());
    Serial.print("state: "); Serial.print(cheez.state());
    Serial.print(' '); Serial.println(cheez.to_str(cheez.state()));
    pclient-&gt;stop();

    /* Wait for news. */
    PubSubClient *sclient = PubNub.subscribe(subchannel);
    if (!sclient) return; // error
    String msg;
    SubscribeCracker ritz(sclient);
    while (!ritz.finished()) {
        ritz.get(msg);
        if (msg.length() &gt; 0) {
            Serial.print("Received: "); Serial.println(msg);
        }
    }
    sclient-&gt;stop();

    delay(1000);
}


What do you guys think?
The error you're reporting is coming from your call to PubNub.publish(). You could have diagnosed the problem by taking outputting and inspecting what you were passing to it.

Your line:

PubNonSubClient *pclient = PubNub.publish(pubchannel, "String(IMU.getAccelX_mss())");


is not going to work. You're sending the string "String(IMU.getAccelX_mss())" to PubNub rather than the value of IMU.getAccelX_mss() as a String. Take the quotes off it:

PubNonSubClient *pclient = PubNub.publish(pubchannel, String(IMU.getAccelX_mss()));


If for some reason the number has to be returned as a string, the code would look like:

PubNonSubClient *pclient = PubNub.publish(pubchannel, "\"" + String(IMU.getAccelX_mss()) + "\"");