Gmail, obtaining IP

ben1
2023-02-23
2023-02-26
  • ben1 - 2023-02-23

    Hello everyone!

    First post, very new to Codesys.

    My background is an electrician and a PLC programmer.

    I am currently using a Schneider M262 PLC on Machine Expert software, which is built on Codesys.

    Firstly, I am very new to this - Codesys is like a leap from easy PLC coding to much more in-depth (and smart people) computer coding, in my opinion!

    Anyhoo, I am enjoying it and learning quite a bit, but I bring you a problem today that I hope you can help with!

    I have recently worked out how to send an email from the PLC using Gmail. It is working great!

    If anyone is familiar, I am using the library EMailHandling 2.1.00 (Schneider Electric), which requires an input i_sServerIP - The servers IP address (which btw it seems very strict on it being the IP and not the servers name).

    Now a quick ping to smtp.gmail.com provides me with an IP which I can use to get this bad boy talking.

    Now the problem being, as far as I can read and as one colleague has unfortunately found on another project - these IP's are ever-changing. So at some point the email will stop working as the Gmail server IP I am using will no longer exist.

    So my question is; I wonder if I can run some form of ping, to smtp.gmail.com, from the code, to obtain the IP address?

    It was my idea that I could just run this every month or something and find a new IP to ensure emails are working. Or every time it fails, or whatever!

    But I have googled & searched here and I cannot seem to find an answer!

    Thank you for your time, please let me know if you need more info!!! (Or if there is a better/easier way to go about what I am doing!)

     

    Last edit: ben1 2023-02-23
  • TimvH

    TimvH - 2023-02-23

    You need to have access to a DNS server which can provide this for you. There seems to be a function block for this in Machine Expert:
    https://product-help.schneider-electric.com/Machine%20Expert/V1.1/en/TcpUdpCo/TcpUdpCo/Function_Blocks/Function_Blocks-61.htm

    Google also has a DNS server which you could use. The IP address is 8.8.8.8. The default port is 53, but the documentation of the FB describes that you do not have to enter this.

     
    • ben1 - 2023-02-23

      Hello my fast replying friend,

      Thank you so much for the answer!

      That block looks perfect, I will try it out and let everyone know!!!

       
    • ben1 - 2023-02-24

      Hello pal!

      This block worked great, it appears to grab an IP no issues...

      However πŸ˜…

      For some reason this IP won't work?

      I have used DNS Server IP 8.8.8.8 and Domain Name smtp.gmail.com.

      It returns an address, usually appearing to be 172.x.x.x.

      If I try to run the email using this address it fails "Cannot connect to server".

      If I run a ping from my PC's cmd I get an address more like 142.x.x.x.

      I manually change the IP variable I am using for the email to this and try again and it works without a hitch...

      Am I missing something?

      Thanks!

       
  • ben1 - 2023-02-24

    I see I have posted twice there but I cannot find a way to delete the second post, can anyone advise?

     
  • TimvH

    TimvH - 2023-02-25

    Probably you are in a network where another DNS server is available and should be used. Maybe you can connect your laptop to the same network and set your IP setting to automatically get a DNS server IP address. Then check what this IP address is. You could find this out by e.g. opening a CMD box and type: ipconfig /all.
    Then see which IP address your laptop got and use this as IP address for the DNS server in your application.

     
  • ben1 - 2023-02-26

    You sir, are a guru.
    Thank you!!!

     

Log in to post a comment.