Duplex communication between two arduinos using NRF24L01 (WiFi module) - c++

I am trying a two way chat between two Arduinos using serial monitor. I tried all the examples online but didn't work

Are you using wired or wireless communication interface?
Make a circuit according to the given diagram. Connect the RX and TX pins of Arduino vice versa (the first TX pin of Arduino to the RX pin of another and the first RX pin of Arduino to the TX pin of another). Also, common the ground pin of both Arduino.
Image: connection diagram
This is one way communication and for two way communication use simple data handling operation to achieve your task and i will let it to you. Just try it more and if you don't find the final solution then let me know once :)
At the sender side use this code:
char str[5] = "Arduino"; //String data
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.write(str,5); //Write the serial data
delay(1000);
}
At the receiver side use this code
char str[10]; //Initialized variable to store recieved data
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}
void loop() {
Serial.readBytes(str,5); //Read the serial data and store in var
Serial.println(str); //Print data on Serial Monitor
delay(1000);
}

Related

How to block until WiringPi finishes to sent serial data (UART)?

Device description (for context, skip it if you don't feel comfortable with electronic):
For a simple device, the communication is done in half-duplex UART (TX and RX are on the same wire), in the following way:
One pin (write-mode) indicate if the UART is sending or receiving (1: TX, 0:RX)
One pin write to the wire (TX).
One pin read from the wire (RX).
When the write-mode is in TX (writing), the RX pin is in high-impedance and TX in transmitting. While the write-mode is in RX (reading), the TXpin is in high-impedance and RXreceiving.
This is just for context, I do not expect electronic question/answers here.
WiringPI sample:
For this to happens, I have the following sample:
#include <wiringPi.h>
#include <wiringSerial.h>
int main()
{
wiringPiSetup ();
auto fd = serialOpen ("/dev/ttyAMA0", 115200);
pinMode(0, OUTPUT);
for(size_t i=0; i<10; ++i)
{
digitalWrite(0, HIGH);
serialPutchar(fd, '\x55');
digitalWrite(0, LOW);
delay(1000);
}
serialClose(fd);
}
Using an oscilloscope, I can clearly see that the write-mode pin is reset before the UART end to send the data.
Obviously, I tried to add some "delay" or empty-loop to adjust, but this is not reliable for μs times (due to usual precision in timers on OS).
The question:
How to synchronize, so the write-mode pin is reset just after the UART byte is sent? (No more than approximately 150μs later).
I see 2 ways to implement this:
1. I can't test this right now, but it seems you could use the
void serialFlush (int fd) ;
According to the docs "This discards all data received, or waiting to be send down the given device" see at http://wiringpi.com/reference/serial-library/
(Edit: after re-reading that sentence, it's clear that it would indeed flush also the data to be written, so this option is out...)
Using tcdrain() (https://linux.die.net/man/3/tcdrain), where you pass the fd given back by serialOpen()
Set blocking on the file descriptor.
How to restore file descriptor operation to blocking mode can be found in many places. You may use code from this answer and do:
set_blocking_mode(fd, 1);
serialPutchar(fd, '\x55');
After that write() inside serialPutchar will be blocking. Note that fd is opened with O_NONBLOCK in serialOpen().

arduino mega + ENC28J60 ethernet module direct connection to PC to receive/send UDP

I want to use my Arduino Mega (with a sensor shield) and an ENC28J60 Ethernet module (directly connected to my PC) to send and receive UDP from a flight simulator (X-Plane 11, which is capable of sending UDP via the network).
The network module: http://www.ebay.de/itm/281353516180?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT
I can't manage to write the received UDP data into the serial monitor however.
I am not even sure I really do receive UDP.
The connection between Ethernet module and PC seems to be fine, as the green LED of the Ethernet module's I/O is permanently on, and the yellow one is blinking as soon as I start my flight sim and send UDP from there.
I have tried both, standard Ethernet cable and crossover.
I have have tried 2 ways of connecting the ENC28J60 Ethernet module to the Arduino Mega sensor shield, according to 2 different guides.
Standard wiring for the Arduino Uno
Enc28j60 SO to Arduino pin 12
Enc28j60 SI to Arduino pin 11
Enc28j60 SCK to Arduino pin 13
Enc28j60 CS to Arduino pin 10
Enc28j60 VCC to Arduino 3V3 pin
Enc28j60 GND to Arduino Gnd pin
Recommended wiring for the Arduino Mega
https://en.code-bude.net/2013/06/22/how-to-use-enc28j60-ethernet-shield-with-arduino-mega-2560/
GND to GND
3.3 to 3.3V
SO to Pin50
SI to Pin51
SCK to Pin52
CS to Pin53
I also tried several libs:
EtherCard: It is recommended to set the cs pin to 53 in the library files, which I did. Plus one should use this line of code in the sketch (which did not compile. Error is with the use of sizeof in combination with Ethernet::buffer)
ether.begin(sizeof Ethernet::buffer, mac, 53)
UIPEthernet (I assume I could use standard wiring here, as it is said that this lib uses standard Ethernet shield settings?)
None of the combinations made it possible to have any output in the serial monitor.
One of the sketches I have tried is the following:
#include <Dhcp.h>
#include <Dns.h>
#include <ethernet_comp.h>
#include <UIPClient.h>
#include <UIPEthernet.h>
#include <UIPServer.h>
#include <UIPUdp.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 6); // local IP - address of my Arduino
unsigned int localPort = 49001; // local port to listen - default X-Plane port
byte buf = 00; // buffer for UDP packet (BYTE, not char)
EthernetUDP Udp; // An EthernetUDP instance to send and receive packets over UDP
//-------------------------------------------------------------------------------
void setup()
{
Ethernet.begin(sizeof Ethernet::buffer, mac, 53)
Ethernet.begin(mac,ip); // start the Ethernet
Udp.begin(localPort); //..and UDP:
Serial.begin(9600); // init serial port
}
void loop() {
int packetSize = Udp.parsePacket(); // Checks for the presence of a UDP packet, and returns its size
if(packetSize) // UDP packet was received and its size defined
{
Serial.println();
Serial.print("Packet size: ");
Serial.println(packetSize); // Packet Size in bytes
// When Udp.read used without parameters, it returns next char (byte in this case) :
Serial.println("Xplane Data:");
for (int i =0; i/<packetSize; i++)
{
buf = Udp.read();
Serial.print(buf);
Serial.print("-");
}
}
delay(10);
}
So my questions are:
What is the easiest way to test both connections:
PC -> Ethernet module and
Ethernet module -> Arduino?
Do I need to set the used Arduino pins in my sketch or does the lib do that?
Is the sketch supposed to work properly?
https://github.com/jcw/ethercard download this. and compile and run udplistener example.
https://sourceforge.net/projects/sockettest/ download this program(SocketTest). SocketTest is very simple program.
And in arduino code,(ether.udpServerListenOnPort(&udpSerialPrint, 1337);)
you change port number that you want. "udpSerialPrint" is a callback function."1337" is a port number. If exist udp data , the program call this function.

xbee arduino communication programming

I tried testing the system and I’m not sure if the problem is with the xbee’s, the transmitting code, or the recieveing code. Before I post my code I will explain what we are doing with the signals. We have three analog signals that will be sent serially through one xbee using an arduino and xbee shield. We want to send these signals to the receiving xbee where the arduino will output these signals to be connected to a third arduino through wires to be used in a Simulink program. We are using an arduino mega for the transmitting side and an arduino uno for the receiving side. I was told I need to do serial streaming but I’m not sure how that’s done. I understand the xbee and arduinos both digitize signals but we are hoping to get a signal very similar to the analog signals we are transmitting. Any amount of help is greatly appreciated!!
This is how I have my xbees configured (series 1) both in AT mode:
Transmitting Xbee:
Channel:10
Pan id: 1234
MY: 10
DL: 11
Receiving Xbee:
Channel:10
Pan ID: 1234
MY: 11
DL: 10
transmitting Arduino code:
void setup() {
Serial.begin(9600);
}
void loop() {
// read the input on analog pins
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
int sensorValue3 = analogRead(A2);
// print out the value you read:
Serial.println(sensorValue1);
Serial.println(sensorValue2);
Serial.println(sensorValue3);
delay(1);
}
Receiving Arduino code:
int received1=8;
int received2=9;
int received3=10;
void setup(){
pinMode(received1, OUTPUT);
pinMode(received2, OUTPUT);
pinMode(received3, OUTPUT);
Serial.begin(9600);
}
void loop(){
if(Serial.available() )
{
byte output1 = Serial.read();
byte output2 = Serial.read();
byte output3 = Serial.read();
digitalWrite(received1, HIGH);
digitalWrite(received2, HIGH);
digitalWrite(received3, HIGH);
}
}
It sounds like you're using the XBee modules in "AT mode" or "transparent serial" mode where anything received on the serial port of module A is sent out of the serial port of module B, and vice versa.
If that's the case, it may help to do your initial development with the serial ports of your two devices connected directly to each other. Work out your serial protocol there, and then try to run it with the XBee modules in place as a serial cable replacement.
Consider the format of the data that you're sending, and how you will process it on the other end. How will you separate the readings and identify which analog input they belong to? With your current code, you print the readings on separate lines, but it won't be clear which is A0. Maybe you want to send them on a single line with a comma in between each reading?
On the receiving end, you need to convert the text back to an integer using a C function like atoi() or strtoul().
If you're trying to create an analog output on the Arduino, it might be possible with a digital output that's using PWM (pulse width modulation). This Instructable does a decent job of describing that concept.

Is there a clean way of disabiling RX control from USBCore in leonardo?

At the moment I'm trying to use the sparkfun promicro to control the RX pin at will using this sketch https://www.sparkfun.com/tutorials/338 .
However I have run into a problem where despite having control over the RX and TX led, it is being interfered by the USBCore.cpp in arduino. I am wondering if there is a clean way to disable control by USBCore over RX and TX pins, while still leaving the USB serial alone, so that I can control these pins directly, even while receiving and sending serial data.
/* Pro Micro Test Code
by: Nathan Seidle
modified by: Jim Lindblom
SparkFun Electronics
date: January 20, 2012
license: Public Domain - please use this code however you'd like.
It's provided as a learning tool.
This code is provided to show how to control the SparkFun
ProMicro's TX and RX LEDs within a sketch. It also serves
to explain the difference between Serial.print() and
Serial1.print().
*/
int RXLED = 17; // The RX LED has a defined Arduino pin
// The TX LED was not so lucky, we'll need to use pre-defined
// macros (TXLED1, TXLED0) to control that.
void setup()
{
pinMode(RXLED, OUTPUT); // Set RX LED as an output
// TX LED is set as an output behind the scenes
Serial.begin(9600); //This pipes to the serial monitor
Serial1.begin(9600); //This is the UART, pipes to sensors attached to board
}
void loop()
{
Serial.println("Hello world"); // Print "Hello World" to the Serial Monitor
Serial1.println("Hello!"); // Print "Hello!" over hardware UART
digitalWrite(RXLED, HIGH); // set the LED on
TXLED1; //TX LED is not tied to a normally controlled pin
delay(1000); // wait for a second
digitalWrite(RXLED, LOW); // set the LED off
TXLED0;
delay(1000); // wait for a second
}
If there is no way to tackle this cleanly without modifying the arduino enviroment, then I'll shall modify USBCore.cpp . However its probbly bad practice to do so.
If its possible in your scenario, you could use pin 17 as an INPUT, hopefully freeing up another pin which you can then use as an OUTPUT.
To do this, just use pinMode() to set pin 17 to an INPUT.
That effectively disables the RXLED functionality. When USBCore write a high to that pin, it merely turns on the pullup resistor. As long as the device driving the input can sink enough current even when the pullup is on, this will have no effect. And so there is no need to modify USBCore.
Edit: The LED lights up when pin 17 is low, which means the source of the signal needs to sink current. That can be avoided if its an issue by severing the PCB track next to the LED, or by desoldering the LED or resistor.

How do I read the output of an AT command in Arduino?

How do I capture the output from an AT command on an Arduino?
I'm using the Arduino Uno R3 with a GSM shield. I have all the AT commands (they can be seen here ) and I can enter them just fine if I use the terminal and get output. However how can I capture the resulting output via code? The code below shows what I've tried but it does not work. In particular where I attempt to get the analog input and then print out the result.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8);
void setup()
{
char sensorValue[32] ="";
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("\r");
//Wait for a second while the modem sends an "OK"
delay(1000);
//Because we want to send the SMS in text mode
Serial.println("AT+CMGF=1\r");
delay(1000);
mySerial.println("AT+CADC?"); //Query the analog input for data
Serial.println(Serial.available());
Serial.println(Serial.read()); //Print out result???
//Start accepting the text for the message
//to be sent to the number specified.
//Replace this number with the target mobile number.
Serial.println("AT+CMGS=\"+MSISDN\"\r");
delay(1000);
Serial.println("!"); //The text for the message
delay(1000);
Serial.write(26); //Equivalent to sending Ctrl+Z
}
void loop()
{
/*
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
*/
}
I get the outputs:
AT+CMGF=1
AT+CADC? 21 13
or
AT+CMGF=1
AT+CADC? 18 65
Regardless of changes in my analog source
Take a look at the documentation of the SoftwareSerial read function here.
When you read from the GSM device serial interface, you cannot take for granted that there are bytes to be read on the buffer.
It's very likely that mySerial.read() returns -1 (no bytes available), as Arduino runs that code before the GSM device can provide something on the serial port.
You should use the available function (documentation here) to test the serial interface for incoming bytes. You could use it with a timeout to avoid infinite waiting.
The best thing you could try is to write a separate class to handle serial operations (read, write, timeouts, delays, etc).
Also, I wrote a GPRS driver for Arduino once.
I had a problem with the power supply that required me to install an extra capacitor on the GPRS device and use a power supply with more than 2A of output current.