Running a server, that needs to send occasional emails [SOLVED]

Greetings.

My first post, so please pardon any faux-pas...

I'm building an Arduino Mega2560 based application (with W5100 ethernet shield) for my water system. It reads conductivity, water levels etc, which for now it displays on a TFT and on a webpage. It also needs to send emails, in case some of the levels fall outside of normal operation.

I successfully implemented the TFT and the webserver from the Examples in Arduino IDE, using fixed IP (Ethernet.begin(mac, IP))... getting readings as supposed.

I'm also successfully sending emails through smtp2go, using their sketch. This one uses Ethernet.begin(mac), relying on a DHCP server. it also works.

Here's the problem:

As long as I use Ethernet.begin(mac) to operate in DHCP mode, both the server and the emailing seems to work.

If I use Ethernet.begin(mac, IP), SMTP2GO's email server refuses to connect (client.connect(emailServer, port) = 0).

I've been looking at Client and Server runing at the same time - Programming Questions - Arduino Forum, about Client and server running at the same time. The "server" code is essentially identical to what I'm using, but the "client" is different enough, that I can't make the connection (no pun intended).

Cheers

If I use Ethernet.begin(mac, IP), SMTP2GO's email server refuses to connect (client.connect(emailServer, port) = 0).

Sounds like your using a wrong IP address. If you get the IP by DHCP your router ensures you get an IP that works on your network. If you choose that IP yourself you may choose one that the router doesn't accept to route to the internet.

As we have no clue about your network setup we cannot help you further here.

Hello Pylon;

Thanks for the suggestion.

my internal network is 192.168.yyy.xxx. before assigning a fixed IP, I went in and looked for a free one. turns out to be 190. This didn't work, for sending emails. It does work to get NTP time and for the webserver.

When I let the DHCP assign the address, I get 86. With this NTP, server and email work.

So, I assigned 86 as the fixed IP. NTP works, server works, sending email doesn't...

My workaround for this will be (actually safer, I think) to reserve an IP (like 190) when the arduino asks for a IP address. passable solution, but irritating...

Why do you need to assign the IP if everything works when you use DHCP?

"but the "client" is different enough, that I can't make the connection (no pun intended)."

Well, you might post your code to show what changes you made. Assigning an IP in the client function sounds odd unless maybe it is the IP address of the mail server.

So, I assigned 86 as the fixed IP. NTP works, server works, sending email doesn't...

Correct me if I'm wrong: sending the email is the only function that needs access to the Internet. Maybe your router don't allow internet access if the client didn't requested the IP by DHCP.

Try to set a fixed IP on your laptop and then access the Internet. Does that work?

If you want a device to have a specific IP address leave DHCP enabled on the device and look in the configuration of your router. Most routers* allow you to reserve a specific IP address for a particular MAC address. If you do that then the device gets its IP address using DHCP and the router gives it the address you want it to have.

*Probably some of the free ones supplied with broadband connections don't allow this.

PerryBebbington:
If you want a device to have a specific IP address leave DHCP enabled on the device and look in the configuration of your router. Most routers* allow you to reserve a specific IP address for a particular MAC address. If you do that then the device gets its IP address using DHCP and the router gives it the address you want it to have.

Hello Perry;

Yes, this is exactly how I will proceed. My MikroTik RouterBoard 3011 calls this "make Static", instead of "reserve". Same outcome

Thanks all for contributing.

Case closed (there must be way of designating this thread as such).

pylon:
Correct me if I'm wrong: sending the email is the only function that needs access to the Internet. Maybe your router don't allow internet access if the client didn't requested the IP by DHCP.

Try to set a fixed IP on your laptop and then access the Internet. Does that work?

Hello Pylon, there are two functions, which access the Internet: getting NTP time, which works fine both in DHCP and fixed IP. The email function is the one, which only works in DHCP mode.

Let's think no further though. Staying in DHCP and "reserving IP" via my router is how I will proceed.

(and yes, the PC I'm writing from has a fixed IP and can access the internet.)

Thanks for your help all!

Case closed (there must be way of designating this thread as such).

Edit your first post, you can change the title. Prepend something like [SOLVED] to the title.