How to Send Data to ThingSpeak from Arduino Easily
To send data to
ThingSpeak from an Arduino, connect your Arduino to the internet using a WiFi or Ethernet module, then use HTTP GET requests to update your ThingSpeak channel with sensor values. Use the WiFiClient or EthernetClient library to send data by calling the ThingSpeak API URL with your API key and field data.Syntax
Sending data to ThingSpeak involves making an HTTP GET request to a URL with your API key and data fields. The general URL format is:
http://api.thingspeak.com/update?api_key=YOUR_API_KEY&field1=VALUE1&field2=VALUE2
Here:
- api_key: Your unique write API key from ThingSpeak channel.
- field1, field2, ...: The data fields you want to update.
- VALUE1, VALUE2, ...: The sensor or variable values you send.
In Arduino code, you use a client object (like WiFiClient) to connect to api.thingspeak.com on port 80 and send this GET request as plain text.
arduino
client.connect("api.thingspeak.com", 80); client.print("GET /update?api_key=YOUR_API_KEY&field1=" + String(value1) + " HTTP/1.1\r\n"); client.print("Host: api.thingspeak.com\r\n"); client.print("Connection: close\r\n\r\n");
Example
This example shows how to send a temperature value to ThingSpeak using an ESP8266 WiFi module with Arduino. It connects to WiFi, reads a dummy temperature, and updates ThingSpeak every 20 seconds.
arduino
#include <ESP8266WiFi.h> const char* ssid = "YOUR_WIFI_SSID"; const char* password = "YOUR_WIFI_PASSWORD"; const char* host = "api.thingspeak.com"; const char* apiKey = "YOUR_THINGSPEAK_WRITE_API_KEY"; WiFiClient client; void setup() { Serial.begin(115200); delay(10); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { float temperature = 25.0; // Example sensor value Serial.print("Connecting to "); Serial.println(host); if (!client.connect(host, 80)) { Serial.println("Connection failed"); return; } String url = "/update?api_key=" + String(apiKey) + "&field1=" + String(temperature); Serial.print("Requesting URL: "); Serial.println(url); client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); unsigned long timeout = millis(); while (client.available() == 0) { if (millis() - timeout > 5000) { Serial.println(">>> Client Timeout !"); client.stop(); return; } } while(client.available()) { String line = client.readStringUntil('\r'); Serial.print(line); } Serial.println(); Serial.println("Data sent to ThingSpeak"); delay(20000); // Wait 20 seconds to update again }
Output
HTTP/1.1 200 OK
Data sent to ThingSpeak
Common Pitfalls
- Wrong API Key: Using the read API key instead of the write API key will prevent data updates.
- Not waiting between updates: ThingSpeak limits updates to once every 15 seconds; sending data faster causes errors.
- WiFi connection issues: Not checking WiFi status or failing to connect will stop data sending.
- Incorrect URL format: Missing parameters or wrong field names cause data not to update.
Always check serial output for connection and response status to debug.
arduino
/* Wrong way: Missing API key */ client.print("GET /update?field1=25 HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: close\r\n\r\n"); /* Right way: Include API key */ client.print("GET /update?api_key=YOUR_API_KEY&field1=25 HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: close\r\n\r\n");
Quick Reference
- Use your ThingSpeak channel's write API key to send data.
- Send data with HTTP GET request to
http://api.thingspeak.com/update. - Include data fields as
field1=,field2=, etc. - Wait at least 15 seconds between updates to avoid errors.
- Check WiFi connection before sending data.
Key Takeaways
Connect Arduino to the internet using WiFi or Ethernet before sending data.
Send data to ThingSpeak by making an HTTP GET request with your write API key and field values.
Wait at least 15 seconds between updates to comply with ThingSpeak limits.
Always verify your API key and URL format to avoid errors.
Use serial output to debug connection and data sending issues.