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:-

COM PORT Results

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 <ESP8266WiFi.h>
#define PubNub_BASE_CLIENT WiFiClient
#include <PubNub.h>
#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.println("Connecting to ");

       WiFi.begin(ssid, pass); 
       while (WiFi.status() != WL_CONNECTED) 
      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 < 0) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    Serial.print("Status: ");
    while(1) {}

void loop() {
    // read the sensor
  // display the data

    /* Publish message. You could use `auto` here... */
    PubNonSubClient *pclient = PubNub.publish(pubchannel, "String(IMU.getAccelX_mss())");
    if (!pclient) return;
    PublishCracker cheez;
    /** 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()));

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


What do you guys think?

  • I hope your pubkey and subkey aren't sensitive data – Jaromanda X Mar 4 at 0:20
  • no, its just demo key. but will change it just in case. – Sultan Morbiwala Mar 4 at 13:29

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()) + "\"");
  • I tried but it doesn't work. In Pubnub documentation its written to send that data in JSON format. eg - "\"message\"" . I know about uploading string but don't know about uploading data as a variable. You know how to upload variable as JSON. – Sultan Morbiwala Mar 3 at 17:13
  • You'll need ArduinoJson to make JSON easier - but if the data you are sending is very simple, then there's probably no need for ArduinoJson library - please add to the question the expected format of the data you are sending – Jaromanda X Mar 4 at 0:17
  • I just need raw sensor reading for now – Sultan Morbiwala Mar 4 at 13:45
  • The answer I gave you sends the raw sensor reading to the server. You keep saying you need to send JSON. What should the JSON look like? – John Romkey Mar 5 at 5:03
  • Expected format should be "\"IMU.getAccelX_mss()\"" .Just that imu.get needs to show an actual reading. – Sultan Morbiwala Mar 5 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.