how to implement a TCP socket in serverSocket.cpp ?



  • how to implement a TCP socket in serverSocket.cpp ?

    i'm try to use the WiFiClient client to connect to a host. but it can't get connected



  • As coded today, the printer firmware does not "initiate" or "open outbound (from the printer)" connections. It will accept "inbound" connections...

    Therefore, your question:

    how to implement a TCP socket in serverSocket.cpp ?

    Has a couple of answers:

    1. There is a socket server there already, acting as the client to other hosts. It supports (with proper configuration) HTTP, Telnet, and FTP.

    2. If you really mean "how can I implement a TCP Client inside the printer that will initiate connections to other hosts on the network"... the fundamental answer is "Write code for it."

    However... those are probably not very useful answers... tell us more about what you are trying to accomplish. Perhaps a "use case" or two.



  • @eberlin said in how to implement a TCP socket in serverSocket.cpp ?:

    serverSocket.cpp

    I would try to analyze how tcp is done in the Duet firmware: in
    https://github.com/dc42/RepRapFirmware/tree/dev/src/Duet
    is some code how to establish different connections. From here you can find out who uses those methods and how (I mean some other code inside RepRapFirmware).
    I would start with the code in Network.cpp and Webserver.cpp



  • @eberlin an alternative is (if it's your try of Arduino) to install an ESP8266 on the Arduino, make a http connection from there to Duet, und make those rr_ commands which allow transferring files etc.

    That's the way I will attach added functionality like added sensors to the printer. There is a library https://github.com/esp8266/Arduino for example for startup.

    Some info about rr_ commands can be found in the main page of DWC:
    https://github.com/chrishamm/DuetWebControl

    BTW ESP8266 can run standalone and can be programmed with Arduino IDE, Depends on your requirements which option you want to use, I looked into ESP32 a bit also.



  • thanks for your help.

    i've tried to implement a get request on an ESP8266 modul (NodeMCU LUA Amica V2) with arduino.
    This code works:

    #include <ESP8266WiFi.h>

    const char* ssid = "xxx";
    const char* password = "xxx";

    const char* host = "mynodejsServer";

    void setup()
    {
    Serial.begin(115200);
    Serial.println();

    Serial.printf("Connecting to %s ", ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
    delay(500);
    Serial.print(".");
    }
    Serial.println(" connected");
    }

    void loop()
    {
    WiFiClient client;

    Serial.printf("\n[Connecting to %s ... ", host);
    if (client.connect(host, 80
    ))
    {
    Serial.println("connected]");

    Serial.println("[Sending a request]");
    client.print(String("GET ") +"/test" + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n" +
                 "\r\n"
                );
    
    Serial.println("[Response:]");
    while (client.connected())
    {
      if (client.available())
      {
        String line = client.readStringUntil('\n');
        Serial.println(line);
      }
    }
    client.stop();
    Serial.println("\n[Disconnected]");
    

    }
    else
    {
    Serial.println("connection failed!]");
    client.stop();
    }
    delay(5000);
    }

    i took the important part of this code and copied it into the SocketServer.cpp, like this:

    void getRequest(){

     const char* host = "mynodejsServer";
      WiFiClient client;
    
    
    	  if (client.connect(host, 80
    	 	   ))
    	 	   {
    	 	   //  Serial.println("connected]");
    
    	 	     Serial.println("[Sending a request]");
    	 	     client.print(String("GET ") +"/test" + " HTTP/1.1\r\n" +
    	 	                  "Host: " + host + "\r\n" +
    	 	                  "Connection: close\r\n" +
    	 	                  "\r\n"
    	 	                 );
    
    	 	     Serial.println("[Response:]");
    	 	     while (client.connected())
    	 	     {
    	 	       if (client.available())
    	 	       {
    	 	         String line = client.readStringUntil('\n');
    	 	       //  Serial.println(line);
    	 	       }
    	 	     }
    	 	     client.stop();
    	 	     Serial.println("\n[Disconnected]");
    	 	   }
    

    }
    i called this method in case NetworkCommand::connCreate:

    the esp8266 on the duetBoard dont send the get request with the same code.
    does the duetBoard hold back the request ?
    but the network protocol is enabled...:

    M586
    HTTP is enabled on port 80
    FTP is disabled
    TELNET is enabled on port 23



  • I still think you are going about this "backwards"...

    but you still haven't said what you are trying to do.



  • @eberlin What @Danal says, is valid, maybe we help into the wrong direction.

    But to help with your code: if you begin with something basic like establish a connection, you should debug every step. Because the reason could be something trivial like the hostname you have in the code unknown, or something other. So I would uncomment the Serial.print lines and maybe even add some addition at every step, so you can see what works and what not. After you find which line does not work, please tell us the messages you got back.



  • @eberlin Another idea is to make a connection from your PC to the Duet, with a tool like discussed in
    https://stackoverflow.com/questions/4797534/how-do-i-manually-fire-http-post-requests-with-firefox-or-chrome
    and send the http requests from inside your browser. You need to have the Wifi connected to your Duet (if you use DWC with this PC, this is fulfilled). Instead of the hostname you can use the IP address of Duet in most cases.

    E.g. postman seems a good tool (I don't know it).



  • thanks for your ideas

    @danal @JoergS5
    my goal is to send the current printer status to my nodejs server. the server runs on a cloud server.
    the server app should deliver a website with the printer status and some buttons to control the printer.
    the website on the server should have the same functionality's as the DWC from chrishamm.
    to use the DWC i need to connect my "Duet Wifi" with my router and after that , every PC in the local network can reach the website. but the nodejs server cant reach the local IP of the duet board, after the printer succcenfully connects to the router. i'am searching for a general solution to control numerous duet printer. and my plan is, that the printer sends the request, to open a socket to the server.

    the server should manage multiple tcp sockets to different duet wifi printer.

    use case:
    the user can control the printer over the internet, for example "start print" or "stop print".

    use case:
    the user can check the current status of the printer over the internet.

    @JoergS5
    i can't Serial.println my steps, because the ESP8266-modul is connected to the duet board. i didn't get any response because the firmware dont any message trough..

    the only idea i had, was to buy an esp8266 modul and write there the code. after i tested the code at this seperate modul , i was sure that the code is correct and copied it in the right place of the SocketServer.cpp



  • @eberlin One thing to start with is how to handle the internet connection for your Duet to the internet and from the internet.

    Please search for internet in the forum, you will find some threads how to connect and how to protect, e.g.
    https://forum.duet3d.com/topic/6746/secure-your-printers-before-it-s-too-late/19
    https://forum.duet3d.com/topic/6064/acces-duetwifi-from-the-internet
    https://forum.duet3d.com/topic/4283/what-not-to-spend-time-resource-developing

    There is a tool here to monitor several printers, you may want to look at this also.
    https://forum.duet3d.com/topic/6351/printer-farm/2
    The MutliDuetWebMonitor, I think it's from @Danal :
    https://github.com/DanalEstes/MultiDuetWebMonitor



  • @joergs5

    multiDuetWebmonitor is nice, but it's also cannot connect to the printer over the internet. he can reach the ip , if the PC is in the same network.

    the links show tips to connect with VPN. i dont wont to use this way.
    i know that the WifiModul on the Duet board is in general able to connect to server outside of the local network. but something in the DuetWifiSocketServer Firmware is blocking this way. i know that, because dc42 told me in a thread, that he does that.
    he implemented a TCP socket in the SocketServer.cpp, but i dont know how..

    this is the best way to connect the printer with the server, because every user can use this without to prepare there router and so on ..



  • Thank you for describing what you are trying to do.

    It is my very strong recommendation that your NodeJS server open the connection and "poll" the printer. I say this because:

    1. NO CHANGES to Duet/RepRap firmware are required to do this. It can and will work instantly whenever you add code to your NodeJS server to perform this function.

    2. At some level, you are trying to solve the "how do I connect the Duet/RepRap/Printer on my private network to my NodeJS server (presumably running in a 'cloud' data center somewhere) that is visible on the public internet.

    Phrasing it that way makes it much more clear that no changes to the Duet/RepRap are required... getting that private/public connection to work is the munch more important piece.

    Yet another way to phrase the same thing: You discovered that adding code to RepRap to open the connection didn't solve anything. It (mostly) not about who opens, or sends, or etc... it is about getting from one to the other.



  • So, what will work? You need:

    • Static IP or Dynamic DNS on your Home Network to ISP connection.

    • Static IP for the printer itself inside your Home Network.

    • A Reverse Proxy. Personally, I'd run this on a Raspberry Pi. There are lots of other ways.

    There are tons of guides on the internet for the first three of the above things... just google...

    • Almost goes without saying... but just for clarity... security on that reverse proxy; security that keeps the bad guys out and lets your NodeJS in. I phrase it that way because picking what security you use is largely up to whatever you wish to code into NodeJS.

    • NodeJS to open the printer by the External IP (very first bullet above), connect, poll, etc. From the things you've already done, it looks like you have the skills, etc. If you need tips or get stuck, I'd be happy to help.

    That's how I would do it...



  • This post is deleted!


  • @eberlin you should follow the hints from @Danal how to open your router and address the internal Duet. If you send the TCP connection from your node.js to the Duet, this channel will be used to get the response back and should work through your router.

    To open the router, it is good to restrict access to the server network MAC address where your node.js runs on, so nobody else can call in.

    The Duet's Wifi is http only, no https. So traffic to or from Duet is not protected, Wifi passwords or other sensitive data could be spyed. That's the reason for the Raspberry solution. This was a separate Thread in the Duet forum.

    Many years ago I used a different solution: an old PC, installed with two network cards, and a software called floppyfw on it. This allowed specified firewall rules with allowances and restrictions.

    If the WifiSocketServer blocks something: do you know the thread so I can read and try to understand it?



  • @joergs5 said in how to implement a TCP socket in serverSocket.cpp ?:

    Many years ago I used a different solution: an old PC, installed with two network cards, and a software called floppyfw on it. This allowed specified firewall rules with allowances and restrictions.

    Exactly!

    That's what the reverse proxy on the Pi will do...



  • status is the easy part. connect to duet with
    http://a.b.c.d/rr_connect?password="xxx"&time="2018-04-09T21:46:00"
    and then poll
    http://a.b.c.d/rr_status?type=3

    I do that on my node-red server that provides a GUI with the status of a printer


 

Looks like your connection to Duet3D was lost, please wait while we try to reconnect.