Main Content

Bulk-Update Using a Raspberry Pi Board

This example shows how to collect data using a Wi-Fi-connected Raspberry Pi board running Python 2.7. You can continuously collect CPU temperature and CPU utilization every 15 seconds and bulk-update a ThingSpeak channel every 2 minutes. This example uses the Bulk-Write JSON Data API to collect data as a batch and send it to ThingSpeak channels. By using bulk-update, you can reduce the power consumption of your devices. Since the Raspberry Pi board does not come with a real-time clock, you can use the relative timestamp for bulk-update messages.

Setup

Create a channel as shown in Collect Data in a New Channel.

Code

1) Import the necessary libraries for the script.

import json
import time
import os
import psutil
import requests

2) Define global variables that track the last connection time and last update time. Define time intervals to update the data, and post the data to ThingSpeak.

last_connection_time = time.time() # Track the last connection time
last_update_time = time.time()     # Track the last update time
posting_interval = 120             # Post data once every 2 minutes
update_interval = 15               # Update once every 15 seconds

3) Define your ThingSpeak write API key and channel ID settings, along with the ThingSpeak server settings.

write_api_key = "YOUR-CHANNEL-WRITEAPIKEY" # Replace YOUR-CHANNEL-write_api_key with your channel write API key
channel_ID = "YOUR-CHANNELID"              # Replace YOUR-channel_ID with your channel ID
url = https://api.thingspeak.com/channels/+channel_ID+"/bulk_update.json" # ThingSpeak server settings
message_buffer = []

4) Define the function httpRequest that sends data to ThingSpeak and prints the response code from the server. The response code 202 indicates that the server has accepted the request and will process it.

def httpRequest():
    # Function to send the POST request to ThingSpeak channel for bulk update.
        global message_buffer
        bulk_data = json.dumps({'write_api_key':write_api_key,'updates':message_buffer}) # Format the json data buffer
        request_headers = {"User-Agent":"mw.doc.bulk-update (Raspberry Pi)","Content-Type":"application/json","Content-Length":str(len(bulk_data))}
    # Make the request to ThingSpeak
        try:
            print(request_headers)
            response = requests.post(url,headers=request_headers,data=bulk_data)
            print (response) # A 202 indicates that the server has accepted the request
        except e:
            print(e.code) # Print the error code
        message_buffer = [] # Reinitialize the message buffer
        global last_connection_time
        last_connection_time = time.time() # Update the connection time

5) Define the function getData that returns the CPU temperature in Celsius along with the CPU utilization as a percentage.

def getData():
    # Function that returns the CPU temperature and percentage of CPU utilization
        cmd = '/opt/vc/bin/vcgencmd measure_temp'
        process = os.popen(cmd).readline().strip()
        cpu_temp = process.split('=')[1].split("'")[0]
        cpu_usage = psutil.cpu_percent(interval=2)
        return cpu_temp,cpu_usage

6) Define the function updatesJson to continuously update the message buffer every 15 seconds.

def updatesJson():
    # Function to update the message buffer every 15 seconds with data. 
    # And then call the httpRequest function every 2 minutes. 
    # This examples uses the relative timestamp as it uses the "delta_t" parameter.
    # If your device has a real-time clock, you can also provide the absolute timestamp 
    # using the "created_at" parameter.

        global last_update_time
        message = {}
        message['delta_t'] = int(round(time.time() - last_update_time))
        Temp,Usage = getData()
        message['field1'] = Temp
        message['field2'] = Usage
        global message_buffer
        message_buffer.append(message)
    # If posting interval time has crossed 2 minutes update the ThingSpeak channel with your data
        if time.time() - last_connection_time >= posting_interval:
                httpRequest()
                last_update_time = time.time()

7) Run an infinite loop to continuously call the function updatesJson every 15 seconds.

if __name__ == "__main__":  # To ensure that this is run directly and does not run when imported
        while True:
                # If update interval time has crossed 15 seconds update the message buffer with data
            if time.time() - last_update_time >= update_interval:
                updatesJson()

Related Examples

More About