Skip to content

Instantly share code, notes, and snippets.

@joksan
Created April 19, 2017 18:15
Show Gist options
  • Save joksan/68a963be69a24fb81de8185f5f254417 to your computer and use it in GitHub Desktop.
Save joksan/68a963be69a24fb81de8185f5f254417 to your computer and use it in GitHub Desktop.
#include <WiFi101.h>
#include <WiFiUdp.h>
const int pinBoton = 2;
bool estadoBoton;
//Configuracion de la red
char ssid[] = "Demo Servidor LED";
char pass[] = "micropython";
IPAddress dirRemota(10, 42, 0, 150);
IPAddress dirLocal(10, 42, 0, 160);
IPAddress dirDNS(8, 8, 8, 8);
IPAddress dirRouter(10, 42, 0, 1);
IPAddress dirSubred(255, 255, 255, 0);
//Objeto de clase UDP, usado para comunincarse con el server
WiFiUDP udp;
void setup() {
int estado;
//Inicializa el boton
pinMode(pinBoton, INPUT_PULLUP);
estadoBoton = digitalRead(pinBoton);
//Inicializa el puerto serial
Serial.begin(9600);
//while(!Serial);
do {
Serial.print("Conectando a la red: ");
Serial.println(ssid);
//Configura los parametros de red antes de conectarse
WiFi.config(dirLocal, dirDNS, dirRouter, dirSubred);
//Realiza el intento de conexion
estado = WiFi.begin(ssid, pass);
//Si no consigue conectarse, reintenta el proceso
if (estado != WL_CONNECTED) delay(1000);
} while (estado != WL_CONNECTED);
Serial.println("Conexion exitosa");
//Inicializa el objeto de clase UDP
udp.begin(12345);
}
void loop() {
bool nuevoEstadoBoton;
char buffer[1];
//
nuevoEstadoBoton = digitalRead(pinBoton);
if (estadoBoton != nuevoEstadoBoton) {
estadoBoton = nuevoEstadoBoton;
if (estadoBoton)
buffer[0] = '0';
else
buffer[0] = '1';
if (udp.beginPacket(dirRemota, 12345)) {
udp.write(buffer, 1);
udp.endPacket();
}
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment