Arduino Ethernet Shield does not accept connections - tcp

I have been playing around with an arduino ethernet shield, trying to get basic examples to work, to no avail. Here is my setup:
The Arduino Mega 2560 is connected to the computer via usb and the ethernet shield is stacked upon it. I have tried many variations of the examples that come with the arduino software, and none seemed to work properly.After lots of debugging with wireshark, I figured that:
I can't use DHCP, because it just hangs at the Ethernet.begin(mac) call.
When I try with a static ip, the Ethernet.localIP() function returns 0.0.0.0. However, I can ping my device from my computer using the ip I have set and the device seems to receive and send packets properly.The problem now is that for some reason it drops the tcp connections.E.g here is the code I run that comes the closest to working:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,27);
IPAddress server(192,168,2,52);
EthernetClient client;
void setup() {
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("a");
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 23)) {
Serial.println("connected");
}
else {
Serial.println("connection failed");
}
Serial.println(Ethernet.localIP());
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// as long as there are bytes in the serial queue,
// read them and send them out the socket if it's open:
while (Serial.available() > 0) {
char inChar = Serial.read();
if (client.connected()) {
client.print(inChar);
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing:
while(true);
}
}
Its basically the Ethernet/TelnetClient example.
I have set up a telnet server on my computer. Now this is the arduino/computer exchange:
The arduino sends a RST packet, but my server goes on to send it the greeting and login prompt.
I have tried the same with an arduino uno, and have also tried disconnecting the usb and using another power supply.
So, what could be the issue?

The problem is in the connection to the shield, sometimes this shield if it is chinese version, It might have small short-circuit.
I tried disconnect the shield and connect with wire as like as arduino website indicate for arduino uno (the connections with arduino mega aren't correct, so you need connect like arduino uno)
If it don't work , try change the arduino shield. I have a similar problem with the same shield , and normally the problem is the connection to the arduino.
The example should work if the arduino is correctly connected

Strangely, I found that Ethernet fails to initialize if an SD card is inserted and not initialized. So, either take the SD card out, or initialize the SD card:
Sd2Card card;
card.init(speed, pinSelect);
I have some Chinese (SunFounder) version of Ethernet Shield, so it might not be relevant to cards from other manufacturers.

Related

ESP8266 TCP stop working on "heavy" traffic

Context
Hello everybody. I'm working on a project where I need to send about 60 TCP sockets per second to my ESP8266 in order to change a light bulb intensity "in real time". The sockets are really small, like 5 bytes.
Hardware
Server device: NodeMCU 1.0 (ESP-12E Module)
Client device: Linux 16.04 PC sending data with Node.js
-The NodeMCU board is running the last Arduino firmware: https://github.com/esp8266/Arduino
The problem
When I send a lot of TCP packets every second, the ESP8266 wifi eventually stops working. The cpu keeps working but it won't reply any ping or TCP request.
I created a really small program just to test this bug, and here it is the wireshark output.
(192.168.1.11) -> ESP8266
(192.168.1.101) -> Linux PC
As you can see, there is a moment where the ESP8266 stops sending ACKs. Sometimes it will recover after a few seconds, sometimes it doesn't.
Here is the code I use in the ESP8266:
#include <ESP8266WiFi.h>
#define TCP_PORT 17717
#define PIN_LED 2
#define MAX_INTENSITY 255
#define MAX_PWM_FREQ 1023
WiFiServer server(TCP_PORT);
WiFiClient socket;
const char * ssid = "MyWifi";
const char * password = "MyPass";
void setLed(byte intensity) {
analogWrite(PIN_LED, (int)(intensity/(float)MAX_INTENSITY * MAX_PWM_FREQ));
}
void setup() {
Serial.begin(115200);
pinMode(PIN_LED, OUTPUT);
setLed(0);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.print("Ready! IP = ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
if (server.hasClient()) {
socket = server.available();
while (socket.connected()) {
if (socket.available()) {
setLed(socket.read());
}
}
}
}
Any idea of what is going on here?
It's probably because the MAX_SOCK_NUM is 4 defined in the Ethernet.h file. So we cannot connect more than 4 TCP connections which is restricted by the small memory size of the TCP/IP stack in ESP8266 and if we exceed the fixed number the board crashes.

Arduino sending data to ESP8266 using Arduino IDE

Goal: Send two integer values from Arduino Nano to internet via ESP8266 using Arduino IDE
I am new to embedded programing and currently working on a project which sends some integer value from Arduino analog pins to an online database (IP address, port) via esp8266.
At this moment I know how to individually send data from ESP8266 to an IP keeping ESP in client mode. But I don't know how to transfer data generated at Arduno Nano to ESP8266.
#include <ESP8266WiFi.h>
#include<Wire.h>
const char *ssid = "SSID";
const char *password = "asdfghjkl";
const char* host = "192.222.43.1";
int portNum = 986;
WiFiClient client;
WiFiServer server(portNum);
void setup() {
Serial.begin(115200);
Wire.begin();
delay(10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("WIFI OK");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println("Connected to Wifi");
}
String message="";
void loop() {
message = "12,13"; // Message to be sent to ESP8266
if(!client.connected())
{
client.connect(host,portNum);
}
if(message.length()>0)
{
Serial.println(message);
client.println(message);
message="";
}
I can understand I would have to connect the TX-RX pin of Arduino - ESP to pass the data. But for some reason I am not able to make it work.
I would really appreciate if someone can help me understand the process with a simple example.
Thanks.
PS: The reason I had to use Arduino is because the sensor I am using need 2 analog Pins and ESP just have 1.
You connect Arduino Tx to Esp Rx
You connect ESP Tx to your serial device to your PC (so you can read the messages from the ESP in your Terminal window)
On the ESP you use the Wire library you have loaded.
You use the Serial object to listen for incoming data on the ESP's Rx pin.
void loop()
{
while (Serial.available())
{
Do something;
}
}
This works exactly the same as Arduino to Arduino serial comms and there is a nice tutorial here:
Arduino to Arduino comms
WARNING: ESPs use 3.3V and Arduinos use 5V on the Tx and Rx pins. You must not allow 5v to reach the pins of the ESP or it may burn out.
This tutorial shows a safe wiring diagram.
safe wiring diagram
1) Try this sample: simple sample that looks good
2) You have a logic problem in your loop function
a) Your message will be send out as fast as possible because after you leave the loop function you will enter the function again
b) You don't wait for incoming data
I hope the sample helps: I didn't try it because I directly used AT comands instead.

Arduino ethernet shield make a get request but no response

I would like my arduino to send a get request to a uri I have on my local network. I can open a browser and manually paste the request in and it works. I have also made the same request on a webpage using jQuery again no problem.
I have followed the tutorials for the Arduino and I can't see what I am doing wrong if some could help would be very grateful.
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,0,34);
// Set the static IP address
IPAddress ip(192,168,0,177);
// Initialize the Ethernet client library
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Ethernet.begin(mac, ip);
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop()
{
Serial.println("connecting...");
if (client.connect(server, 81)) {
Serial.println("connected");
client.print("GET /tenHsServer/tenHsServer.aspx?t=ab&f=ToggleDevice&d=");
client.print("E3");
client.println(" HTTP/1.1");
client.println("Host: 192.168.0.34");
client.println(""); //mandatory blank line
}
}
For this kind of issue you should learn about web debbugging tools. My favourite is fiddler2. However it is only available for windows. For other plattforms search the net for http debug proxy. Besides that there is always wireshark or ncat.
Once you have such a tool in place just have a look at the generated traffic and compare it with traffic that actually worked. This usually allows to pinpoint the issue rather quickly.

Arduino Wi-Fi shield - can't send a UDP packet

I'm trying to send information from the arduino board to my computer through the Wi-Fi network.
for my project's purposes it has to be a UDP connection
I use the "Send and Receive UDP String" example (http://arduino.cc/en/Tutorial/WiFiSendReceiveUDPString)
with a few changes:
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
int status = WL_IDLE_STATUS;
char ssid[] = "itay_net"; // your network SSID (name)
char pass[] = "0527414540"; // your network password (use for WPA, or use as key for WEP)
unsigned int localPort = 50505; // local port to listen on
IPAddress remote_ip(192, 168, 1, 100);
unsigned int remote_port = 50505;
char ReplyBuffer[] = "acknowledged"; // a string to send back
WiFiUDP Udp;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
delay(10000);
printWifiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
Udp.begin(localPort);
}
void loop() {
int bite_send;
Udp.beginPacket(remote_ip, remote_port);
bite_send = Udp.write("hello");
Udp.endPacket();
Serial.println("the packet was sent");
Serial.println(bite_send);
delay(1000);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
It compiles and connects to the network just fine.
the only problem is that I can't tell if the packet was sent because I see no trace of it on Wireshark.
I also wrote a socket on java that listens to the port (50505) and should display the message from the packet, but it didn't work either.
(I can copy the java code here but i can assure you that it is not the problem 'cause I tested it with a different java server and it worked, so the problem should be on the Arduino side)
a few details to narrow it down:
I believe the "remote ip" is correct but even if it isn't - I still should have seen it in the Wireshark, so it can't be the problem.
I should mention that the Wi-Fi shield works, I successfully sent pings and ran other examples (such as SimpleWebServerWifi).
I'm using an original Arduino Uno R3 board and an original Wi-Fi shield.
The arduino IDE is the newest version.
I updated the Wi-Fi shield with the newest update I found on GitHub.
I also ran the same "Send and Receive UDP String" code (with the necessary changes) on my Ethernet shield and it did work.
I don't know what else to try - please help.
any help will be appreciated.
Itay
I dont think you have a reply buffer packet. google arduino wifisendrecieve and you will see the example they have that has a reply packet labeled as 'acknowledged'. Hope this helps

My Arduino + Ethernet shield WebServer sketch sometimes fails to connect to the client. Causes?

My Arduino web server sketch sporadically fails on:
EthernetClient client = server.available();
if (client)
This morning, it connected just fine on the first run. Now, it can't connect to the client again. A couple of days ago, it worked several times, but failed several time as well. I have the shield connected via an Ethernet cable to my home router. I've verified the IP address assigned to the Arduino. I've tried ports 80 and 8080. What could be going wrong and what else can I try? Could my ISP be blocking something here? Please don't be afraid to suggest the obvious, since I know almost nothing about networks.
If relevant, here is a larger piece of the code, which loops on
Serial.println("Listening");
Code:
#include <SPI.h>
#include <Ethernet.h>n
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xF7, 0x99 };
IPAddress ip(192,168,2,5);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
String roundOpenTag = "";
String roundCloseTag = "";
void setup()
{
// Start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
int ledPin = 8;
// Initialize the digital pin as an output.
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("Setting up");
}
void loop()
{
// Listen for incoming clients
EthernetClient client = server.available();
Serial.println("Listening");
if (client)
{
Serial.println("Server available");
// An HTTP request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
Serial.println("Client connected");
if (client.available())
{
char c = client.read();
I don't see the purpose in including the rest of the sketch. I really appreciate your help.
You have a empty Seiral.begin() in your setup() function. Try removing it.
Edit:
When you call Serial.begin() you have to provide the baut rate(speed) at which you want to communicate. You can read more about the function at Arduino library page.
You had two problems in your code
You had a empty Serial.begin() function call, without any parameter
You had duplicate Serial.begin() function. You had already specified it in the beginning of the setup() function.

Resources