This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Bulk-Update a ThingSpeak Channel Using a Particle Photon Board

This example shows how to use a Particle Photon board connected to a Wi-Fi® network to bulk-update a ThingSpeak™ channel. You can use the Bulk-Write JSON Data API to collect data in a batch and send it to ThingSpeak channels. This process reduces the power usage of your devices. In this example, you collect data every 15 seconds and update your channel once every 2 minutes using a Particle Photon board. Since Particle Photon comes with a real-time clock, you can use the absolute time stamp for bulk-update messages.

 Particle Code to Bulk-Update a Channel Feed

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

  2. Define a one-day limit to synchronize time from the Particle Cloud.

    #define ONE_DAY_MILLIS (24 * 60 * 60 * 1000) // Define 1 day limit for time synchronization
    unsigned long lastSync = millis();

  3. Initialize the data buffer to hold the data.

    char data[800] = ""; // Initialize the data buffer

  4. Initialize the TCP client library.

    TCPClient client; // Initialize the TCP client library

  5. Define the ThingSpeak server, your channel write API key, and your channel ID.

    String server = ""; // ThingSpeak Server
    String WriteAPIKey = "YOUR-CHANNEL-WRITEAPIKEY"; // Replace YOUR-CHANNEL-WRITEAPIKEY with your channel write API key
    String ChannelID = "YOUR-CHANNEL-ID"; // Replace YOUR-CHANNEL-ID with your channel ID

  6. Define other global variables that track the last connection time and last update time. Also, define time intervals to update the data, and post the data to ThingSpeak.

    /* Collect data once every 15 seconds and post data to ThingSpeak channel once every 2 minutes */
    unsigned long lastConnectionTime = 0; // Track the last connection time
    unsigned long lastUpdateTime = 0; // Track the last update time
    const unsigned long postingInterval = 120L * 1000L; // Post data every 2 minutes
    const unsigned long updateInterval = 15L * 1000L; // Update once every 15 seconds
    size_t state = 0; // Keep note of first time the updateData() is called

  7. Do not modify the default setup method.

    void setup() {

  8. Use the loop method to call the updateData method to update the data buffer with data once every 15 seconds. Also, request time synchronization from the Particle Cloud once a day.

    void loop() {
        // If update time has reached 15 seconds, then update the data buffer
        if (millis() - lastUpdateTime >= updateInterval) {
        // If last time synchronization is more than one day
        if (millis() - lastSync > ONE_DAY_MILLIS) {
        // Request time synchronization from the Particle Cloud
        lastSync = millis();

  9. Define the updateData method to continuously update the data buffer with data. Since Particle Photon has a built-in real-time clock, you can use the time_format=absolute parameter to define absolute timestamps between successive messages. If your device does not have a real-time clock, you can use a relative timestamp. Replace time_format=absolute with time_format=relative. Format the messages in CSV format as mentioned in Bulk-Write JSON Data. Call the httpRequest method to send data to ThingSpeak every two minutes.

    // Update the data buffer
    void updateData(){
        /* CSV format to bulk update
       *  This function uses the absolute timestamp as it uses the "time_format=absolute" parameter. If your device does not have a real-time clock, 
       *  you can also provide the relative timestamp in seconds using the "time_format=relative" parameter.
        strcat(data,String(Time.local())); // Absolute time stamp
        strcat(data,"%2C"); // URL encoding for ","
        long randNumber = random(1,300);
        strcat(data,String(randNumber)); // Data to post to field 1.
        randNumber = random(1,300);
        strcat(data,String(randNumber)); // Data to post to field 2
        strcat(data,"%2C%2C%2C%2C%2C%2C%2C%2C"); //Include commas after fields 2-8 and lattitude for 8 commas.
        randNumber = random(1,300);
        strcat(data,String(randNumber)); // Mock data to post to longitude
        strcat(data,"%2C%7C"); // URL encoding for ",|". End with | at the end of a message
        state = 1; 
        // If posting interval time has reached 2 minutes, then update the ThingSpeak channel with your data
        if (millis() - lastConnectionTime >= postingInterval) {
            state = 0;
            size_t len = strlen(data);
            data[len-3] = '\0'; // Remove the | from the last message
            httpRequest(data); // Call httpRequest to send the data to ThingSpeak
            data[0] = '\0'; // Reinitialise the data buffer
        lastUpdateTime = millis(); // Update the last update time

  10. Define the httpRequest method to send data to ThingSpeak and to print the response code from the server. The response code 202 indicates that the server has accepted the request and is processing the data.

    // Update the ThingSpeakchannel with data
    void httpRequest(char* csvBuffer) {
         /* CSV format to bulk update
          * This function posts the data to ThingSpeak server
        // Compute the data buffer length
        String data_length = String(strlen(csvBuffer));
        // Close any connection before sending a new request
        // POST data to ThingSpeak
        if (client.connect(server, 80)) {
            client.println("POST /channels/"+ChannelID+"/bulk_update HTTP/1.1");
            client.println("Host: "+server);
            client.println("User-Agent: mw.doc.bulk-update (Particle Photon)");
            client.println("Connection: close");
            client.println("Content-Type: application/x-www-form-urlencoded");
            client.println("Content-Length: "+data_length);
        else {
            Particle.publish("Failure","Failed to update ThingSpeak channel");
        delay(1000); // Wait to receive the response
        String resp = String(client.parseInt());
        Particle.publish("Response code",resp); // Print the response code. 202 indicates that the server has accepted the response
        lastConnectionTime = millis(); // Update the last conenction time

Related Examples

More About