Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  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 (http://community.thingspeak.com/documentation/)
  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://www.thingspeak.com/users/new
  17.    * Register your Arduino by selecting Devices, Add New Device
  18.    * Once the Arduino is registered, click Generate Unique MAC Address
  19.    * Enter the new MAC Address in this sketch under "Local Network Settings"
  20.    * Create a new Channel by selecting Channels and then Create New Channel
  21.    * Enter the Write API Key in this sketch under "ThingSpeak Settings"
  22.  
  23.  Arduino Requirements:
  24.  
  25.    * Arduino with Ethernet Shield or Arduino Ethernet
  26.    * Arduino 1.0 IDE
  27.    
  28.   Network Requirements:
  29.  
  30.    * Ethernet port on Router    
  31.    * DHCP enabled on Router
  32.    * Unique MAC Address for Arduino
  33.  
  34.  Created: October 17, 2011 by Hans Scharler (http://www.iamshadowlord.com)
  35.  
  36.  Additional Credits:
  37.  Example sketches from Arduino team, Ethernet by Adrian McEwen
  38.  
  39. updated by Rick Klink to read DS18B20 temperatures and upload to thingspeak
  40. updated by Ondrej Gottwald to read 4 DS18B20 temperatures and upload to thingspeak
  41.  
  42.  
  43. */
  44. #include <OneWire.h>
  45. #include <DallasTemperature.h>
  46. #include <SPI.h>
  47. #include <Ethernet.h>
  48.  
  49. // Local Network Settings
  50. byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xD5, 0x63 }; // Must be unique on local network
  51.  
  52. // ThingSpeak Settings
  53. char thingSpeakAddress[] = "api.thingspeak.com";
  54. //**********************************************
  55. // Tady zadejte writeAPIkey získaný z ThingSpeak
  56. //**********************************************
  57. String writeAPIKey = "XXXXXXXXXXXXXXXX";  
  58.  
  59. const int updateThingSpeakInterval = 30 * 1000;      // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
  60.  
  61. // Variable Setup
  62. long lastConnectionTime = 0;
  63. boolean lastConnected = false;
  64. int failedCounter = 0;
  65.  
  66. // Initialize Arduino Ethernet Client
  67. EthernetClient client;
  68.  
  69. // Data wire is plugged into pin 3 on the Arduino
  70. #define ONE_WIRE_BUS 3
  71. #define TempRes 10  // set the resolution to 10 bit (resolution for ds18b20 9, 10, 11, 12)
  72.  
  73. // Setup a oneWire instance to communicate with any OneWire devices
  74. OneWire oneWire(ONE_WIRE_BUS);
  75. // Pass our oneWire reference to Dallas Temperature.
  76. DallasTemperature sensors(&oneWire);
  77. // Assign the addresses of 1-Wire temp sensors.
  78.                                                                                  // Tady zadejte vlastni adresy cidel DS18B20
  79. DeviceAddress RedTemp = { 0x28, 0xD2, 0xCA, 0xF3, 0x00, 0x00, 0x00, 0xD2 };      // ruda,   adresa: 0x28, 0xD2, 0xCA, 0xF3, 0x00, 0x00, 0x00, 0xD2
  80. DeviceAddress YellowTemp = {  0x28, 0x7E, 0x22, 0xF9, 0x03, 0x00, 0x00, 0xE8 };  // zluta,  adresa: 0x28, 0x7E, 0x22, 0xF9, 0x03, 0x00, 0x00, 0xE8
  81. DeviceAddress BlueTemp = {  0x28, 0x37, 0xBF, 0x50, 0x03, 0x00, 0x00, 0x6A };    // modra,  adresa: 0x28, 0x37, 0xBF, 0x50, 0x03, 0x00, 0x00, 0x6A
  82. DeviceAddress GreenTemp = {  0x28, 0xBC, 0x28, 0xF9, 0x03, 0x00, 0x00, 0xA4 };   // zelena, adresa: 0x28, 0xBC, 0x28, 0xF9, 0x03, 0x00, 0x00, 0xA4
  83.  
  84.  
  85.  
  86. void setup()
  87. {
  88.   // Start Serial for debugging on the Serial Monitor
  89.   Serial.begin(9600);
  90.  
  91.   // Start Ethernet on Arduino
  92.   startEthernet();
  93.   sensors.begin();
  94.   // set the resolution to TempRes value
  95.   sensors.setResolution(RedTemp, TempRes);
  96.   sensors.setResolution(YellowTemp, TempRes);
  97.   sensors.setResolution(BlueTemp, TempRes);
  98.   sensors.setResolution(GreenTemp, TempRes);
  99. }
  100.  
  101. void loop(void)
  102. {
  103.   // Read value from Analog Input Pin 0
  104.   //String analogPin0 = String(analogRead(A0), DEC);
  105.  
  106.   // Print Update Response to Serial Monitor
  107.   if (client.available())
  108.   {
  109.     char c = client.read();
  110.     Serial.print(c);
  111.   }
  112.  
  113.   // Disconnect from ThingSpeak
  114.   if (!client.connected() && lastConnected)
  115.   {
  116.     Serial.println("...disconnected");
  117.     Serial.println();
  118.    
  119.     client.stop();
  120.   }
  121.  
  122.   //******************
  123.   //Print Temperatures
  124.   //******************
  125.   Serial.print("Getting temperatures...\n\r");
  126.   sensors.requestTemperatures();
  127.   float RT = sensors.getTempC(RedTemp);  //float = xx,xx ; int = xx
  128.   Serial.print("Red Temp is: ");
  129.   Serial.print(RT);
  130.   Serial.print("C: ");
  131.   Serial.print("\n\r");
  132.   float YT = sensors.getTempC(YellowTemp); //float = xx,xx ; int = xx
  133.   Serial.print("Yellow Temp is: ");
  134.   Serial.print(YT);
  135.   Serial.print("C: ");
  136.   Serial.print("\n\r");
  137.   float BT = sensors.getTempC(BlueTemp); //float = xx,xx ; int = xx
  138.   Serial.print("Blue Temp is: ");
  139.   Serial.print(BT);
  140.   Serial.print("C: ");
  141.   Serial.print("\n\r");
  142.   float GT = sensors.getTempC(GreenTemp);  //float = xx,xx ; int = xx
  143.   Serial.print("Green Temp is: ");
  144.   Serial.print(GT);
  145.   Serial.print("C: ");
  146.   Serial.print("\n\r");
  147.  
  148.   // Update ThingSpeak
  149.   if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
  150.   {
  151.   updateThingSpeak("field1="+String(RT, DEC)+"&field2="+String(YT, DEC)+"&field3="+String(BT, DEC)+"&field4="+String(GT, DEC));
  152.   }
  153.  
  154.   // Check if Arduino Ethernet needs to be restarted
  155.   if (failedCounter > 3 ) {startEthernet();}
  156.  
  157.   lastConnected = client.connected();
  158. }
  159.  
  160. void updateThingSpeak(String tsData)
  161. {
  162.   if (client.connect(thingSpeakAddress, 80))
  163.   {        
  164.     client.print("POST /update HTTP/1.1\n");
  165.     client.print("Host: api.thingspeak.com\n");
  166.     client.print("Connection: close\n");
  167.     client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
  168.     client.print("Content-Type: application/x-www-form-urlencoded\n");
  169.     client.print("Content-Length: ");
  170.     client.print(tsData.length());
  171.     client.print("\n\n");
  172.  
  173.     client.print(tsData);
  174.    
  175.     lastConnectionTime = millis();
  176.    
  177.     if (client.connected())
  178.     {
  179.       Serial.println("Connecting to ThingSpeak...");
  180.       Serial.println();
  181.      
  182.       failedCounter = 0;
  183.     }
  184.     else
  185.     {
  186.       failedCounter++;
  187.  
  188.       Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");  
  189.       Serial.println();
  190.     }
  191.    
  192.   }
  193.   else
  194.   {
  195.     failedCounter++;
  196.    
  197.     Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");  
  198.     Serial.println();
  199.    
  200.     lastConnectionTime = millis();
  201.   }
  202. }
  203.  
  204. void startEthernet()
  205. {
  206.  
  207.   client.stop();
  208.  
  209.   Serial.println("Connecting Arduino to network...");
  210.   Serial.println();  
  211.  
  212.   delay(1000);
  213.  
  214.   // Connect to network amd obtain an IP address using DHCP
  215.   if (Ethernet.begin(mac) == 0)
  216.   {
  217.     Serial.println("DHCP Failed, reset Arduino to try again");
  218.     Serial.println();
  219.   }
  220.   else
  221.   {
  222.     Serial.println("Arduino connected to network using DHCP");
  223.     Serial.println();
  224.   }
  225.  
  226.   delay(1000);
  227. }