can't maintain connection with Arduino ethernet shield & client

I'm having trouble maintaining the connection between my Arduino (as it says in the title, with an official ethernet shield, using the client libraries) and a emoncms setup on a debian virtual machine on the local network.

Here is how I handle mac and server addresses (at least, this gives me the most success) :

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte server[] = {192, 168, 1, 107};

EthernetClient client;

After this within setup I connect using 'Ethernet.begin(mac)', and 'client.connect(server, 80)' within if statements that tell me that I am successfully connected.

After I move to loop I have 'client.available()' in an if statement, and it is always coming up false, skipping my if statement block. I'm not sure what is causing my connection to become lost, any ideas?

kisskovi's picture

Re: can't maintain connection with Arduino ethernet shield & client

Hi,

try, using client.connected instead of client.available 
else seems ok to me

cheers,

Kent88's picture

Re: can't maintain connection with Arduino ethernet shield & client

Thank you, my connection is now working, but I'm still not getting any information sent to my emoncms server.

I'm trying to send temperatures, I have an input "localTemp" and I am trying to send the contents of a float variable 'localTempF', and I am trying to send it like so:

client.println("GET /emoncms3/api/post.json?apikey=(retractedAPIkey)&json={localTemp:localTempF} HTTP/1.0");

I have a feeling that this is sending this all as a string rather than sending the value at any point. How can I modify this so that emoncms can see the information properly?

Robert Wall's picture

Re: can't maintain connection with Arduino ethernet shield & client

I take it the language is Arduino C. It looks as if it is indeed sending a string - the whole thing including the variable name is inside the quote marks. That means it won't be recognised as a variable name. You need 3 print statements:

client.print("GET /emoncms3/api/post.json?apikey=(retractedAPIkey)&json={localTemp:");
client.print(localTempF);
client.println("} HTTP/1.0");

You cannot build a print command using the standard Serial class as you can with printf, so it has to be done like this. Note there are no quotes around localTempF which makes it print the value. The other two statements print the text enclosed by the quotes.

Kent88's picture

Re: can't maintain connection with Arduino ethernet shield & client

I broke it down to three lines (two print and lastly a println) and still no go, nothing is showing up on my emoncms server.

Interestingly, when I accidentally had the first line as println I sent a 0 value to the server.

Then, when I adjusted it so that the second line had println the correct value was sent, but it appears to eventually lose the connection.

Robert Wall's picture

Re: can't maintain connection with Arduino ethernet shield & client

I should have looked up the correct data format instead of just correcting the obvious mistake! Take a look here for the format you require: http://openenergymonitor.org/emon/emoncms/using-emoncms#Sending_data_to_...

So you need to be doing:

client.print("/emoncms3/api/post?apikey= (retractedAPIkey)(retractedAPIkey) &json={localTemp:");
client.print(localTempF);
client.println("}");

If you take apart the nanode emonBase code in GitHub, you'll see how to build the string in the correct format.

 

Kent88's picture

Re: can't maintain connection with Arduino ethernet shield & client

actually, I just removed the back bracket '}' and it worked. Don't ask me why. I'm going to capture a 'snapshot' of all I've got so far, and then start tinkering and see stuff like what you just posted works.

I appreciate the help, after all, I'll probably need more soon.

Comment viewing options

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