[189] | 1 | /*
|
---|
| 2 | WriteMultipleVoltages
|
---|
| 3 |
|
---|
| 4 | Reads analog voltages from pins 0-7 and writes them to the 8 fields of a channel on ThingSpeak every 20 seconds.
|
---|
| 5 |
|
---|
| 6 | ThingSpeak ( https://www.thingspeak.com ) is a free IoT service for prototyping
|
---|
| 7 | systems that collect, analyze, and react to their environments.
|
---|
| 8 |
|
---|
| 9 | Copyright 2015, The MathWorks, Inc.
|
---|
| 10 |
|
---|
| 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.
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | #ifdef SPARK
|
---|
| 16 | #include "ThingSpeak/ThingSpeak.h"
|
---|
| 17 | #else
|
---|
| 18 | #include "ThingSpeak.h"
|
---|
| 19 | #endif
|
---|
| 20 |
|
---|
| 21 | /// ***********************************************************************************************************
|
---|
| 22 | // This example selects the correct library to use based on the board selected under the Tools menu in the IDE.
|
---|
| 23 | // Yun, Wired Ethernet shield, wi-fi shield, esp8266, and Spark are all supported.
|
---|
| 24 | // With Uno and Mega, the default is that you're using a wired ethernet shield (http://www.arduino.cc/en/Main/ArduinoEthernetShield)
|
---|
| 25 | // If you're using a wi-fi shield (http://www.arduino.cc/en/Main/ArduinoWiFiShield), uncomment the line below
|
---|
| 26 | // ***********************************************************************************************************
|
---|
| 27 | //#define USE_WIFI_SHIELD
|
---|
| 28 | #ifdef ARDUINO_ARCH_AVR
|
---|
| 29 |
|
---|
| 30 | #ifdef ARDUINO_AVR_YUN
|
---|
| 31 | #include "YunClient.h"
|
---|
| 32 | YunClient client;
|
---|
| 33 | #else
|
---|
| 34 |
|
---|
| 35 | #ifdef USE_WIFI_SHIELD
|
---|
| 36 | #include <SPI.h>
|
---|
| 37 | // 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
|
---|
| 38 | // If you get "multiple definition of `WiFi'" -- comment out the line below.
|
---|
| 39 | #include <WiFi.h>
|
---|
| 40 | char ssid[] = "<YOURNETWORK>"; // your network SSID (name)
|
---|
| 41 | char pass[] = "<YOURPASSWORD>"; // your network password
|
---|
| 42 | int status = WL_IDLE_STATUS;
|
---|
| 43 | WiFiClient client;
|
---|
| 44 | #else
|
---|
| 45 | // Use wired ethernet shield
|
---|
| 46 | #include <SPI.h>
|
---|
| 47 | #include <Ethernet.h>
|
---|
| 48 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
---|
| 49 | EthernetClient client;
|
---|
| 50 | #endif
|
---|
| 51 | #endif
|
---|
| 52 | // On Arduino: 0 - 1023 maps to 0 - 5 volts
|
---|
| 53 | #define VOLTAGE_MAX 5.0
|
---|
| 54 | #define VOLTAGE_MAXCOUNTS 1023.0
|
---|
| 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 | // On ESP8266: 0 - 1023 maps to 0 - 1 volts
|
---|
| 64 | #define VOLTAGE_MAX 1.0
|
---|
| 65 | #define VOLTAGE_MAXCOUNTS 1023.0
|
---|
| 66 | #endif
|
---|
| 67 |
|
---|
| 68 | #ifdef SPARK
|
---|
| 69 | TCPClient client;
|
---|
| 70 | #define VOLTAGE_MAX 3.3
|
---|
| 71 | #define VOLTAGE_MAXCOUNTS 4095.0
|
---|
| 72 | #endif
|
---|
| 73 |
|
---|
| 74 | /*
|
---|
| 75 | *****************************************************************************************
|
---|
| 76 | **** Visit https://www.thingspeak.com to sign up for a free account and create
|
---|
| 77 | **** a channel. The video tutorial http://community.thingspeak.com/tutorials/thingspeak-channels/
|
---|
| 78 | **** has more information. You need to change this to your channel, and your write API key
|
---|
| 79 | **** IF YOU SHARE YOUR CODE WITH OTHERS, MAKE SURE YOU REMOVE YOUR WRITE API KEY!!
|
---|
| 80 | *****************************************************************************************/
|
---|
| 81 | unsigned long myChannelNumber = 31461;
|
---|
| 82 | const char * myWriteAPIKey = "LD79EOAAWRVYF04Y";
|
---|
| 83 |
|
---|
| 84 | void setup() {
|
---|
| 85 | #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ESP8266)
|
---|
| 86 | #ifdef ARDUINO_AVR_YUN
|
---|
| 87 | Bridge.begin();
|
---|
| 88 | #else
|
---|
| 89 | #if defined(USE_WIFI_SHIELD) || defined(ARDUINO_ARCH_ESP8266)
|
---|
| 90 | WiFi.begin(ssid, pass);
|
---|
| 91 | #else
|
---|
| 92 | Ethernet.begin(mac);
|
---|
| 93 | #endif
|
---|
| 94 | #endif
|
---|
| 95 | #endif
|
---|
| 96 |
|
---|
| 97 | ThingSpeak.begin(client);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | void loop() {
|
---|
| 101 | // Read the input on each pin, convert the reading, and set each field to be sent to ThingSpeak.
|
---|
| 102 | // On Arduino: 0 - 1023 maps to 0 - 5 volts
|
---|
| 103 | // On ESP8266: 0 - 1023 maps to 0 - 1 volts
|
---|
| 104 | // On Particle: 0 - 4095 maps to 0 - 3.3 volts
|
---|
| 105 | float pinVoltage = analogRead(A0) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
|
---|
| 106 | ThingSpeak.setField(1,pinVoltage);
|
---|
| 107 | #ifndef ARDUINO_ARCH_ESP8266
|
---|
| 108 | // The ESP8266 only has one analog input, so skip this
|
---|
| 109 | pinVoltage = analogRead(A1) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
|
---|
| 110 | ThingSpeak.setField(2,pinVoltage);
|
---|
| 111 | pinVoltage = analogRead(A2) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
|
---|
| 112 | ThingSpeak.setField(3,pinVoltage);
|
---|
| 113 | pinVoltage = analogRead(A3) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
|
---|
| 114 | ThingSpeak.setField(4,pinVoltage);
|
---|
| 115 | pinVoltage = analogRead(A4) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
|
---|
| 116 | ThingSpeak.setField(5,pinVoltage);
|
---|
| 117 | pinVoltage = analogRead(A5) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
|
---|
| 118 | ThingSpeak.setField(6,pinVoltage);
|
---|
| 119 | pinVoltage = analogRead(A6) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
|
---|
| 120 | ThingSpeak.setField(7,pinVoltage);
|
---|
| 121 | pinVoltage = analogRead(A7) * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
|
---|
| 122 | ThingSpeak.setField(8,pinVoltage);
|
---|
| 123 | #endif
|
---|
| 124 |
|
---|
| 125 | // Write the fields that you've set all at once.
|
---|
| 126 | ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
|
---|
| 127 |
|
---|
| 128 | delay(20000); // ThingSpeak will only accept updates every 15 seconds.
|
---|
| 129 | }
|
---|