In a previous post I described PHP scripts that would store sensor data in a CSV file and display the data in a graph.
Now I wanted to give two Arduino/ENC28J60 programs that would send that data. One using POST, the other one using GET, using the EtherCard library. That proved a bit harder than I thought.
Using POST was easy, the program below does that. It reads the value of two analog value and sends those to a server side PHP script that stores the data in a CSV file
#include <EtherCard.h> #define PATH "POST-data.php" // ethernet interface mac address, must be unique on the LAN byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 }; const char website[] PROGMEM = "yoursite.com";// in case you use that byte Ethernet::buffer[700]; uint32_t timer; Stash stash; void setup () { Serial.begin(57600); Serial.println("POST Data"); if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0) Serial.println( "Failed to access Ethernet controller"); if (!ether.dhcpSetup()) Serial.println("DHCP failed"); ether.printIp("IP: ", ether.myip); ether.printIp("GW: ", ether.gwip); ether.printIp("DNS: ", ether.dnsip); if (!ether.dnsLookup(website)) Serial.println("DNS failed"); ether.parseIp (ether.hisip, "192.168.2.10");// in case of local server ether.printIp("SRV: ", ether.hisip); //in case of local server } void loop () { ether.packetLoop(ether.packetReceive()); if (millis() > timer) { timer = millis() + 60000; byte val = random(255); String val1 = String(val); val = random(100) * analogRead(A2) / 4; String val2 = String(val); byte sd = stash.create(); stash.print("temp="); stash.print(val1); stash.print("&"); stash.print("temp2="); stash.print(val2); stash.save(); Stash::prepare(PSTR("POST /$F HTTP/1.1" "\r\n" "Host: $F" "\r\n" "Content-Length: $D" "\r\n" "Content-Type: application/x-www-form-urlencoded" "\r\n" "\r\n" "$H"), PSTR(PATH), website, stash.size(), sd); // send the packet - this also releases all stash buffers once done ether.tcpSend(); } }
Trying this with GET, proved a lot harder, and in fact I have to admitt defeat here. I just could not get it done, even when I tested code that was said to work, it didnt. It seems the problem has mainly to do with writing to a local server, not a distant server, though that ‘bug’ was supposedly fixed. Another issue is the sparse documentation on the ‘Stash’ routine.
Sure, doing a GET request with a W5100 based board is not that hard and I presume using the UIPEthernetlibrary also makes it easier, but I wanted to try with Ethercard. I guess I failed. It is quite possible to do a GET call to a remote website, but to a local server remains a problem
So I fell back to the UIPEthernet library:
#include <SPI.h> #include <UIPEthernet.h> //https://github.com/ntruchsess/arduino_uip/releases/tag/UIPEthernet_v1.59 int dd = 0;// mock variable 1 String ddd = String(dd); int ww = 255;// mock variable2 String www = String(ww); byte mac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 }; // 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, 2, 10); // numeric IP for website (no DNS) //char server[] = "yourwebsite.com"; // name address for website (using DNS) // Set the static IP address to use if the DHCP fails to assign IPAddress ip(192, 168, 2, 40); // 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); Serial.println("start"); // start the Ethernet connection: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); // try to congifure using IP address instead of DHCP: Ethernet.begin(mac, ip); } // give the Ethernet shield a second to initialize: Serial.println("wait a moment"); delay(1000); } void sendData() { Serial.println("connecting..."); if (client.connect(server, 80)) { Serial.println("connected"); // Make your GET request: client.println("GET /saveTemp.php?pwd=raspiTemp&temp=" + ddd + "&temp2=" + www + " HTTP/1.1"); client.println("Host: 192.168.2.10"); client.println("Connection: close"); client.println(); } else { // if you didn't get a connection to the server: Serial.println("connection failed"); } // if there are incoming bytes available // from the server, read them and print them: if (client.available()) { char c = client.read(); Serial.print(c); } } void loop() { sendData(); // if the server's disconnected, stop the client: if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); } delay(10000); dd = dd + 1; ww = ww - 1; ddd = String(dd); www = String(ww); }