[Solved]Lan Module and WiFi



  • Hi!

    Could be possible to use the Lan module and the WiFi at the same time? I have noticed when I stack the Lan, to the M5 Basic, I start to lose the WiFi communication, maybe I need a cap somewhere...

    Best regards,

    Elvis


  • M5Stack

    @elvis6m Hi, I'm a SHIO.

    Our engineer said that you can use WiFI and LAN module at the same time.
    Please show us your code if you can.

    thank you.



  • Hello are there any solution ?
    or an example code ?
    Thanks for help
    Thomas


  • 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