ThingSpeak Communication Library
Enables an Arduino or ESP8266 to write or read data to or from ThingSpeak™
Public Member Functions
ThingSpeakClass Class Reference

Enables an Arduino, ESP8266 or other compatible hardware to write or read data to or from ThingSpeak, an open data platform for the Internet of Things with MATLAB analytics and visualization. More...

Public Member Functions

bool begin (Client &client, const char *customHostName, unsigned int port)
 Initializes the ThingSpeak library and network settings using a custom installation of ThingSpeak. More...
 
bool begin (Client &client, IPAddress customIP, unsigned int port)
 Initializes the ThingSpeak library and network settings using a custom installation of ThingSpeak. More...
 
bool begin (Client &client)
 Initializes the ThingSpeak library and network settings using the ThingSpeak.com service. More...
 
int writeField (unsigned long channelNumber, unsigned int field, int value, const char *writeAPIKey)
 Write an integer value to a single field in a ThingSpeak channel. More...
 
int writeField (unsigned long channelNumber, unsigned int field, long value, const char *writeAPIKey)
 Write a long value to a single field in a ThingSpeak channel. More...
 
int writeField (unsigned long channelNumber, unsigned int field, float value, const char *writeAPIKey)
 Write a floating point value to a single field in a ThingSpeak channel. More...
 
int writeField (unsigned long channelNumber, unsigned int field, const char *value, const char *writeAPIKey)
 Write a string to a single field in a ThingSpeak channel. More...
 
int writeField (unsigned long channelNumber, unsigned int field, String value, const char *writeAPIKey)
 Write a String to a single field in a ThingSpeak channel. More...
 
int setField (unsigned int field, int value)
 Set the value of a single field that will be part of a multi-field update. To write multiple fields at once, call setField() for each of the fields you want to write, and then call writeFields() More...
 
int setField (unsigned int field, long value)
 Set the value of a single field that will be part of a multi-field update. To write multiple fields at once, call setField() for each of the fields you want to write, and then call writeFields() More...
 
int setField (unsigned int field, float value)
 Set the value of a single field that will be part of a multi-field update. To write multiple fields at once, call setField() for each of the fields you want to write, and then call writeFields() More...
 
int setField (unsigned int field, const char *value)
 Set the value of a single field that will be part of a multi-field update. To write multiple fields at once, call setField() for each of the fields you want to write, and then call writeFields() More...
 
int setField (unsigned int field, String value)
 Set the value of a single field that will be part of a multi-field update. To write multiple fields at once, call setField() for each of the fields you want to write, and then call writeFields() More...
 
int setLatitude (float latitude)
 Set the latitude of a multi-field update. To record latitude, longitude and elevation of a write, call setField() for each of the fields you want to write, setLatitude() / setLongitude() / setElevation(), and then call writeFields() More...
 
int setLongitude (float longitude)
 Set the longitude of a multi-field update. To record latitude, longitude and elevation of a write, call setField() for each of the fields you want to write, setLatitude() / setLongitude() / setElevation(), and then call writeFields() More...
 
int setElevation (float elevation)
 Set the elevation of a multi-field update. To record latitude, longitude and elevation of a write, call setField() for each of the fields you want to write, setLatitude() / setLongitude() / setElevation(), and then call writeFields() More...
 
int setStatus (const char *status)
 Set the status of a multi-field update. To record a status message on a write, call setStatus() then call writeFields(). Use status to provide additonal details when writing a channel update. Additonally, status can be used by the ThingTweet App to send a message to Twitter. More...
 
int setStatus (String status)
 Set the status of a multi-field update. To record a status message on a write, call setStatus() then call writeFields(). Use status to provide additonal details when writing a channel update. Additonally, status can be used by the ThingTweet App to send a message to Twitter. More...
 
int setTwitterTweet (const char *twitter, const char *tweet)
 Set the Twitter account and message to use for an update to be tweeted. To send a message to twitter call setTwitterTweet() then call writeFields() More...
 
int setTwitterTweet (String twitter, const char *tweet)
 Set the Twitter account and message to use for an update to be tweeted. To send a message to twitter call setTwitterTweet() then call writeFields() More...
 
int setTwitterTweet (const char *twitter, String tweet)
 Set the Twitter account and message to use for an update to be tweeted. To send a message to twitter call setTwitterTweet() then call writeFields() More...
 
int setTwitterTweet (String twitter, String tweet)
 Set the Twitter account and message to use for an update to be tweeted. To send a message to twitter call setTwitterTweet() then call writeFields() More...
 
int setCreatedAt (const char *createdAt)
 Set the created-at date of a multi-field update. To record created-at of a write, call setField() for each of the fields you want to write, setCreatedAt(), and then call writeFields() More...
 
int setCreatedAt (String createdAt)
 Set the created-at date of a multi-field update. To record created-at of a write, call setField() for each of the fields you want to write, setCreatedAt(), and then call writeFields() More...
 
int writeFields (unsigned long channelNumber, const char *writeAPIKey)
 Write a multi-field update. Call setField() for each of the fields you want to write, setLatitude() / setLongitude() / setElevation(), and then call writeFields() More...
 
int writeRaw (unsigned long channelNumber, const char *postMessage, const char *writeAPIKey)
 Write a raw POST to a ThingSpeak channel. More...
 
int writeRaw (unsigned long channelNumber, String postMessage, const char *writeAPIKey)
 Write a raw POST to a ThingSpeak channel. More...
 
String readStringField (unsigned long channelNumber, unsigned int field, const char *readAPIKey)
 Read the latest string from a private ThingSpeak channel. More...
 
String readStringField (unsigned long channelNumber, unsigned int field)
 Read the latest string from a public ThingSpeak channel. More...
 
float readFloatField (unsigned long channelNumber, unsigned int field, const char *readAPIKey)
 Read the latest float from a private ThingSpeak channel. More...
 
float readFloatField (unsigned long channelNumber, unsigned int field)
 Read the latest float from a public ThingSpeak channel. More...
 
long readLongField (unsigned long channelNumber, unsigned int field, const char *readAPIKey)
 Read the latest long from a private ThingSpeak channel. More...
 
long readLongField (unsigned long channelNumber, unsigned int field)
 Read the latest long from a public ThingSpeak channel. More...
 
int readIntField (unsigned long channelNumber, unsigned int field, const char *readAPIKey)
 Read the latest int from a private ThingSpeak channel. More...
 
int readIntField (unsigned long channelNumber, unsigned int field)
 Read the latest int from a public ThingSpeak channel. More...
 
String readStatus (unsigned long channelNumber, const char *readAPIKey)
 Read the latest status from a private ThingSpeak channel. More...
 
String readStatus (unsigned long channelNumber)
 Read the latest status from a public ThingSpeak channel. More...
 
String readCreatedAt (unsigned long channelNumber, const char *readAPIKey)
 Read the created-at timestamp associated with the latest update to a private ThingSpeak channel. More...
 
String readCreatedAt (unsigned long channelNumber)
 Read the created-at timestamp associated with the latest update to a private ThingSpeak channel. More...
 
String readRaw (unsigned long channelNumber, String URLSuffix)
 Read a raw response from a public ThingSpeak channel. More...
 
String readRaw (unsigned long channelNumber, String URLSuffix, const char *readAPIKey)
 Read a raw response from a private ThingSpeak channel. More...
 
int getLastReadStatus ()
 Get the status of the previous read. More...
 

Detailed Description

Enables an Arduino, ESP8266 or other compatible hardware to write or read data to or from ThingSpeak, an open data platform for the Internet of Things with MATLAB analytics and visualization.

Member Function Documentation

bool begin ( Client &  client,
const char *  customHostName,
unsigned int  port 
)

Initializes the ThingSpeak library and network settings using a custom installation of ThingSpeak.

Parameters
clientEthernetClient, YunClient, TCPClient, or WiFiClient created earlier in the sketch
customHostNameHost name of a custom install of ThingSpeak
portPort number to use with a custom install of ThingSpeak
Returns
Always returns true This does not validate the information passed in, or generate any calls to ThingSpeak.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;
#include "ThingSpeak.h"
void setup() {
Ethernet.begin(mac);
ThingSpeak.begin(client,"api.thingspeak.com", 80);
}
bool begin ( Client &  client,
IPAddress  customIP,
unsigned int  port 
)

Initializes the ThingSpeak library and network settings using a custom installation of ThingSpeak.

Parameters
clientEthernetClient, YunClient, TCPClient, or WiFiClient created earlier in the sketch
customIPIP address of a custom install of ThingSpeak
portPort number to use with a custom install of ThingSpeak
Returns
Always returns true This does not validate the information passed in, or generate any calls to ThingSpeak.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;
#include "ThingSpeak.h"
void setup() {
Ethernet.begin(mac);
ThingSpeak.begin(client,IPAddress(184,106,153,149), 80);
}
bool begin ( Client &  client)

Initializes the ThingSpeak library and network settings using the ThingSpeak.com service.

Parameters
clientEthernetClient, YunClient, TCPClient, or WiFiClient created earlier in the sketch
Returns
Always returns true This does not validate the information passed in, or generate any calls to ThingSpeak.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;
#include "ThingSpeak.h"
void setup() {
Ethernet.begin(mac);
ThingSpeak.begin(client);
}
int getLastReadStatus ( )

Get the status of the previous read.

Returns
Generally, these are HTTP status codes. Negative values indicate an error generated by the library. Possible response codes:
  • 200: OK / Success
  • 404: Incorrect API key (or invalid ThingSpeak server address)
  • -101: Value is out of range or string is too long (> 255 characters)
  • -201: Invalid field number specified
  • -210: setField() was not called before writeFields()
  • -301: Failed to connect to ThingSpeak
  • -302: Unexpected failure during write to ThingSpeak
  • -303: Unable to parse response
  • -304: Timeout waiting for server to respond
  • -401: Point was not inserted (most probable cause is the rate limit of once every 15 seconds)
Remarks
The read functions will return zero or empty if there is an error. Use this function to retrieve the details.
void loop() {
String message = ThingSpeak.readStringField(myChannelNumber, 1);
int resultCode = ThingSpeak.getLastReadStatus();
if(resultCode == 200)
{
Serial.print("Latest message is: ");
Serial.println(message);
}
else
{
Serial.print("Error reading message. Status was: ");
Serial.println(resultCode);
}
delay(30000);
}
String readCreatedAt ( unsigned long  channelNumber,
const char *  readAPIKey 
)

Read the created-at timestamp associated with the latest update to a private ThingSpeak channel.

Parameters
channelNumberChannel number
readAPIKeyRead API key associated with the channel. If you share code with others, do not share this key
Returns
Value read (UTF8 string). An empty string is returned if there was no created-at timestamp written to the channel or in case of an error. Use getLastReadStatus() to get more specific information.
void loop() {
String value = ThingSpeak.readCreatedAt(myChannelNumber);
Serial.print("Latest update timestamp is: ");
Serial.print(value);
delay(30000);
}
String readCreatedAt ( unsigned long  channelNumber)

Read the created-at timestamp associated with the latest update to a private ThingSpeak channel.

Parameters
channelNumberChannel number
Returns
Value read (UTF8 string). An empty string is returned if there was no created-at timestamp written to the channel or in case of an error. Use getLastReadStatus() to get more specific information.
void loop() {
String value = ThingSpeak.readCreatedAt(myChannelNumber);
Serial.print("Latest update timestamp is: ");
Serial.print(value);
delay(30000);
}
float readFloatField ( unsigned long  channelNumber,
unsigned int  field,
const char *  readAPIKey 
)

Read the latest float from a private ThingSpeak channel.

Parameters
channelNumberChannel number
fieldField number (1-8) within the channel to read from.
readAPIKeyRead API key associated with the channel. If you share code with others, do not share this key
Returns
Value read, or 0 if the field is text or there is an error. Use getLastReadStatus() to get more specific information. Note that NAN, INFINITY, and -INFINITY are valid results.
void loop() {
float voltage = ThingSpeak.readFloatField(myChannelNumber, 1, myReadAPIKey);
Serial.print("Latest voltage is: ");
Serial.print(voltage);
Serial.println("V");
delay(30000);
}
float readFloatField ( unsigned long  channelNumber,
unsigned int  field 
)

Read the latest float from a public ThingSpeak channel.

Parameters
channelNumberChannel number
fieldField number (1-8) within the channel to read from.
Returns
Value read, or 0 if the field is text or there is an error. Use getLastReadStatus() to get more specific information. Note that NAN, INFINITY, and -INFINITY are valid results.
void loop() {
float voltage = ThingSpeak.readFloatField(myChannelNumber, 1);
Serial.print("Latest voltage is: ");
Serial.print(voltage);
Serial.println("V");
delay(30000);
}
int readIntField ( unsigned long  channelNumber,
unsigned int  field,
const char *  readAPIKey 
)

Read the latest int from a private ThingSpeak channel.

Parameters
channelNumberChannel number
fieldField number (1-8) within the channel to read from.
readAPIKeyRead API key associated with the channel. If you share code with others, do not share this key
Returns
Value read, or 0 if the field is text or there is an error. Use getLastReadStatus() to get more specific information.
Remarks
If the value returned is out of range for an int, the result is undefined.
void loop() {
int value = ThingSpeak.readIntField(myChannelNumber, 1, myReadAPIKey);
Serial.print("Latest value is: ");
Serial.print(value);
delay(30000);
}
int readIntField ( unsigned long  channelNumber,
unsigned int  field 
)

Read the latest int from a public ThingSpeak channel.

Parameters
channelNumberChannel number
fieldField number (1-8) within the channel to read from.
Returns
Value read, or 0 if the field is text or there is an error. Use getLastReadStatus() to get more specific information.
Remarks
If the value returned is out of range for an int, the result is undefined.
void loop() {
int value = ThingSpeak.readIntField(myChannelNumber, 1);
Serial.print("Latest value is: ");
Serial.print(value);
delay(30000);
}
long readLongField ( unsigned long  channelNumber,
unsigned int  field,
const char *  readAPIKey 
)

Read the latest long from a private ThingSpeak channel.

Parameters
channelNumberChannel number
fieldField number (1-8) within the channel to read from.
readAPIKeyRead API key associated with the channel. If you share code with others, do not share this key
Returns
Value read, or 0 if the field is text or there is an error. Use getLastReadStatus() to get more specific information.
void loop() {
long value = ThingSpeak.readLongField(myChannelNumber, 1, myReadAPIKey);
Serial.print("Latest value is: ");
Serial.print(value);
delay(30000);
}
long readLongField ( unsigned long  channelNumber,
unsigned int  field 
)

Read the latest long from a public ThingSpeak channel.

Parameters
channelNumberChannel number
fieldField number (1-8) within the channel to read from.
Returns
Value read, or 0 if the field is text or there is an error. Use getLastReadStatus() to get more specific information.
void loop() {
long value = ThingSpeak.readLongField(myChannelNumber, 1);
Serial.print("Latest value is: ");
Serial.print(value);
delay(30000);
}
String readRaw ( unsigned long  channelNumber,
String  URLSuffix 
)

Read a raw response from a public ThingSpeak channel.

Parameters
channelNumberChannel number
URLSuffixRaw URL to write to ThingSpeak as a String. See the documentation at https://thingspeak.com/docs/channels#get_feed
Returns
Response if successful, or empty string. Use getLastReadStatus() to get more specific information.
Remarks
This is low level functionality that will not be required by most users.
void loop() {
String response = ThingSpeak.readRaw(myChannelNumber, String("feeds/days=1"));
Serial.print("Response: ");
Serial.print(response);
delay(30000);
}
String readRaw ( unsigned long  channelNumber,
String  URLSuffix,
const char *  readAPIKey 
)

Read a raw response from a private ThingSpeak channel.

Parameters
channelNumberChannel number
URLSuffixRaw URL to write to ThingSpeak as a String. See the documentation at https://thingspeak.com/docs/channels#get_feed
readAPIKeyRead API key associated with the channel. If you share code with others, do not share this key
Returns
Response if successful, or empty string. Use getLastReadStatus() to get more specific information.
Remarks
This is low level functionality that will not be required by most users.
void loop() {
String response = ThingSpeak.readRaw(myChannelNumber, String("feeds/days=1"), myReadAPIKey);
Serial.print("Response: ");
Serial.print(response);
delay(30000);
}
String readStatus ( unsigned long  channelNumber,
const char *  readAPIKey 
)

Read the latest status from a private ThingSpeak channel.

Parameters
channelNumberChannel number
readAPIKeyRead API key associated with the channel. If you share code with others, do not share this key
Returns
Value read (UTF8 string). An empty string is returned if there was no status written to the channel or in case of an error. Use getLastReadStatus() to get more specific information.
void loop() {
String value = ThingSpeak.readStatus(myChannelNumber, myReadAPIKey);
Serial.print("Latest status is: ");
Serial.print(value);
delay(30000);
}
String readStatus ( unsigned long  channelNumber)

Read the latest status from a public ThingSpeak channel.

Parameters
channelNumberChannel number
Returns
Value read (UTF8 string). An empty string is returned if there was no status written to the channel or in case of an error. Use getLastReadStatus() to get more specific information.
void loop() {
String value = ThingSpeak.readStatus(myChannelNumber, myReadAPIKey);
Serial.print("Latest status is: ");
Serial.print(value);
delay(30000);
}
String readStringField ( unsigned long  channelNumber,
unsigned int  field,
const char *  readAPIKey 
)

Read the latest string from a private ThingSpeak channel.

Parameters
channelNumberChannel number
fieldField number (1-8) within the channel to read from.
readAPIKeyRead API key associated with the channel. If you share code with others, do not share this key
Returns
Value read (UTF8 string), or empty string if there is an error. Use getLastReadStatus() to get more specific information.
void loop() {
String message = ThingSpeak.readStringField(myChannelNumber, 1, myReadAPIKey);
Serial.print("Latest message is: ");
Serial.println(message);
delay(30000);
}
String readStringField ( unsigned long  channelNumber,
unsigned int  field 
)

Read the latest string from a public ThingSpeak channel.

Parameters
channelNumberChannel number
fieldField number (1-8) within the channel to read from.
Returns
Value read (UTF8), or empty string if there is an error. Use getLastReadStatus() to get more specific information.
void loop() {
String message = ThingSpeak.readStringField(myChannelNumber, 1);
Serial.print("Latest message is: ");
Serial.println(message);
delay(30000);
}
int setCreatedAt ( const char *  createdAt)

Set the created-at date of a multi-field update. To record created-at of a write, call setField() for each of the fields you want to write, setCreatedAt(), and then call writeFields()

Parameters
createdAtDesired timestamp to be included with the channel update as a String. The timestamp string must be in the ISO 8601 format. Example "2017-01-12 13:22:54"
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
Remarks
Timezones can be set using the timezone hour offset parameter. For example, a timestamp for Eastern Standard Time is: "2017-01-12 13:22:54-05". If no timezone hour offset parameter is used, UTC time is assumed.
See also
setField(), writeFields()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, sensor3Meaning);
ThingSpeak.setField(4, timeRead);
ThingSpeak.setCreatedAt("2017-01-06T13:56:28");
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setCreatedAt ( String  createdAt)

Set the created-at date of a multi-field update. To record created-at of a write, call setField() for each of the fields you want to write, setCreatedAt(), and then call writeFields()

Parameters
createdAtDesired timestamp to be included with the channel update as a String. The timestamp string must be in the ISO 8601 format. Example "2017-01-12 13:22:54"
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
Remarks
Timezones can be set using the timezone hour offset parameter. For example, a timestamp for Eastern Standard Time is: "2017-01-12 13:22:54-05". If no timezone hour offset parameter is used, UTC time is assumed.
See also
setField(), writeFields()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, sensor3Meaning);
ThingSpeak.setField(4, timeRead);
ThingSpeak.setCreatedAt("2017-01-06T13:56:28");
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setElevation ( float  elevation)

Set the elevation of a multi-field update. To record latitude, longitude and elevation of a write, call setField() for each of the fields you want to write, setLatitude() / setLongitude() / setElevation(), and then call writeFields()

Parameters
elevationElevation of the measurement (meters above sea level)
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
See also
setField(), setLatitude(), setLongitude(), writeFields()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, sensor3Meaning);
ThingSpeak.setField(4, timeRead);
setLatitude(42.2833);
setLongitude(-71.3500);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setField ( unsigned int  field,
int  value 
)

Set the value of a single field that will be part of a multi-field update. To write multiple fields at once, call setField() for each of the fields you want to write, and then call writeFields()

Parameters
fieldField number (1-8) within the channel to set
valueInteger value (from -32,768 to 32,767) to set.
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
See also
setLatitude(), setLongitude(), setElevation(), writeFields()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, sensor3Meaning);
ThingSpeak.setField(4, timeRead);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setField ( unsigned int  field,
long  value 
)

Set the value of a single field that will be part of a multi-field update. To write multiple fields at once, call setField() for each of the fields you want to write, and then call writeFields()

Parameters
fieldField number (1-8) within the channel to set
valueLong value (from -2,147,483,648 to 2,147,483,647) to write.
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
See also
setLatitude(), setLongitude(), setElevation(), writeFields()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, sensor3Meaning);
ThingSpeak.setField(4, timeRead);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setField ( unsigned int  field,
float  value 
)

Set the value of a single field that will be part of a multi-field update. To write multiple fields at once, call setField() for each of the fields you want to write, and then call writeFields()

Parameters
fieldField number (1-8) within the channel to set
valueFloating point value (from -999999000000 to 999999000000) to write. If you need more accuracy, or a wider range, you should format the number yourself (using dtostrf) and setField() using the resulting string.
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
See also
setLatitude(), setLongitude(), setElevation(), writeFields()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, sensor3Meaning);
ThingSpeak.setField(4, timeRead);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setField ( unsigned int  field,
const char *  value 
)

Set the value of a single field that will be part of a multi-field update. To write multiple fields at once, call setField() for each of the fields you want to write, and then call writeFields()

Parameters
fieldField number (1-8) within the channel to set
valueString to write (UTF8). ThingSpeak limits this to 255 bytes.
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
See also
setLatitude(), setLongitude(), setElevation(), writeFields()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, sensor3Meaning);
ThingSpeak.setField(4, timeRead);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setField ( unsigned int  field,
String  value 
)

Set the value of a single field that will be part of a multi-field update. To write multiple fields at once, call setField() for each of the fields you want to write, and then call writeFields()

Parameters
fieldField number (1-8) within the channel to set
valueString to write (UTF8). ThingSpeak limits this to 255 bytes.
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
See also
setLatitude(), setLongitude(), setElevation(), writeFields()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, sensor3Meaning);
ThingSpeak.setField(4, timeRead);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setLatitude ( float  latitude)

Set the latitude of a multi-field update. To record latitude, longitude and elevation of a write, call setField() for each of the fields you want to write, setLatitude() / setLongitude() / setElevation(), and then call writeFields()

Parameters
latitudeLatitude of the measurement (degrees N, use negative values for degrees S)
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
See also
setField(), setLongitude(), setElevation(), writeFields()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, sensor3Meaning);
ThingSpeak.setField(4, timeRead);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
setLatitude(42.2833);
setLongitude(-71.3500);
delay(20000);
}
int setLongitude ( float  longitude)

Set the longitude of a multi-field update. To record latitude, longitude and elevation of a write, call setField() for each of the fields you want to write, setLatitude() / setLongitude() / setElevation(), and then call writeFields()

Parameters
longitudeLongitude of the measurement (degrees E, use negative values for degrees W)
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
See also
setField(), setLatitude(), setElevation(), writeFields()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, sensor3Meaning);
ThingSpeak.setField(4, timeRead);
setLatitude(42.2833);
setLongitude(-71.3500);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setStatus ( const char *  status)

Set the status of a multi-field update. To record a status message on a write, call setStatus() then call writeFields(). Use status to provide additonal details when writing a channel update. Additonally, status can be used by the ThingTweet App to send a message to Twitter.

Parameters
statusString to write (UTF8). ThingSpeak limits this to 255 bytes.
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
See also
writeFields()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, timeRead);
ThingSpeak.setStatus(sensor3Meaning);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setStatus ( String  status)

Set the status of a multi-field update. To record a status message on a write, call setStatus() then call writeFields(). Use status to provide additonal details when writing a channel update. Additonally, status can be used by the ThingTweet App to send a message to Twitter.

Parameters
statusString to write (UTF8). ThingSpeak limits this to 255 bytes.
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
See also
writeFields()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, timeRead);
ThingSpeak.setStatus(sensor3Meaning);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setTwitterTweet ( const char *  twitter,
const char *  tweet 
)

Set the Twitter account and message to use for an update to be tweeted. To send a message to twitter call setTwitterTweet() then call writeFields()

Parameters
twitterTwitter account name as a String.
tweetTwitter message as a String (UTF-8) limited to 140 character.
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
Remarks
Prior to using this feature, a twitter account must be linked to your ThingSpeak account. Do this by logging into ThingSpeak and going to Apps, then ThingTweet and clicking Link Twitter Account.
See also
writeFields(),getLastReadStatus()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, timeRead);
ThingSpeak.setTwitterTweet("YourTwitterAccountName",sensor3Meaning);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setTwitterTweet ( String  twitter,
const char *  tweet 
)

Set the Twitter account and message to use for an update to be tweeted. To send a message to twitter call setTwitterTweet() then call writeFields()

Parameters
twitterTwitter account name as a String.
tweetTwitter message as a String (UTF-8) limited to 140 character.
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
Remarks
Prior to using this feature, a twitter account must be linked to your ThingSpeak account. Do this by logging into ThingSpeak and going to Apps, then ThingTweet and clicking Link Twitter Account.
See also
writeFields(),getLastReadStatus()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, timeRead);
ThingSpeak.setTwitterTweet("YourTwitterAccountName",sensor3Meaning);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setTwitterTweet ( const char *  twitter,
String  tweet 
)

Set the Twitter account and message to use for an update to be tweeted. To send a message to twitter call setTwitterTweet() then call writeFields()

Parameters
twitterTwitter account name as a String.
tweetTwitter message as a String (UTF-8) limited to 140 character.
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
Remarks
Prior to using this feature, a twitter account must be linked to your ThingSpeak account. Do this by logging into ThingSpeak and going to Apps, then ThingTweet and clicking Link Twitter Account.
See also
writeFields(),getLastReadStatus()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, timeRead);
ThingSpeak.setTwitterTweet("YourTwitterAccountName",sensor3Meaning);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int setTwitterTweet ( String  twitter,
String  tweet 
)

Set the Twitter account and message to use for an update to be tweeted. To send a message to twitter call setTwitterTweet() then call writeFields()

Parameters
twitterTwitter account name as a String.
tweetTwitter message as a String (UTF-8) limited to 140 character.
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
Remarks
Prior to using this feature, a twitter account must be linked to your ThingSpeak account. Do this by logging into ThingSpeak and going to Apps, then ThingTweet and clicking Link Twitter Account.
See also
writeFields(),getLastReadStatus()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, timeRead);
ThingSpeak.setTwitterTweet("YourTwitterAccountName",sensor3Meaning);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int writeField ( unsigned long  channelNumber,
unsigned int  field,
int  value,
const char *  writeAPIKey 
)

Write an integer value to a single field in a ThingSpeak channel.

Parameters
channelNumberChannel number
fieldField number (1-8) within the channel to write to.
valueInteger value (from -32,768 to 32,767) to write.
writeAPIKeyWrite API key associated with the channel. If you share code with others, do not share this key
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
Remarks
Visit https://thingspeak.com/docs/channels for more information about channels, API keys, and fields. ThingSpeak limits the number of writes to a channel to once every 15 seconds.
void loop() {
int sensorValue = analogRead(A0);
ThingSpeak.writeField(myChannelNumber, 1, sensorValue, myWriteAPIKey);
delay(20000);
}
int writeField ( unsigned long  channelNumber,
unsigned int  field,
long  value,
const char *  writeAPIKey 
)

Write a long value to a single field in a ThingSpeak channel.

Parameters
channelNumberChannel number
fieldField number (1-8) within the channel to write to.
valueLong value (from -2,147,483,648 to 2,147,483,647) to write.
writeAPIKeyWrite API key associated with the channel. If you share code with others, do not share this key
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
Remarks
Visit https://thingspeak.com/docs/channels for more information about channels, API keys, and fields. ThingSpeak limits the number of writes to a channel to once every 15 seconds.
void loop() {
int sensorValue = analogRead(A0);
ThingSpeak.writeField(myChannelNumber, 1, sensorValue, myWriteAPIKey);
delay(20000);
}
int writeField ( unsigned long  channelNumber,
unsigned int  field,
float  value,
const char *  writeAPIKey 
)

Write a floating point value to a single field in a ThingSpeak channel.

Parameters
channelNumberChannel number
fieldField number (1-8) within the channel to write to.
valueFloating point value (from -999999000000 to 999999000000) to write. If you need more accuracy, or a wider range, you should format the number using dtostrf and writeField().
writeAPIKeyWrite API key associated with the channel. If you share code with others, do not share this key
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
Remarks
Visit https://thingspeak.com/docs/channels for more information about channels, API keys, and fields. ThingSpeak limits the number of writes to a channel to once every 15 seconds.
void loop() {
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
ThingSpeak.writeField(myChannelNumber, 1, voltage, myWriteAPIKey);
delay(20000);
}
int writeField ( unsigned long  channelNumber,
unsigned int  field,
const char *  value,
const char *  writeAPIKey 
)

Write a string to a single field in a ThingSpeak channel.

Parameters
channelNumberChannel number
fieldField number (1-8) within the channel to write to.
valueString to write (UTF8 string). ThingSpeak limits this field to 255 bytes.
writeAPIKeyWrite API key associated with the channel. If you share code with others, do not share this key
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
Remarks
Visit https://thingspeak.com/docs/channels for more information about channels, API keys, and fields. ThingSpeak limits the number of writes to a channel to once every 15 seconds.
void loop() {
int sensorValue = analogRead(A0);
if (sensorValue > 512) {
ThingSpeak.writeField(myChannelNumber, 1, "High", myWriteAPIKey);
}
else {
ThingSpeak.writeField(myChannelNumber, 1, "Low", myWriteAPIKey);
}
delay(20000);
}
int writeField ( unsigned long  channelNumber,
unsigned int  field,
String  value,
const char *  writeAPIKey 
)

Write a String to a single field in a ThingSpeak channel.

Parameters
channelNumberChannel number
fieldField number (1-8) within the channel to write to.
valueCharacter array (zero terminated) to write (UTF8). ThingSpeak limits this field to 255 bytes.
writeAPIKeyWrite API key associated with the channel. If you share code with others, do not share this key
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
Remarks
Visit https://thingspeak.com/docs/channels for more information about channels, API keys, and fields. ThingSpeak limits the number of writes to a channel to once every 15 seconds.
void loop() {
int sensorValue = analogRead(A0);
String meaning;
if (sensorValue < 400) {
meaning = String("Too Cold!");
} else if (sensorValue > 600) {
meaning = String("Too Hot!");
} else {
meaning = String("Just Right");
}
ThingSpeak.writeField(myChannelNumber, 1, meaning, myWriteAPIKey);
delay(20000);
}
int writeFields ( unsigned long  channelNumber,
const char *  writeAPIKey 
)

Write a multi-field update. Call setField() for each of the fields you want to write, setLatitude() / setLongitude() / setElevation(), and then call writeFields()

Parameters
channelNumberChannel number
writeAPIKeyWrite API key associated with the channel. If you share code with others, do not share this key
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
See also
setField(), setLatitude(), setLongitude(), setElevation()
void loop() {
int sensor1Value = analogRead(A0);
float sensor2Voltage = analogRead(A1) * (5.0 / 1023.0);
String sensor3Meaning;
int sensor3Value = analogRead(A2);
if (sensor3Value < 400) {
sensor3Meaning = String("Too Cold!");
} else if (sensor3Value > 600) {
sensor3Meaning = String("Too Hot!");
} else {
sensor3Meaning = String("Just Right");
}
long timeRead = millis();
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, sensor3Meaning);
ThingSpeak.setField(4, timeRead);
setLatitude(42.2833);
setLongitude(-71.3500);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int writeRaw ( unsigned long  channelNumber,
const char *  postMessage,
const char *  writeAPIKey 
)

Write a raw POST to a ThingSpeak channel.

Parameters
channelNumberChannel number
postMessageRaw URL to write to ThingSpeak as a string. See the documentation at https://thingspeak.com/docs/channels#update_feed.
writeAPIKeyWrite API key associated with the channel. If you share code with others, do not share this key
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
Remarks
This is low level functionality that will not be required by most users.
void loop() {
const char postMessage[] = "field1=23&created_at=2014-12-31%2023:59:59";
ThingSpeak.setField(1, sensor1Value);
ThingSpeak.setField(2, sensor2Voltage);
ThingSpeak.setField(3, sensor3Meaning);
ThingSpeak.setField(4, timeRead);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(20000);
}
int writeRaw ( unsigned long  channelNumber,
String  postMessage,
const char *  writeAPIKey 
)

Write a raw POST to a ThingSpeak channel.

Parameters
channelNumberChannel number
postMessageRaw URL to write to ThingSpeak as a String. See the documentation at https://thingspeak.com/docs/channels#update_feed.
writeAPIKeyWrite API key associated with the channel. If you share code with others, do not share this key
Returns
HTTP status code of 200 if successful. See getLastReadStatus() for other possible return values.
Remarks
This is low level functionality that will not be required by most users.
void loop() {
String postMessage = String("field1=23&created_at=2014-12-31%2023:59:59");
ThingSpeak.writeRaw(myChannelNumber, postMessage, myWriteAPIKey);
delay(20000);
}