Send an HTTP POST request using the Arduino network module and ENC28J60 Ethernet LAN

I just bought a new Ethernet LAN ENC28J60 Ethernet network module on Ebay, and I want to send POST Data with this module to the specified web address, for example. www.mydomain.com/example.php

I searched Google for some examples, but all I could see was an example for an arduino shield, and not for the module that I have. With this module, I use the following libraries:

"etherShield.h"
"ETHER_28J60.h"

I want to send one or two specified POSTS (variables) to php formular, but I don't know how to do this.

+5
source share
6 answers

First of all, you need to install the following library: https://github.com/jcw/ethercard

connect your module to arduino with 6 pins:

  • ENC SO β†’ Arduino pin 12
  • ENC SI β†’ Arduino 11 pin
  • ENC SCK β†’ Arduino 13 pin
  • ENC CS β†’ Contact Arduino 8
  • ENC VCC β†’ Contact Arduino 3V3
  • ENC GND β†’ Arduino Gnd pin

then use the following code:

#include <EtherCard.h> // your variable #define PATH "example.php" #define VARIABLE "test" // ethernet interface mac address, must be unique on the LAN byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; char website[] PROGMEM = "www.mydomain.com"; byte Ethernet::buffer[700]; uint32_t timer; Stash stash; void setup () { Serial.begin(57600); Serial.println("\n[webClient]"); if (ether.begin(sizeof Ethernet::buffer, mymac) == 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.printIp("SRV: ", ether.hisip); } void loop () { ether.packetLoop(ether.packetReceive()); if (millis() > timer) { timer = millis() + 10000; byte sd = stash.create(); stash.print("variable="); stash.print(VARIABLE); stash.print("&action=Submit"); stash.save(); // generate the header with payload - note that the stash size is used, // and that a "stash descriptor" is passed in as argument using "$H" Stash::prepare(PSTR("POST http://$F/$F.csv HTTP/1.0" "\r\n" "Host: $F" "\r\n" "Content-Length: $D" "\r\n" "Content-Type: application/x-www-form-urlencoded" "\r\n" "\r\n" "$H"), website, PSTR(PATH), website, stash.size(), sd); // send the packet - this also releases all stash buffers once done ether.tcpSend(); } } 
+6
source

you can try this library, which is fully compatible with the Ethernet-lib stock:

https://github.com/ntruchsess/arduino_uip

Examples from Arduino-IDE Ethernet by setting this library to [arduino-dir] / libraries / UIPEthernet, open standard Ethernet examples (for example: examples-> Ethernet-> WebServer) and replace "Ethenet.h" with "UIPEthernet. h ".

+1
source

As for the netheads answer, I am having trouble getting POST data on the server. It was receiving header information, I just could not access the post data using $ _POST or any other way. I found that determining the type of content solved this problem. Just change the part of the HTTP header as follows:

  Stash::prepare(PSTR("POST http://$F/$F.csv HTTP/1.0" "\r\n" "Host: $F" "\r\n" "Content-Length: $D" "\r\n" "Content-Type: application/x-www-form-urlencoded" "\r\n" "\r\n" "$H"), website, PSTR(PATH), website, stash.size(), sd); 
+1
source

Arduino v1.6.12

  #include <EtherCard.h> // your variable #define PATH "example.php" #define VARIABLE "test" // ethernet interface mac address, must be unique on the LAN byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; const char website[] PROGMEM = "www.google.com"; byte Ethernet::buffer[700]; uint32_t timer; Stash stash; void setup () { Serial.begin(57600); Serial.println("\n[webClient]"); if (ether.begin(sizeof Ethernet::buffer, mymac) == 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.printIp("SRV: ", ether.hisip); } void loop () { ether.packetLoop(ether.packetReceive()); if (millis() > timer) { timer = millis() + 10000; byte sd = stash.create(); stash.print("variable="); stash.print(VARIABLE); stash.print("&action=Submit"); stash.save(); // generate the header with payload - note that the stash size is used, // and that a "stash descriptor" is passed in as argument using "$H" Stash::prepare(PSTR("POST http://$F/$F.csv HTTP/1.0" "\r\n" "Host: $F" "\r\n" "Content-Length: $D" "\r\n" "\r\n" "$H"), website, PSTR(PATH), website, stash.size(), sd); // send the packet - this also releases all stash buffers once done ether.tcpSend(); } } 
+1
source

If someone wants to call the API, you need to remove .csv in the request, like so:

  byte sd = stash.create(); stash.print("variable="); //change this to your post variable stash.print(VARIABLE); stash.print("&action=Submit"); stash.save(); // generate the header with payload - note that the stash size is used, // and that a "stash descriptor" is passed in as argument using "$H" //remove the .csv Stash::prepare(PSTR("POST http://$F/$F HTTP/1.0" "\r\n" "Host: $F" "\r\n" "Content-Length: $D" "\r\n" "Content-Type: application/x-www-form-urlencoded" "\r\n" "\r\n" "$H"), website, PSTR(PATH), website, stash.size(), sd); 

Additional information: if you use a local server for testing, you can look at the Wampp or Xampp Server access log to see if your request has reached the server or not.

And use const in this line:

 char const website[] PROGMEM = "www.mydomain.com"; 
0
source

Hello and thanks for the example, but I am having problems sending a message to wunderground.com with this code.

If I put on the navigation tab "weatherstation.wunderground.com/weatherstation/updateweatherstation.php?ID=MIAMIA&PASSWORD=pepito&dateutc=2019-01-17+15%3A27%3A08&tempf=60&humidity=56"

everything is correct and the server receives the information (I also use httplib.HTTPConnection () in python and everything is correct), but I can not do it with VARIABLE. I tried all combinations of PATH and VARIABLE, removed spaces, etc.

Of course, I can get the IP and DNS is working fine and connect to the server.

Could you help me? Thank you

Put all the code if you have an idea of ​​the Stash function.

Thanks in advance.

 #include <EtherCard.h> // ethernet interface mac address, must be unique on the LAN byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; const char website[] PROGMEM = "weatherstation.wunderground.com"; byte Ethernet::buffer[700]; uint32_t timer; Stash stash; void setup () { Serial.begin(57600); Serial.println("\n[webClient]"); if (ether.begin(sizeof Ethernet::buffer, mymac) == 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.printIp("SRV: ", ether.hisip); } void loop () { ether.packetLoop(ether.packetReceive()); if (millis() > timer) { timer = millis() + 10000; byte sd = stash.create(); stash.print("/weatherstation/updateweatherstation.php? ID=I******&PASSWORD=********&dateutc=2019-01- 17+17%3A47%3A02&tempf=52&humidity=70"); stash.save(); // generate the header with payload - note that the stash size is used, // and that a "stash descriptor" is passed in as argument using "$H" Stash::prepare(PSTR("GET /update HTTP/1.1" "\r\n" "Host: $F" "\r\n" "Connection: close" "\r\n" "Content-Type: text/plain; charset=utf-8" "\r\n" "Content-Length: $D" "\r\n" "\r\n" "$H"), website, PSTR(""), stash.size(), sd); // send the packet - this also releases all stash buffers once done ether.tcpSend(); Serial.println("Mandado"); } } 
-one
source

Source: https://habr.com/ru/post/1492743/


All Articles