9
9
Copyright 2017, The MathWorks, Inc.
10
10
11
11
Documentation for the ThingSpeak Communication Library for Arduino is in the extras/documentation folder where the library was installed.
12
- See the accompaning licence file for licensing information.
12
+ See the accompanying license file for licensing information.
13
13
*/
14
14
15
15
#include " ThingSpeak.h"
16
16
17
17
// ***********************************************************************************************************
18
18
// This example selects the correct library to use based on the board selected under the Tools menu in the IDE.
19
- // Yun, Ethernet shield, WiFi101 shield, esp8266, ESP32 and MXR1000 are all supported. Please note, ADC analogRead() for ESP32 has not yet
20
- // been implemented in the SparkFun library. It will always return 0.
19
+ // Yun, Ethernet shield, WiFi101 shield and MKR1000 are supported.
20
+ // EPS8266 and ESP32 are not compatible with this example.
21
21
// With Yun, the default is that you're using the Ethernet connection.
22
22
// If you're using a wi-fi 101 or ethernet shield (http://www.arduino.cc/en/Main/ArduinoWiFiShield), uncomment the corresponding line below
23
23
// ***********************************************************************************************************
24
24
25
25
// #define USE_WIFI101_SHIELD
26
26
// #define USE_ETHERNET_SHIELD
27
27
28
+ #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
29
+ #error "EPS8266 and ESP32 are not compatible with this example."
30
+ #endif
28
31
29
- #if !defined(USE_WIFI101_SHIELD) && !defined(USE_ETHERNET_SHIELD) && !defined(ARDUINO_SAMD_MKR1000) && !defined(ARDUINO_AVR_YUN) && !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32)
32
+ #if !defined(USE_WIFI101_SHIELD) && !defined(USE_ETHERNET_SHIELD) && !defined(ARDUINO_SAMD_MKR1000) && !defined(ARDUINO_AVR_YUN)
30
33
#error "Uncomment the #define for either USE_WIFI101_SHIELD or USE_ETHERNET_SHIELD"
31
34
#endif
32
35
33
36
#if defined(ARDUINO_AVR_YUN)
34
37
#include " YunClient.h"
35
38
YunClient client;
36
39
#else
37
- #if defined(USE_WIFI101_SHIELD) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
40
+ #if defined(USE_WIFI101_SHIELD) || defined(ARDUINO_SAMD_MKR1000)
38
41
// Use WiFi
39
- #ifdef ARDUINO_ARCH_ESP8266
40
- #include < ESP8266WiFi.h>
41
- #elif defined(ARDUINO_ARCH_ESP32)
42
- #include < WiFi.h>
43
- #else
44
- #include < SPI.h>
45
- #include < WiFi101.h>
46
- #endif
42
+ #include < SPI.h>
43
+ #include < WiFi101.h>
47
44
char ssid[] = " <YOURNETWORK>" ; // your network SSID (name)
48
45
char pass[] = " <YOURPASSWORD>" ; // your network password
49
46
int status = WL_IDLE_STATUS;
69
66
// On Due: 0 - 1023 maps to 0 - 3.3 volts
70
67
#define VOLTAGE_MAX 3.3
71
68
#define VOLTAGE_MAXCOUNTS 1023.0
72
- #elif ARDUINO_ARCH_ESP8266
73
- // On ESP8266: 0 - 1023 maps to 0 - 1 volts
74
- #define VOLTAGE_MAX 1.0
75
- #define VOLTAGE_MAXCOUNTS 1023.0
76
- #elif ARDUINO_ARCH_ESP32
77
- // On ESP32: 0 - 4096 maps to 0 - 1 volts
78
- #define VOLTAGE_MAX 1.0
79
- #define VOLTAGE_MAXCOUNTS 4095.0
80
69
#endif
81
70
82
71
@@ -95,8 +84,8 @@ void setup() {
95
84
#ifdef ARDUINO_AVR_YUN
96
85
Bridge.begin ();
97
86
#else
98
- #if defined(ARDUINO_ARCH_ESP8266) || defined( USE_WIFI101_SHIELD) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_ARCH_ESP32 )
99
- WiFi.begin (ssid, pass);
87
+ #if defined(USE_WIFI101_SHIELD) || defined(ARDUINO_SAMD_MKR1000)
88
+ WiFi.begin (ssid, pass);
100
89
#else
101
90
Ethernet.begin (mac);
102
91
#endif
@@ -109,28 +98,9 @@ void setup() {
109
98
void loop () {
110
99
// Read the input on each pin, convert the reading, and set each field to be sent to ThingSpeak.
111
100
// On Uno,Mega,Yun: 0 - 1023 maps to 0 - 5 volts
112
- // On ESP8266: 0 - 1023 maps to 0 - 1 volts
113
- // On ESP32: 0 - 4095 maps to 0 - 1 volts, but will always return 0 as analogRead() has not been implemented yet
114
101
// On MKR1000,Due: 0 - 4095 maps to 0 - 3.3 volts
115
102
float pinVoltage = analogRead (A0) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
116
103
ThingSpeak.setField (1 ,pinVoltage);
117
- #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32)
118
- // The ESP8266 only has one analog input, so skip this
119
- pinVoltage = analogRead (A1) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
120
- ThingSpeak.setField (2 ,pinVoltage);
121
- pinVoltage = analogRead (A2) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
122
- ThingSpeak.setField (3 ,pinVoltage);
123
- pinVoltage = analogRead (A3) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
124
- ThingSpeak.setField (4 ,pinVoltage);
125
- pinVoltage = analogRead (A4) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
126
- ThingSpeak.setField (5 ,pinVoltage);
127
- pinVoltage = analogRead (A5) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
128
- ThingSpeak.setField (6 ,pinVoltage);
129
- pinVoltage = analogRead (A6) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
130
- ThingSpeak.setField (7 ,pinVoltage);
131
- pinVoltage = analogRead (A7) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
132
- ThingSpeak.setField (8 ,pinVoltage);
133
- #endif
134
104
135
105
// Write the fields that you've set all at once.
136
106
ThingSpeak.writeFields (myChannelNumber, myWriteAPIKey);
0 commit comments