├── Ethernet ├── Arduino_to_ThingSpeak.ino └── Arduino_to_ThingTweet.ino ├── LICENSE ├── README.textile └── Yun └── TalkBack_to_Arduino.ino /Ethernet/Arduino_to_ThingSpeak.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Arduino --> ThingSpeak Channel via Ethernet 4 | 5 | The ThingSpeak Client sketch is designed for the Arduino and Ethernet. 6 | This sketch updates a channel feed with an analog input reading via the 7 | ThingSpeak API (https://thingspeak.com/docs) 8 | using HTTP POST. The Arduino uses DHCP and DNS for a simpler network setup. 9 | The sketch also includes a Watchdog / Reset function to make sure the 10 | Arduino stays connected and/or regains connectivity after a network outage. 11 | Use the Serial Monitor on the Arduino IDE to see verbose network feedback 12 | and ThingSpeak connectivity status. 13 | 14 | Getting Started with ThingSpeak: 15 | 16 | * Sign Up for New User Account - https://thingspeak.com/users/new 17 | * Create a new Channel by selecting Channels and then Create New Channel 18 | * Enter the Write API Key in this sketch under "ThingSpeak Settings" 19 | 20 | Arduino Requirements: 21 | 22 | * Arduino with Ethernet Shield or Arduino Ethernet 23 | * Arduino 1.0+ IDE 24 | 25 | Network Requirements: 26 | 27 | * Ethernet port on Router 28 | * DHCP enabled on Router 29 | * Unique MAC Address for Arduino 30 | 31 | Created: October 17, 2011 by Hans Scharler (http://www.nothans.com) 32 | 33 | Additional Credits: 34 | Example sketches from Arduino team, Ethernet by Adrian McEwen 35 | 36 | */ 37 | 38 | #include 39 | #include 40 | 41 | // Local Network Settings 42 | byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network 43 | 44 | // ThingSpeak Settings 45 | char thingSpeakAddress[] = "api.thingspeak.com"; 46 | String writeAPIKey = "XXXMX2WYYR0EVZZZ"; 47 | const int updateThingSpeakInterval = 16 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval) 48 | 49 | // Variable Setup 50 | long lastConnectionTime = 0; 51 | boolean lastConnected = false; 52 | int failedCounter = 0; 53 | 54 | // Initialize Arduino Ethernet Client 55 | EthernetClient client; 56 | 57 | void setup() 58 | { 59 | // Start Serial for debugging on the Serial Monitor 60 | Serial.begin(9600); 61 | 62 | // Start Ethernet on Arduino 63 | startEthernet(); 64 | } 65 | 66 | void loop() 67 | { 68 | // Read value from Analog Input Pin 0 69 | String analogValue0 = String(analogRead(A0), DEC); 70 | 71 | // Print Update Response to Serial Monitor 72 | if (client.available()) 73 | { 74 | char c = client.read(); 75 | Serial.print(c); 76 | } 77 | 78 | // Disconnect from ThingSpeak 79 | if (!client.connected() && lastConnected) 80 | { 81 | Serial.println("...disconnected"); 82 | Serial.println(); 83 | 84 | client.stop(); 85 | } 86 | 87 | // Update ThingSpeak 88 | if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval)) 89 | { 90 | updateThingSpeak("field1="+analogValue0); 91 | } 92 | 93 | // Check if Arduino Ethernet needs to be restarted 94 | if (failedCounter > 3 ) {startEthernet();} 95 | 96 | lastConnected = client.connected(); 97 | } 98 | 99 | void updateThingSpeak(String tsData) 100 | { 101 | if (client.connect(thingSpeakAddress, 80)) 102 | { 103 | client.print("POST /update HTTP/1.1\n"); 104 | client.print("Host: api.thingspeak.com\n"); 105 | client.print("Connection: close\n"); 106 | client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n"); 107 | client.print("Content-Type: application/x-www-form-urlencoded\n"); 108 | client.print("Content-Length: "); 109 | client.print(tsData.length()); 110 | client.print("\n\n"); 111 | 112 | client.print(tsData); 113 | 114 | lastConnectionTime = millis(); 115 | 116 | if (client.connected()) 117 | { 118 | Serial.println("Connecting to ThingSpeak..."); 119 | Serial.println(); 120 | 121 | failedCounter = 0; 122 | } 123 | else 124 | { 125 | failedCounter++; 126 | 127 | Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")"); 128 | Serial.println(); 129 | } 130 | 131 | } 132 | else 133 | { 134 | failedCounter++; 135 | 136 | Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")"); 137 | Serial.println(); 138 | 139 | lastConnectionTime = millis(); 140 | } 141 | } 142 | 143 | void startEthernet() 144 | { 145 | 146 | client.stop(); 147 | 148 | Serial.println("Connecting Arduino to network..."); 149 | Serial.println(); 150 | 151 | delay(1000); 152 | 153 | // Connect to network amd obtain an IP address using DHCP 154 | if (Ethernet.begin(mac) == 0) 155 | { 156 | Serial.println("DHCP Failed, reset Arduino to try again"); 157 | Serial.println(); 158 | } 159 | else 160 | { 161 | Serial.println("Arduino connected to network using DHCP"); 162 | Serial.println(); 163 | } 164 | 165 | delay(1000); 166 | } 167 | -------------------------------------------------------------------------------- /Ethernet/Arduino_to_ThingTweet.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Arduino --> ThingTweet via Ethernet 4 | 5 | The ThingTweet sketch is designed for the Arduino + Ethernet Shield. 6 | This sketch updates a Twitter status via the ThingTweet App 7 | (https://thingspeak.com/docs/thingtweet) using HTTP POST. 8 | ThingTweet is a Twitter proxy web application that handles the OAuth. 9 | 10 | Getting Started with ThingSpeak and ThingTweet: 11 | 12 | * Sign Up for a New User Account for ThingSpeak - https://www.thingspeak.com/users/new 13 | * Link your Twitter account to the ThingTweet App - Apps / ThingTweet 14 | * Enter the ThingTweet API Key in this sketch under "ThingSpeak Settings" 15 | 16 | Arduino Requirements: 17 | 18 | * Arduino with Ethernet Shield or Arduino Ethernet 19 | * Arduino 1.0 IDE 20 | * Twitter account linked to your ThingSpeak account 21 | 22 | Network Requirements: 23 | 24 | * Ethernet port on Router 25 | * DHCP enabled on Router 26 | * Unique MAC Address for Arduino 27 | 28 | Created: October 17, 2011 by Hans Scharler (http://www.nothans.com) 29 | Updated: December 7, 2012 by Hans Scharler (http://www.nothans.com) 30 | 31 | Additional Credits: 32 | Example sketches from Arduino team, Ethernet by Adrian McEwen 33 | 34 | */ 35 | 36 | #include 37 | #include 38 | 39 | // Local Network Settings 40 | byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network 41 | 42 | // ThingSpeak Settings 43 | char thingSpeakAddress[] = "api.thingspeak.com"; 44 | String thingtweetAPIKey = "XXXMX2WYYR0EV68M"; 45 | 46 | // Variable Setup 47 | long lastConnectionTime = 0; 48 | boolean lastConnected = false; 49 | int failedCounter = 0; 50 | 51 | // Initialize Arduino Ethernet Client 52 | EthernetClient client; 53 | 54 | void setup() 55 | { 56 | // Start Serial for debugging on the Serial Monitor 57 | Serial.begin(9600); 58 | 59 | // Start Ethernet on Arduino 60 | startEthernet(); 61 | 62 | delay(1000); 63 | 64 | // Update Twitter via ThingTweet 65 | updateTwitterStatus("My thing is social @thingspeak"); 66 | } 67 | 68 | void loop() 69 | { 70 | // Print Update Response to Serial Monitor 71 | if (client.available()) 72 | { 73 | char c = client.read(); 74 | Serial.print(c); 75 | } 76 | 77 | // Disconnect from ThingSpeak 78 | if (!client.connected() && lastConnected) 79 | { 80 | Serial.println("...disconnected"); 81 | Serial.println(); 82 | 83 | client.stop(); 84 | } 85 | 86 | // Check if Arduino Ethernet needs to be restarted 87 | if (failedCounter > 3 ) {startEthernet();} 88 | 89 | lastConnected = client.connected(); 90 | } 91 | 92 | void updateTwitterStatus(String tsData) 93 | { 94 | if (client.connect(thingSpeakAddress, 80)) 95 | { 96 | // Create HTTP POST Data 97 | tsData = "api_key="+thingtweetAPIKey+"&status="+tsData; 98 | 99 | client.print("POST /apps/thingtweet/1/statuses/update HTTP/1.1\n"); 100 | client.print("Host: api.thingspeak.com\n"); 101 | client.print("Connection: close\n"); 102 | client.print("Content-Type: application/x-www-form-urlencoded\n"); 103 | client.print("Content-Length: "); 104 | client.print(tsData.length()); 105 | client.print("\n\n"); 106 | 107 | client.print(tsData); 108 | 109 | lastConnectionTime = millis(); 110 | 111 | if (client.connected()) 112 | { 113 | Serial.println("Connecting to ThingSpeak..."); 114 | Serial.println(); 115 | 116 | failedCounter = 0; 117 | } 118 | else 119 | { 120 | failedCounter++; 121 | 122 | Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")"); 123 | Serial.println(); 124 | } 125 | 126 | } 127 | else 128 | { 129 | failedCounter++; 130 | 131 | Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")"); 132 | Serial.println(); 133 | 134 | lastConnectionTime = millis(); 135 | } 136 | } 137 | 138 | void startEthernet() 139 | { 140 | 141 | client.stop(); 142 | 143 | Serial.println("Connecting Arduino to network..."); 144 | Serial.println(); 145 | 146 | delay(1000); 147 | 148 | // Connect to network amd obtain an IP address using DHCP 149 | if (Ethernet.begin(mac) == 0) 150 | { 151 | Serial.println("DHCP Failed, reset Arduino to try again"); 152 | Serial.println(); 153 | } 154 | else 155 | { 156 | Serial.println("Arduino connected to network using DHCP"); 157 | Serial.println(); 158 | } 159 | 160 | delay(1000); 161 | } 162 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2015 Hans Scharler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.textile: -------------------------------------------------------------------------------- 1 | h1. ThingSpeak 2 | 3 | "ThingSpeak":http://www.thingspeak.com is an open “Internet of Things” application and API to store and retrieve data from things using HTTP over the Internet or via a Local Area Network. With ThingSpeak, you can create sensor logging applications, location tracking applications, and a social network of things with status updates. 4 | 5 | In addition to storing and retrieving numeric and alphanumeric data, the ThingSpeak API allows for numeric data processing such as timescaling, averaging, median, summing, and rounding. Each ThingSpeak Channel supports data entries of up to 8 data fields, latitude, longitude, elevation, and status. The channel feeds support JSON, XML, and CSV formats for integration into applications. 6 | 7 | The ThingSpeak application also features time zone management, read/write API key management and JavaScript-based charts from Highslide Software / Torstein Hønsi. 8 | 9 | Support for ThingSpeak is available on the "ThingSpeak Community":http://community.thingspeak.com/ site which features a "Blog":http://community.thingspeak.com/, "Forum":http://community.thingspeak.com/forum/, "Documentation":https://thingspeak.com/docs, and "Tutorials":https://thingspeak.com/docs/tutorials. 10 | 11 | h1. Arduino 12 | 13 | Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. 14 | 15 | Support for Arduino is available at "Arduino.cc":http://www.arduino.cc 16 | 17 | h1. Examples 18 | 19 | h3. Ethernet 20 | 21 | Examples are hosted on the "ThingSpeak Support Library for Arduino":https://github.com/mathworks/thingspeak-arduino/tree/master/examples/ArduinoEthernet 22 | 23 | h3. Yun 24 | 25 | Examples are hosted on the "ThingSpeak Support Library for Arduino":https://github.com/mathworks/thingspeak-arduino/tree/master/examples/ArduinoYun 26 | -------------------------------------------------------------------------------- /Yun/TalkBack_to_Arduino.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | TalkBack --> Arduino Yun via Wi-Fi 4 | 5 | The TalkBack App sketch is designed for the Arduino Yun connected to a 6 | Wi-Fi network and the Arduino 1.5.4 IDE or newer. This sketch allows the 7 | Arduino to request commands stored by a ThingSpeak TalkBack via the 8 | TalkBack API (https://thingspeak.com/docs/talkback). 9 | 10 | Getting Started with ThingSpeak: 11 | 12 | * Sign Up for New User Account - https://www.thingspeak.com/users/new 13 | * Create a new Channel by selecting Channels and then Create New Channel 14 | * Create a TalkBack by selecting Apps, TalkBack, and New TalkBack 15 | * Enter the TalkBack API Key in this sketch under "ThingSpeak Settings" 16 | * Enter the TalkBack ID in this sketch under "ThingSpeak Settings" 17 | 18 | Arduino Requirements: 19 | 20 | * Arduino Yun 21 | * Arduino 1.5.4 IDE or newer 22 | 23 | Network Requirements: 24 | 25 | * Router with Wi-Fi 26 | * DHCP enabled on Router 27 | 28 | Created: Jan 30, 2014 by Hans Scharler (http://www.nothans.com) 29 | 30 | Additional Credits: 31 | Example sketches from Arduino team, ThingSpeak and Yun Example by Tenet Technetronics 32 | 33 | */ 34 | 35 | #include "Bridge.h" 36 | #include "HttpClient.h" 37 | 38 | //ThingSpeak Settings 39 | String thingSpeakAPI = "api.thingspeak.com"; 40 | String talkBackAPIKey = "XXXXXXXXXXXXXXXX"; 41 | String talkBackID = "YYYY"; 42 | const int checkTalkBackInterval = 15 * 1000; // Time interval in milliseconds to check TalkBack (number of seconds * 1000 = interval) 43 | 44 | // Variable Setup 45 | long lastConnectionTime = 0; 46 | 47 | void setup() 48 | { 49 | // Setup On-board LED 50 | pinMode(13, OUTPUT); 51 | digitalWrite(13, LOW); 52 | delay(1000); 53 | digitalWrite(13, HIGH); 54 | delay(1000); 55 | digitalWrite(13, LOW); 56 | 57 | // Initialize Bridge 58 | Bridge.begin(); 59 | 60 | // Initialize Serial 61 | Serial.begin(9600); 62 | while(!Serial); 63 | } 64 | 65 | void loop() 66 | { 67 | // Check ThingSpeak for TalkBack Commands 68 | checkTalkBack(); 69 | delay(checkTalkBackInterval); 70 | } 71 | 72 | void checkTalkBack() 73 | { 74 | HttpClient client; 75 | 76 | String talkBackCommand; 77 | char charIn; 78 | String talkBackURL = "http://" + thingSpeakAPI + "/talkbacks/" + talkBackID + "/commands/execute?api_key=" + talkBackAPIKey; 79 | 80 | // Make a HTTP GET request to the TalkBack API: 81 | client.get(talkBackURL); 82 | 83 | while (client.available()) { 84 | charIn = client.read(); 85 | talkBackCommand += charIn; 86 | } 87 | 88 | // Turn On/Off the On-board LED 89 | if (talkBackCommand == "TURN_ON") 90 | { 91 | Serial.println(talkBackCommand); 92 | digitalWrite(13, HIGH); 93 | } 94 | else if (talkBackCommand == "TURN_OFF") 95 | { 96 | Serial.println(talkBackCommand); 97 | digitalWrite(13, LOW); 98 | } 99 | 100 | Serial.flush(); 101 | delay(1000); 102 | } 103 | --------------------------------------------------------------------------------