Skip to content

Commit ba74a4a

Browse files
Add files via upload
- Re-organized Examples by connectivity hardware. This was done to simply the example code and make it more readable. - Added support for MKR VIDOR 4000, MKR GSM 1400 and MKR ETH Shield - Shifted documentation to README.md
1 parent 55c8afa commit ba74a4a

File tree

68 files changed

+3578
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+3578
-18
lines changed

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#ThingSpeak Communication Library for Arduino, ESP8266 and ESP32
1+
# ThingSpeak Communication Library for Arduino, ESP8266 and ESP32
22

33
This library enables an Arduino 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.
44

55
Complete technical documentation is in the extras/documentation folder inside the library.
66

77
ThingSpeak offers free data storage and analysis of time-stamped numeric or alphanumeric data. Users can access ThingSpeak by visiting http://thingspeak.com and creating a ThingSpeak user account.
88

9-
ThingSpeak stores data in channels. Channels support an unlimited number of timestamped observations (think of these as rows in a spreadsheet). Each channel has up to 8 fields (think of these as columns in a speadsheet). Check out this [video](https://www.mathworks.com/videos/introduction-to-thingspeak-107749.html) for an overview.
9+
ThingSpeak stores data in channels. Channels support an unlimited number of timestamped observations (think of these as rows in a spreadsheet). Each channel has up to 8 fields (think of these as columns in a speadsheet). Check out this [video](http://www.mathworks.com/videos/introduction-to-thingspeak-107749.html) for an overview.
1010

1111
Channels may be public, where anyone can see the data, or private, where only the owner and select users can read the data. Each channel has an associated Write API Key that is used to control who can write to a channel. In addition, private channels have one or more Read API Keys to control who can read from private channel. An API Key is not required to read from public channels. Each channel can have up to 8 fields. One field is created by default.
1212

@@ -29,20 +29,22 @@ In the Particle Web IDE, click the libraries tab, find ThingSpeak, and choose "I
2929

3030
## Compatible Hardware:
3131

32-
* Arduino/Genuino or compatible using a WiFi101. Note: Use the WiFi101 library version 0.13.0 or older. WiFi101 library versions 0.14.0 and newer have a bug that stops this ThingSpeak library from working properly.
33-
* Arduino/Genuino or compatible using Ethernet shield (we have tested with Uno and Mega).
34-
* Arduino Yun running OpenWRT-Yun Release 1.5.3. Note: November 13th, 2014 or later. There are known issues with earlier versions. Visit this page to get the latest version.
35-
* Arduino MKR1000. Note: Use the WiFi101 library version 0.13.0 or older. WiFi101 library versions 0.14.0 and newer have a bug that stops this ThingSpeak library from working properly.
36-
* ESP8266. Tested with [SparkFun ESP8266 Thing - Dev Board](https://www.sparkfun.com/products/13711) and [NodeMCU 1.0 module](http://www.seeedstudio.com/depot/NodeMCU-v2-Lua-based-ESP8266-development-kit-p-2415.html)
37-
* ESP32. Tested with [SparkFun ESP32 Thing - Dev Board](https://www.sparkfun.com/products/13907)
32+
* Arduino/Genuino or compatible using a WiFi Shield
33+
* Arduino/Genuino or compatible using a WiFi Shield 101
34+
* Arduino/Genuino or compatible using an Ethernet Shield
35+
* Arduino/Genuino or compatible using a MKR ETH Shield
36+
* Arduino MKR1000 (use the WiFi101 library version 0.13.0 or older. WiFi101 library versions 0.14.0 and newer have a bug that stops this ThingSpeak library from working properly)
37+
* Arduino MKR1010
38+
* Arduino VIDOR 4000
39+
* Arduino GSM 14000
40+
* Arduino Yún (Rev1 and Rev2)
41+
* ESP8266 (tested with [SparkFun ESP8266 Thing - Dev Board](https://www.sparkfun.com/products/13711) and [NodeMCU 1.0 module](http://www.seeedstudio.com/depot/NodeMCU-v2-Lua-based-ESP8266-development-kit-p-2415.html))
42+
* ESP32 (tested with [SparkFun ESP32 Thing - Dev Board](https://www.sparkfun.com/products/13907))
3843

3944
## Examples:
4045

41-
The library includes several examples to help you get started. These are accessible in the Examples/ThingSpeak menu off the File menu in the Arduino IDE.
46+
The library includes several examples to help you get started. These are accessible in Examples > ThingSpeak menu of the Arduino IDE.
4247

43-
* **CheerLights:** Reads the latest CheerLights color on ThingSpeak, and sets an RGB LED.
44-
* **ReadLastTemperature:** Reads the latest temperature from the public MathWorks weather station in Natick, MA on ThingSpeak.
45-
* **ReadPrivateChannel:** Reads the latest voltage value from a private channel on ThingSpeak.
46-
* **ReadWeatherStation:** Reads the latest weather data from the public MathWorks weather station in Natick, MA on ThingSpeak.
47-
* **WriteMultipleVoltages:** Reads analog voltages from pins 0-7 and writes them to the 8 fields of a channel on ThingSpeak.
48-
* **WriteVoltage:** Reads an analog voltage from pin 0, converts to a voltage, and writes it to a channel on ThingSpeak.
48+
* **ReadField:** Reading from a public channel and a private channel on ThingSpeak.
49+
* **WriteSingleField:** Writing a value to a single field on ThingSpeak.
50+
* **WriteMultipleFields:** Writing values to multiple fields and status in one transaction with ThingSpeak.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
ReadField
3+
4+
Description: Demonstates reading from a public channel which doesn't not require an API key and reading from a private channel which does requires a read API key.
5+
The value read from the public channel is the current outside temperature at MathWorks headquaters in Natick, MA. The value from the
6+
private channel is an example counter that increments every 10 seconds.
7+
8+
Hardware: Arduino Ethernet
9+
10+
!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
11+
12+
Note:
13+
- Requires the Ethernet library
14+
15+
ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and
16+
analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel.
17+
18+
Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed.
19+
See https://www.mathworks.com/help/thingspeak/index.html for the full ThingSpeak documentation.
20+
21+
For licensing information, see the accompanying license file.
22+
23+
Copyright 2018, The MathWorks, Inc.
24+
*/
25+
26+
#include "ThingSpeak.h"
27+
#include <SPI.h>
28+
#include <Ethernet.h>
29+
#include "secrets.h"
30+
31+
byte mac[] = SECRET_MAC;
32+
33+
// Set the static IP address to use if the DHCP fails to assign
34+
IPAddress ip(192, 168, 0, 177);
35+
IPAddress myDns(192, 168, 0, 1);
36+
37+
EthernetClient client;
38+
39+
// Weather station channel details
40+
unsigned long weatherStationChannelNumber = SECRET_CH_ID_WEATHER_STATION;
41+
unsigned int temperatureFieldNumber = 4;
42+
43+
// Counting channel details
44+
unsigned long counterChannelNumber = SECRET_CH_ID_COUNTER;
45+
const char * myCounterReadAPIKey = SECRET_READ_APIKEY_COUNTER;
46+
unsigned int counterFieldNumber = 1;
47+
48+
void setup() {
49+
Ethernet.init(10); // Most Arduino Ethernet hardware
50+
Serial.begin(115200); //Initialize serial
51+
52+
// start the Ethernet connection:
53+
Serial.println("Initialize Ethernet with DHCP:");
54+
if (Ethernet.begin(mac) == 0) {
55+
Serial.println("Failed to configure Ethernet using DHCP");
56+
// Check for Ethernet hardware present
57+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
58+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
59+
while (true) {
60+
delay(1); // do nothing, no point running without Ethernet hardware
61+
}
62+
}
63+
if (Ethernet.linkStatus() == LinkOFF) {
64+
Serial.println("Ethernet cable is not connected.");
65+
}
66+
// try to congifure using IP address instead of DHCP:
67+
Ethernet.begin(mac, ip, myDns);
68+
} else {
69+
Serial.print(" DHCP assigned IP ");
70+
Serial.println(Ethernet.localIP());
71+
}
72+
// give the Ethernet shield a second to initialize:
73+
delay(1000);
74+
75+
ThingSpeak.begin(client); // Initialize ThingSpeak
76+
}
77+
78+
void loop() {
79+
80+
int statusCode = 0;
81+
82+
// Read in field 4 of the public channel recording the temperature
83+
float temperatureInF = ThingSpeak.readFloatField(weatherStationChannelNumber, temperatureFieldNumber);
84+
85+
// Check the status of the read operation to see if it was successful
86+
statusCode = ThingSpeak.getLastReadStatus();
87+
if(statusCode == 200){
88+
Serial.println("Temperature at MathWorks HQ: " + String(temperatureInF) + " deg F");
89+
}
90+
else{
91+
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
92+
}
93+
94+
delay(15000); // No need to read the temperature too often.
95+
96+
// Read in field 1 of the private channel which is a counter
97+
long count = ThingSpeak.readLongField(counterChannelNumber, counterFieldNumber, myCounterReadAPIKey);
98+
99+
// Check the status of the read operation to see if it was successful
100+
statusCode = ThingSpeak.getLastReadStatus();
101+
if(statusCode == 200){
102+
Serial.println("Counter: " + String(count));
103+
}
104+
else{
105+
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
106+
}
107+
108+
delay(15000); // No need to read the counter too often.
109+
110+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Use this file to store all of the private credentials
2+
// and connection details
3+
4+
// Enter a MAC address for your controller below.
5+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
6+
#define SECRET_MAC {0x90, 0xA2, 0xDA, 0x10, 0x40, 0x4F}
7+
8+
#define SECRET_CH_ID_WEATHER_STATION 12397 //MathWorks weather station
9+
10+
#define SECRET_CH_ID_COUNTER 298725 //Test channel for counting
11+
#define SECRET_READ_APIKEY_COUNTER "SODG0O2UZVGKWAWG" //API Key for Test channel
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
WriteMultipleFields
3+
4+
Description: Writes values to fields 1,2,3,4 and status in a single ThingSpeak update every 20 seconds.
5+
6+
Hardware: Arduino Ethernet
7+
8+
!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
9+
10+
Note:
11+
- Requires the Ethernet library
12+
13+
ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and
14+
analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel.
15+
16+
Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed.
17+
See https://www.mathworks.com/help/thingspeak/index.html for the full ThingSpeak documentation.
18+
19+
For licensing information, see the accompanying license file.
20+
21+
Copyright 2018, The MathWorks, Inc.
22+
*/
23+
24+
25+
#include "ThingSpeak.h"
26+
#include <SPI.h>
27+
#include <Ethernet.h>
28+
#include "secrets.h"
29+
30+
byte mac[] = SECRET_MAC;
31+
32+
// Set the static IP address to use if the DHCP fails to assign
33+
IPAddress ip(192, 168, 0, 177);
34+
IPAddress myDns(192, 168, 0, 1);
35+
36+
EthernetClient client;
37+
38+
unsigned long myChannelNumber = SECRET_CH_ID;
39+
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
40+
41+
// Initialize our values
42+
int number1 = 0;
43+
int number2 = random(0,100);
44+
int number3 = random(0,100);
45+
int number4 = random(0,100);
46+
String myStatus = "";
47+
48+
void setup() {
49+
Ethernet.init(10); // Most Arduino Ethernet hardware
50+
Serial.begin(115200); //Initialize serial
51+
52+
// start the Ethernet connection:
53+
Serial.println("Initialize Ethernet with DHCP:");
54+
if (Ethernet.begin(mac) == 0) {
55+
Serial.println("Failed to configure Ethernet using DHCP");
56+
// Check for Ethernet hardware present
57+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
58+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
59+
while (true) {
60+
delay(1); // do nothing, no point running without Ethernet hardware
61+
}
62+
}
63+
if (Ethernet.linkStatus() == LinkOFF) {
64+
Serial.println("Ethernet cable is not connected.");
65+
}
66+
// try to congifure using IP address instead of DHCP:
67+
Ethernet.begin(mac, ip, myDns);
68+
} else {
69+
Serial.print(" DHCP assigned IP ");
70+
Serial.println(Ethernet.localIP());
71+
}
72+
// give the Ethernet shield a second to initialize:
73+
delay(1000);
74+
75+
ThingSpeak.begin(client); // Initialize ThingSpeak
76+
}
77+
78+
void loop() {
79+
80+
// set the fields with the values
81+
ThingSpeak.setField(1, number1);
82+
ThingSpeak.setField(2, number2);
83+
ThingSpeak.setField(3, number3);
84+
ThingSpeak.setField(4, number4);
85+
86+
// figure out the status message
87+
if(number1 > number2){
88+
myStatus = String("field1 is greater than field2");
89+
}
90+
else if(number1 < number2){
91+
myStatus = String("field1 is less than field2");
92+
}
93+
else{
94+
myStatus = String("field1 equals field2");
95+
}
96+
97+
// set the status
98+
ThingSpeak.setStatus(myStatus);
99+
100+
// write to the ThingSpeak channel
101+
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
102+
if(x == 200){
103+
Serial.println("Channel update successful.");
104+
}
105+
else{
106+
Serial.println("Problem updating channel. HTTP error code " + String(x));
107+
}
108+
109+
// change the values
110+
number1++;
111+
if(number1 > 99){
112+
number1 = 0;
113+
}
114+
number2 = random(0,100);
115+
number3 = random(0,100);
116+
number4 = random(0,100);
117+
118+
delay(20000); // Wait 20 seconds to update the channel again
119+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Use this file to store all of the private credentials
2+
// and connection details
3+
4+
// Enter a MAC address for your controller below.
5+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
6+
#define SECRET_MAC {0x90, 0xA2, 0xDA, 0x10, 0x40, 0x4F}
7+
8+
#define SECRET_CH_ID 000000 // replace 0000000 with your channel number
9+
#define SECRET_WRITE_APIKEY "XYZ" // replace XYZ with your channel write API Key
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
WriteSingleField
3+
4+
Description: Writes a value to a channel on ThingSpeak every 20 seconds.
5+
6+
Hardware: Arduino Ethernet
7+
8+
!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
9+
10+
Note:
11+
- Requires the Ethernet library
12+
13+
ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and
14+
analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel.
15+
16+
Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed.
17+
See https://www.mathworks.com/help/thingspeak/index.html for the full ThingSpeak documentation.
18+
19+
For licensing information, see the accompanying license file.
20+
21+
Copyright 2018, The MathWorks, Inc.
22+
*/
23+
24+
25+
#include "ThingSpeak.h"
26+
#include <SPI.h>
27+
#include <Ethernet.h>
28+
#include "secrets.h"
29+
30+
byte mac[] = SECRET_MAC;
31+
32+
// Set the static IP address to use if the DHCP fails to assign
33+
IPAddress ip(192, 168, 0, 177);
34+
IPAddress myDns(192, 168, 0, 1);
35+
36+
EthernetClient client;
37+
38+
unsigned long myChannelNumber = SECRET_CH_ID;
39+
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
40+
41+
int number = 0;
42+
43+
void setup() {
44+
Ethernet.init(10); // Most Arduino Ethernet hardware
45+
Serial.begin(115200); //Initialize serial
46+
47+
// start the Ethernet connection:
48+
Serial.println("Initialize Ethernet with DHCP:");
49+
if (Ethernet.begin(mac) == 0) {
50+
Serial.println("Failed to configure Ethernet using DHCP");
51+
// Check for Ethernet hardware present
52+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
53+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
54+
while (true) {
55+
delay(1); // do nothing, no point running without Ethernet hardware
56+
}
57+
}
58+
if (Ethernet.linkStatus() == LinkOFF) {
59+
Serial.println("Ethernet cable is not connected.");
60+
}
61+
// try to congifure using IP address instead of DHCP:
62+
Ethernet.begin(mac, ip, myDns);
63+
} else {
64+
Serial.print(" DHCP assigned IP ");
65+
Serial.println(Ethernet.localIP());
66+
}
67+
// give the Ethernet shield a second to initialize:
68+
delay(1000);
69+
70+
ThingSpeak.begin(client); // Initialize ThingSpeak
71+
}
72+
73+
void loop() {
74+
75+
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
76+
// pieces of information in a channel. Here, we write to field 1.
77+
int x = ThingSpeak.writeField(myChannelNumber, 1, number, myWriteAPIKey);
78+
if(x == 200){
79+
Serial.println("Channel update successful.");
80+
}
81+
else{
82+
Serial.println("Problem updating channel. HTTP error code " + String(x));
83+
}
84+
85+
// change the value
86+
number++;
87+
if(number > 99){
88+
number = 0;
89+
}
90+
91+
delay(20000); // Wait 20 seconds to update the channel again
92+
}

0 commit comments

Comments
 (0)