Remote Control Relay with emoncms.org and Arduino

I wish to control a relay through emoncms.org from a web page located on my Android mobile phone.
To do this, I had thought of creating a feed. So, I can update the value of the feed through the website or web page located in my phone or in a PC. In my house, an Arduino with Ethernet module must be able to read the feed and depending on the values ​​received, switch on or off the relay.
I have created the input and the corresponding feed. From a browser I can give values to the feed. But the problem is that I don't know how Arduino can read the last value of that feed. 

I know that sentence is like: http://emoncms.org/feed/value.json?id=32427&apikey=xxxxxxxxxxx

But how to transfer this into the arduino with the GET method?

I've tried this (Arduino web client sample modification):

 

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
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:

char server[] = "www.emoncms.org";    // name address for Google (using DNS)
char apikey[] = "myapikey";

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,0,177);

// 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;

void setup() {
// Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("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);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
   
    client.print("GET /feed/value.json?apikey=");
    client.print(apikey);
    client.print("?id=32427");
    client.println(" HTTP/1.1");
    
    client.println("Host:emoncms.org");
    client.println("User-Agent: Arduino-ethernet");
    client.println("Connection: close");
    client.println();
  }
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}

 

 

But the response is:

connecting...
connected
HTTP/1.1 200 OK
Date: Tue, 04 Mar 2014 21:37:29 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.9
Vary: Accept-Encoding
Content-Length: 49
Connection: close
Content-Type: text/html

{"success":false,"message":"Feed does not exist"}
disconnecting.

 

 

I´ve tried with another id feed and is the same return...

Can anyone help me ?

Thanks.

 

 

Schism's picture

Re: Remote Control Relay with emoncms.org and Arduino

To make life easier for yourself, you can just do the request to emonCMS manually in the browser until it works :)

I think your problem is likely to be this:

client.print("GET /feed/value.json?apikey=");
    client.print(apikey);
    client.print("?id=32427");

separate URL parameters with "&" ...

aicanduit's picture

Re: Remote Control Relay with emoncms.org and Arduino

 

Aaaaaargh !

Thank you very much ! Problem solved !

before:

   client.print("?id=32427");

after:

   client.print("&id=32427");

arwed's picture

Re: Remote Control Relay with emoncms.org and Arduino

Hi, I also want to do something like this. I tried another approach: From a webpage I send an update to a specific input according the Input API helper via e.g.

http://emoncms/emoncms/input/post.json?node=30&json={setStatus:1,setpoint1:1000,setpoint2:550,setpoint3:400}

to modify setpoint inputs in my local emoncms installation. These inputs are processed into feeds as usual.

Then I did a little hacking in the paketgen_module.php so that I can define variables there to grap a diven feed value and broadcast it to all nodes that are interested in. This sounds a little bit cumbersome but it gives a bit more flexibility by getting different ways to modify setpoints: 1) via API call as above 2) from a room thermostat like node that has up and down buttons to modify setpoint inputs and feeds via the RFM12 payload.

Another node, receiving the setpoint values from emoncms, than controls the heater. With that way you don't need network capabilities on your arduino, only a simple emontx node with RFM12.

I hope my description is understandable.

A.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.