Azuria

Arduino DS18b20 to plotly

Oct 13th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.90 KB | None | 0 0
  1. /* YourDuinoStarter Example: Sending and Plotting data on the Net with plot.ly
  2.  - WHAT IT DOES: Sends outdoor and indoor temperature to plot.ly server which plots it.
  3.  - SEE the comments after "//" on each line below
  4.  - CONNECTIONS:
  5.  
  6.  - DS18B20 Sensors:   +5V, Gnd, Signals to pin 3
  7.  -
  8.  - V1.20 02/13/2015 Adding DS18B20 Code
  9.  
  10.  
  11.  Sign up to plotly here: https://plot.ly
  12.  View your API key and stream tokens here: https://plot.ly/settings
  13.  Download the plot.ly Arduino library here: https://github.com/plotly/arduino-api
  14.  UnZip and copy the plotly_streaming_ethernet folder to your libraries folder
  15.  Get your external IP address: https://www.google.com/search?as_q=public+ip+address
  16.  -----( You must enter the following items specifically for YOUR setup )-----
  17.  Username terryking
  18.  API Key  y2zzw4ib0y
  19.  Your tokens  zvxy5hd1vf  2p1iitbjhg
  20.  Your IP address:  209.134.45.3
  21.  -----------------------------------------------------------------
  22.  */
  23.  
  24. /*-----( Import needed libraries )-----*/
  25. #include <Ethernet.h>
  26. #include <SPI.h>
  27. #include "plotly_streaming_ethernet.h"
  28.  
  29. // Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html
  30. #include <OneWire.h>      // 1-wire bus including DS18B20
  31.  
  32. //Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
  33. #include <DallasTemperature.h>  // Read temperature from DS18B20
  34.  
  35. /*-----( Declare Constants and Pin Numbers )-----*/
  36. #define ONE_WIRE_BUS 3 // DS18B20 on Pin 3
  37. #define nTraces 2    // Number of traces on the graph
  38. #define LED_PIN 13   // Onboard LED
  39.  
  40. /*-----( Declare Variables )-----*/
  41. // DS18B20 Temperature Sensor Variables
  42. int    OutDoorTempDS_C;  // Values returned from DS18B20 chip
  43. int    OutDoorTempDS_F;
  44. int    InDoorTempDS_C;
  45. int    InDoorTempDS_F;
  46.  
  47. // Supply as many tokens as data traces
  48. // e.g. if you want to ploty A0 and A1 vs time, supply two tokens
  49. char *tokens[nTraces] = { "zvxy5hd1vf", "2p1iitbjhg"};
  50. byte mac[] = { 0xCA, 0xBC, 0xAB, 0xCA, 0xB0, 0x08 }; // Or make up your own
  51. byte my_ip[] = { 209, 134, 45, 3 }; // google will tell you: "public ip address"
  52. bool success; // Result of graph.init
  53.  
  54. /*-----( Declare objects )-----*/
  55. // arguments: username, api key, streaming token(s), filename, number of traces
  56. plotly graph = plotly("terryking", "y2zzw4ib0y", tokens, "filename", nTraces);
  57.  
  58. //----( Set up Temperature Measurement )----
  59. OneWire ourWire(ONE_WIRE_BUS);  // Set up a oneWire instance
  60.  
  61. /* Tell Dallas Temperature Library to use oneWire Library */
  62. DallasTemperature sensors(&ourWire);
  63.  
  64.  
  65.  
  66. void setup()  /****** SETUP: RUNS ONCE ******/
  67. {
  68.   // Open serial communications and wait for port to open:
  69.   Serial.begin(9600);
  70.   pinMode(LED_PIN, OUTPUT);
  71.   startEthernet();
  72.   graph.fileopt = "overwrite"; // See the "Usage" section in https://github.com/plotly/arduino-api for details
  73.  
  74.   success = graph.init();
  75.   if (!success) {
  76.     while (true) {
  77.     }
  78.   }
  79.   graph.openStream();
  80. } // END Setup
  81.  
  82.  
  83.  
  84. //unsigned long x;  // What ARE these for??
  85. //int y;
  86.  
  87. void loop()
  88. {
  89.   getTemperatures();
  90.  
  91.   graph.plot(millis(), OutDoorTempDS_F, tokens[0]);
  92.   graph.plot(millis(), InDoorTempDS_F , tokens[1]);
  93.  
  94.   delay(60000);  // 1 minute
  95.  
  96. }//--(end main loop )---
  97.  
  98. /*-----( Declare User-written Functions )-----*/
  99.  
  100. void startEthernet()
  101. {
  102.   Serial.println("... Initializing ethernet");
  103.   if (Ethernet.begin(mac) == 0)
  104.   {
  105.     Serial.println("... Failed to configure Ethernet using DHCP");
  106.     // no point in carrying on, so do nothing forevermore:
  107.     // try to congifure using IP address instead of DHCP:
  108.     Ethernet.begin(mac, my_ip);
  109.   }
  110.   Serial.println("... Done initializing ethernet");
  111.   delay(1000);
  112. }// END startEthernet
  113.  
  114.  
  115. void getTemperatures(void)
  116. {
  117.  
  118.   //  OutDoorTempDS_F = 8 ;
  119.   //  InDoorTempDS_F  = 65;
  120.   //-------------( DS18B20 Temperatures )-----------------
  121.   /*-----------( Read the DS18B20 Temperature Sensor, and check for errors. )-----*/
  122.  
  123.  
  124.  
  125.   //  Serial.println("=======(DS18B20 DATA )=============");
  126.  
  127.   //  Serial.println();
  128.   //  Serial.print("Requesting DS18B20 temperature...");
  129.   sensors.requestTemperatures(); // Send the command to get temperatures
  130.   //  Serial.println("DONE");
  131.  
  132.   OutDoorTempDS_C = sensors.getTempCByIndex(0);
  133.   //  Serial.print("Device 1 (index 0) = ");
  134.   //  Serial.print(OutDoorTempDS_C);
  135.   //  Serial.println(" Degrees C");
  136.  
  137.   OutDoorTempDS_F = sensors.getTempFByIndex(0);
  138.   //  Serial.print("Device 1 (index 0) = ");
  139.   //  Serial.print(OutDoorTempDS_F);
  140.   //  Serial.println(" Degrees F");
  141.  
  142.   InDoorTempDS_C = sensors.getTempCByIndex(1);
  143.   //  Serial.print("Device 2 (index 1) = ");
  144.   //  Serial.print(InDoorTempDS_C);
  145.   //  Serial.println(" Degrees C");
  146.  
  147.   InDoorTempDS_F = sensors.getTempFByIndex(1);
  148.   //  Serial.print("Device 2 (index 1) = ");
  149.   //  Serial.print(InDoorTempDS_F);
  150.   //  Serial.println(" Degrees F");
  151.  
  152.  
  153. } // END getTemperatures
  154.  
  155.  
  156. //*********( THE END )***********
Advertisement
Add Comment
Please, Sign In to add comment