Cannot post to RPi from Arduino

OK I have pulled all my hair out and looked all over the place trying to figure out what I am not doing or doing wrong here.

1) I can post to the emoncms on my Raspberry Pi from a browser using any of the inputAPI help links. i.e.:

http://192.168.1.151/emoncms/input/post?node=1&csv=100,150,200

or

http://192.168.1.151/emoncms/input/post?node=1&csv=100,150,200&apikey=[my api key]

If I am not signed in with that browser.

Both of these work.

2) When I try to post to the site with my arduino I get this error message:

connecting...
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>501 Method Not Implemented</title>
</head><body>
<h1>Method Not Implemented</h1>
<p>http://192.168.1.151/emoncms/input/post?node=1&amp;csv=100,150,200&amp;apikey=[my api key] to /index.html not supported.<br />
</p>
<hr>
<address>Apache/2.2.22 (Debian) Server at 127.0.1.1 Port 80</address>
</body></html>

disconnecting.

I have tried changing the permissions of the entire www folder to 777 with no joy.

Here is the Arduino code:

----------------------------------------
#include <SPI.h>
#include <Ethernet.h>
#define APIKEY         "[my api key]" // Emon api key here when not logged in
//#define NODEID         2 // replace your node ID
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192,168,1,180);
// initialize the library instance:
EthernetClient client;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(192,168,1,151);      // numeric IP for my Emon server
//char server[] = "api.cosm.com";   // name address for cosm API
unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
boolean lastConnected = false;                 // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to cosm.com
int tempPin0 = 0; //the analog pin the LM34's Vout (sense) pin is connected to
                        //the resolution is 10 mV / degree farenheit
                      
void setup() {
  // start serial port:
  Serial.begin(115200);
// start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // DHCP failed, so use a fixed IP address:
    Ethernet.begin(mac, ip);
  }
}
void loop() {
  // read the analog sensor:
  //int sensorReading = analogRead(A0);
  float temp0 = getVoltage(tempPin0);  //getting the voltage reading from the temperature sensor
temp0 = temp0 * 100;
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
// if there's no net connection, but there was one last time
   // through the loop, then stop the client:
if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
// if you're not connected, and ten seconds have passed since
   // your last connection, then connect again and send data:
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    sendData(temp0);
  }
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void sendData(int thisData) {
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    //client.print("http://192.168.1.151/emoncms/input/post?node=1&csv=75,125,175");
    //client.print("http://192.168.1.151/emoncms/input/post?node=1&csv=75,125,175&apikey=[my api key]");
    //client.print(thisData);
    //client.println(" HTTP/1.0");
    //client.println("http://192.168.1.151/emoncms");
    //client.println("http://192.168.1.151/emoncms/input/post?json={power:200}&apikey=c830039bdc2c48534961534f401947fe");
    client.println("http://192.168.1.151/emoncms/input/post?node=1&csv=100,150,200&apikey=[my api key]");
    //client.println(thisData);
    //client.println("&apikey=");
    //client.println(APIKEY);
    //client.println(" HTTP/1.1");
    client.println();
    }
}
int getLength(int someValue) {
  // there's at least one byte:
  int digits = 1;
  // continually divide the value by ten,
  // adding one to the digit count for each
  // time you divide, until you're at 0:
  int dividend = someValue /10;
  while (dividend > 0) {
    dividend = dividend /10;
    digits++;
  }
  // return the number of digits:
  return digits;
}
/*
* getVoltage() - returns the voltage on the analog input defined by
* pin
*/
float getVoltage(int pin){
return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range
                                        // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}
---------------------------------------

As you can see I have many lines that I have tried and commented out because they did not work.

Any insight would be appreciated. I am out of patience.

ArchonOSX's picture

Re: Cannot post to RPi from Arduino

My son is studying programming and solved this one for me.

For anyone else with the same problem here is some help.

The url GET request must be used, and sent to the RPi phrased like so:

client.print("GET http://[YOURIPADDRESSHERE]/emoncms/input/post?csv=[yourvaluehere]");

to create a request with a variable as a value you need to phrase it like so:

client.print("GET http://[YOURIPADDRESSHERE]/emoncms/input/post?csv=");

client.print([YOURVARIABLEHERE]);

client.println("&apikey=[YOURAPIKEYHERE]");

Notice, you must use client.print for the first lines to construct the url. Only the LAST line get a client.println statement.

Thanks to anyone who read my post.

Hope this helps someone.

Comment viewing options

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