Unintentional strange characters added to packets during udp communication in Arduino - arduino

Stackoverflow!
Unusual strange characters are added to packets during udp communication in Arduino.
Hi. Stack overflow. I encountered problems that I could not solve while using Udp with Arduino. I have looked up a lot of information to solve this problem. I also tested various cases. But I can not solve it.
The problem occurs from the 24th byte of the packet. The characters exceeding 23 characters are converted into strange characters and the subsequent sentences are not output. Is the packet transmission of udp communication limited to 24 bytes? If so, I am glad, but I want to know how to solve it. Please help me.
The test environment is as follows. Two Arduino are connected via a LAN cable. In addition, the following source code has been uploaded to Arduino. The serial monitor is as follows.
TX serial monitor
.
.
17:46:31.521 -> Sending UDP message
17:46:32.516 -> Sending UDP message
17:46:33.514 -> Sending UDP message
17:46:34.519 -> Sending UDP message
17:46:35.515 -> Sending UDP message
17:46:36.510 -> Sending UDP message
RX serial monitor
17:38:51.664 -> 010010010101001010101001K;⸮ <----------problem
17:38:52.662 -> Received packet of size 31
17:38:52.662 -> From 192.168.1.251, port 5678
17:38:52.662 -> Contents:
17:38:52.662 -> 01001001010100101010100j⸮ <------- problem
17:38:53.663 -> Received packet of size 31
17:38:53.663 -> From 192.168.1.251, port 5678
17:38:53.663 -> Contents:
17:38:53.663 -> 010010010101001010101001;ے <------------problem
17:38:56.770 -> Received packet of size 31
---------Source Code-------------
///////////TX
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#define MSG_INTERVAL 1000
// network parameters
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0E, 0x05, 0x04}; // ethernet interface MAC address
IPAddress localIp(192, 168, 1, 251); // local ip address
IPAddress destIp(192, 168, 1, 15); // destination ip address
unsigned int port = 5678; // destination port
// EthernetUDP to send and receive messages.
EthernetUDP Udp;
// message string
char message[] = "0100100101010010101010010101010";
// timing
//unsigned long previousLedMillis;
//unsigned long ledInterval;
unsigned long previousSendMillis;
unsigned long sendInterval;
// setup the arduino and shields
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start ethernet and udp
Ethernet.begin(mac,localIp); // static ip version
// open UDP port
Udp.begin(port);
// show the local ip address (useful for dhcp)
Serial.print("Local IP: ");
Serial.println(Ethernet.localIP());
// initialize send interval
sendInterval = MSG_INTERVAL;
}
// do tasks
void loop() {
// check if udp string has to be sent
if(millis() - previousSendMillis >= sendInterval) {
sendMessage();
}
}
// send udp string
void sendMessage() {
Serial.println("Sending UDP message");
// store current millis
previousSendMillis = millis();
// send udp message
Udp.beginPacket(destIp, port);
Udp.write(message);
Udp.endPacket();
}
/////RX
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 15);
IPAddress remIp(92, 168, 1, 176);
unsigned int localPort = 5678; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
void setup()
{
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
pinMode(5, OUTPUT);
Serial.begin(115200);
Serial.print("Local IP: ");
Serial.println(Ethernet.localIP());
}
void loop()
{
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
//Udp.read(packetBuffer,1024);
Serial.println("Contents:");
Serial.println(packetBuffer);
//for (int i=0;i<packetSize;i++){
// Serial.print(packetBuffer[i]);
//}
//Serial.print('\n');
}
}

yes you have right, some problem with length 24!!
if you read inside the EthernetUDP.h file, you will see:
#define UDP_TX_PACKET_MAX_SIZE 24
so if you want use more characters, dont use this const, choose your personal size.
change
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
to
char packetBuffer[50];
or use another define:
#define UDP_MAX_BUFFER 50 //for example
char packetBuffer[UDP_MAX_BUFFER];

Related

Sending UDP from Arduino Uno to InfluxDB

So I’ve got an Arduino Uno and the Ethernet Shield V2 and connected these to a temperature sensor. Everything is working fine, the temperature is shown as desired, the problem is that I don’t seem to be able to save the results in my influxDB database.
Here my sketch:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
const int sensorPin = A0;
int sensorVal;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
byte host = {192, 168, 0, 153};
unsigned int port = 8089; // local port to listen on
EthernetUDP Udp;
void setup(){
//////////////////////
// PUT YOUR SETUP CODE HERE TO RUN ONCE
//////////////////////
Serial.begin(9600); // open serial port
Ethernet.begin(mac, host);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can’t run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println(“Ethernet cable is not connected.”);
}
// start UDP
Udp.begin(port);
}
float getTemperature() {
sensorVal = analogRead(sensorPin);
float voltage = (sensorVal/1024.0) * 5.0;
float temperatureC = (voltage - 0.5)*100;
return temperatureC;
}
void loop(){
//////////////////////
// PUT YOUR MAIN CODE HERE; TO RUN REPEATEDLY
//////////////////////
String line, temperature;
delay(1000);
temperature = String(getTemperature(), 2);
Serial.println(temperature);
line = String(“temperature value=” + temperature);
Serial.println(line);
Serial.println(“Sending UDP packet…”);
Udp.beginPacket(host, port);
Udp.print(“temperature value=”);
Udp.print(temperature);
Udp.endPacket();
}
These are the setting from the config file of the influxDB:
[[udp]]
enabled = true
bind-address = “:8089”
database = “arduino”
retention-policy = “”
InfluxDB precision for timestamps on received points ("" or “n”, “u”, “ms”, “s”, “m”, “h”)
precision = “s”
I would appreciate if somebody could give me some clues about what I’m doing wrong.
Cheers
According to the documentation, you want to pass a remote IP (not local IP) when you call Udp.beginPacket.
Does host represent a remote IP address? Looks like you are using host as a local IP to begin Ethernet as well. It is likely that you are not sending packets to a remote host. Make sure you pass a local IP to Ethernet.begin() and pass a remote IP to Udp.beginPacket().

Sending sensor data via UDP

I'm trying to send sensor data via UDP. At the moment I'm struggling with the "packing" of the UDP packets. It says "incomingData" is not declared when I try to send it.
I would appreciate any kind of advice.
Code below.
Thank you :)
//Version 1.012
//necessary libraries
#include <SPI.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h>
//Pin settings
#define CTD 19
//Network Settings
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0xEC, 0xAB }; //set MAC Address Ethernet Shield (Backside)
byte ip[] = { XXX, XXX, X, X }; //set IP-Address
byte gateway[] = { XXX, XXX, X, X }; //set Gateway
byte subnet[] = { 255, 255, 255, 1 }; //set Subnetmask
//local UDP port to listen on
unsigned int localPort = 4000;
//Recipient IP
IPAddress RecipientIP(127, 0, 0, 1);
//Recipient UDP port
unsigned int RecipientPort = 4444;
//Buffer for sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
//EthernetUDP instance
EthernetUDP Udp;
void setup()
{
//Start Ethernet
Ethernet.begin(mac, ip);
//Start UDP
Udp.begin(localPort);
//for debug only
Serial.begin(9600);
//Serial baud rate for CTD
Serial1.begin(1200);
//Version 1.012
Serial.print("Version 1.012");
//CTD
pinMode(CTD, INPUT);
}
void loop()
{
//If CTD is sending
if (Serial1.available())
{
//read incoming data
int incomingData = Serial1.read();
//for debug only
Serial.print("Data: ");
Serial.println(incomingData, BIN);
}
//Send UDP packets
int packetSize = Udp.parsePacket();
if (packetSize) {
// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
// send to the IP address and port
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(incomingData);
Udp.endPacket();
}
}
In your code you have declared incomingData as int inside void loop () function inside if (Serial1.available()) .
But if the above if loop fails the incomingData will not be declared and say packetsize is greater than zero ( packet available) then if (packetSize) segment will be executed.Thus incomingData is not declared but it is used .That's why you get the error you stated.

arduino Ethernet ENC28J60

Hi I am absolute newbee using ENC28J60, I want to upload some data to my seerver (in php) :
I take a php hosting from 0fees and now I can send the data to my server using : http://ashutest123.0fees.us/dataupload1.php?data=somedata and check the uploaded list of data in the table as http://ashutest123.0fees.us/showdata.php ——— u can take a view yourself.
I wrote an arduino code (using Aruino Uno and ENC28J60 module from ebay.in) using UIPEthernet lib
#include <UIPEthernet.h> // Used for Ethernet
// **** ETHERNET SETTING ****
// Arduino Uno pins: 10 = CS, 11 = MOSI, 12 = MISO, 13 = SCK
// Ethernet MAC address - must be unique on your network - MAC Reads T4A001 in hex (unique in your network)
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };
// For the rest we use DHCP (IP address and such)
EthernetClient client;
char server[] = "ashutest123.0fees.us"; // IP Adres (or name) of server to dump data to
int interval = 5000; // Wait between dumps
void setup() {
Serial.begin(9600);
Ethernet.begin(mac);
Serial.println("Tweaking4All.com - Temperature Drone - v2.0");
Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
Serial.print("IP Address : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server IP : ");
Serial.println(Ethernet.dnsServerIP());
}
void loop() {
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("-> Connected");
// Make a HTTP request:
client.print( "GET /dataupload1.php?");
client.print("data=");
client.print( "somedata" );
client.println( " HTTP/1.1");
client.print( "Host: " );
client.println(server)
client.println( "Connection: close" );
client.println();
client.println();
client.stop();
}
else {
// you didn't get a connection to the server:
Serial.println("--> connection failed/n");
}
delay(interval);
}
when I run it in serial monitor it shows all ip, dns, gateway etc addresses as 0.0.0.0 ---- seems no dhcp abtained
then shows "connected"
no data goes to my server
Please help me I am in a need of it
Thanks in advance
You might want to try this example code, it might help you alot to understand your problem and where it come from.
#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[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};
// 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);
// this check is only needed on the Leonardo:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port 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 (;;)
;
}
// print your local IP address:
printIPAddress();
}
void loop() {
switch (Ethernet.maintain())
{
case 1:
//renewed fail
Serial.println("Error: renewed fail");
break;
case 2:
//renewed success
Serial.println("Renewed success");
//print your local IP address:
printIPAddress();
break;
case 3:
//rebind fail
Serial.println("Error: rebind fail");
break;
case 4:
//rebind success
Serial.println("Rebind success");
//print your local IP address:
printIPAddress();
break;
default:
//nothing happened
break;
}
}
void printIPAddress()
{
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
Then I let you edit your code with your custom functions to get all informations you might need.
Note that if you set an hard-coded IP address even in a DHCP range it will be working.
V.

DhcpAddressPrinter in the Arduino example can not work

I use the combination of Arduino mega r3 and ethernet shield. When using the example of DhcpAddressPrinter, I can not get any result. I did not change any code.
#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[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// 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);
// this check is only needed on the Leonardo:
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(;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
void loop() {
}
Afterwards, I added some "println" in the code as follows:
#include
#include
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// 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);
// this check is only needed on the Leonardo:
Serial.println("1");
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("2");
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("3");
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
void loop() {
}
I can get the result 1 and 2 from Serial Monitor, but cannot receive 3;
So I doubt that the function Ethernet.begin(mac) is keeping running all the time and don't know why.
I have change the mac address to others, but get the same result.
Are you using a microSD in the same time ? Sometimes it can create problems with DHCP on the Ethernet Shield

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