[189] | 1 | /*
|
---|
| 2 | ReadPrivateChannel
|
---|
| 3 |
|
---|
| 4 | Reads the latest voltage value from a private channel on ThingSpeak every 30 seconds
|
---|
| 5 | and prints to the serial port debug window.
|
---|
| 6 |
|
---|
| 7 | For an example of how to read from a public channel, see ReadChannel example
|
---|
| 8 |
|
---|
| 9 | ThingSpeak ( https://www.thingspeak.com ) is a free IoT service for prototyping
|
---|
| 10 | systems that collect, analyze, and react to their environments.
|
---|
| 11 |
|
---|
| 12 | Copyright 2016, The MathWorks, Inc.
|
---|
| 13 |
|
---|
| 14 | Documentation for the ThingSpeak Communication Library for Arduino is in the extras/documentation folder where the library was installed.
|
---|
| 15 | See the accompaning licence file for licensing information.
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | #ifdef SPARK
|
---|
| 19 | #include "ThingSpeak/ThingSpeak.h"
|
---|
| 20 | #else
|
---|
| 21 | #include "ThingSpeak.h"
|
---|
| 22 | #endif
|
---|
| 23 |
|
---|
| 24 | // ***********************************************************************************************************
|
---|
| 25 | // This example selects the correct library to use based on the board selected under the Tools menu in the IDE.
|
---|
| 26 | // Yun, Wired Ethernet shield, wi-fi shield, esp8266, and Spark are all supported.
|
---|
| 27 | // With Uno and Mega, the default is that you're using a wired ethernet shield (http://www.arduino.cc/en/Main/ArduinoEthernetShield)
|
---|
| 28 | // If you're using a wi-fi shield (http://www.arduino.cc/en/Main/ArduinoWiFiShield), uncomment the line below
|
---|
| 29 | // ***********************************************************************************************************
|
---|
| 30 | //#define USE_WIFI_SHIELD
|
---|
| 31 | #ifdef ARDUINO_ARCH_AVR
|
---|
| 32 |
|
---|
| 33 | #ifdef ARDUINO_AVR_YUN
|
---|
| 34 | #include "YunClient.h"
|
---|
| 35 | YunClient client;
|
---|
| 36 | #else
|
---|
| 37 |
|
---|
| 38 | #ifdef USE_WIFI_SHIELD
|
---|
| 39 | #include <SPI.h>
|
---|
| 40 | // ESP8266 USERS -- YOU MUST COMMENT OUT THE LINE BELOW. There's a bug in the Arduino IDE that causes it to not respect #ifdef when it comes to #includes
|
---|
| 41 | // If you get "multiple definition of `WiFi'" -- comment out the line below.
|
---|
| 42 | #include <WiFi.h>
|
---|
| 43 | char ssid[] = "<YOURNETWORK>"; // your network SSID (name)
|
---|
| 44 | char pass[] = "<YOURPASSWORD>"; // your network password
|
---|
| 45 | int status = WL_IDLE_STATUS;
|
---|
| 46 | WiFiClient client;
|
---|
| 47 | #else
|
---|
| 48 | // Use wired ethernet shield
|
---|
| 49 | #include <SPI.h>
|
---|
| 50 | #include <Ethernet.h>
|
---|
| 51 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
---|
| 52 | EthernetClient client;
|
---|
| 53 | #endif
|
---|
| 54 | #endif
|
---|
| 55 | #endif
|
---|
| 56 |
|
---|
| 57 | #ifdef ARDUINO_ARCH_ESP8266
|
---|
| 58 | #include <ESP8266WiFi.h>
|
---|
| 59 | char ssid[] = "<YOURNETWORK>"; // your network SSID (name)
|
---|
| 60 | char pass[] = "<YOURPASSWORD>"; // your network password
|
---|
| 61 | int status = WL_IDLE_STATUS;
|
---|
| 62 | WiFiClient client;
|
---|
| 63 | #endif
|
---|
| 64 |
|
---|
| 65 | // Particle Core, Photon, and Electron the results are published to the Particle dashboard using events.
|
---|
| 66 | // Go to http://dashboard.particle.io, click on the logs tab, and you'll see the events coming in.
|
---|
| 67 | #ifdef SPARK
|
---|
| 68 | TCPClient client;
|
---|
| 69 | #endif
|
---|
| 70 |
|
---|
| 71 | /*
|
---|
| 72 | *****************************************************************************************
|
---|
| 73 | **** Visit https://www.thingspeak.com to sign up for a free account and create
|
---|
| 74 | **** a channel. The video tutorial http://community.thingspeak.com/tutorials/thingspeak-channels/
|
---|
| 75 | **** has more information. You need to change this to your channel, and your read API key
|
---|
| 76 | **** IF YOU SHARE YOUR CODE WITH OTHERS, MAKE SURE YOU REMOVE YOUR READ API KEY!!
|
---|
| 77 | *****************************************************************************************
|
---|
| 78 |
|
---|
| 79 | This is the ThingSpeak channel used in the write examples (31461). It is private, and requires a
|
---|
| 80 | read API key to access it. We'll read from the first field.
|
---|
| 81 | */
|
---|
| 82 | unsigned long myChannelNumber = 31461;
|
---|
| 83 | const char * myReadAPIKey = "NKX4Z5JGO4M5I18A";
|
---|
| 84 |
|
---|
| 85 | void setup() {
|
---|
| 86 | #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ESP8266)
|
---|
| 87 | Serial.begin(9600);
|
---|
| 88 | #ifdef ARDUINO_AVR_YUN
|
---|
| 89 | Bridge.begin();
|
---|
| 90 | #else
|
---|
| 91 | #if defined(USE_WIFI_SHIELD) || defined(ARDUINO_ARCH_ESP8266)
|
---|
| 92 | WiFi.begin(ssid, pass);
|
---|
| 93 | #else
|
---|
| 94 | Ethernet.begin(mac);
|
---|
| 95 | #endif
|
---|
| 96 | #endif
|
---|
| 97 | #endif
|
---|
| 98 |
|
---|
| 99 | ThingSpeak.begin(client);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | void loop() {
|
---|
| 103 | // Read the latest value from field 1 of channel 31461
|
---|
| 104 | float voltage = ThingSpeak.readFloatField(myChannelNumber, 1, myReadAPIKey);
|
---|
| 105 | #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ESP8266)
|
---|
| 106 | Serial.print("Latest voltage is: ");
|
---|
| 107 | Serial.print(voltage);
|
---|
| 108 | Serial.println("V");
|
---|
| 109 | #endif
|
---|
| 110 | #ifdef SPARK
|
---|
| 111 | Particle.publish("thingspeak-readvoltage", "Latest voltage is: " + String(voltage) + "V",60,PRIVATE);
|
---|
| 112 | #endif
|
---|
| 113 | delay(30000);
|
---|
| 114 | }
|
---|