LED Control using Cosm and ENC28J60 as ethernet shield.

Hi guys, I have the Arduino dueminalove with ENC28J60 as the ethernet shield. I’m using the EtherCard library for all my programs. It contains an example to send random data to Cosm which goes like this :

#include <EtherCard.h>

// change these settings to match your own setup
#define FEED    "5942"
#define APIKEY  "bXkPFCiYm57f7flLyD86bm0HK3TXsfuQF-Jeyh3HeMg"

// ethernet interface mac address, must be unique on the LAN
byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

char website[] PROGMEM = "api.cosm.com";

byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;

void setup () {
  Serial.begin(57600);
  Serial.println("\n[webClient]");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
    
  ether.printIp("SRV: ", ether.hisip);
}

void loop () {
  ether.packetLoop(ether.packetReceive());
  
  if (millis() > timer) {
    timer = millis() + 10000;
    
    // generate two fake values as payload - by using a separate stash,
    // we can determine the size of the generated message ahead of time
    byte sd = stash.create();
    stash.print("0,");
    stash.println((word) millis() / 123);
    stash.print("1,");
    stash.println((word) micros() / 456);
    stash.save();
    
    // generate the header with payload - note that the stash size is used,
    // and that a "stash descriptor" is passed in as argument using "$H"
    Stash::prepare(PSTR("PUT http://$F/v2/feeds/$F.csv HTTP/1.0" "\r\n"
                        "Host: $F" "\r\n"
                        "X-CosmApiKey: $F" "\r\n"
                        "Content-Length: $D" "\r\n"
                        "\r\n"
                        "$H"),
            website, PSTR(FEED), website, PSTR(APIKEY), stash.size(), sd);

    // send the packet - this also releases all stash buffers once done
    ether.tcpSend();
  }
}

I uploaded the above code to the micro controller and it worked fine. I could see the changes in the data stream. Hence i conclude that the EtherCard library can be used to connect to Cosm.

While searching on google i came across this : http://community.cosm.com/node/112
But the thing is that all the codes in there are based on the Ethernet library for the Wiznet ethernet shield. So is there any way i can rewrite them using the EtherCard library? I really need this as I have to submit a project on Internet of Things in my university. Any help would be really appreciated.

Thanks in advance.

The Ethernet library takes advantage of the fact that the Wiznet handles the TCP stuff on-board. You'd have to write all that code your self to use the Ethernet library as-is.

That's a lot of work and why the ENC28J60 really isn't a good option in most cases for the Arduino.

ssd_20072: Hi guys, I have the Arduino dueminalove with ENC28J60 as the ethernet shield. But the thing is that all the codes in there are based on the Ethernet library for the Wiznet ethernet shield. So is there any way i can rewrite them using the EtherCard library? I really need this as I have to submit a project on Internet of Things in my university. Any help would be really appreciated.

I think the real solution is to ditch the ENC28J60, come forth with $20, and get a Wiznet, like god and cosm intended.

Having said that I believe it is possible to get a result, and there was something in the "Help me..." section of the cosm forum a month or so ago. It just looks more trouble than it's worth to march to a different drum, particularly if it's towards a deadline at uni.

well now that i have bought it so will give it a shot. I'm pretty sure that with some guidance i'll be able to do it. Have you got any links or tutorials for the codes and basicfunctions? Any guidance from u guys will be really appreciated. Thanks

As I said, there was something on the cosm forum recently. Good luck with that....... I'm sure you are aware that there are other internet of things facilities, like ThingSpeak, and you may find a more appropriate venue for your choice of hardware.

And note this gem..... http://arduino.cc/forum/index.php/topic,124577.15.html

Actually its quite simple to code using the EtherCard library.
I connected a DS18B20 temperature to my Arduino and wrote a simple code to post the sensor data to Cosm and it worked!!!

#include <EtherCard.h>
#include <OneWire.h>
#include <DallasTemperature.h>

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

// change these settings to match your own setup
#define FEED    "106182"
#define APIKEY  "ApiKey"

// ethernet interface mac address, must be unique on the LAN
byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

char website[] PROGMEM = "api.cosm.com";

byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;

static char statusstr[10];

void setup () {
  Serial.begin(57600);
  Serial.println("\n[webClient]");

  if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0) 
    Serial.println( "Failed to access Ethernet controller");
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
    
  ether.printIp("SRV: ", ether.hisip);
  
  sensors.begin();
  }

void loop () {
  
   if(Stash::freeCount()<5){ // reset EtherCard stash if running low
    Stash::initMap(56); 
  }
  
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

  if (millis() > timer) {
    timer = millis() + 30000;
    
    sensors.requestTemperatures();
    dtostrf(sensors.getTempCByIndex(0),4,4,statusstr);
    
    byte sd = stash.create();
    stash.print("0,");
    stash.println(statusstr);
    stash.save();
    
   Stash::prepare(PSTR("PUT http://$F/v2/feeds/$F.csv HTTP/1.0" "\r\n"
                        "Host: $F" "\r\n"
                        "X-PachubeApiKey: $F" "\r\n"
                        "Content-Length: $D" "\r\n"
                        "\r\n"
                        "$H"),
            website, PSTR(FEED), website, PSTR(APIKEY), stash.size(), sd);

    // send the packet - this also releases all stash buffers once done
    ether.tcpSend();
  }
}

Already started working on a code to control Arduino from Cosm. :wink:

Cheers!!

I see you don't include the cosm.h header, is it because you use ethercard library?

He doesn't use HttpClient either. Both of these are for use with the W5100 shield, while the Ethercard handles the ENC.

It is noteworthy that there is an increasing number of determined heroes using the ENC on cosm. Now that I have a W5100 to fall back on, I am considering putting an ENC module on a proto board and joining tham. This is simply because it will fit easier in the box I have.