Skip to content

Instantly share code, notes, and snippets.

@ejherran
Created October 12, 2017 04:09
Show Gist options
  • Save ejherran/cbae3cbd293a81c99b29600426ed8692 to your computer and use it in GitHub Desktop.
Save ejherran/cbae3cbd293a81c99b29600426ed8692 to your computer and use it in GitHub Desktop.
Arduino Ethernet Test Web Client
#include <EtherCard.h>
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; // Mac de la tarjeta ether... Debe ser unico en la red local
const static uint8_t ip[] = {10,14,0,56}; // Ip de la tarjeta
const static uint8_t gw[] = {10,14,0,1}; // Ip de la puerta de enlace
const static uint8_t mask[] = {255,255,255,0}; // Mascara de la tarjea
const static uint8_t dns[] = {10,14,0,1}; // Ip del servidor DNS, si existe
const static uint8_t hisip[] = {10,14,0,104}; // Ip de la aplicacion remota
const static int hisport = 8080; // Puerto de la aplicacion remota
byte Ethernet::buffer[700]; // Buffer de la tarjeta
static uint32_t timer; // Acumulador de tiempo
const char website[] PROGMEM = "10.14.0.104"; // Nombre del servidor remoto, si es una IP fina puede fallar
//const char website[] PROGMEM = "google.com"; // Forma alternativa con nmbre de dominio
static void my_callback (byte status, word off, word len) // Funcion que se ejecyta al recibir la respuesta de la aplicacion remota
{
Serial.println(">>>");
Ethernet::buffer[off+300] = 0;
Serial.print((const char*) Ethernet::buffer + off);
Serial.println("...");
}
void setup ()
{
Serial.begin(9600);
Serial.println("\n[webClient]");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) // Activa la tarjeta
Serial.println( "Error al acceder al controlador ethernet");
if (!ether.staticSetup(ip,gw,dns,mask)) // Configura manualmente la tarjeta, s epuede usar ether.dhcpSetup() si se esta conectado a una red con dhcp
Serial.println("Error al configurar las IP");
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
if (!ether.dnsLookup(website)) // Trata de cinfigurar la ip del servidor por DNS
{
Serial.println("DNS failed");
ether.copyIp(ether.hisip, hisip); // Si falla el DNS se configura manualmente
ether.hisport = hisport;
}
ether.printIp("SRV: ", ether.hisip);
ether.printIp("PORT: ", ether.hisport);
}
void loop ()
{
ether.packetLoop(ether.packetReceive()); // Activa el loop principal de la tarjeta
if (millis() > timer) // Verifica que el proceso se ejecte cada 5 segundos
{
timer = millis() + 5000; // Lmite a 5 segundos
Serial.println();
Serial.print("<<< REQ ");
ether.browseUrl(PSTR("/?"), "data={\"token\":\"12345\",\"temperatura\":\"54\"}", website, my_callback); // Envia la peticion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment