Sending HTTP POST Request with Arduino and ENC28J60 Ethernet LAN Network Module

26,238

Solution 1

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

connect your module to arduino with 6 pins:

  • ENC SO -> Arduino pin 12
  • ENC SI -> Arduino pin 11
  • ENC SCK -> Arduino pin 13
  • ENC CS -> Arduino pin 8
  • ENC VCC -> Arduino 3V3 pin
  • ENC GND -> Arduino Gnd pin

then use 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();
  }
}

Solution 2

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();
  }
}

Solution 3

If anyone is looking to call an API, you need to remove .csv in the request, Like this:

 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 are using local server to test, you can view the Access Log of Wampp or Xampp Server to know weather your request has reached the server or not.

And use const in this line:

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

Solution 4

you may want to try this library that is fully compatible to the stock Ethernet-lib:

https://github.com/ntruchsess/arduino_uip

Examples from Arduino-IDE Ethernet work by installing this lib in [arduino-dir]/libraries/UIPEthernet, open the standard Ethernet-examples (e.g.: examples->Ethernet->WebServer) and replace the include "Ethenet.h" by "UIPEthernet.h".

Solution 5

With regards to netheads answer, I was having trouble receiving the POST data at the server end. It was receiving header information, I just couldn't access the post data using $_POST or by any other means. I found that specifying the content type solved this problem. Just change the HTTP header part 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);
Share:
26,238
Jakub Pastuszuk
Author by

Jakub Pastuszuk

Updated on January 18, 2020

Comments

  • Jakub Pastuszuk
    Jakub Pastuszuk over 4 years

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

    I resereached google for some examples, but all I could saw were examples for arduino shield, not module I have. With this module I'm using following libraries:

    "etherShield.h"
    "ETHER_28J60.h"

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