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. By using bulk update, you can reduce the power consumption 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 the Particle Photon comes with a real-time clock, you can use the absolute time stamp for bulk-update messages.

Setup

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

Code

1) 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(); 

2) Initialize the data buffer to hold the data.

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

3) Initialize the TCP client library.

TCPClient client; // Initialize the TCP client library

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

String server = "api.thingspeak.com"; // 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.

5) Create global variables that track the last connection time and last update time. Then 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.

6) Do not modify the default setup method.

void setup() {
}

7) 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) {
      updateData();
  }
    // If last time synchronization is more than one day
    if (millis() - lastSync > ONE_DAY_MILLIS) {
    // Request time synchronization from the Particle Cloud
    Particle.syncTime();
    lastSync = millis();
  }
}

8) 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 absolute time in the API call. 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. To use relative timestamps, 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.
   */
    if(state==0){
        strcpy(data,"write_api_key="+WriteAPIKey+"&time_format=absolute&updates=");
    }
    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.
    strcat(data,"%2C");
    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.
}

9) 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.
    client.stop();
    // 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);
        client.println();
        client.println(csvBuffer);
    }
    else {
        Particle.publish("Failure","Failed to update ThingSpeak channel");
    }
    delay(1000); // Wait to receive the response.
    client.parseFloat();
    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