Thingspeak Programming

Hello,

I have been playing around with thing speak the last 2 days and am having some trouble. Examples are limited online for Thingspeak programming for the Arduino. To start, I took the Thingspeak code and loaded to the Uno. i am posting Field 1 to the Thingspeak Channel. But when i make the changes to the code according to the Thingspeak website, there is never an option for adding an additional window for field 2.

Here is the code provided by Thingspeak as an example for updating 2 fields:

#include <SPI.h>
#include <Ethernet.h>

// Local Network Settings
byte mac[]     = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network

// ThingSpeak Settings
byte server[]  = { 184, 106, 153, 149 }; // IP Address for the ThingSpeak API
String writeAPIKey = "WDSIVUV36PJP22W0";    // Write API Key for a ThingSpeak Channel
const int updateInterval = 15000;        // Time interval in milliseconds to update ThingSpeak   
EthernetClient client;

// Variable Setup
long lastConnectionTime = 0; 
boolean lastConnected = false;
int resetCounter = 0;

void setup()
{
  Ethernet.begin(mac);
  Serial.begin(9600);
  delay(1000);
}

void loop()
{
  String analogPin0 = String(analogRead(A0), DEC);
  String analogPin1 = String(analogRead(A1), DEC);
  
  // Print Update Response to Serial Monitor
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }
  
  // Disconnect from ThingSpeak
  if (!client.connected() && lastConnected)
  {
    Serial.println();
    Serial.println("...disconnected.");
    Serial.println();
    
    client.stop();
  }
  
  // Update ThingSpeak
  if(!client.connected() && (millis() - lastConnectionTime > updateInterval))
  {
    updateThingSpeak("field1="+analogPin0+"&field2="+analogPin1);
  }
  
  lastConnected = client.connected();
}

void updateThingSpeak(String tsData)
{
 if(client.connect(server, 80)>0)
  { 
    Serial.println("Connected to ThingSpeak...");
    Serial.println();
        
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(tsData.length());
    client.print("\n\n");

    client.print(tsData);
    
    lastConnectionTime = millis();
    
    resetCounter = 0;
    
  }
  Serial.println(analogRead(A0));
}

UPDATE 17 OCTOBER 2014:

The above code did work. I am very unfamiliar with using Thingpspeak and did not know you had to to into Channel Settings and add Field 2.

{
    updateThingSpeak ("field1="+analogPin0+"&field2="+analogPin1);
  }

I don’t think you can do that. Try:

{
    updateThingSpeak("field1="+analogPin0); 
    updateThingSpeak("field2="+analogPin1);
  }

And please, always post your code in code tags. Use the # button above the smileys.

String analogPin0 = String(analogRead(A0), DEC);

That is a dumb name for a variable that holds the string representation of the value read from pin 0.

Henry_Best: I don't think you can do that. Try:

{
    updateThingSpeak("field1="+analogPin0); 
    updateThingSpeak("field2="+analogPin1);
  }

And please, always post your code in code tags. Use the # button above the smileys.

I tried the suggestion above with no luck.

And I did take your suggestion with the # button. My apologies!

Thanks

PaulS: String analogPin0 = String(analogRead(A0), DEC);

That is a dumb name for a variable that holds the string representation of the value read from pin 0.

The code from above is a direct copy from the Thingspeak example on their website.

In which ways would you change it?

The code from above is a direct copy from the Thingspeak example on their website.

It's still stupid. Generally, variables that have pin in the name contain pin numbers. Those that have state in the name contain state information read from the digital pin. Those that have value in the name contain values.

Not to mention that that bit of code calls THREE String constructors when none is really needed.

sprintf() and a char array beats the hell out of the String class.

PaulS:

The code from above is a direct copy from the Thingspeak example on their website.

It's still stupid. Generally, variables that have pin in the name contain pin numbers. Those that have state in the name contain state information read from the digital pin. Those that have value in the name contain values.

Not to mention that that bit of code calls THREE String constructors when none is really needed.

sprintf() and a char array beats the hell out of the String class.

Since I am only a few weeks into programming and trying to learn as much as I can. According to the ThingSpeak site, updateThingSpeak expects a string value. Thats why it is converted to a string.

Check this out. Look toward the bottom of the page under Common Issues - http://community.thingspeak.com/tutorials/arduino/using-an-arduino-ethernet-shield-to-update-a-thingspeak-channel/

Now, I know that the examples on their website are very poor being that they are outdated, but i can't find anything else that will work.

Do you have any suggestion as to how the updateThingSpeak code should change from what I have?

According to the ThingSpeak site, updateThingSpeak expects a string value.

Then, it's stupid. C strings (NULL terminated arrays of chars) were around decades before the stupid String class was developed. They ARE the preferred way of dealing with character data.