source: rtos_arduino/trunk/arduino_lib/libraries/thingspeak-arduino/examples/WriteVoltage/WriteVoltage.ino@ 189

Last change on this file since 189 was 189, checked in by ertl-honda, 8 years ago

Thinkgspeakのサポート

File size: 4.3 KB
Line 
1/*
2 WriteVoltage
3
4 Reads an analog voltage from pin 0, and writes it to 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 // On Particle: 0 - 4095 maps to 0 - 3.3 volts
71 #define VOLTAGE_MAX 3.3
72 #define VOLTAGE_MAXCOUNTS 4095.0
73#endif
74
75/*
76 *****************************************************************************************
77 **** Visit https://www.thingspeak.com to sign up for a free account and create
78 **** a channel. The video tutorial http://community.thingspeak.com/tutorials/thingspeak-channels/
79 **** has more information. You need to change this to your channel, and your write API key
80 **** IF YOU SHARE YOUR CODE WITH OTHERS, MAKE SURE YOU REMOVE YOUR WRITE API KEY!!
81 *****************************************************************************************/
82unsigned long myChannelNumber = 31461;
83const char * myWriteAPIKey = "LD79EOAAWRVYF04Y";
84
85void setup() {
86 #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ESP8266)
87 #ifdef ARDUINO_AVR_YUN
88 Bridge.begin();
89 #else
90 #if defined(USE_WIFI_SHIELD) || defined(ARDUINO_ARCH_ESP8266)
91 WiFi.begin(ssid, pass);
92 #else
93 Ethernet.begin(mac);
94 #endif
95 #endif
96 #endif
97
98 ThingSpeak.begin(client);
99}
100
101void loop() {
102 // read the input on analog pin 0:
103 int sensorValue = analogRead(A0);
104 // Convert the analog reading
105 // On Arduino: 0 - 1023 maps to 0 - 5 volts
106 // On ESP8266: 0 - 1023 maps to 0 - 1 volts
107 // On Particle: 0 - 4095 maps to 0 - 3.3 volts
108 float voltage = sensorValue * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
109
110 // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
111 // pieces of information in a channel. Here, we write to field 1.
112 ThingSpeak.writeField(myChannelNumber, 1, voltage, myWriteAPIKey);
113 delay(20000); // ThingSpeak will only accept updates every 15 seconds.
114}
Note: See TracBrowser for help on using the repository browser.