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

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

Related

Broadcasting messages via UDP over esp8266

I am connecting 4 nodemcu (esp8266). 1 esp8266 is used as access point and others are connected to it.
When I send UDP packets as a broadcast message to all the esp8266 via with esp8266 as access point it is not recieved by the others however when I use a home router or even by mobile hotspot as access point, the broadcast messages are received by the other esp8266.
Also, I have posted here a part of my code that is used for UDP so there may be some variables that you will see as undeclared but they are originally declared and the code is working when I connect it with Access Point that is not esp8266
Code For Access Point:
#include
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.print("Setting soft-AP ... ");
boolean result = WiFi.softAP("ssid", "password123456");
if(result == true)
{
Serial.println("Ready");
}
else
{
Serial.println("Failed!");
}
}
void loop()
{
Serial.printf("Stations connected = %d\n",
WiFi.softAPgetStationNum());
delay(3000);
}
Code for sending UPD packet as broadcast:
unsigned int localPort = 2000;
IPAddress SendIP(192,168,43,255);
setup()
{
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(udp.localPort());
}
loop()
{
udp.beginPacket(SendIP, 2000);
udp.write("p");
udp.endPacket();
}
Code for Recieving UDP packets:
void loop()
{
int packetSize = udp.parsePacket();
if(packetSize)
{
udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
p = packetBuffer[0];
Serial.println(p);
function();
}
Please tell me whats the problem with using esp8266 as access point to send UDP packets.
And if esp8266 can not be used please tell me any other chip that can do the work, I want to make a portable system so I cannot use the router.
The default IP address of ESP8266 router in AP mode is 192.168.244.1.
Try to change sending address to 192.168.244.255.

Wemos D1 ESP8266, ioBroker (Raspberry Pi 3 Model B) MQTT connection failed

I have a Raspberry Pi 3 Model B with ioBroker (Raspbian light Stretch) as an MQTT broker and Wemos D1 ESP 8266 with a test script. Both devices are connected to the network via Wi-Fi.
Good news:
1) I can send the MQTT signal from the phone (myMQTT apps) and it will be displayed in the ioBroker logs (the phone is connected to Wi-Fi, MicroTik).
I can send an MQTT signal from my laptop (connected to Wi-Fi or ethernet).
I can send a successful MQTT signal from a Debian virtual server (on vmware).
2) Wemos D1 successfully connects to the test server test.mosquitto.org.
The bad news:
Wemos D1 does not want to connect to ioBroker on the local network and reports the error "Attempting MQTT connection ... failed, rc = -2 try again in 5 seconds".
Why all devices except Wemos D1 ESP 8266 successfully connect to mqtt broker?
Could it be the case in the firewall?
Tell me, please, what should I do to solve this problem.
/*
Basic ESP8266 MQTT example
This sketch demonstrates the capabilities of the pubsub library in combination
with the ESP8266 board/library.
It connects to an MQTT server then:
- publishes "hello world" to the topic "outTopic" every two seconds
- subscribes to the topic "inTopic", printing out any messages
it receives. NB - it assumes the received payloads are strings not binary
- If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
else switch it off
It will reconnect to the server if the connection is lost using a blocking
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
achieve the same result without blocking the main loop.
To install the ESP8266 board, (using Arduino 1.6.4+):
- Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
- Select your ESP8266 in "Tools -> Board"
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "XXX";
const char* password = "XXX";
const char* mqtt_server = "test.mosquitto.org";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(5000);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode (WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(1000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 50, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
}
Screenshots:
1) netstat -tulpn | grep LISTEN
2) ioBrocker log
3) Arduino IDE
UPDATE:
I did not find where you can change or disable listen port 1883 tcp6.
But I managed to establish a connection between the devices by replacing the Mikrotik router with Keenetic.
Now we need to figure out what's wrong with the settings of the router.
Your screenshot:
shows that ioBroker is listening on the IPv4 loopback interface (127.0.0.1) for ports 9000 and 9001, and on a tcp6 (IPv6) interface for ports 8081, 8082 and 1883.
That means it's only reachable via IPv4 from programs running on the same server as it, or from programs running on computers capable of speaking IPv6.
The ESP8266 is not capable of speaking IPv6.
You need to reconfigure ioBroker to listen on 0.0.0.0:mqtt so that IPv4 software can reach it.

Arduino UNO wifi changing my DNS using Wifi Config

I am trying to set the DNS address of my Arduino UNO wifi, but compiler returned 'IPAddress' does not name a type is there any other way to change my device DNS address?, my code is below.
#include <SPI.h>
#include <WiFi.h>
// the IP address for the shield:
IPAddress dns(8, 8, 8, 8); //Google dns
char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;
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");
while(true); // don't continue
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// print your WiFi shield's IP address:
WiFi.setDNS(dns);
Serial.print("Dns configured.");
}
void loop () {
}

Arduino Ethernet Shield does not accept connections

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.

Arduino Ethernet Shield Client.connect returns error code -5

I have a brand new Ethernet shield on Arduino Uno and have worked through many (non-Ethernet) examples without any issues, until I tried to use the Ethernet shield.
Using the provided EthernetClient example, I get a connection failed. The return code is -5 (and I could only find answers for -4 through 1).
/*
Web client
This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
Created 18 Dec 2009
Modified 9 Apr 2012
by David A. Mellis
*/
#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[] = {0x90, 0xA2, 0xDA, 0x0D, 0x4E, 0x71 };;
char server[] = "google.com"; // Google
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// Start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// Give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
Serial.println("Obtaining local IP address");
IPAddress myIPAddress = Ethernet.localIP();
Serial.println(myIPAddress);
// if you get a connection, report back via serial:
int ret = client.connect(server, 80);
if (ret == 1) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("Connection failed");
Serial.println(ret);
Serial.println(client.status());
}
}
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);
}
// If the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("Disconnecting.");
client.stop();
// Do nothing forevermore:
for(;;)
;
}
}
The results are always:
Connecting...
Obtaining local IP address
192.168.0.7
Connection failed
-5
0
disconnecting.
Not sure why this helped, but adding a delay after the Serial is intialized, before beginning Ethernet, and also increasing the delay before using Ethernet seemed to work.
/*
Web client
This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
modified 9 Apr 2012
by David A. Mellis
*/
#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[] = {0x90, 0xA2, 0xDA, 0x0D, 0x4E, 0x71 };;
char server[] = "google.com"; // Google
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
delay(5000);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// give the Ethernet shield a second to initialize:
delay(5000);
Serial.println("connecting...");
Serial.println("Obtaining local IP");
IPAddress myIPAddress = Ethernet.localIP();
Serial.println(myIPAddress);
// if you get a connection, report back via serial:
int ret = client.connect(server, 80);
if (ret == 1) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
Serial.println(ret);
Serial.println(client.status());
}
}
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);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
for(;;)
;
}
}
That Google IP address (173.194.33.104) is not valid now. Try to use 74.125.226.242 instead:
IPAddress server(74,125,226,242); // Google
And before your try it on Arduino, ensure your can open this IP address in your browser:
http://74.125.226.242
Try defining the server IP address as it is shown on the Arduino Reference Page:
byte server[] = { 64, 233, 187, 99 }; // Google
Try a couple different example programs. There have been some revisions with the move to IDE 1.0 that could affect compatibility.

Resources