Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Create a WiFi access point and provide a web server on it. */
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <WiFiUDP.h>
- /* Set these to your desired credentials. */
- const char *ssid = "ESPap";
- const char *password = "thereisnospoon";
- // UDP variables
- unsigned int localPort = 8888;
- WiFiUDP UDP;
- boolean udpConnected = false;
- char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
- char ReplyBuffer[] = "acknowledged"; // a string to send back
- ESP8266WebServer server(80);
- /* Just a little test message. Go to http://192.168.4.1 in a web browser
- * connected to this access point to see it.
- */
- void handleRoot() {
- server.send(200, "text/html", "<h1>You are connected</h1>");
- }
- void setup() {
- delay(1000);
- Serial.begin(115200);
- Serial.println();
- Serial.print("Configuring access point...");
- /* You can remove the password parameter if you want the AP to be open. */
- WiFi.softAP(ssid, password);
- IPAddress myIP = WiFi.softAPIP();
- Serial.print("AP IP address: ");
- Serial.println(myIP);
- server.on("/", handleRoot);
- server.begin();
- Serial.println("HTTP server started");
- udpConnected = connectUDP();
- if (udpConnected)
- {
- // initialise pins
- pinMode(D5,OUTPUT);
- pinMode(D0,OUTPUT);
- digitalWrite(D0,HIGH);
- }
- }
- void loop()
- {
- server.handleClient();
- if(udpConnected)
- {
- // if there’s data available, read a packet
- int packetSize = UDP.parsePacket();
- if(packetSize)
- {
- // turn LED on
- digitalWrite(D5,HIGH);
- delay(50);
- digitalWrite(D5,LOW);
- }
- delay(10);
- }
- }
- boolean connectUDP()
- {
- boolean state = false;
- Serial.println("");
- Serial.println("Connecting to UDP");
- if(UDP.begin(localPort) == 1)
- {
- Serial.println("Connection successful");
- state = true;
- }
- else
- {
- Serial.println("Connection failed");
- }
- return state;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement