Comments (26)
No, there's no specific function for that in the library.
Actually there's no way to determine that in a portable way. (where 'portable' refers to 'does work in every environment', so there ain't no such function in the lib. What can be detected is if the link itself goes down (cable unplugged or switch turned of), but everything that is behind the next hop (switch) is not easy detectable. To determine link-status one would have to read ENC28J60 LLSTAT and LSTAT-bits from PHSTAT1 and PHSTAT2 (which the library as it's now ignores and I don't have plans to change that as it increases the size without adding real benefit - because it's just the link, not the overall connectivity to the internet).
The library will tell you whether a Client (tcp) is still connected to the remote side (where 'connected' in terms of tcp/ip means 'not disconnected and not timed-out' which implies that it will not detect a disconnection from the internet on the protocol-level faster than the tcp-timeouts). For UDP there's nothing on the protocoll-level that gives you a reliable feedback (for both tcp and udp these are not errors but the nature of each protocoll).
If you need to detect such situation you have to implement you own checks on top of tcp or udp (e.g. trying to connect to a known site on the internet. Or - if you communicate with a known peer that runs your own software - implement a protocol that does send 'keepalive'-data if no actuall data has to be transmitted).
- Norbert
from arduino_uip.
Thanks Norbert, i'll try to connect to a known site on the internet. And congratulations for your work, its awesome.
from arduino_uip.
Hi Norbert,
As I said before I'm using udpserver example, im trying to connect to google.com to check if I'm online but I am a bit lost.
What can be the easiest way to do it by using functions of your library? I was thinking on udp.beginpacket but it seems that you need the IP address rather than the domain's name (that is what I want to use). I've been checking examples and your library but my programming skills aren't the best and the library is huge. I think that in some cases the library uses dns.h to resolve domain ip but I am a bit lost.
Thanks
from arduino_uip.
UIPUDP::beginPacket(const char *host, uint16_t port) does allow to connect remote host via UDP, but google most likely will not respond to that (because of UDP, better use Client.connect(hostname,port) for that as this is TCP).
You may rather want to discuss this issue on the Arduino networking forum http://forum.arduino.cc/index.php?board=11.0
as this is about how networking, protocoll and internet works in general and would have to be done the same using stock Ethernet-library.
from arduino_uip.
Thanks, i finally managed to get it working changing from udp to tcp (starting from tcpserver and echoes).
On the other side, I wanted to check if the client is connected by using client.connected, but it only returns 1 when a message is received, and after that, until u send again some message it returns 0.
Didn't u implement it?
Thanks.
from arduino_uip.
thank you for reporting this. This is not intended and clearly a bug, that I just managed to reproduce on my side as well. It's strange that the current implementation does not work as expected though, but I'm confident to find out why ;-)
from arduino_uip.
hmm... I had a logical error in my test-setup that I corrected and now cannot reproduce this error any more. Can you please post the code that doesn't work on your side?
from arduino_uip.
Hi, I was wrong!, I've tested again starting from echoserver from bibi21000 and just added the while client.connected as some ethernet standard library examples do, and it looks like works fine while you are connected, but if you close the connection from the client (i'm using a random android TCP client app) it gets stuck into while client.connected bucle and some random stuff appears at serial monitor (out of bucle doesn't appear).
I've also noticed that client.connect from tcpclient returns 1 after several seconds even if you don't have an internet connection, and after that it disconnects. thats the log from serial monitor after unplugging internet cable:
Is that the stuff you want from library or it should return 0 after failing to connect?
Thanks and sorry for having so many questions!.
from arduino_uip.
I can reproduce the first issue. It's allready fixed in pre-release-branch 'static-memory' (https://github.com/ntruchsess/arduino_uip/tree/static-memory).
I cannot reproduce the second issue though, but don't have the code you are actually running.
On my side this code:
#include "UIPEthernet.h"
EthernetClient client;
signed long next;
void setup() {
Serial.begin(9600);
uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
Ethernet.begin(mac);
Serial.print("localIP: ");
Serial.println(Ethernet.localIP());
Serial.print("subnetMask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("gatewayIP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("dnsServerIP: ");
Serial.println(Ethernet.dnsServerIP());
next = 0;
}
void loop() {
if (((signed long)(millis() - next)) > 0)
{
next = millis() + 5000;
Serial.println("Client connect");
// replace hostname with name of machine running tcpserver.pl
// if (client.connect("server.local",5000))
if (client.connect(IPAddress(192,168,0,1),5000))
{
Serial.println("Client connect succeeded");
Serial.println(client.connected() ? "connected" : "not connected");
client.println("DATA from Client");
while(client.available()==0)
{
if (next - millis() < 0)
goto close;
}
int size;
while((size = client.available()) > 0)
{
uint8_t* msg = (uint8_t*)malloc(size);
size = client.read(msg,size);
Serial.write(msg,size);
free(msg);
}
close:
Serial.println(client.connected() ? "connected" : "not connected");
//disconnect client
Serial.println("Client disconnect");
client.stop();
}
else
Serial.println("Client connect failed");
Serial.println(client.connected() ? "connected" : "not connected");
}
}
produces this following output if it fails to connect (both cases 'remote service not running' or 'cable unplugged'):
localIP: 192.168.0.58
subnetMask: 255.255.255.0
gatewayIP: 192.168.0.1
dnsServerIP: 192.168.0.1
Client connect
Client connect failed
not connected
Client connect
Client connect failed
not connected
Client connect
Client connect failed
not connected
Client connect
...
If you open a connection and unplug the cable the connection will remain in state 'connected' for quit a long time. This is not an error but due to the design of how the tcp-protocoll deals with unreliable connections. Client.connected() returns true as long the socket is in tcp ESTABLISHED state. See http://en.wikipedia.org/wiki/Transmission_Control_Protocol for what this means.
Basically you cannot use Client.connected to detect an unplugged cable before the tcp-timeout has expired. You only can use the return value of 'Client.connect()'
from arduino_uip.
Thanks again Norbert.
First issue is solved by using pre-release branch.
About second issue, I'm using same code than you, but connecting to a host (google.com for example) instead of an ip.
Your code replacing ip for bad hostname:
I've tested it unplugging the ethernet cable and writing a host that doesn't exist and I'm having that serial monitor output:
Sorry for not pasting my code before.
from arduino_uip.
yes, I can reproduce this. While I've just fixed a flaw in UIPClient.connected() (in branch static-memory), it shows there's something broken in Dns.cpp
from arduino_uip.
I tried the code above but I am getting
localIP: 0.0.0.0
subnetMask: 0.0.0.0
gatewayIP: 0.0.0.0
dnsServerIP: 0.0.0.0
Client connect
Client connect failed
not connected
Client connect
I am on a network with different numbers like 10.0.1.X
on a atmega328 =(
from arduino_uip.
the code above requires an dhcp-server to provide ip-address and other network settings. Apparently this did'nt happen in your case. You may specify local-ip, subnet, gateway and dns (all optional) using the Ethernet.begin()-methods that have more parameters.
If you are able to use a packet-sniffer like wireshark you can trace wether dhcp-packets actually reach your dhcp-server (if there is any) and check the responses.
The Ethernet-cable has to be connected when Arduino is started (or resetted) in order to retrive the ip-address. In the code given obove it will only try once.
from arduino_uip.
Hello How can I change the cs pin 10 by 8. I'm trying to change Enc28J60Network.h file.
#define ENC28J60_CONTROL_CS 8
#define SPI_MOSI 11
#define SPI_MISO 12
#define SPI_SCK 13
but it does not work. Can you help me?
from arduino_uip.
these defines look good, but please discuss details this in issue #24
from arduino_uip.
I see. So I in order to retrieve network settings I should have to reconnect the cable in every reset right?, or by resetting the enc28j60 should be enough?
Greetings!
from arduino_uip.
to be precise: 'connected' means 'in connected state'. No need to unplug and reconnect.
from arduino_uip.
I tested with the following code, but still it seems it is not working
#include <UIPEthernet.h>
EthernetUDP udp;
unsigned long next;
void setup() {
Serial.begin(9600);
uint8_t mac[6] = {0x74,0x69,0x69,0x2D,0x30,0x31};
Ethernet.begin(mac,IPAddress(10,0,1,25),IPAddress(10,0,1,1),IPAddress(10,0,1,1));
next = millis()+5000;
Serial.print("localIP: ");
Serial.println(Ethernet.localIP());
Serial.print("subnetMask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("gatewayIP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("dnsServerIP: ");
Serial.println(Ethernet.dnsServerIP());
}
and I only have four cables connected, I disconnected the INT from the ehercard configuration but it looks it didnt help, what arduino version are you using to test it out? and what atmega?
Greetings!
from arduino_uip.
So far I could test on Uno, Nano (both Atmega328), Mega256 and Netuino (Atmega32-based AVR-NetIO-board from 'Pollin', a germen electronics dealer).
Int is not used by the lib. You need 6 (better 7) cables (Vcc, Ground, MOSI, MISO, SCLK and CS), Reset is optional but very usefull for a clean start.
- Norbert
from arduino_uip.
what version of arduino are you using?
from arduino_uip.
@beckmx Arduino 1.0.5, The Uno is an R3, (the others I can check later). But I don't think that's relevant - you might have mixed up the cables - e.g. swapped MISO and MOSI, then the library just reads 0 bytes via SPI.
from arduino_uip.
support for other values than (Hardware-)SS for ENC28J60_CONTROL_CS is fixed.
Still have to check out the issue with EthernetClient.connect()-return value.
from arduino_uip.
could fix an issue with Client.connected() -> see #28
EthernetClient.connect() remains work in progress
from arduino_uip.
fixed connect return value in #29
from arduino_uip.
Thanks Norbert, i'll check if its working for me as soon as possible. Anyway, GREAT WORK!!
from arduino_uip.
reverted change introduced in #29. As @nilrog pointed out this broke compatibility with stock ethernet-lib.
use client.connect(hostname,port) this way:
const char* host = "www.arduino.cc";
if (client.connect(host,80)>0) {
// your code is called only when dns was successful.
}
from arduino_uip.
Related Issues (20)
- How to change the CS pin in software? HOT 3
- Can anyone advise on the current status of this library? HOT 9
- How to hookup the enc module to mega2560 to use this library? HOT 1
- How to make HTTPS connection using UIPEthernet lib? HOT 1
- Arduino Due
- ENC28j60 bitwise OR 0x80
- Starting Etherenet from an external class
- client.available() always returns false HOT 1
- How to recover the IP address of the client?
- Cannot open source file, Error
- How to send the data over tcp ? HOT 1
- how to send packet over tcp with static ip
- Arduino micro and enc28j60 doesn't work for me HOT 2
- where is udp.h HOT 1
- enc28j60 server connection only 4 times HOT 1
- enc28j60 server connection only 4 times
- compiler warning import => include
- ESP8266 freeze when TCP connection intempestively close.
- Arduino nano every
- error: #include nested too deeply #include <Arduino.h> & error: #include nested too deeply #include "clock-arch.h"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arduino_uip.