Saltar al contenido

Envío de solicitud HTTP POST con Arduino y módulo de red LAN Ethernet ENC28J60

Recabamos por diferentes espacios y de esta forma tener para ti la solución para tu dilema, si continúas con alguna inquietud puedes dejar un comentario y contestaremos porque estamos para servirte.

Solución:

En primer lugar, debe instalar la siguiente biblioteca: https://github.com/jcw/ethercard

conecte su módulo a arduino con 6 pines:

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

luego usa el siguiente código:

#include 

// 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" "rn"
                "Host: $F" "rn"
                "Content-Length: $D" "rn"
                "Content-Type: application/x-www-form-urlencoded" "rn"
                "rn"
                "$H"),
    website, PSTR(PATH), website, stash.size(), sd);

    // send the packet - this also releases all stash buffers once done
    ether.tcpSend();
  

Arduino v1.6.12

 #include 

// 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" "rn"
                        "Host: $F" "rn"
                        "Content-Length: $D" "rn"
                        "rn"
                        "$H"),
            website, PSTR(PATH), website, stash.size(), sd);

    // send the packet - this also releases all stash buffers once done
    ether.tcpSend();
  

Si alguien está buscando llamar a una API, debe eliminar .csv en la solicitud, así:

 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" "rn"
            "Host: $F" "rn"
            "Content-Length: $D" "rn"
            "Content-Type: application/x-www-form-urlencoded" "rn"
            "rn"
            "$H"),
website, PSTR(PATH), website, stash.size(), sd);

Información adicional, si está utilizando un servidor local para probar, puede ver el Registro de acceso de Wampp o Xampp Server para saber si su solicitud ha llegado al servidor o no.

Y usa const en esta línea:

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

Ten en cuenta dar difusión a esta reseña si te fue útil.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *