I did managed to upload mpu9250 sensor data on PubNub and nodeMcu but not able to use the received data to control servo

Look at the Port reading and see that servo angle is still showing '0'

COM PORT Reading


char buf[500] = {};
int imu = IMU.getAccelX_mss() * 10;

sprintf(buf, "\"%d\"", imu);


PubNonSubClient *pclient = PubNub.publish(pubchannel, buf);
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()));
Serial.print("IMU Reading: "); Serial.println(IMU.getAccelX_mss());


/* 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);
        pos = msg.toInt();
        Serial.print("value of servo angle: "); Serial.println(pos);



How do I convert a string to integer so that it can be used in servo?

  • Hint: Might need to remove the extra characters ("..") before converting from a string to an integer. – Mikael Patel Mar 6 at 22:22
  • oh yes, let me try it – Sultan Morbiwala Mar 7 at 6:35

This is the code which worked for me:-

        msg.remove(0,1); msg.remove(msg.length());
        pos = msg.toInt();
        Serial.print("value of servo angle: "); Serial.println(pos);

I had to remove this character (" ") by using msg.remove(0,1) & msg.remove(msg.length()) that removes starting and ending string.

  • cleaner: msg.trim(); Serial.println("value of servo angle:" + String(msg.toInt()); – dandavis Mar 7 at 18:09
  • nice, much better – Sultan Morbiwala Mar 15 at 7:37
  • but msg.trim() didn't work – Sultan Morbiwala Mar 15 at 8:28
  • it's a void mutative method in ardunio, not a transformer like in other languages. – dandavis Mar 15 at 14:58

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.