User Tools

Site Tools


thed1program

The program for the Wemos D1 Mini

#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

extern "C"
{
  #include "user_interface.h"  
}

int status = WL_IDLE_STATUS;
const char* ssid = "NETGEAR-24-G";  //  your network SSID (name)
const char* pass = "hanneke1";   // your network password
unsigned int localPort = 2390;    // local port to listen for UDP packets

IPAddress ipMulti (192,168,1,255);
IPAddress ip(192,168,1,101);
IPAddress gateway(192,168,1,1);
IPAddress subAddress(255,255,255,0);


char packetBuffer[512]; //buffer to hold incoming and outgoing packets

WiFiUDP Udp;  // A UDP instance to let us send and receive packets over UDP

void setup() {
  // put your setup code here, to run once:
  // Open serial communications and wait for port to open:
  Serial.begin(115200);


  //Setting up WiFi
  WiFi.config(ip,gateway,subAddress);

  //Connect to AP
  WiFi.begin(ssid, pass);
  WiFi.mode(WIFI_STA);

// Wait for connect to AP
  Serial.print("[Connecting]");
  Serial.print(ssid);
  int tries=0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    tries++;
    if (tries > 30){
      break;
    }
  }
  Serial.println();

  printWifiStatus();

  Serial.println("Connected to wifi");
  Serial.print("Udp server started at port ");
  Serial.println(localPort);
  Udp.begin(localPort);

  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
}

void loop() {
  int noBytes = Udp.parsePacket();
  if ( noBytes ) {
    Serial.print(millis() / 1000);
    Serial.print(":Packet of ");
    Serial.print(noBytes);
    Serial.print(" received from ");
    Serial.print(Udp.remoteIP());
    Serial.print(":");
    Serial.println(Udp.remotePort());
    // We've received a packet, read the data from it
    Udp.read(packetBuffer,noBytes); // read the packet into the buffer
    

    // display the packet contents in HEX
    for (int i=1;i<=noBytes;i++){
      Serial.print(packetBuffer[i-1]);
      if (i % 32 == 0){
        Serial.println();
      }
      else Serial.print(' ');
    } // end for
    Serial.println();

    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); //Send Answer to Network (Broadcast)  
    //O N _ N O 1
    if(noBytes > 6 && packetBuffer[0] == 'O' && packetBuffer[1]=='F' && packetBuffer[2]=='F' && packetBuffer[3]=='_' && packetBuffer[4]=='N' && packetBuffer[5]=='O' && packetBuffer[6]=='1'){  
      digitalWrite(D1, 0);
      Udp.write("OFF");
    }
    else if(noBytes > 6 && packetBuffer[0] == 'O' && packetBuffer[1]=='F' && packetBuffer[2]=='F' && packetBuffer[3]=='_' && packetBuffer[4]=='N' && packetBuffer[5]=='O' && packetBuffer[6]=='2'){  
      digitalWrite(D2, 0);
      Udp.write("OFF");
    }
    else if(noBytes > 6 && packetBuffer[0] == 'O' && packetBuffer[1]=='F' && packetBuffer[2]=='F' && packetBuffer[3]=='_' && packetBuffer[4]=='N' && packetBuffer[5]=='O' && packetBuffer[6]=='3'){  
      digitalWrite(D3, 0);
      Udp.write("OFF");
    }
    else if(noBytes > 6 && packetBuffer[0] == 'O' && packetBuffer[1]=='F' && packetBuffer[2]=='F' && packetBuffer[3]=='_' && packetBuffer[4]=='N' && packetBuffer[5]=='O' && packetBuffer[6]=='4'){  
      digitalWrite(D4, 0);
      Udp.write("OFF");
    }
    else if(noBytes > 5 && packetBuffer[0] == 'O' && packetBuffer[1]=='N'  && packetBuffer[2]=='_' && packetBuffer[3]=='N' && packetBuffer[4]=='O' && packetBuffer[5]=='1'){
       digitalWrite(D1, 1);
       Udp.write("ON");
    }
    else if(noBytes > 5 && packetBuffer[0] == 'O' && packetBuffer[1]=='N'  && packetBuffer[2]=='_' && packetBuffer[3]=='N' && packetBuffer[4]=='O' && packetBuffer[5]=='2'){
      digitalWrite(D2, 1);
      Udp.write("ON");
    }
    else if(noBytes > 5 && packetBuffer[0] == 'O' && packetBuffer[1]=='N'  && packetBuffer[2]=='_' && packetBuffer[3]=='N' && packetBuffer[4]=='O' && packetBuffer[5]=='3'){
      digitalWrite(D3, 1);
      Udp.write("ON");
    }
     else if(noBytes > 5 && packetBuffer[0] == 'O' && packetBuffer[1]=='N'  && packetBuffer[2]=='_' && packetBuffer[3]=='N' && packetBuffer[4]=='O' && packetBuffer[5]=='4'){
      digitalWrite(D4, 1);
      Udp.write("ON");
    }
    else
       Udp.write("ERROR");

    Udp.endPacket();
  } // end if

}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

thed1program.txt · Last modified: 2018/01/03 23:20 by 92.246.24.25