1 | /*
|
---|
2 | ReadLastTemperature
|
---|
3 |
|
---|
4 | Reads the latest temperature from the MathWorks weather station in Natick, MA
|
---|
5 | https://thingspeak.com/channels/12397 on ThingSpeak, and prints to
|
---|
6 | the serial port debug window every 30 seconds.
|
---|
7 |
|
---|
8 | ThingSpeak ( https://www.thingspeak.com ) is a free IoT service for building
|
---|
9 | systems that collect, analyze, and react to their environments.
|
---|
10 |
|
---|
11 | Copyright 2016, The MathWorks, Inc.
|
---|
12 |
|
---|
13 | Documentation for the ThingSpeak Communication Library for Arduino is in the extras/documentation folder where the library was installed.
|
---|
14 | See the accompaning licence file for licensing information.
|
---|
15 | */
|
---|
16 |
|
---|
17 | #ifdef SPARK
|
---|
18 | #include "ThingSpeak/ThingSpeak.h"
|
---|
19 | #else
|
---|
20 | #include "ThingSpeak.h"
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | // ***********************************************************************************************************
|
---|
24 | // This example selects the correct library to use based on the board selected under the Tools menu in the IDE.
|
---|
25 | // Yun, Wired Ethernet shield, wi-fi shield, esp8266, and Spark are all supported.
|
---|
26 | // With Uno and Mega, the default is that you're using a wired ethernet shield (http://www.arduino.cc/en/Main/ArduinoEthernetShield)
|
---|
27 | // If you're using a wi-fi shield (http://www.arduino.cc/en/Main/ArduinoWiFiShield), uncomment the line below
|
---|
28 | // ***********************************************************************************************************
|
---|
29 | //#define USE_WIFI_SHIELD
|
---|
30 | #ifdef ARDUINO_ARCH_AVR
|
---|
31 |
|
---|
32 | #ifdef ARDUINO_AVR_YUN
|
---|
33 | #include "YunClient.h"
|
---|
34 | YunClient client;
|
---|
35 | #else
|
---|
36 |
|
---|
37 | #ifdef USE_WIFI_SHIELD
|
---|
38 | #include <SPI.h>
|
---|
39 | // 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
|
---|
40 | // If you get "multiple definition of `WiFi'" -- comment out the line below.
|
---|
41 | #include <WiFi.h>
|
---|
42 | char ssid[] = "<YOURNETWORK>"; // your network SSID (name)
|
---|
43 | char pass[] = "<YOURPASSWORD>"; // your network password
|
---|
44 | int status = WL_IDLE_STATUS;
|
---|
45 | WiFiClient client;
|
---|
46 | #else
|
---|
47 | // Use wired ethernet shield
|
---|
48 | #include <SPI.h>
|
---|
49 | #include <Ethernet.h>
|
---|
50 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
---|
51 | EthernetClient client;
|
---|
52 | #endif
|
---|
53 | #endif
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | #ifdef ARDUINO_ARCH_ESP8266
|
---|
57 | #include <ESP8266WiFi.h>
|
---|
58 | char ssid[] = "<YOURNETWORK>"; // your network SSID (name)
|
---|
59 | char pass[] = "<YOURPASSWORD>"; // your network password
|
---|
60 | int status = WL_IDLE_STATUS;
|
---|
61 | WiFiClient client;
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | // On Particle Core, Photon, and Electron the results are published to the Particle dashboard using events.
|
---|
65 | // Go to http://dashboard.particle.io, click on the logs tab, and you'll see the events coming in.
|
---|
66 | #ifdef SPARK
|
---|
67 | TCPClient client;
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | /*
|
---|
71 | This is the ThingSpeak channel number for the MathwWorks weather station
|
---|
72 | https://thingspeak.com/channels/12397. It senses a number of things, including
|
---|
73 | Wind Direction, Wind Speed, Humidity, Temperature, Rainfall, and Atmospheric Pressure.
|
---|
74 |
|
---|
75 | Temperature is stored in field 4
|
---|
76 | */
|
---|
77 |
|
---|
78 | unsigned long weatherStationChannelNumber = 12397;
|
---|
79 | unsigned int temperatureFieldNumber = 4;
|
---|
80 |
|
---|
81 | void setup() {
|
---|
82 | #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ESP8266)
|
---|
83 | Serial.begin(9600);
|
---|
84 | #ifdef ARDUINO_AVR_YUN
|
---|
85 | Bridge.begin();
|
---|
86 | #else
|
---|
87 | #if defined(USE_WIFI_SHIELD) || defined(ARDUINO_ARCH_ESP8266)
|
---|
88 | WiFi.begin(ssid, pass);
|
---|
89 | #else
|
---|
90 | Ethernet.begin(mac);
|
---|
91 | #endif
|
---|
92 | #endif
|
---|
93 | #endif
|
---|
94 |
|
---|
95 | ThingSpeak.begin(client);
|
---|
96 |
|
---|
97 | }
|
---|
98 |
|
---|
99 | void loop() {
|
---|
100 | // Read the latest value from field 4 of channel 12397
|
---|
101 | float temperatureInF = ThingSpeak.readFloatField(weatherStationChannelNumber, temperatureFieldNumber);
|
---|
102 | #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ESP8266)
|
---|
103 | Serial.print("Current temp is: ");
|
---|
104 | Serial.print(temperatureInF);
|
---|
105 | Serial.println(" degrees F");
|
---|
106 | #endif
|
---|
107 | #ifdef SPARK
|
---|
108 | Particle.publish("thingspeak-lasttemp", String::format("Current temp %.1f degrees F",temperatureInF),60,PRIVATE);
|
---|
109 | #endif
|
---|
110 | delay(30000); // Note that the weather station only updates once a minute
|
---|
111 | }
|
---|