logo elektroda
logo elektroda
X
logo elektroda

Automatic reboot of TP-LINK MR3020 router via ESP8266 - how to get TOKEN?

futek2 2142 13
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18810051
    futek2
    Level 19  
    Hello to those interested in the topic of rebooting the router to which our ESP8266 or ESP32 is connected over WIFI.
    I have attempted to automate the router control via ESP8266, which works as WIFI to the ES5000 solar inverter and sends electrical data to the PV MONITOR server.
    And the problem is that every now and then my MR3020 is with firmware sometimes it doesn't connect to the internet but when I do a reboot by logging in it all comes back to normal.
    But I'm not always at home to do this and recently I went away and the situation lasted a few days.
    And the thought came to me that it was possible to do a reboot of the router by sending the appropriate command line to the router to do this reboot from the ESP8266.
    I made a few attempts and none of them worked, because you need to read the TOKEN
    Here is a link to this description.
    https://russiachess.ru/puteshestviya-i-turizm...ulyarno-perezagruzhat-wi-fi-router-teper.html

    The description linked below already takes this into account, but it is written in PYTON and I can't manage to write it in the ARDUINO IDE as a piece of code that I could append to the code I already have on the ESP8266.
    https://www.pvsm.ru/python/268081


    session_id I read it from the browser but it changes after every login to TP_LINK.

    My code snippet looks like this.

     HTTPClient http; 
      http.begin("user admin:admin http://192.168.1.1//userRpm/SysRebootRpm.htm"); 
      http.addHeader("Authorization:", "session_id = QkTuTYmbTP");
      http.addHeader("Content-Type:", "application/json");
      http.addHeader("Accept: */*");
      http.addHeader("Content-Length: 50");
      http.POST("{"entity_id": "input_boolean.keep_ventilation_on"}");
      String payload = http.getString();
      Serial.print("payload: ");
      Serial.println(payload);
      http.end();  
    .

    Please support me as I have never done logging and sending commands to the router.

    Greetings
    Futek2
  • ADVERTISEMENT
  • #2 18810079
    xury
    Automation specialist
    I think you are taking the wrong approach. Firstly if your router crashes then you won't connect to the ESP. Secondly it might be better than a software reboot to just take power off the router with a relay for a while. Thirdly this router can run on openwrt and this simplifies things, as you can make a script that reboots the router e.g. when there is no ping to google DNS.
  • ADVERTISEMENT
  • #3 18810106
    futek2
    Level 19  
    Hello.
    Thanks for your interest in the subject.
    I have already tested it .
    I even uploaded the software from the Polish site but something does not support the E3372 modem very well.
    The router works because I log into it via the web . The problem is that I have it in the attic of the building to have a better signal to the modem E3372 and control the voltage is again additional circuits.
    But people write that it can be done, perhaps on an older version of the software without token would be easy.
    For now I pause to make up my mind.
    When I will go through with the topic again, I will manage to do something and will share with others here on the forum.
    Because maybe they are interested.
    I am of good opinion that the topic can be handled together.
    A similar topic has already been described.

    https://www.elektroda.pl/rtvforum/topic941139.html

    https://www.elektroda.pl/rtvforum/topic1549110.html

    https://gist.github.com/vicwomg/9b0bc6f1ea22e08379651a36877e0420

    And perhaps this will support more TP_LINK router models.

    Greetings.
    Futek2

    Added after 3 [hours] 20 [minutes]:

    Searching the web I came across this forum

    http://www.jfwhome.com/2012/06/18/reboot-tp-l...ter-remotely-or-automatically/comment-page-1/

    But they also have a problem, because on new versions it doesn't work, because you have to read the session_id.
    But on older versions of the software it worked.
    I am positive that this will be solved and it will work remotely.

    Futek2
  • #4 18810612
    Ibuprom
    Level 26  
    futek2 wrote:
    I even loaded the software from the Polish site, but something does not support the E3372 modem very well.
    .
    If you are writing about Gargoyle it is no wonder that miracles happen. I've never had this software work stably in contrast to the images from openwrt.org, although the fact is that there you have to do more manually - but for almost everything you will find a working tutorial. And as the previous speaker wrote - cut the power, because usb support and general crashes are usually hardware-based, not software-based - and commands rarely help here.

    Besides - the problem is not with the stability of the hardware as such, but with poor power supply/low power efficiency of the usb modem - that's why the router crashes. Often the router needs to be reworked (remove/modify power switching keys, improve power filtering) and use a better power supply than the one supplied. Then no woo-doo with resets is needed.
  • #5 18810800
    futek2
    Level 19  
    After a few hours of pause, I manage to read this token.
    And this is such a line from the windows command line:

    @curl --user admin:admin http://192.168.1.1 > sesion_id.txt
    .

    After sending to the router, the response is saved in the file sesion_id.txt .
    A part of this answer looks like this with the fragment what is needed for further processing.

    <script type="text/javascript">
    var session_id = "rKtwZJBJi7";
    </script>


    And the selection of the token I do like this or this needed part rKtwZJBJi7

    Now all the way to the windows command line

    @curl --user admin:admin http://192.168.1.1 | grep session_id  | grep var | cut -c 19- | cut -c 1-10   > tok.txt


    The lines above can be saved as a bat file.
    The attached file contains the necessary programs, which must be in the same directory if someone wants to test it on his router.
    But it is already a step forward.
    Maybe now it will be possible to reboot the router.

    I am of the opinion that once this is done with windows it will go downhill.

    Best regards.
    Futek2
  • #6 18810838
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #7 18811275
    futek2
    Level 19  
    Hello to those interested in the topic.
    Thanks to kol Erbit for the info you sent.
    Today I slept in and read the forum again.

    http://www.jfwhome.com/2012/06/18/reboot-tp-l...ter-remotely-or-automatically/comment-page-1/

    Guys have done a lot of testing and based on their results I searched for an older version of the software for MR3020 V1.0 and succeeded in the hamster is attached and in addition has a Polish menu.
    In this version there is no SESION_ID and it works with this line below from windows level is attached as .bat file

    @curl --referer http://192.168.1.1/ --user admin:admin http://192.168.1.1/userRpm/SysRebootRpm.htm?Reboot=Reboot > reboot.txt

    This works fine with the previously attached files, which must be in the same directory.

    In the reboot.txt file you can see how the MR3020 responds.

    Now it is clear that it works, it is just a matter of writing the code to make it work with the ESP8266.

    I think I will still have some support if they have a problem with the code.

    Futek2
  • ADVERTISEMENT
  • #8 18811465
    krzbor
    Level 27  
    And why not do it as @xury suggested? After all, all you need to do is have the ESP (which also connects via WiFi to the network) query DNS or NTP, or even communicate with the PV MONITOR server every specified time (e.g. 1 minute). If it doesn't get a response it will trigger a relay for 30 seconds, which will cut off the current to the router. Here it is a good idea to use a relay with an NC output, as it will normally be shorted almost all the time. Such an ESP can do more, e.g. when connectivity returns, record information in the log or send a restart email.
    The sequence could be as follows:
    1. connectivity check every 1 minute.
    2. no communication starts check every 10 sec
    3. after 6 attempts to fail - cut power to the router for 30 seconds
    4. wait for reboot (need to measure time e.g. 5 minutes).
    5. go to 1.
  • #9 18811552
    futek2
    Level 19  
    The router is in the attic and the ESP8266 by the inverter is about 7 metres away. For now I have other things to do, maybe in the evening I will do something else. Because the test is done and it works. As for the suspension of the connection to the Internet, my observations are such days that it happens when, for example, at night it rains. P.p. the modem changes the connection with the BTS looking for a station and finally it hangs up and from the router side everything is fine. Maybe I will approach how to do a self shutdown of the E3372 modem that was similar to the reboot of the MR3020.
    But it will take some time because the modem also sends a token.
    Futek2
  • #10 18811592
    krzbor
    Level 27  
    futek2 wrote:
    The router is in the loft and the ESP8266 at the inverter is about 7 metres away
    This in the loft make a second ESP that exclusively monitors the connection (such a watchdog). I even found something like this: Link .
  • #11 18812063
    futek2
    Level 19  
    Hello.
    Thanks kol krzbor for the good advice.
    But I like things simple. Just require knowledge, skill and patience....
    I have a good hope that it will work without additional equipment.
    And colleagues who know how to program ESP8266 will help for the good of the community because they have shared their knowledge more than once, which was helpful to me.
    I am grateful to them because I learned a lot and many useful simple projects were created.
    Because my programming education started with QBASIC from DOS then there was no windows yet and the beginning was ATARI but then I was just over 20 years old.
    Greetings.
    Futek2
  • #12 18814110
    futek2
    Level 19  
    Hello to those interested in the topic.
    After many tests before, everything went well and is tested many times and the remote reset of the TP_LINK computer with MR3020 is working.
    Below is the code for the procedure and attached is the PINGER library which needs to be manually installed into the ARDUINO IDE - zip file attached.
    Add the following lines at the beginning of the code:

    #include <Pinger.h> .

    Below is the code for the procedure to remotely reset TP_LINK _MR3020 to the ESP8266 module.

    
    void TP_LINK_REBOOT()
    {
        Pinger pinger;
    // dane logowania do ustwien routera - zmienic na swoje potrzeby
    String   user="admin";
    String   login="admin";
    const char* TP_LINK = "192.168.1.1";
    
     if (WiFi.status() == WL_CONNECTED) {
      Pinger pinger;
      int responseTime = pinger.Ping(IPAddress(8, 8, 8, 8));
      if(responseTime == -1)  // tej wartosci nie zmieniac
        {
         Serial.println("BRAK POLACZENIA Z INTERNETEM");
          delay(500);
            HTTPClient http; 
            delay(500);
            Serial.println("RESET RUTERA MR3020"); 
            Serial.print("[HTTP] begin...\n");
            http.begin("http://"+user+":"+login+"@"+TP_LINK+"/userRpm/SysRebootRpm.htm?Reboot=Reboot");
            int httpCode = http.GET();
            http.end();
             }
          else 
           {
           Serial.println("POLECZENIE Z INTERNETEM CZYNNE");
           Serial.print(responseTime);
           Serial.println("ms.\n");
          }
         }
        }  
      
    .

    Please find attached the software for the router TP-LINK MR3020 with which this code for remote reset works, because it is a version that does not send the TOKEN when logging in.
    I think that the code will also work with other TP-LINKs with older versions of the software.
    But this should be checked individually on the router you own.

    If I have time, I will write the code to read TOKEN so that it can work with newer software versions of TP_LINK routers.
    Thanks to everyone for their advice and I think improvements are on the way.

    Please do not hesitate to ask for comments and suggestions.

    Best regards.
    Futek2
  • #13 18815813
    khoam
    Level 42  
    What happens if the http.begin() function returns false i.e. does not execute correctly?
  • #14 18816182
    futek2
    Level 19  
    Hello to those interested in the topic .
    Thanks for the question kol. khoam doing tests every time the command was executed. So I have simplified the code and if it is not executed in the first time, then in the second time.
    From this I have received, the response looks like it sends the html code of the whole page. So I think this will allow you to select the token and read it after logging in and then only send the command to reboot the router. With more time I will work on improving and reading this token so that it also works with newer versions of the software in TP_LINK.
    For the time being, this solves my problem, the code that I have tested has been simplified in its entirety, and this is what the part with the reading of the response would look like:

    int httpCode = http.GET();
            Serial.print("[HTTP] GET...\n");
            if(httpCode > 0)
            {
            Serial.printf("[HTTP] GET... code: %d\n", httpCode);
               if(httpCode == HTTP_CODE_OK)
               {
                    String payload = http.getString();
                    Serial.println(payload);
                }
              } 
              else
              {
                 Serial.printf("[HTTP] GET... failed, error: %s\n",        
                http.errorToString(httpCode).c_str());
              }
            http.end();
    .

    The problem is partially solved and after the exchange of the software in the TP LINK MR3020 to an older version solves the problem.
    And since a few days everything is fine, but it would be worthwhile to improve the code.

    Regards.
    Best regards. Futek2

Topic summary

The discussion revolves around automating the reboot of a TP-LINK MR3020 router using an ESP8266 module. The user experiences intermittent internet connectivity issues with the MR3020, prompting the need for a remote reboot solution. Various suggestions are made, including using a relay to cut power to the router, implementing OpenWRT firmware for scripting capabilities, and parsing session tokens for reboot commands. The user successfully retrieves the session token and shares a method to reboot the router via command line. Additionally, a code snippet for the ESP8266 is provided to facilitate the reboot process, along with troubleshooting tips for improving router stability and power supply issues.
Summary generated by the language model.
ADVERTISEMENT