How to use data.sparkfun with ENC28j60

Everything pertaining to the data.sparkfun.com service, the phant project which powers it, and user projects which talk to the service.

Moderators: phalanx, toddtreece, Brennen@SparkFun

Post Reply
WaggaBob
Posts: 1
Joined: Fri Jan 29, 2016 5:59 am

How to use data.sparkfun with ENC28j60

Post by WaggaBob » Fri Jan 29, 2016 6:25 am

Hi everyone!
I'm trying to break from the norm here by making my first post on these forums an answer rather than a question :D I saw these very cheap ethernet modules (Australian $8) which are know as ENC28j60. Way cheaper than the usual ethernet shields or adaptors I've seen so I grabbed one to muck around with. It's taken several days of fiddling but it's now posting happily to "https://data.sparkfun.com/franks_office". Below are some short snippets that might help someone else looking to use these modules, it's the stuff I wish I found a week ago when I googled "ENC28j60, phant" etc.

There are two libraries, Ethercard and UIPEthernet. I tried UIPEthernet, it's quite big and didn't seem to be quite the "drop in replacement" for the official Arduino ethernet library that it's claimed to be. So I tried using Ethercard... wow that's tricky to get sending data to Sparkfun! I ended up going back to UiPEthernet and it's now working but taking up a lot of memory.

Wiring - the CS pin goes to d8 if using the ethercard library but d10 if you use UIPEthernet!

I reduced my serial speed to 9600 otherwise I just got rubbish on the serial monitor. I think this was the problem with UIPEthernet at first.

And finally here is the code below, I've taken out a bunch of comments to try and keep the sketch size down (it's about 93%). I'm using a waterproof temperature sensor on the Dallas One-Wire system for the PC power supply temp and a DHT11 temperatiuree/humidity sensor for the room.

Code: Select all

#include <SPI.h> // Required to use Ethernet
#include <UIPEthernet.h> // The UIPEthernet library includes the client
#include <OneWire.h>
#include <DallasTemperature.h>
#include "DHT.h"

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#define DHTPIN 3     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

// Enter a MAC address for your controller below.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server(54,86,132,254);
EthernetClient client;

const String publicKey = "aGLo40WVjwS33J9EAqmy";
const String privateKey = "---insert your private key here---";
const byte NUM_FIELDS = 3;
const String fieldNames[NUM_FIELDS] = {"pc_temp", "room_temp", "room_humidity"};
String fieldData[NUM_FIELDS];

float heat;


void setup()
{
  sensors.begin(); // Start up the library
  dht.begin();
  Serial.begin(9600);
  setupEthernet(); // Set Up Ethernet
  Serial.println(F("=========== Ready to Post ==========="));
}


void loop()
{
    // Get temperature from One-wire sensor
    sensorReqTemp();

    // Get room temperature and humidity from DHT11
    //roomTemp = dht.readTemperature();
    //humid = dht.readHumidity();
        
    // Gather data:
    fieldData[0] = heat;
    fieldData[1] = dht.readTemperature();
    fieldData[2] = dht.readHumidity();
    
    Serial.println("Data to post:");
    for (int i=0; i<NUM_FIELDS; i++)
    {
      Serial.print(fieldNames[i]);
      Serial.print(" = ");
      Serial.println(fieldData[i]);
    }
    Serial.println("Posting!");
    postData(); // the postData() function does all the work, check it out below.

    delay(600000);
}


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&temp=24 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
  {
    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();
      Serial.print(c);
    }      
  }
  Serial.println();
  client.stop();
}

void setupEthernet()
{
  Serial.println("Setting up Ethernet...");
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    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);
  }
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
  // give the Ethernet shield a second to initialize:
  delay(1000);
}


void sensorReqTemp()
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  sensors.requestTemperatures(); // Send the command to get temperatures
  heat = (sensors.getTempCByIndex(0));
  // Serial.println(heat); // Why "byIndex"? 
  // You can have more than one IC on the same bus. 
  // 0 refers to the first IC on the wire
}
Hope that helps!

Post Reply