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