Here's the code for my Arduino MEGA2560 and whatever Ethernet shield I got.
It came from sparkfun's example page and I just added reading the six analog inputs, averaging several samples and converting to degree-F before pushing the data to sparkfun.
/*****************************************************************
Phant_Ethernet.ino
Post data to SparkFun's data stream server system (phant) using
an Arduino and an Ethernet Shield.
Jim Lindblom @ SparkFun Electronics
Original Creation Date: July 3, 2014
This sketch uses an Arduino Uno to POST sensor readings to
SparkFun's data logging streams (http://data.sparkfun.com). A post
will be initiated whenever pin 3 is connected to ground.
Before uploading this sketch, there are a number of global vars
that need adjusting:
1. Ethernet Stuff: Fill in your desired MAC and a static IP, even
if you're planning on having DCHP fill your IP in for you.
The static IP is only used as a fallback, if DHCP doesn't work.
2. Phant Stuff: Fill in your data stream's public, private, and
data keys before uploading!
Development environment specifics:
IDE: Arduino 1.0.5
Hardware Platform: RedBoard & PoEthernet Shield
This code is beerware; if you see me (or any other SparkFun
employee) at the local, and you've found our code helpful, please
buy us a round!
Much of this code is largely based on David Mellis' WebClient
example in the Ethernet library.
Distributed as-is; no warranty is given.
*****************************************************************/
#include <SPI.h> // Required to use Ethernet
#include <EthernetV2_0.h> // The Ethernet library includes the client
///////////////////////
// Ethernet Settings //
///////////////////////
// Enter a MAC address for your controller below.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(54,86,132,254); // numeric IP for data.sparkfun.com
char server[] = "data.sparkfun.com"; // name address for data.sparkFun (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,2,200);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
/////////////////
// Phant Stuff //
/////////////////
const String publicKey = "YOURPUBLIKKEY";
const String privateKey = "YOURPRIVATEKEY";
const byte NUM_FIELDS = 6;
const String fieldNames[NUM_FIELDS] = {"boilerout", "boilerreturn", "zone2out", "zone2return", "zone3out", "zone3return"};
String fieldData[NUM_FIELDS];
#define BOILEROUT 0
#define BOILERRETURN 1
#define ZONE2OUT 2
#define ZONE2RETURN 3
#define ZONE3OUT 4
#define ZONE3RETURN 5
#define LEDPIN 13
//////////////////////
// Input Pins, Misc //
//////////////////////
#define SDCARD_CS 4
bool print = 0;
void setup()
{
// Set Up Ethernet:
// start serial port:
Serial.begin(9600);
pinMode(SDCARD_CS,OUTPUT);
pinMode(0, INPUT);
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(LEDPIN, OUTPUT);
digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card
// give the ethernet module time to boot up:
delay(1000);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
if(print) Serial.println("Failed to configure Ethernet using DHCP");
}
Serial.println(F("=========== Ready to Stream ==========="));
}
void loop()
{
int i,j;
float temps[NUM_FIELDS][10];
float average[NUM_FIELDS];
char buffer[80]; // Buffer to convert from binary to ASCII
// Read each sensor ten times at a rate of 1 Hz. THis takes (10 times NUM_SAFIELDS) in seconds (60 for this example)
for(i=0;i<10;i++) {
for(j=8;j<8+NUM_FIELDS;j++) {
analogRead(j);
delay(10);
analogRead(j);
delay(10);
temps[j-8][i] = ((((analogRead(j) * 5.0) / 1024.0) - 0.5) * 100 * 9.0 / 5.0) + 32.0;
if(print) Serial.print(j-8); if(print) Serial.print(" ");if(print) Serial.println(temps[j-8][i]);
delay(166);
}
}
// Calculate the sum of the last ten samples
for(j=0;j<NUM_FIELDS;j++) {
average[j] = 0.0;
}
for(i=0;i<10;i++) {
for(j=0;j<NUM_FIELDS;j++) {
average[j] += temps[j][i];
}
}
// Calculate the average of the samples and fill in the output fieldData array
for(j=0;j<NUM_FIELDS;j++) {
fieldData[j] = dtostrf(average[j] / 10.0, 3, 1, buffer);
if(print) Serial.print(fieldData[j]); if(print) Serial.print(" ");
}
if(print) Serial.println();
digitalWrite(LEDPIN, HIGH);
if(print) Serial.println("Posting!");
postData(); // the postData() function does all the work
digitalWrite(LEDPIN, LOW);
}
void postData()
{
// Make a TCP connection to remote host
if (client.connect(server, 80))
{
// Post the data! Request should look a little something like:
// GET /input/publicKey?private_key=privateKey&light=1024&switch=0&name=Jim HTTP/1.1\n
// Host: data.sparkfun.com\n
// Connection: close\n
// \n
client.print("GET /input/");
client.print(publicKey);
client.print("?private_key=");
client.print(privateKey);
for (int i=0; i<NUM_FIELDS; i++)
{
client.print("&");
client.print(fieldNames[i]);
client.print("=");
client.print(fieldData[i]);
}
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
}
else
{
if(print) Serial.println(F("Connection failed"));
}
// Check for a response from the server, and route it
// out the serial port.
while (client.connected())
{
if ( client.available() )
{
char c = client.read();
if(1) Serial.print(c);
}
}
if(1) Serial.println();
client.stop();
}
void setupEthernet()
{
if(print) Serial.println("Setting up Ethernet...");
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
if(print) Serial.println(F("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, ip);
}
if(print) Serial.print("My IP address: ");
if(print) Serial.println(Ethernet.localIP());
// give the Ethernet shield a second to initialize:
delay(1000);
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.