I'm new to the arduino and need help. I understand what I am looking at, but I have a hard time understanding how different lines of code relate to one another. Below is my code for an arduino + ethernet shield + a one wire temp sensor. I have placed numbers like this: -------1------- in the code that relate to questions below. If you are kind enough to answer, assume I know nothing (because I don't). For more info on pachube visit www.pachube.com.
/* ==============================
* This code, which assumes you're using the official Arduino Ethernet shield,
* updates a Pachube feed with your analog-in values and grabs values from a Pachube
* feed - basically it enables you to have both "local" and "remote" sensors.
*
* Tested with Arduino 14
*
* Pachube is www.pachube.com - connect, tag and share real time sensor data
* code by usman (www.haque.co.uk), may 2009
* copy, distribute, whatever, as you like.
*
* v1.1 - added User-Agent & fixed HTTP parser for new Pachube headers
* and check millis() for when it wraps around
*
* =============================== */
#include <Ethernet.h>
#include <string.h>
#undef int() // needed by arduino 0011 to allow use of stdio
#include <stdio.h> // for function sprintf
#define SHARE_FEED_ID 2970 // this is your Pachube feed ID that you want to share to
#define REMOTE_FEED_ID 310 // this is the ID of the remote Pachube feed that you want to connect to
#define REMOTE_FEED_DATASTREAMS 4 // make sure that remoteSensor array is big enough to fit all the remote data streams -------1-------
#define UPDATE_INTERVAL 10000 // if the connection is good wait 10 seconds before updating again - should not be less than 5
#define RESET_INTERVAL 10000 // if connection fails/resets wait 10 seconds before trying again - should not be less than 5
#define PACHUBE_API_KEY ***// fill in your API key -> I know to fill mine in here
byte mac[] = { 0xAA, 0xAB, 0xBB, 0xBC, 0xCC, 0x99 }; // make sure this is unique on your network -------2-------
byte ip[] = { 192, 168, 0, 144 }; // no DHCP so we set our own IP address -------3-------
byte remoteServer[] = { 209,40,205,190 }; // pachube.com
float remoteSensor[REMOTE_FEED_DATASTREAMS]; // we know that feed 256 has floats - this might need changing for feeds without floats
void setup()
{
Serial.begin(57600);
setupEthernet();
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop()
{
// call 'pachube_in_out' at the beginning of the loop, handles timing, requesting
// and reading. use serial monitor to view debug messages
pachube_in_out();
// then put your code here, you can access remote sensor values
// by using the remoteSensor float array, e.g.: -------4-------
analogWrite(3, (int)remoteSensor[3] * 10); // remoteSensor is a float // -------5a-------
analogWrite(5, (int)remoteSensor[1]);
//my code starts here:
//TMP36 Pin Variables
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to -------5b-------
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures
/*
* setup() - this function runs once when you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup()
{
Serial.begin(9600); //Start the serial connection with the computer
//to view the result open the serial monitor
}
void loop() // run over and over again
{
//getting the voltage reading from the temperature sensor
int reading = analogRead(sensorPin);
// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 5.0 / 1024;
// print out the voltage
Serial.print(voltage); Serial.println(" volts");
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((volatge - 500mV) times 100)
Serial.print(temperatureC); Serial.println(" degress C");
// now convert to Fahrenheight
float temperatureF = (temperatureC * 9 / 5) + 32;
Serial.print(temperatureF); Serial.println(" degress F");
delay(1000); //waiting a second
}
//my code ends here.
// you can have code that is time sensitive (e.g. using 'delay'), but
// be aware that it will be affected by a short pause during connecting
// to and reading from ethernet (approx. 0.5 to 1 sec).
// e.g. this code should carry on flashing regularly, with brief pauses
// every few seconds during Pachube update.
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(100);
}
How do i know the number of datastreams? Is it just how many sensors you have?
Can the mac address be anything or is there a for this must conform to?
Do you have to set your own IP? Or was the example saying they don't have dhcp so they had to assign one?
Do I put my code there or does it go where I put it? See comments //my code starts here and //my code ends here.
5a/b Do I need to change any of this so that it reads the correct sensor and if so what do I change.
What else do I need to know/do.
I just want to get a working feed onto pachube. Any help is appreciated. I hope that I could eventually put together a step-by-step tutorial that is a little more detailed than those available from the pachube site. Thanks in advance.
I looked at the code and it looks like you have two loop functions and one is incomplete.
I am working on a similar project myself. I have an ethernet shield (got it this week) and a tmp36 sensor (well more then 1).
I took the webserver example and modified it to show the temperature instead of the raw readings from the analog pins. I think this is a much easier way to go then trying to use a tmp36 example.
I will try to answer some of your questions:
I don't know where you got this code, but it looks like it is trying to write something to a analog pins, that is not what you want to do.
Yes, as long as it doesn't conflict with anything else on the network. I left it as is. The chance of having something that conflicts is extremely small.
Yes, you have to assign an ip. You will want to use something in your network. My network is 192.168.0.0, so I am using 192.168.0.66 for the ethernet shield.
Ok now I see where you got the 2 loop() functions you pasted some code from a tmp36 example including the setup() and loop() function. You can only have 1 of each.
5a. Again I don't know where you got this code and why it is trying to write to the analog pin. This is something you don't want to do.
5b. Yes, you would need to have the sensor pin be the same as the one that has your sensor on.
Start with something that works if you are going to play with the code. Start with some of the examples and expand on them a little at at time, making sure they work at each little step. Don't try to paste 2 example together, that is a little more difficult to get working.
Here is the code that I wrote last night and cleaned up this morning. It uses the ip 192.168.0.66 and reads all the analog ports and gets the sensor readings. I calculate an average temperature for 60 seconds, and also display the realtime/current temperature. I only have one sensor on pin 1 right now, but the code will work for all or one pin having an sensor. You can just ignore all the other sensors or fix the loops to only look at 1 pin. Just use a web browser and point it to the ip of your ethernet shield (192.168.0.66). It should display something like:
#include <Ethernet.h>
// mac and ip address of the ethernet interface
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 66 };
// run the server on port 80
Server server(80);
// average temperature information
int temp[6][10];
int tempSize = 10;
int tempIndex = 0;
unsigned long tempTime = 0;
//----------------------------------------------------------------------------
// setup the ethernet interface and start the server
//----------------------------------------------------------------------------
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
}
//----------------------------------------------------------------------------
// records the temperature into an array for getting the average temperature
//----------------------------------------------------------------------------
void recordTemp()
{
// get the current time
unsigned long time = millis();
// get the temperature every 6 seconds
if (time - tempTime < 6000) {
return;
}
// set the last time we got the temperature to now
tempTime = time;
// loop over the sensors and record the temperature
for (int i = 0; i < 6; i++) {
int reading = analogRead(i);
temp[i][tempIndex] = reading;
}
// increment the index and make sure it doesn't go over the size
tempIndex++;
tempIndex = tempIndex % tempSize;
}
//----------------------------------------------------------------------------
// calculate the average temerature based on the index/sensor passed in
//----------------------------------------------------------------------------
float getTemp(byte index)
{
// add up the total for the array element
float total = 0;
for (int i = 0; i < tempSize; i++) {
total += temp[index][i];
}
// calculate the average reading
float readings = total / tempSize;
// convert the readings to temerature (F)
float voltage = readings * 5.0 / 1024;
return voltage * 100;
}
//----------------------------------------------------------------------------
// prints out the temerature infomation in html
//----------------------------------------------------------------------------
void printTemperatureInfoInHtml(Client &client)
{
// send a standard http response header and html tags
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html><body><pre>");
for (int i = 0; i < 6; i++) {
client.print(i);
// output average temperature
client.print(" - avg temperture: ");
client.print(getTemp(i));
client.print(", realtime temp: ");
int reading = analogRead(i);
float voltage = reading * 5.0 / 1024;
float tmp = voltage * 100;
client.println(tmp);
}
client.println("</pre></body></html>");
}
//----------------------------------------------------------------------------
// takes care of the http client connecting to the server
// prints out the temperature information when it does
//----------------------------------------------------------------------------
void handleClientConnection()
{
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (c == '\n' && current_line_is_blank) {
// print out the temerature information to the client
printTemperatureInfoInHtml(client);
break;
}
if (c == '\n') {
// we're starting a new line
current_line_is_blank = true;
} else if (c != '\r') {
// we've gotten a character on the current line
current_line_is_blank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}
//----------------------------------------------------------------------------
// the main loop
//----------------------------------------------------------------------------
void loop()
{
// record the temerature for all the sensors (0 - 5)
recordTemp();
// handle the client connecting to the server
handleClientConnection();
// sleep for a bit, we don't need to be in a hurry (does this save power?)
delay(100);
}