[Solved]Lan Module and WiFi


  • M5Stack

    @elvis6m I also need your code.



  • Thanks @elvis6m for your attention. Here is the siplified code,
    //
    // Ethernet Settings * i try to use Example "M5Stack/Modules/W5500/Webserver"
    // Problem: M5Stack LAN Module works with this settings but only seperate code not together with WiFi at same time,
    // _________& OTA was as well not possible
    // _________https://m5stack.readthedocs.io/en/master/product-documents/modules/m5stack_lan_module.html
    //
    // MyProject : connect to home WiFi accesspoint, if done, then Button A pass an UDP message to 2channel LAN relais
    //____________connected at the RJ45 M5Stack LAN Module.
    // (so fare works only via home wifi accesspint to lan, but not when we connect the crossovercable to M5StackLAN)
    //
    //
    #include <M5Stack.h>
    #include "WiFi.h"

    #include <WebServer.h>
    WebServer server(80);

    //for static IP address configuration
    IPAddress staticIP(192, 168, 178, 39); //M5stack static ip
    IPAddress gateway(192, 168, 178, 100); //IP Address of your WiFi Router (Gateway)
    IPAddress subnet(255, 255, 255, 0); //Subnet mask
    IPAddress dns(192, 168, 178, 100); //DNS

    #include <AsyncUDP.h>
    #include <WiFiUdp.h>
    #include <ArduinoOTA.h>

    //UDP
    WiFiUDP Udp, Udp2 ;
    char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

    #define TFT_GREY 0x5AEB

    //SSID and Password of your WiFi router
    const char* WiFi_hostname = "M5Stack"; // works with M5Stack, NudeMCU , D1 Mini(Board 12E Module).
    const char* ssid = "test"; //"SSID"type your ssid,
    const char* password = "test"; //type your password,

    void setup() {
    M5.begin();
    M5.Lcd.clear();
    M5.Lcd.println("m5.begin");

    WiFi.setHostname(WiFi_hostname); //Name im WiFi router
    //WiFi.config(staticIP, subnet, gateway, dns);
    WiFi.begin(ssid, password); //Connect to your WiFi router
    WiFi.mode(WIFI_STA);
    Serial.println("");
    Serial.println("wiwfi");
    Serial.println("");
    M5.Lcd.println("..");

    // wait for connection
    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
    M5.Lcd.print(F("."));
    }

    //OTA
    // Port defaults to 8266
    ArduinoOTA.setPort(8266);
    // Hostname defaults to esp8266-[ChipID]
    ArduinoOTA.setHostname(WiFi_hostname);
    ArduinoOTA.onStart( {

    M5.Lcd.clear();
    M5.Lcd.println("..start update prozess" );
    //  display.display();
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_SPIFFS
      type = "filesystem";
    }
    
    
    Serial.println("Start updating " + type);
    

    });
    ArduinoOTA.onEnd( {
    Serial.println("\nEnd");
    });
    ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));

    M5.Lcd.setTextSize(3);
    M5.Lcd.setCursor(20, 60);
    M5.Lcd.print("..update" );
    M5.Lcd.print( String((progress / (total / 100))) + " % " );
    

    });
    ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[ % u]: ", error);
    if (error == OTA_AUTH_ERROR) {
    Serial.println("Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
    Serial.println("Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
    Serial.println("Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
    Serial.println("Receive Failed");
    } else if (error == OTA_END_ERROR) {
    Serial.println("End Failed");
    }
    });
    ArduinoOTA.begin();
    Serial.println("Ready");
    M5.Lcd.setTextSize(1); //zurücksetzen
    //OTA ENDE

    startWebServer(); // starting webserver
    M5.Lcd.setCursor(0, 0);
    M5.Lcd.setTextSize(2);
    M5.Lcd.println(String(WiFi_hostname));
    M5.Lcd.println(WiFi.localIP());
    M5.Lcd.setTextSize(1);

    } // ENDE SETUP

    //==============================================================
    //xxxxxxxxxxxxxxxxxxxxxxxxxxx LOOP xxxxxxxxxxxxxxxxxxxxxxxxxxx
    //==============================================================
    void loop() {
    ArduinoOTA.handle();
    delay(80);
    server.handleClient(); //Handle client requests
    M5.update();
    if (M5.BtnA.wasReleased()) {
    M5.Lcd.print('A');
    RelaissendUDP();
    } else if (M5.BtnB.wasReleased()) {
    M5.Lcd.print('B');
    } else if (M5.BtnC.wasReleased()) {
    M5.Lcd.print('C');
    }
    } // END of LOOP

    void startWebServer()
    {
    // Root-URL
    server.on("/", handleRoot);

    // further path
    server.on("/m5", {
    server.send(200, "text/plain", "m5 path hello");
    });

    // relaistrigger
    server.on("/11", RelaissendUDP);
    // and further more if need.

    // if path not valid
    server.onNotFound(handleNotFound);

    // server ok so, go
    server.begin();
    Serial.println("HTTP server started");
    }

    void handleNotFound()
    {
    String message = "File Not Found\n\n";
    message += "URI: ";
    message += server.uri();
    message += "\nMethod: ";
    message += (server.method() == HTTP_GET) ? "GET" : "POST";
    message += "\nArguments: ";
    message += server.args();
    message += "\n";
    for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
    }
    server.send(404, "text/plain", message);
    }

    void handleRoot()
    {
    String content = String(WiFi_hostname)+"<br> this button trigger the relais same way button A at M5stack <a href="/11">RELAISTRIGGER</a></body></html>";
    String s = "<!DOCTYPE html><html><head>";
    s += "<meta name="viewport" content="width=device-width,user-scalable=0">";
    s += "<title>";
    s += "M5Stack";
    s += "</title></head><body>";
    s += content;
    s += "</body></html>";
    server.send(200, "text/html", "OK http connect: " + s);
    }

    //LAN 2Chanal Relais
    void RelaissendUDP ()
    {
    // 2WayRelais
    IPAddress 2channelRelais(192, 168, 178, 32);
    unsigned int UDP2port = 6723; //sendePport 6723 UDP für 2WayRelais, TCP port 6722
    const int UDP_PACKET_SIZE = 4; //packetgroesse wictig rüf Relaisansteuerung
    byte packet2Buffer[ UDP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
    boolean packet2Rcvd = true;

    delay(10);
    memset(packet2Buffer, 0, UDP_PACKET_SIZE);
    sprintf((char*)packet2Buffer, "11:5"); //PCB Relais2, close for 5seconds
    Udp.beginPacket(2channelRelais, UDP2port);
    Udp.write(packet2Buffer, UDP_PACKET_SIZE);
    Udp.endPacket();
    delay(10);
    memset(packet2Buffer, 0, UDP_PACKET_SIZE);
    sprintf((char*)packet2Buffer, "12:4"); //PCB Relais2, close for 4seconds
    Udp.beginPacket(2channelRelais, UDP2port);
    Udp.write(packet2Buffer, UDP_PACKET_SIZE);
    Udp.endPacket();
    }



  • @thom said in Lan Module and WiFi:

    " char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,"
    this row can be deleted.



  • @m5-docs
    Hello @elvis6m let me ask for an uptate , thank you, best regards Thomas



  • Hello There, are the an way to get it solved ? :-/
    so far i couldn't find it.
    Greeets Thomas

    http://forum.m5stack.com/topic/763/solved-lan-module-and-wifi



  • @salty_good Hello, are there any solution meawhile?



  • Hello Thomas

    If I read your code correctly the UDP packets are sent out via WiFi instead of the Ethernet interface. I think you'll need to include Ethernet2 library (from M5 Stack W5500 WebServer example) and then include and declare the following:

    #include <Ethernet2.h>
    #include <EthernetUdp2.h>
    
    EthernetUDP Udp;
    

    instead of

    WiFiUDP Udp, Udp2 ;
    

    The Ethernet interface needs to be initialised as well with something like this (taken from M5Stack W5500 WebServer example):

    #define SCK 18
    #define MISO 19
    #define MOSI 23
    #define CS 26
    
    // Enter a MAC address and IP address for your controller below.
    // The IP address will be dependent on your local network:
    byte mac[] = {
      0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
    };
    IPAddress ip(192, 168, 1, 177);
    
      SPI.begin(SCK, MISO, MOSI, -1);
      Ethernet.init(CS);
      // start the Ethernet connection and the server:
      Ethernet.begin(mac, ip);
    

    Note: all of the above is untested and might need some additional fine tuning.

    Good luck!

    Cheers
    Felix



  • Thanks Felix,
    meanwhile i got it work with via ethernet.h , same wway like your post.

    But the main skill was and still is unsolved, how get it work with LAN and WIFI together.
    OR anway switch within one code between wifi and lan.

    so fare i dint found an way to update the sketch .bin via LAN.
    Almost all very nice codes working perfekly via WIFI webserver upload .
    like :
    "HTTPUpload& upload = server.upload();"
    or
    " t_httpUpdate_return ret = ESPhttpUpdate.update(update_server, 80, update_uri, firmware_version);"

    but via ethernet, i am not able to get it wokrk i found nothing so fare :-/

    greeets Thomas



  • Hello Thomas

    glad to hear you've got Ethernet working.

    From what I can tell the HTTPUpdate library only supports OTA over WiFi connections. So in order to use Ethernet for OTA I guess you'd need to modify the HTTPUpdate library to use an Ethernet connection instead of WiFi.

    Greetings
    Felix



  • Hello again

    I think the following library should allow you to switch between WiFi or Ethernet OTA:

    https://github.com/espressif/arduino-esp32/tree/master/libraries/Update

    Use it with either a WiFiClient or EthernetClient client.

    Cheers
    Felix