Project

General

Profile

(Resolved) Connecting to Thingspeak using EtherCard

Added by greg over 5 years ago

Does anyone have any code to connect to Thingspeak.

Pachube/Cosm/Xively seems to have gone downhill and gone commercial - I would like to connect to Thingspeak but cannot work out how to do this.

If it helps there is a client sketch for connection but it’s using Ethernet.h

http://community.thingspeak.com/arduino/ThingSpeakClient.pde

Update: Well after fiddeling around I eventually got it working!

stash.print(“field1=”);
stash.print((word) millis() / 123);
stash.print(“&field2=”);
stash.print((word) millis() / 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 ("POST /update HTTP/1.1" "\r\n"
"Host: \$F" "\r\n"
"Connection: close" "\r\n"
"X-THINGSPEAKAPIKEY: \$F" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\r\n"
"Content-Length: \$D" "\r\n"
"\r\n"
"\$H"),
website, PSTR (APIKEY), stash.size(), sd);

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


Replies (8)

RE: [Solved] Connecting to Thingspeak using EtherCard - Added by teding over 5 years ago

I went from thingspeak to exosite.com
checkout http://exosite.com/project/basic-arduino-temperature-web-monitor as a starter
Much better dashboard

This is part a my sketch, the stash.freeCount() part solves a bug with te stash running out of rescources

#define CIK "17eabb6163a46067xxxxxxxxxxxx"    // <-- FILL IN YOUR DEVICE CIK HERE (https://portals.exosite.com -> Add Device)!
char website[] PROGMEM = "m2.exosite.com" ; 


    byte sd = stash.create();
    stash.print("Barometer=");
    stash.print((float)PayL_10.pres/100,1);   
    stash.print("&T_outside=");
    stash.print((float)PayL_20.temp_c/100,1);  
    stash.print("&Power=");
    stash.print(PayL_25.tdiffE); 
    stash.print("&Solar=");
    stash.println(PayL_24.Gridpower);   
    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("POST /api:v1/stack/alias HTTP/1.1" "\r\n"
      "Host: $F" "\r\n"
      "X-Exosite-CIK: " "$F" "\r\n"
      "Content-Type: application/x-www-form-urlencoded; charset=utf-8" "\r\n"
      "Accept: application/xhtml+xml" "\r\n"
      "Content-Length: $D" "\r\n"
      "\r\n"
      "$H"),
    website, PSTR(CIK), stash.size(), sd);
    session = ether.tcpSend();     // send the packet - this also releases all stash buffers once done
    freeCount = stash.freeCount();
    if (freeCount <= 3) {   Stash::initMap(56); } 

  }
  reply = ether.tcpReply(session);
  if (reply != 0){
    Serial.println(F(" >>>REPLY recieved...."));
    Serial.println(reply);
  }

RE: [Solved] Connecting to Thingspeak using EtherCard - Added by greg over 5 years ago

Thanks for the code. I"ll take a look at Exosite as well.

I think the freecount statement just stopped my project from random crashing! :)

RE: [Solved] Connecting to Thingspeak using EtherCard - Added by greg over 5 years ago

I thought the freecount solved my problems but its still crashing. The Stash::initMap(56) made things better but it still crashes.

RE: [Solved] Connecting to Thingspeak using EtherCard - Added by teding over 5 years ago

check for
- freeram run low

int freeRam () {  
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

void setup(){
  Serial.begin(57600);
  Serial.print("Free Ram is: ");
  Serial.println(freeRam());
  • buffer to small to hold everthing

byte Ethernet::buffer[500]; //could be to small
</pre

RE: [Solved] Connecting to Thingspeak using EtherCard - Added by jcw over 5 years ago

The stash problem, as far as I can tell, seems to be related to a reply not coming in - so before the TCP/IP back-and-forth ends, a new request is sent out. The code probably doesn’t know how to cope with that and loses track of the old request, not cleaning up the stash usage.

RE: [Solved] Connecting to Thingspeak using EtherCard - Added by greg over 5 years ago

I’ve checked the freeram and available ram - more than enough. My buffer is also 700.

I’ve added in:

const char* reply = ether.tcpReply(session);
if (reply != 0) {
Serial.println(“>>> Response received—”);
}

I notice when reply !=0 it falls over. I’ve now tried to add in Stash::initMap(56); to the above so when it falls over it does a reset but that doesnt seem to help. The initMap doesnt seem to do anything on my sketch.

I can detect when it falls over - is there another way to completely reset the ethernet module? I’ve also tried the stash.cleanup();

To add to the that - when it does fall over I can no longer ping the arduino.

RE: [Solved] Connecting to Thingspeak using EtherCard - Added by teding over 5 years ago

I add a at the begin of tcpip.cpp in the ethercard libary.

#define SERIAL TRUE

And this to my sketch
and

  reply = ether.tcpReply(session);
  if (reply != 0){
    Serial.println(F(" >>>REPLY recieved...."));
    Serial.println(reply);
  }

this way you can see both in and outgoing tcp traffic on the serial mon.
Maybe this will help you to debug

    (1-8/8)