Traditional factory automation and remote monitoring systems have overhead costs of up to six figures per machine or more. With complex legal contracts from equipment manufacturers, licensing, consulting, and installation costs, putting together a system to remotely monitor your machines is a lot more complicated than it should be.

Using off the shelf hardware with a secure cloud platform is a great alternative to implementing expensive SCADA systems for non-critical monitoring applications like counting the number of boxes on a conveyor belt, monitoring the cycle time of a machine for optimization, or gathering data from your production line.

Box Counter Example Setup

In this tutorial we’ll show you how to quickly deploy a sensor that can count the number of objects moving on a conveyor belt in real time. This can be used for quality control, logistics, and to maximize the efficiency of your production line.

You might think that the complexity of setting up a system from scratch would be too time consuming, but using tools like Temboo Code Generation and widely available low cost hardware, you can set up an entire network within a few hours. IoT Platforms like Temboo allow you to copy and paste code from one device to another, while managing APIs and updates from the web. This makes deploying and maintaining an IoT system a lot more manageable than having to manually interface with every sensor node.

Setting Up A Production Line Monitoring System

We’ll be using a Dragino Yún Shield along with an Arduino Uno as our hardware platform. The Dragino Yún will act as a bridge to connect our sensor node to a WiFi network.

We’ll also use a photoelectric sensor, which uses infrared light to detect how far away an object is. The Arduino will read a value from the sensor at a rate of 5 times per second, which should be more than enough for an average conveyor belt moving boxes. Every 20 seconds, the Arduino will upload the latest object count.These rates can be increased for high speed processes as well. Our program will count how many objects pass by, and store these values in a Google Sheet which your organization can access via browser or mobile app.

Requirements:

  • Arduino or Draguino Yún
  • Arduino Zero (Other hardware platforms can be used, but will require modification to the provided code)
  • IR Distance Sensor
    • Note the max range of this sensor – you can use a more powerful sensor if a higher resolution or longer range is needed. Make sure that the Arduino can provide enough power to the sensor, otherwise you will need to use an external power source.
  • USB Cable and Power Adapter
  • WiFi Access
  • A Temboo account

To begin, we’ll show you how to make your Arduino Yún add rows of data to a Google Sheet. You can use this to log data from sensors connected to your Yún. This sketch uses our Google > Sheets > AppendValues Choreo.

Step 1: Get Your Temboo Account And API Credentials Set Up

  • Make sure you have a Temboo account. If you don’t already have one, you can register for a free account here.
  • Since this sketch uses a Google spreadsheet you’ll also need a Google account.
  • Log in to Google’s Developer Console and create a new project if you haven’t done so already.
  • Under the API Manager section, select Library and make sure you’ve enabled API access for the Google Sheets API.
  • Select Credentials in the API Manager section and create a new Client ID specifying Web application for the Application type.
  • When configuring the Consent Screen, make sure you fill out the Email Address and Product Name fields.
  • Save the Consent Screen details and specify the callback URL below as the Authorized Redirect URI. Make sure to replace ACCOUNT_NAME with your Temboo account name.

https://ACCOUNT_NAME.temboolive.com/callback/google

Google OAuth

The InitializeOAuth choreo will return an authorization URL and a callback ID (required for the FinalizeOAuth step).

  • Open a new web browser, navigate to the authorization URL returned by the InitializeOAuth Choreo, and click “Accept” to gran the app access to your Google Account.

Google Permissions

  • Go to the Google > OAuth > FinalizeOAuth Choreo page, and specify the callback ID returned earlier by the InitializeOAuth Choreo. Then click Generate Code to run the Choreo from our site. This process will return a Refresh Token which can be used along with the Client ID and Client Secret to authenticate with Google.
  • Create a Google Sheet. In this example, our spreadsheet has two columns as seen below: time (in milliseconds) and sensor values.

Google Spreadsheet

A screenshot of a spreadsheet taking data from a Yún – note the column names.

  • When viewing your spreadsheet, you’ll see a spreadsheet ID in your browser’s URL bar. Copy this ID because you’ll need it when running the AppendValues Choreo. In the example below, the highlighted part is the Spreadsheet ID.

Spreadsheet ID

  • Make sure that you have the latest version of the Arduino IDE. You should also check that you have the newest version of the Temboo Library by checking the Arduino Library Manager.
  • Make sure that your Yún is connected to the internet. Arduino has a helpful guide if you need assistance.

Step 2: Write the Sketch

Copy the sketch code below into a new tab in your Arduino IDE. This code calls the AppendValues Choreo, and you will need to replace the placeholder values in the code with your own Google account details and the ID of your Google Sheet.

The placeholders include:
“your-google-client-id”;
“your-google-client-secret”;
“Your-google-refresh-token”;
“Your-spreadsheet-id”;

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
  SendDataToGoogleSpreadsheet

  Demonstrates appending a row of data to a Google spreadsheet from the Arduino Yun 
  using the Temboo Arduino Yun SDK.  

  This example code is in the public domain.
*/

#include 
#include 
#include "TembooAccount.h" // contains Temboo account information

/*** SUBSTITUTE YOUR VALUES BELOW: ***/

// Note that for additional security and reusability, you could
// use #define statements to specify these values in a .h file.

const String GOOGLE_CLIENT_ID = "your-google-client-id";
const String GOOGLE_CLIENT_SECRET = "your-google-client-secret";
const String GOOGLE_REFRESH_TOKEN = "your-google-refresh-token";

// The ID of the spreadsheet you want to send data to
// which can be found in the URL when viewing your spreadsheet at Google. For example, 
// the ID in the URL below is: "1tvFW2n-xFFJCE1q5j0HTetOsDhhgw7_998_K4sFtk"
// Sample URL: https://docs.google.com/spreadsheets/d/1tvFW2n-xFFJCE1q5j0HTetOsDhhgw7_998_K4sFtk/edit
const String SPREADSHEET_ID = "your-spreadsheet-id";

int numRuns = 1;   // execution count, so this doesn't run forever
int maxRuns = 100;   // the max number of times the Google Spreadsheet Choreo should run

void setup() {
  
  // for debugging, wait until a serial console is connected
  Serial.begin(9600);
  delay(4000);
  while(!Serial);

  Serial.print("Initializing the bridge... ");
  Bridge.begin();
  Serial.println("Done!\n");
}

void loop()
{

  // while we haven't reached the max number of runs...
  if (numRuns <= maxRuns) {

    Serial.println("Running AppendValues - Run #" + String(numRuns++));

    // get the number of milliseconds this sketch has been running
    unsigned long now = millis();
    
    Serial.println("Getting sensor value...");

    // get the value we want to append to our spreadsheet
    unsigned long sensorValue = getSensorValue();

    Serial.println("Appending value to spreadsheet...");

    // we need a Process object to send a Choreo request to Temboo
    TembooChoreo AppendValuesChoreo;

    // invoke the Temboo client
    // NOTE that the client must be reinvoked and repopulated with
    // appropriate arguments each time its run() method is called.
    AppendValuesChoreo.begin();
    
    // set Temboo account credentials
    AppendValuesChoreo.setAccountName(TEMBOO_ACCOUNT);
    AppendValuesChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    AppendValuesChoreo.setAppKey(TEMBOO_APP_KEY);
    
    // identify the Temboo Library choreo to run (Google > Sheets > AppendValues)
    AppendValuesChoreo.setChoreo("/Library/Google/Sheets/AppendValues");
    
    // set the required Choreo inputs
    // see https://www.temboo.com/library/Library/Google/Sheets/AppendValues/ 
    // for complete details about the inputs for this Choreo
    
    // your Google Client ID
    AppendValuesChoreo.addInput("ClientID", GOOGLE_CLIENT_ID);

    // your Google Client Secret
    AppendValuesChoreo.addInput("ClientSecret", GOOGLE_CLIENT_SECRET);

    // your Google Refresh Token
    AppendValuesChoreo.addInput("RefreshToken", GOOGLE_REFRESH_TOKEN);

    // the title of the spreadsheet you want to append to
    AppendValuesChoreo.addInput("SpreadsheetID", SPREADSHEET_ID);

    // convert the time and sensor values to a json array
    String rowData = "[[\"" + String(now) + "\", \"" + String(sensorValue) + "\"]]";

    // add the RowData input item
    AppendValuesChoreo.addInput("Values", rowData);

    // run the Choreo and wait for the results
    // The return code (returnCode) will indicate success or failure 
    unsigned int returnCode = AppendValuesChoreo.run();

    // return code of zero (0) means success
    if (returnCode == 0) {
      Serial.println("Success! Appended " + rowData);
      Serial.println("");
    } else {
      // return code of anything other than zero means failure  
      // read and display any error messages
      while (AppendValuesChoreo.available()) {
        char c = AppendValuesChoreo.read();
        Serial.print(c);
      }
    }

    AppendValuesChoreo.close();
  }

  Serial.println("Waiting...");
  delay(5000); // wait 5 seconds between AppendValues calls
}

// this function simulates reading the value of a sensor 
unsigned long getSensorValue() {
  return analogRead(A0);
}

Step 3: Create Your Header File

The sketch above references the TembooAccount.h header file, which contains your Temboo account information.

Copy this code into a new tab in Arduino and call it TembooAccount.h.

#define TEMBOO_ACCOUNT “ACCOUNT_NAME”  // your Temboo account name
#define TEMBOO_APP_KEY_NAME “APP_NAME”  // your Temboo app key name
#define TEMBOO_APP_KEY  “APP_KEY”  // your Temboo app key

Your app name and key can be found here: https://temboo.com/account/applications

Step 4: Set Up Your Production Line Monitoring Hardware

Now that we’ve set up Google Sheets to log our sensor data, let’s connect our IR distance sensor to our arduino. Make sure your Arduino is powered off before connecting the sensor. Connect the red wire (+5V) to the 5V pin on the Arduino Yún, black wire (Ground) to the GND pin, and the yellow wire (Data) to pin A0. Power your board up and give it up to a minute to boot up.

If you haven’t set up your Arduino Yún to connect to a WiFi network yet, check out this guide on how to get it up and running.

Diagram of IR sensor setup on an Arduino

That’s it! Your Production Line Counter is Ready

Open up the Google Sheet that you associated with your choreo, and watch the data being updated every 15 seconds. The first column shows a timestamp of how long your program has been running. The second column stores a count of how many objects passed over the IR distance sensor. In our code, we are incrementing the counter based on the falling edge of the object, and the sensor is polling at a rate of 5Hz. Feel free to modify our code to suit the needs of your project. If you have any questions, send us a message via live chat on the bottom right corner of this window!

Temboo can be used to quickly generate code in over a dozen programming languages and several hardware platforms. One example of how you can extend the functionality of this project is to send an SMS employees when your production rate drops under a certain threshold.

Object Counter

Check out our Quickstart Guides for more ideas on how you can connect your facilities to the Internet of Things without breaking the bank.

Interested in hearing more about the latest Industrial IoT technologies and solutions? Contact us for more information on how to implement IoT solutions at your business or follow Temboo on TwitterLinkedInFacebookInstagram or YouTube.

Posted by:Jessica Califano, Product Outreach

Jessica is on the Product Outreach team at Temboo. When she's not creating content about the latest developments in IoT, she enjoys traveling, photography, and design. She's also passionate about gender equality, advocating for women in STEM fields, and environmentalism.