|
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 <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 |
| -// Weather station channel details |
39 |
| -unsigned long weatherStationChannelNumber = SECRET_CH_ID_WEATHER_STATION; |
40 |
| -unsigned int temperatureFieldNumber = 4; |
41 |
| - |
42 |
| -// Counting channel details |
43 |
| -unsigned long counterChannelNumber = SECRET_CH_ID_COUNTER; |
44 |
| -const char * myCounterReadAPIKey = SECRET_READ_APIKEY_COUNTER; |
45 |
| -unsigned int counterFieldNumber = 1; |
46 |
| - |
47 |
| -void setup() { |
48 |
| - Ethernet.init(10); // Most Arduino Ethernet hardware |
49 |
| - Serial.begin(115200); //Initialize serial |
50 |
| - |
51 |
| - // start the Ethernet connection: |
52 |
| - Serial.println("Initialize Ethernet with DHCP:"); |
53 |
| - if (Ethernet.begin(mac) == 0) { |
54 |
| - Serial.println("Failed to configure Ethernet using DHCP"); |
55 |
| - // Check for Ethernet hardware present |
56 |
| - if (Ethernet.hardwareStatus() == EthernetNoHardware) { |
57 |
| - Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); |
58 |
| - while (true) { |
59 |
| - delay(1); // do nothing, no point running without Ethernet hardware |
60 |
| - } |
61 |
| - } |
62 |
| - if (Ethernet.linkStatus() == LinkOFF) { |
63 |
| - Serial.println("Ethernet cable is not connected."); |
64 |
| - } |
65 |
| - // try to congifure using IP address instead of DHCP: |
66 |
| - Ethernet.begin(mac, ip, myDns); |
67 |
| - } else { |
68 |
| - Serial.print(" DHCP assigned IP "); |
69 |
| - Serial.println(Ethernet.localIP()); |
70 |
| - } |
71 |
| - // give the Ethernet shield a second to initialize: |
72 |
| - delay(1000); |
73 |
| - |
74 |
| - ThingSpeak.begin(client); // Initialize ThingSpeak |
75 |
| -} |
76 |
| - |
77 |
| -void loop() { |
78 |
| - |
79 |
| - int statusCode = 0; |
80 |
| - |
81 |
| - // Read in field 4 of the public channel recording the temperature |
82 |
| - float temperatureInF = ThingSpeak.readFloatField(weatherStationChannelNumber, temperatureFieldNumber); |
83 |
| - |
84 |
| - // Check the status of the read operation to see if it was successful |
85 |
| - statusCode = ThingSpeak.getLastReadStatus(); |
86 |
| - if(statusCode == 200){ |
87 |
| - Serial.println("Temperature at MathWorks HQ: " + String(temperatureInF) + " deg F"); |
88 |
| - } |
89 |
| - else{ |
90 |
| - Serial.println("Problem reading channel. HTTP error code " + String(statusCode)); |
91 |
| - } |
92 |
| - |
93 |
| - delay(15000); // No need to read the temperature too often. |
94 |
| - |
95 |
| - // Read in field 1 of the private channel which is a counter |
96 |
| - long count = ThingSpeak.readLongField(counterChannelNumber, counterFieldNumber, myCounterReadAPIKey); |
97 |
| - |
98 |
| - // Check the status of the read operation to see if it was successful |
99 |
| - statusCode = ThingSpeak.getLastReadStatus(); |
100 |
| - if(statusCode == 200){ |
101 |
| - Serial.println("Counter: " + String(count)); |
102 |
| - } |
103 |
| - else{ |
104 |
| - Serial.println("Problem reading channel. HTTP error code " + String(statusCode)); |
105 |
| - } |
106 |
| - |
107 |
| - delay(15000); // No need to read the counter too often. |
108 |
| - |
109 |
| -} |
| 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 2020, The MathWorks, Inc. |
| 24 | +*/ |
| 25 | + |
| 26 | +#include <Ethernet.h> |
| 27 | +#include "secrets.h" |
| 28 | +#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros |
| 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 | +// Weather station channel details |
| 39 | +unsigned long weatherStationChannelNumber = SECRET_CH_ID_WEATHER_STATION; |
| 40 | +unsigned int temperatureFieldNumber = 4; |
| 41 | + |
| 42 | +// Counting channel details |
| 43 | +unsigned long counterChannelNumber = SECRET_CH_ID_COUNTER; |
| 44 | +const char * myCounterReadAPIKey = SECRET_READ_APIKEY_COUNTER; |
| 45 | +unsigned int counterFieldNumber = 1; |
| 46 | + |
| 47 | +void setup() { |
| 48 | + Ethernet.init(10); // Most Arduino Ethernet hardware |
| 49 | + Serial.begin(115200); //Initialize serial |
| 50 | + while (!Serial) { |
| 51 | + ; // wait for serial port to connect. Needed for Leonardo native USB port only |
| 52 | + } |
| 53 | + |
| 54 | + // start the Ethernet connection: |
| 55 | + Serial.println("Initialize Ethernet with DHCP:"); |
| 56 | + if (Ethernet.begin(mac) == 0) { |
| 57 | + Serial.println("Failed to configure Ethernet using DHCP"); |
| 58 | + // Check for Ethernet hardware present |
| 59 | + if (Ethernet.hardwareStatus() == EthernetNoHardware) { |
| 60 | + Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); |
| 61 | + while (true) { |
| 62 | + delay(1); // do nothing, no point running without Ethernet hardware |
| 63 | + } |
| 64 | + } |
| 65 | + if (Ethernet.linkStatus() == LinkOFF) { |
| 66 | + Serial.println("Ethernet cable is not connected."); |
| 67 | + } |
| 68 | + // try to congifure using IP address instead of DHCP: |
| 69 | + Ethernet.begin(mac, ip, myDns); |
| 70 | + } else { |
| 71 | + Serial.print(" DHCP assigned IP "); |
| 72 | + Serial.println(Ethernet.localIP()); |
| 73 | + } |
| 74 | + // give the Ethernet shield a second to initialize: |
| 75 | + delay(1000); |
| 76 | + |
| 77 | + ThingSpeak.begin(client); // Initialize ThingSpeak |
| 78 | +} |
| 79 | + |
| 80 | +void loop() { |
| 81 | + |
| 82 | + int statusCode = 0; |
| 83 | + |
| 84 | + // Read in field 4 of the public channel recording the temperature |
| 85 | + float temperatureInF = ThingSpeak.readFloatField(weatherStationChannelNumber, temperatureFieldNumber); |
| 86 | + |
| 87 | + // Check the status of the read operation to see if it was successful |
| 88 | + statusCode = ThingSpeak.getLastReadStatus(); |
| 89 | + if(statusCode == 200){ |
| 90 | + Serial.println("Temperature at MathWorks HQ: " + String(temperatureInF) + " deg F"); |
| 91 | + } |
| 92 | + else{ |
| 93 | + Serial.println("Problem reading channel. HTTP error code " + String(statusCode)); |
| 94 | + } |
| 95 | + |
| 96 | + delay(15000); // No need to read the temperature too often. |
| 97 | + |
| 98 | + // Read in field 1 of the private channel which is a counter |
| 99 | + long count = ThingSpeak.readLongField(counterChannelNumber, counterFieldNumber, myCounterReadAPIKey); |
| 100 | + |
| 101 | + // Check the status of the read operation to see if it was successful |
| 102 | + statusCode = ThingSpeak.getLastReadStatus(); |
| 103 | + if(statusCode == 200){ |
| 104 | + Serial.println("Counter: " + String(count)); |
| 105 | + } |
| 106 | + else{ |
| 107 | + Serial.println("Problem reading channel. HTTP error code " + String(statusCode)); |
| 108 | + } |
| 109 | + |
| 110 | + delay(15000); // No need to read the counter too often. |
| 111 | + |
| 112 | +} |
0 commit comments