PlotlyExampleTemperature01

From ArduinoInfo
Jump to: navigation, search

Plotly ( https://plot.ly) Web Plot of Arduino DS18B20 Temperatures


(Under Construction!)

/* YourDuinoStarter Example: Sending and Plotting data on the Net with plot.ly
 - WHAT IT DOES: Sends outdoor and indoor temperature to plot.ly server which plots it.
 - SEE the comments after "//" on each line below
 - CONNECTIONS:

 - DS18B20 Sensors:   +5V, Gnd, Signals to pin 3
 -
 - V1.20 02/13/2015 Adding DS18B20 Code


 Sign up to plotly here: https://plot.ly
 View your API key and stream tokens here: https://plot.ly/settings
 Download the plot.ly Arduino library here: https://github.com/plotly/arduino-api
 UnZip and copy the plotly_streaming_ethernet folder to your libraries folder
 Get your external IP address: https://www.google.com/search?as_q=public+ip+address
 -----( You must enter the following items specifically for YOUR setup )-----
 Username terryking
 API Key  y2zzw4ib0y
 Your tokens  zvxy5hd1vf  2p1iitbjhg
 Your IP address:  209.134.45.3
 -----------------------------------------------------------------
 */

/*-----( Import needed libraries )-----*/
#include <Ethernet.h>
#include <SPI.h>
#include "plotly_streaming_ethernet.h"

// Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html
#include <OneWire.h>      // 1-wire bus including DS18B20 

//Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>  // Read temperature from DS18B20

/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS 3 // DS18B20 on Pin 3
#define nTraces 2    // Number of traces on the graph
#define LED_PIN 13   // Onboard LED

/*-----( Declare Variables )-----*/
// DS18B20 Temperature Sensor Variables
int    OutDoorTempDS_C;  // Values returned from DS18B20 chip
int    OutDoorTempDS_F;
int    InDoorTempDS_C;
int    InDoorTempDS_F;

// Supply as many tokens as data traces
// e.g. if you want to ploty A0 and A1 vs time, supply two tokens
char *tokens[nTraces] = { "zvxy5hd1vf", "2p1iitbjhg"};
byte mac[] = { 0xCA, 0xBC, 0xAB, 0xCA, 0xB0, 0x08 }; // Or make up your own
byte my_ip[] = { 209, 134, 45, 3 }; // google will tell you: "public ip address"
bool success; // Result of graph.init

/*-----( Declare objects )-----*/
// arguments: username, api key, streaming token(s), filename, number of traces
plotly graph = plotly("terryking", "y2zzw4ib0y", tokens, "filename", nTraces);

//----( Set up Temperature Measurement )----
OneWire ourWire(ONE_WIRE_BUS);  // Set up a oneWire instance

/* Tell Dallas Temperature Library to use oneWire Library */
DallasTemperature sensors(&ourWire);



void setup()  /****** SETUP: RUNS ONCE ******/
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);
  startEthernet();
  graph.fileopt = "overwrite"; // See the "Usage" section in https://github.com/plotly/arduino-api for details

  success = graph.init();
  if (!success) {
    while (true) {
    }
  }
  graph.openStream();
} // END Setup



//unsigned long x;  // What ARE these for??
//int y;

void loop()
{
  getTemperatures();

  graph.plot(millis(), OutDoorTempDS_F, tokens[0]);
  graph.plot(millis(), InDoorTempDS_F , tokens[1]);

  delay(60000);  // 1 minute

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

void startEthernet()
{
  Serial.println("... Initializing ethernet");
  if (Ethernet.begin(mac) == 0)
  {
    Serial.println("... Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, my_ip);
  }
  Serial.println("... Done initializing ethernet");
  delay(1000);
}// END startEthernet


void getTemperatures(void)
{

  //  OutDoorTempDS_F = 8 ;
  //  InDoorTempDS_F  = 65;
  //-------------( DS18B20 Temperatures )-----------------
  /*-----------( Read the DS18B20 Temperature Sensor, and check for errors. )-----*/



  //  Serial.println("=======(DS18B20 DATA )=============");

  //  Serial.println();
  //  Serial.print("Requesting DS18B20 temperature...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  //  Serial.println("DONE");

  OutDoorTempDS_C = sensors.getTempCByIndex(0);
  //  Serial.print("Device 1 (index 0) = ");
  //  Serial.print(OutDoorTempDS_C);
  //  Serial.println(" Degrees C");

  OutDoorTempDS_F = sensors.getTempFByIndex(0);
  //  Serial.print("Device 1 (index 0) = ");
  //  Serial.print(OutDoorTempDS_F);
  //  Serial.println(" Degrees F");

  InDoorTempDS_C = sensors.getTempCByIndex(1);
  //  Serial.print("Device 2 (index 1) = ");
  //  Serial.print(InDoorTempDS_C);
  //  Serial.println(" Degrees C");

  InDoorTempDS_F = sensors.getTempFByIndex(1);
  //  Serial.print("Device 2 (index 1) = ");
  //  Serial.print(InDoorTempDS_F);
  //  Serial.println(" Degrees F");


} // END getTemperatures


//*********( THE END )***********