Tweeting from Bean

Disclaimer: We are temporarily not offering support for the Node-RED project. The reason for this is because we are focusing our attention on an official Node.js SDK and CLI Loader, rather than the forked and unsupported libraries that our Bean Node-RED project is currently based on. Once the Node SDKs and CLI Loaders are stable, we will determine if we need to rewrite the Node-RED implementation with the new SDKs, and fully support it again.

Introduction

Say you want to build an app that tweets Bean's temperature. Normally, you'd have to go through the process of writing software that can speak to both Twitter and your Bean. If you use Node-RED, you can avoid having to write that software since it understands how to communicate with both Twitter and your Bean!

In this tutorial, you'll build a Node-RED flow that connects your Bean's temperature sensor to a Twitter account.

Before You Begin

This tutorial assumes you have completed the Getting Started guide and that you understand how to upload a sketch to Bean using Bean Loader.

Software

Hardware

Program Your Bean

Connect to your Bean and upload this Arduino sketch using Bean Loader:

int8_t temp = 0;

void setup() {
  // We don't need to do anything here; Serial is configured automatically on Bean
}

void loop() {
  int8_t newTemp = Bean.getTemperature();
  if (newTemp != temp) {
    temp = newTemp;
    Serial.println(temp);
  }

  Bean.sleep(10000);
}

Here's what the code does:

  • Line 1: Declare temp as an 8-bit long integer. Declaring it outside loop causes it to keep its value between loops.
  • Line 7: The loop function. This function is executed continuously until Bean is either turned off or unprogrammed.
  • Line 8: Read the temperature value from bean.
  • Line 9: Check if the temperature has changed since the last loop.
  • Line 10: If the temperature has changed, save the new value for reference next time.
  • Line 11: Send the temperature information back to Node-RED followed by a newline character \n. This allows Node-RED to separate the messages.
  • Line 14: Sleep for 10 seconds. This prevents Twitter from being bombarded with tweets from Bean.

Program Bean on Node-RED

Once you have programmed Bean, disconnect from your Bean in Bean Loader. Then start Node-RED and open it in your browser:

[object Object]

Add a Bean serial node to your flow. Double-click it to configure it. Ensure your Bean is selected and the node is splitting incoming data on the character \n:

[object Object]

Notice the \n character is configued as the input delimiter. This matches our Arduino sketch, since we use Serial.println to add \n to our temperature data.

Next, add a Tweet node to your flow. Join the two nodes by clicking and dragging a connection line between the two:

[object Object]

Notice that the connector for the Tweet node is on the left side. Some nodes have connectors on both sides, indicating they can both receive incoming messages AND send outgoing messages.

Fill out the information for your Twitter login. Once that's done, click Deploy in the upper-right corner.

[object Object]

Now, go ahead and check your Twitter account to see tweets from your Bean!

Conclusion

In this guide, we showed you how to implement the Bean's iOS SDK. This guide should be served as a stepping stone for other projects you may want to create!

Troubleshooting

Having trouble with this guide? Try the steps listed in General Bean troubleshooting. Or reach out to our community at Beantalk for direct help with your problem.