ENC28J60 EtherShield  & HTTP GET

Hi all,

I'm thinking about getting an Ethernet Shield based on the ENC28J60 purely because of its low price.

My Goal is to use a URL Like:http://localhost/DataLogger.php?Name=Test&Value=255 To send the Sensor variables to a MYSQL database.

The above URL successfully updated the server.

I'm no expert so looking at the examples baffles me.

Has anyone got an example that can help?

The official Ethernet shield s £30 - £40 where as the ENC28J60 can be purchase for as little as £12.00 (ebay China).

Thanks In advance

Darrell

I'm thinking about getting an Ethernet Shield based on the ENC28J60 purely because of its low price.

I considered the same thing but decided to spend the extra $$$ for the official ethernet shild instead of having to fiddle around with a board that may take significant effort to get to work.

It will possibly need a bit more work, but you can use the ENC28J60 shield too. Have a look at this code, Download: The tuxgraphics TCP/IP stack, 3rd, 4th and 5th generation and also the EtherCard library at http://cafe.jeelabs.net/software/

Hi guys, I'm working on the same chipset, and also trying to write data to a php script, in the same format as the original poster. I've tried a few samples and I cant seem to get anything to talk to my server. is there a way to do a simple line such as:

es.ES_client_browse_url("GET /write.php?" && sensorName1 && "=" && sensorVal1);

is something along those lines possible?

es.ES_client_browse_url("GET /write.php?" && sensorName1 && "=" && sensorVal1);

&& is the logical AND operator. So, no that will not work to concatenate strings and values. You should be able to concatenate the strings and variables using sprintf() and a character array that is large enough, and then pass that character array to the function.

Thanks PaulS, This gave me what I needed, and I'm now posting to my php script with data correctly.

One last question: It seems sprintf() is limited to INT strings, and will not pass floats.
When I give the %s a float value, it fails to pass it to php, but if i give it an INT, it passes through.
That being said, through some manipulation, I am able to get it to populate data to two decimal points, but something is wrong with the way that is converting, as the numbers it generates are incorrect.

Is there a way I can get 2 decimal places of data to pass through?

The %s format specifier is to embed strings in the output. To embed integers, you should use the %d format.

You are correct that the %f format is not supported.

You could store the float into two integers:

float val = 12.56903;
int whole = val;
int frac = (float)(val - (float)whole + 0.0005) * 1000.0; // frac = 569

Then, use %d.%.3d as the format specifier, and whole and frac as the variable to be output. This will output the value with three decimal places. If you want just 2, change 1000.0 to 100.0, 0.0005 to 0.005, and %.3d to %.2d.

The %.3d (or %.2d) specifier tells sprintf to use leading 0s. Otherwise, 12.0043 would result in whole = 12, frac = 4, and 12.4 being output.

PaulS, thanks again, that did the trick for me, and i'm able to pass 2-decimal data to my php / MySQL now. I have one more question: The example code I found was from "Andy" at http://blog.thiseldo.co.uk/?p=418 and in his example, he has the whole part of writing to php with sprintf in a While() statement, which of course screws my whole Loop(), although it does of course write the data.

I've noticed one other strange behavior, (referencing my attached code), when I have the delay set longer than 30 seconds, it does not consistently write data to my database -- for example if I increase it to 60000 ms (60 sec). The only thing I can figure is that 60 seconds is too long and the socket connection to the server times out during that period. I'm sure I can fix that once it is out of the while() however.

I am trying to move it to a normal sub so I can call to it from my loop(), once new data is populated, or even more simple just have it in my loop(). However, I cannot get it to write data when I do that. Can someone offer some advice? Below is my sketch that works with the while(), can someone offer a method to move it out of the while() ?

thank you!

here is my full sketch:
http://bishoptechnology.com/pub/SunTrackerSQL.pde

Get rid of the while(1) statement that surrounds the code to log the event. The while loop doesn't surround everything you want to do, and is silly in a function that is called in an endless loop anyway.

Thanks again Paul... The only other issue with that, as I just commented out the while() line and it's matching } at the end, is the "continue;" which is only valid in a while, do, for. in this case it stops sending data to my php again. should "continue" be replaced with something else, or otherwise restructure this part of the code?

The continue says to abort the loop and begin the next pass (if there is to be one) right away. Since you've removed the loop, you should remove the continue, too.

I'm having trouble understanding why that seems to break the function, because when I remove the while () and the continue; the sketch no longer writes to php. I'm wondering why the example code calls the continue, which seems to indicate it bypasses that last line:
es.ES_www_server_reply(buf,dat_p);

and, under what conditions does "continue;" break at that point or not break and execute the above mentioned line?

That whole business of communicating with the server doesn't make any sense to me. It appears to try to ping the server. If that fails, it sends some data. If it succeeds, it reads the response.

If it can't ping the server, how the heck is it going to write to the server?

I don't understand why you are pinging the server if it isn't time to write to the server.

I don't understand why you call sunPos() AGAIN if it is time to write to the server. You've called sunPos() already in this ass through lloop().

I don't see the purpose of the variable resend, or start_web_client. I don't understand why start_web_client is declared volatile.

I don't understand your rationale for not dumping that code at the buttom and simply making it do what you want it to do.

I think we're both very much on the same page as far as the level of misunderstanding in this code!

I forgot to remove the call to sunPos(), which was necessary when the function was in a while() -- that allowed me to go outside of the while() to grab new data, otherwise the data was stale. That line should be removed.

I do not understand this portion of the code at all, and have not been able to find any documentation as to what it does. It seems excessively complex for just calling a string of variables to a php script.

The rationale of the code is unknown to me as well, as all I did was mash up some example snippets from the writer of the library, and have not been able to get it to do anything functional when I stray very far at all from the snippet, because I dont understand what it is doing exactly. the whole while() loop was certainly not my idea or preference, as it totally screws the rest of my sketch (albeit it writes data. some-freaking-how.).

Do you or anyone else participating in this string have recommendations for a different/better/more documented library for the ENC28J60 chipset?

Okay I am slowly going insane. I finally found an example using the EtherShield library that works perfectly without any WHILE() goofyness, and is very small. I also slimmed up the filename and variable lengths of my php script and variables to keep the buffer short. Here's that working example:
http://bishoptechnology.com/pub/working_SQL.pde

I carefully stitched that in to my code, and it fails. I commented out portions of my code that refer to other pieces of hardware, in the hopes it was just a conflict with the shield, but to no avail. any ideas? Buffer size problems? I am at a total loss as to why this is not writing to my php.
Here's my code:
http://bishoptechnology.com/pub/SunTrackerSQL.pde

I made some progress this morning, but need some senior Arduino / C++ help to fix this one: I took the "working" sample in my previous post, and stepped line by line adding from my "SunTrackerSQL.pde" until it finally broke.

It turns out that my Function "sunPos()" which does all the calculations necessary for this project, is what breaks it. I can have the Function in the code, but as soon as I call to it from the Loop(), then I am no longer writing data to php. remove/comment out the call, and it's writing again (also please note, at this point in debugging, I am calling the sunPos() function, but not using any of the data generated by it, still using static int's for my data for debugging purposes).

At this point it seems to me this is some sort of a memory overflow, as the calculations done by sunPos() are fairly heavy duty, and use a lot of float values. What at this point can I do to make this work?

Sorry for the incessant posting. Making more progress here: I found that it is actually not my "sunPos()" Function specifically that is killing the php data transmission, it is having any delay()'s called. Take the below snippet for example. If you insert a delay anywhere, or if any function called has a delay, it breaks.

void loop()
{
 static uint32_t timetosend;
 uint16_t dat_p;
 dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
  if (dat_p == 0)
  {
   if (millis() - timetosend > 10000)  // every 10 seconds
   {
    timetosend = millis();
    int ElevInt = 50;
    int ElevFrac = 05;
    int AziInt = 150;
    int AziFrac = 06;

    sprintf( statusstr, "?el=%d.%.2d&az=%d.%.2d", ElevInt, ElevFrac, AziInt, AziFrac );
    es.ES_client_browse_url(PSTR(HTTPPATH), statusstr, PSTR(HOSTNAME), &browserresult_callback);
    Serial.print(statusstr);
   }
  }
}