1

Topic: How to restart Wifi

Hello,
I use RemoteXY with ESP32 wifi access point. As a measure of energy saving, I cut the wifi after a while if there is no connection (WiFi.disconnect (); WiFi.mode (WIFI_OFF);)
My question: how to restart RemoteXY when pressing a BP for example?
Thank you in advance for your answers .
Beroy40

2 (edited by Guillaume 2019-07-15 12:22:58)

Re: How to restart Wifi

Welcome,


You can try to call RemoteXY_Init() again.


Or do all the Wifi things yourself and make your own RemoteXY module so that it doesn't mess with Wifi, unlike the original modules.

Here is an example

#pragma once
#include <WiFi.h>
#include "classes/RemoteXY_API.h"

#define REMOTEXY_SEND_BUFFER_LENGTH 256

class CRemoteXY : public CRemoteXY_API
{

protected:

    uint16_t port;

    WiFiServer *server;
    WiFiClient client; 

    uint8_t wifiStatus;
    uint32_t serverTimeOut;

    uint8_t sendBuffer[REMOTEXY_SEND_BUFFER_LENGTH];
    uint16_t sendBufferCount; 
    uint16_t sendBytesAvailable;  

public:

    void begin( const void * _config, const void * _struct, const char * _password, const uint16_t _port )
    {
        port = _port;
        serverTimeOut = 0;
        init( _config, (void *)_struct, _password );
    }

    uint8_t initModule()
    {
        client.stop();

        if ( server == nullptr )
        {
            server = new WiFiServer( port );
        }

        server->begin();
        server->setNoDelay( true );

        #if defined REMOTEXY__DEBUGLOGS
            REMOTEXY__DEBUGLOGS.println( "Server started" );
        #endif

        return 1;
    }

    void handlerModule()
    {
        uint8_t status = WiFi.status();
        if ( status != wifiStatus )
        {
            wifiStatus = status;
            #if defined REMOTEXY__DEBUGLOGS
                if ( wifiStatus == WL_CONNECTED )
                {
                    DEBUGLOGS_write( "Connected to access point" );
                    DEBUGLOGS_write( "IP: " );
                    REMOTEXY__DEBUGLOGS.print( WiFi.localIP() );
                }
            #endif  
        }

        if ( !client )
        {
            client = server->available();

            if ( client )
            {
                resetServerTimeOut();
                #if defined REMOTEXY__DEBUGLOGS
                    DEBUGLOGS_write( "Client connected." );
                #endif
            }
        }

        if ( client )
        {
            if ( client.connected() )
            {
                if ( millis() - serverTimeOut > REMOTEXY_SERVER_TIMEOUT )
                {
                    client.stop();
                    #if defined REMOTEXY__DEBUGLOGS
                        DEBUGLOGS_write( "Client timeout." );
                    #endif
                }
            }
            else
            {
                client.stop();
                #if defined REMOTEXY__DEBUGLOGS
                    DEBUGLOGS_write( "Client disconnected." );
                #endif
            }
        }        
    }


    void sendStart( uint16_t len )
    {
        sendBytesAvailable = len;
        sendBufferCount = 0;
    }

    void sendByte( uint8_t b )
    {
        if ( client )
        {
            if ( client.connected() )
            {
                #if defined REMOTEXY__DEBUGLOGS
                    DEBUGLOGS_writeOutputHex( b );
                #endif
                sendBuffer[sendBufferCount++] = b;
                sendBytesAvailable--;       
                if ( sendBufferCount == REMOTEXY_SEND_BUFFER_LENGTH || sendBytesAvailable == 0 )
                {
                    uint8_t buf[sendBufferCount];
                    for ( uint16_t i = 0; i < sendBufferCount; i++ )
                    {
                        buf[i] = sendBuffer[i];
                    }

                    client.write( buf, sendBufferCount );
                    sendBufferCount = 0;
                    resetServerTimeOut();
                } 
            }
        }
    }  

    uint8_t receiveByte()
    {
        uint8_t b;
        if ( client )
        {
            if ( client.connected() )
            {
                b = client.read();
                #if defined REMOTEXY__DEBUGLOGS
                    DEBUGLOGS_writeInputHex( b );
                #endif
                resetServerTimeOut();
                return b;
            }
        }

        return 0;
    }

    uint8_t availableByte()
    {   
        #if !defined REMOTEXY_WIFI__POINT
            if ( wifiStatus != WL_CONNECTED )
            {
                return 0;
            }
        #endif

        if ( client )
        {
            if ( client.connected() )
            {
                return client.available();
            }
        }
        return 0;
    }  

    void resetServerTimeOut()
    {
        serverTimeOut = millis();
    }

};

It will require some changes in your code.

Add this at the begining of your code

CRemoteXY remotexy;

Then when wifi is (re)started (for example inside the wifi event SYSTEM_EVENT_AP_START):

remotexy.begin( remotexyConfig, &remotexyStruct, remotexyPassword, remotexyPort );

And in loop()

remotexy.handler();

3

Re: How to restart Wifi

Hello
Thank you for your reply.
I have already tried calling "RemoteXY Init ()" but it does not work.
I will try your method.
I'll tell you if it works
Beroy40