Paso 6: Preparar la ESP8266 para la programación
Descargar el IDE de Arduino si no ya lo tienes: https://www.arduino.cc/en/Main/Software
Ahora necesita añadir el nodo ESP8266 del MCU 12E como seleccionable en el IDE de Arduino.
Siga las instrucciones detalladas aquí: http://www.whatimade.today/esp8266-easiest-way-to-...
Ahora tienes todas las opciones de ESP como tableros seleccionables al hacer clic en herramientas -> tablas en el IDE
Copie y pegue este código en el IDE de Arduino:
#include <ESP8266WiFi.h> #include <WiFiUdp.h> int buttonPin = 5; //d1 on esp8266 nodemcu int buttonPin2 = 4; //d2 bool walking = false; bool sentWalking = false; bool sentNotWalking = false; bool firing = false; bool sentFiring = false; bool sentNotFiring = false; const char* ssid = "***********"; // wifi network name const char* password = "*******"; // wifi network password //IPAddress ipBroadCast(255, 255, 255, 255); //ip of computer network IPAddress ipBroadCast(255, 255, 255, 255); //ip of mobile network unsigned int udpRemotePort=2000; const int UDP_PACKET_SIZE = 28; char udpBuffer[ UDP_PACKET_SIZE]; WiFiUDP udp; void setup() { Serial.begin(115200); delay(10); // We start by connecting to a WiFi network Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); Serial.println("Starting UDP"); pinMode(buttonPin, INPUT_PULLUP); pinMode(buttonPin2, INPUT_PULLUP); //send connected message strcpy(udpBuffer, "Connected"); Serial.println("Connected"); udp.beginPacket(ipBroadCast, udpRemotePort); udp.write(udpBuffer, sizeof(udpBuffer)); udp.endPacket(); } void sendFIRE(){ strcpy(udpBuffer, "FIRE"); udp.beginPacket(ipBroadCast, udpRemotePort); udp.write(udpBuffer, sizeof(udpBuffer)); udp.endPacket(); } void sendWALK(){ strcpy(udpBuffer, "WALK"); udp.beginPacket(ipBroadCast, udpRemotePort); udp.write(udpBuffer, sizeof(udpBuffer)); udp.endPacket(); } void sendSTOP(){ strcpy(udpBuffer, "STOP"); udp.beginPacket(ipBroadCast, udpRemotePort); udp.write(udpBuffer, sizeof(udpBuffer)); udp.endPacket(); } void sendDONT(){ strcpy(udpBuffer, "DONT"); udp.beginPacket(ipBroadCast, udpRemotePort); udp.write(udpBuffer, sizeof(udpBuffer)); udp.endPacket(); } void loop() { int buttonValue = digitalRead(buttonPin); int buttonValue2 = digitalRead(buttonPin2); if (buttonValue == LOW){ firing = true; } else { sentFiring = false; } if (buttonValue == HIGH){ firing = false; } else { sentNotFiring = false; } if (firing == true && sentFiring == false){ Serial.println("FIRE"); sendFIRE(); delay(200); sentFiring = true; } if (firing == false && sentNotFiring == false){ Serial.println("DONT"); sendDONT(); delay(200); sentNotFiring = true; } //end firing //start walking if (buttonValue2 == LOW){ walking = true; } else { sentWalking = false; } if (buttonValue2 == HIGH){ walking = false; } else { sentNotWalking = false; } if (walking == true && sentWalking == false){ Serial.println("WALK"); sendWALK(); delay(200); sentWalking = true; } if (walking == false && sentNotWalking == false){ Serial.println("STOP"); sendSTOP(); delay(200); sentNotWalking = true; } }