Arduino sending inconsistent UDP messages - arduino

I'm trying to communicate using UDP an Arduino UNO + Ethernet Board with a PC, but can't do it properly.
Facts:
Arduino code has 2 blocks: first block send a constant message each 5 seconds to the PC. Second block has been implemented as a ECHO (returns what receives)
UDP is correctly initializated
At begining, Arduino starts sending the message "Hello PC!" each 5 seconds, but the UDP.beginPacket doesn't return 1 (as it is supposed to do when it works properly).
in the PC side I have wireshark and PacketSender to check the tx/rx UDP communication.
Wireshart doesn't detect any incoming packages from Arduino when it has been resetted.
A UDP package "Hello arduino!" is sent from the PC to Arduino, throught PacketSender. This packed is received correctly in the Arduino and sent back to PC. Wireshark identify tx UDP packet from PC and the same rx UDP packet from Arduino. This works properly.
Buuuuuuuuuttttttt...... then, after sending a UDP message from the PC and receive the ECHO, the messages from Arduino each 5 seconds start reaching the PC.
First message received on PC after the ECHO, has 21 bytes (although the message sended is supposed to have 11 bytes). The message received is "C!hello hellhello PC!", so there is some kind of buffer trash with "hello PC!" at the end.
The weird thing is that after this, each 5 seconds the PC receives an UDP packet, each time 9 bytes longer (hello PC! is 9 bytes message), and as in the first message received from Arduino, at the end of the message you can find "hello PC!". Here is an example of the 9th message received "PC!hello PC!!lhello PC!45hello!llo!helhello PC!hellhello!ohello PC!hello Phello PC!" (95 bytes length).
I use the standard Arduino Ethernet libraries, so I don't know what is happening and why it doesn't send anything until first message arrives to Arduino (and no idea why it is increasing each time it is sent).
I tested Arduino with Raspberry with Raspbian to ensure the problem wasn't on the PC side, and the Raspberry received the same erratic UDP behaviour.
Arduino Code:
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern#cs.stanford.edu 12/30/2008
byte mac\[\] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x3D};
IPAddress ip(192, 168, 1, 6); //IP of the arduino ethernet board
char link_IP\[\] = {192, 168, 1, 3}; //IP of the PC
unsigned int localPort = 51115; // local port to listen on
char message\[\] = "hello PC!"; //message intended to sended from the arduino
char packetBuffer\[UDP_TX_PACKET_MAX_SIZE\]; // UDP_TX_PACKET_MAX_SIZE = 24
unsigned long millis_before = millis(); //variable for delaying 5 seconds the message
EthernetUDP Udp;
void setup()
{
Serial.begin(9600); //serial com for debugging
Ethernet.begin(mac, ip); //starting Ethernet
if (Udp.begin(localPort)==1) Serial.println("port UDP is open"); //starting UDP on 51115
else while(1); //if UDP not open, infinte bucle
}
void loop()
{
if (millis_before + 5000 < millis()) //loop for waiting 5 seconds between messages
{
if (Udp.beginPacket(link_IP, 51115)==1) Serial.print("TX ready - "); // UDP packet to PC port 51115, if beginPacket returns 1 is OK
Serial.print("Lenght:");
Serial.print(Udp.write(message, 9)); //returns the length of the message
if (Udp.endPacket()==1) Serial.println(" - Sent!"); //if endPacket returns 1, is OK
millis_before = millis(); //reset timer
}
delay(10);
int packetSize = Udp.parsePacket(); //check size of UDP packet
if (packetSize)
{
IPAddress remote = Udp.remoteIP(); //save remote IP
for (int i = 0; i < 4; i++) //bucle for printing the remote IP
{
Serial.print(remote\[i\], DEC);
if (i < 3) Serial.print(".");
}
Serial.print(", port ");
Serial.println(Udp.remotePort()); //printing the remote port
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); // read the packet into packetBufffer
// send a echo to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(packetBuffer, packetSize);
Udp.endPacket();
}
}
Here you can find the PacketSender and Wireshark captures after sending the first message to Arduino and receive the ECHO:
wireshark capture
packet sender capture
I apreciate any help. Thanks in advance

Solved. The problem was in the char array format of the IP to deliver the packet. I defined it as a 4 char array, but it should be defined as a text char array:
char link_IP[] = "192.168.1.3"; //correct !!
char link_IP[] = {192, 168, 1, 3}; // Wrong

Related

Arduino Micro with HC-05: receiving data in weird encoding

I'm just starting using HC-05 Bluetooth module and having some troubles.
Firstly, I connected HC-05 bluetooth module to Arduino Micro board in programming mode (with Key pulled up) and was able to communicate with it sending AT commands (changed name, password, etc).
After that I was pulled down Key pin to let HC-05 operate normally. I was able to find and connect to it (with new name and password, as expected), and even transmit data in both directions. However what I'm receiving is not what I'm expecting. I'm sending some character and receiving sequence of bytes:
When I send a char I receive sequence of bytes 120 248.
b -> 128 0 248.
c -> 248 0 248.
d -> 0 128 248.
Actually, I've tried few mobile apps (Bluetooth Terminal, Bluetooth Terminal HC-05, Serial Bluetooth Terminal) which provides Bluetooth terminal functionality, and they all are producing different bytes sequences. Ones provided above is for Bluetooth Terminal app. I'm obviously missing something..
Could someone give me direction?
P.S. My latest version of sketch is following:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 8);
void setup() {
pinMode(9,INPUT);
pinMode(8,OUTPUT);
Serial.begin(9600);
while (!Serial);
Serial.println("welcome");
mySerial.begin(38400);
}
void loop() {
if (mySerial.available()) {
while (mySerial.available())
{
char c = mySerial.read();
Serial.print((int) c);
Serial.print(" ");
delay(5);
}
Serial.println("");
}
}

Can an I2C Eeprom have two addresses?

I own an EEPROM module with the P24C256 chip. (not AT24C256).
After scanning for available I2C devices, i got response from two devices.
Scanning...
I2C device found at address 0x50 !
I2C device found at address 0x58 !
done
I looked up the datasheet, but cannot find any additional information.
I thought that the device, has two banks for storage, but that does not seem to be the case.
How is this possible? After disconnecting the module, no I2C devices are reported.
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
Yes they can.
Depending on the size of the i2C EEPROM and how the memory blocks are organized you can have 2 or more device address. For example the ST24/25x04 EEPROM are 4 Kbit electrically erasable programmable memories, organized as 2 blocks of 256 x8 bits. That means that I2C gives you two device addresses (0x50 and 0x51 in the memory above), that will access each block with 1 byte address.
It might be the problem with the EEPROM address bits, in case they are in floating state and not connected properly, Could you please check the connections on those address bits? Try to connect them to the same ground as your micro-controller, but yes, as advised above, schematics is highly needed.
That behavior is not implied to by. The datasheet says the address should be only one (but its three lower bits can be chosen).
The schematics for the boards shows only two bits (A0, A1) are selected by the dip-switch, and A2 is tied to the ground.
I see no initialization in the code. What frequency the TWI bus is working at? Also, in the pictures of the module it has jumpers to pull-up resistors unsoldered, and it has 10kOhm pull-up resistors even if jumpers are soldered - that may be too high.
Probably you have issue just because the wire is not returned to the high level fast enough.
Check pull-up resistors are engaged (jumpers are soldered on the board)
Try to add additional pull-ups (about 4.7k) on SCL, SDA
Try to set lower frequency, e.g. Wire.setClock(50000);

i want to transmit data from avr to pc.whats wrong in below connection or code

connection:
USB to TTL interface modules four pins:
1)0v-volt of avr
2)vcc-5 volt avr
3)TXD-TX of avr
4)RXD-RX of avr
and the USB to TTL serial interface module connected to PC using USB to RS-232 convertor DB9 cable.
below is the code:
#include<avr/io.h>
void UART_transmit(unsigned char data);
int main(void)
{
unsigned char i,message[]="i love india\r\n";
DDRD=0x00;
PORTD=0xFF;
UCSRA=0;
UCSRB=1<<TXEN; // transmitter enable
UCSRC=1<<URSEL | 1<<UCSZ1 | 1<<UCSZ0; // 8 data bit, a stop, none parity
UBRRH=0;
UBRRL=5; // for 9600 baud at 1MHz
while(1)
{
for(i=0;message[i];i++)
{
UART_transmit(message[i]);
}
} // while(1) end
} // main() end
void UART_transmit(unsigned char data)
{
while(!(UCSRA & (1<<UDRE)));
UDR=data;
}
RX of each device must be connected to TX of the other, and vice versa, unless you are using a null modem cable, in which case your connections are correct. You might try using a multimeter and google to determine if your connecting cable is straight or null-modem.
I also recommend that you revise your code as outlined in the manufacturer's application note AVR306: Using the AVR® UART in C. This is the authoritative reference on setting up and using the UART on AVR microcontrollers.

XBee S2C and XBee S1 Pro communication

I have an XBee S1 Pro, which is configured as a coordinator. and an XBee S2C, which is configured as an end-node. Both are loaded with the 802.15.4 firmware. By using XCTU, I sent and received the data in Transparent mode.
I have now configured the end-node to API-1 mode and connected it to an Arduino Nano. I want to read the payload. Here is my Arduino code:
void setup() {
Serial.begin(9600);
Serial.println("Setup done...");
while (!Serial) {;}
}
void loop() {
if (Serial.available()) {
for (int i = 0; i < 8;i++) {
byte discard = Serial.read();
}
Serial.write(Serial.read());
Serial.print(",");
}
When I send 'hello' from the coordinator I get:
Setup done...
FFFFFFFF
,FFFFFFFF
,FFFFFFFF
,FFFFFFFF
,FFFFFFFF
,FFFFFFFF
,FFFFFFFF
,FFFFFFFF
,FFFFFFFF
,
Is there an easier way to do this like using the Arduino XBee library?
Could someone kindly help me?
Have you confirmed that the two XBee modules are joined to the same network? My understanding was that S1 was just 802.15.4, and S2 was ZigBee. You should probably start your project with identical networking hardware to eliminate that as a source of problems.
I'd recommend following a tutorial and using that code to establish a working starting point for any code you want to write.
Edit: adding some code
First off, you need to have two serial ports. One for your console where you can see the output of Serial.print() and the other for communicating with the XBee module.
Second, start with this loop:
void loop() {
// echo bytes received on XBee module to serial console
if (XBee.available()) {
Serial.write(XBee.read());
Serial.print(",");
}
// echo bytes received on serial console to XBee module
if (Serial.available()) {
XBee.write(Serial.read());
}
}
This way you're dumping every byte you receive, and only calling XBee.read() when there's data available to read.
You might want to keep your XBee module at 9600 baud, but increase your stdio interface (Serial) to 115200 since you're printing more than one character per byte received from the XBee module.

Booting programs from an Intel Edison board

I just bought an Intel Edison Breakout Board Kit with the Grove Starter Kit Plus I want to write a program which gives the board an IP address, checks for a temperature in a room and if that temperature is exceeded it turns a LED on. I can check what's going on with a RGB display. When I connect the USB OTG port to the my laptop the wifi connection is perfectly detected, the IP address is perfectly received and the temperature control works fine. The program works as I ask for. Now the questions. I would like to execute the same program unplugging the USB connection, so I would like to make this board independent from PC, like if it is a standing alone device
1) if I upload this working program in the board, unplugging the USB and of course giving just the power supply through the jack on the breakout board it does not work anymore and stops on the message "preparing network connection...". But Intel Edison has an integrated wifi module and I really can't understand why it is not able to establish an internet connection anymore without the help of the USB connection with my laptop. What's wrong with it? how to boot and execute this program just with the power supply? This is the setup function() of my program, we don't reach the loop() one so I will not post it
void setup()
{
// ------ LCD IN ACTION ------ //
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// setting the color for the connection
lcd.setRGB(colorR, colorG, colorB);
lcd.print("preparing network connection...");
// scroll 40 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 40; positionCounter++)
{
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(350);
}
// ------ SERIAL CONNECTION ------ //
// opening the serial connection
// Serial.begin(9600); // initialize serial communication
// ------ SETTING I/O PINS ------ //
pinMode(potentiometer, INPUT); // angle sensor's pin for input.
pinMode(12, OUTPUT); // set the blue LED pin mode
pinMode(13, OUTPUT); // set the red LED pin mode
pinMode(pinLed, OUTPUT); // set the green LED pin mode
// ------ WIFI CONNECTION CONTROLS ------ //
// check for the presence of the wifi shield:
if (WiFi.status() == WL_NO_SHIELD)
{
// Serial.println("WiFi shield not present");
while(true); // don't continue
}
// check firmware version
String fv = WiFi.firmwareVersion();
// if( fv != "1.1.0" )
// Serial.println("Please upgrade the firmware");
// ------ WIFI CONNECTION ------ //
// attempt to connect to Wifi network:
while (status != WL_CONNECTED)
{
// Serial.print("Attempting to connect to Network named: ");
// Serial.println(ssid); // print the network name (SSID);
// connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 3 seconds for connection:
delay(3000);
}
server.begin(); // start the web server on port 70
printWifiStatus();
where
void printWifiStatus() {
// print the SSID of the network you're attached to:
// Serial.print("SSID: ");
// Serial.println(WiFi.SSID());
colorR = 255;
colorG = 180;
colorB = 0;
lcd.clear();
lcd.setRGB(colorR, colorG, colorB);
lcd.print(WiFi.SSID());
delay(3000);
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
// Serial.print("IP Address: ");
// Serial.println(ip);
colorR = 0;
colorG = 255;
colorB = 0;
lcd.setCursor(0, 1);
lcd.setRGB(colorR, colorG, colorB);
lcd.print(ip);
delay(3000);
/*
delay(5000);
colorR = 180;
colorG = 255;
colorB = 255;
lcd.setRGB(colorR, colorG, colorB);
lcd.noDisplay();
*/
// print the received signal strength:
long rssi = WiFi.RSSI();
// Serial.print("signal strength (RSSI):");
// Serial.print(rssi);
// Serial.println(" dBm");
// print where to go in a browser:
// Serial.print("To see this page in action, open a browser to http://");
// Serial.println(ip);
}
2) always about the execution of pre-loaded programs: if I load the default blink program of ArduinoIDE, unplug the USB and just give the power supply it works perfectly [this one is correctly self-booted, yes] but if I upload the same program with a Serial.begin() and a Serial.print("Arduino blinking") it doesn't work anymore without the USB connection to my laptop [this is why I commented the Serial stuff before]. Is it because the board is so smart to detect that in the first case the serial connection is not really happening?
3) last question about program storage: when I compile a program with the arduino IDE it gives me the percentage of memory used related to the available one for programs
Sketch is using 103.266 byte (1%) of program memory. The limit is 10.000.000 byte
but Intel Edison has a 4GB eMMC so why there are only 10MB available to programs? in which memory are the programs uploaded?
Thanks in advance to those who will try to help
Do you know how can I do to check where is it stored?
You can check your files on serial communication terminal. If you have possible directories that your file could be in, you can use linux commands like:
cd directory and ls
or even you can 'search' the file if you know the name. You may want to check this site.
or you can use SCP, FTP client programs like WinSCP to see the files on your Edison.
if I upload this working program in the board
Are you sure that your program is flashed into persistent memory ? I do not know this board. In most cases this kind of problems comes when your program is only transferred to RAM memory, but not stored in Flash.
Sketch is using 103.266 byte (1%) of program memory. The limit is
10.000.000 byte
10MB RAM, or Flash memory ?
As far as I understand you use Arduino + Edison ?
Which board 10MB limit corresponds to ? Sketch is using 103.266 byte: is it RAM or Flash ?
Try typing these commands in through putty
ifconfig usb0 down
ifconfig wlan0 down
ifconfig usb0 up
ifconfig wlan0 up
that might do it

Resources