Unable to do Arduino Mega to Arduino Mega serial communication - arduino

Based on the circuit below, I tried hooking up two Arduino Mega for serial communication.
The code for sender:
char mystr[3] = "Hello"; //String data
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}
void loop() {
Serial.write(mystr, 5); //Write the serial data
delay(1000);
}
The code for receiver:
char mystr[5]; //Initialized variable to store received data
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}
void loop() {
Serial.readBytes(mystr, 5); //Read the serial data and store in var
delay(1000);
}
There is no output in the Serial console of Arduino. Could someone please inform me of the possible cause and solution for the same. If I've missed out anything, over- or under-emphasized a specific point let me know in the comments.

If I understood this right you have one Arduino connected to your pc and to another Arduino?
The problem is that you need to specify which Serial port to use:
That is rather easy, just type Serial1 or Serial2 instead of just Serial. That allows you to open 2 Serial ports: One to your other Arduino and one to your Computer for Displaying the results !
LINK: https://www.arduino.cc/en/Tutorial/MultiSerialMega

You need to check available data from serial:
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
Serial.readBytes(mystr, 5);
Serial.print("I received: ");
Serial.println(mystr, DEC);
}
}

Related

Sending data from Arduino UNO to NodeMCU over UART and processing received data on NodeMCU

I'm trying to send data from Arduino UNO to NodeMCU via UART.
What I want to do is that when Arduino UNO sends "on" String to the NodeMCU, NodeMCU lights up its builtin LED, when "off" - it turns off.
I send data from Arduino UNO via standard Serial.println (). On the NodeMCU I use the SoftwareSerial library. I assigned rx and tx to pins D7 and D8 accordingly. Serial ports on Arduino(standard) and NodeMCU(SoftwareSerial) are set at 9600 baud rate.
Standard Serial port (USB) of NodeMCU is set to 115200.
I send the string that I receive from the Arduino to the standard serial port of the node (connected to usb)
The question is:
On the standard port of NodeMCU, which I view through the Arduino IDE, messages coming from the arduino are displayed, and displayed correctly (those that were sent), but the NodeMCU does not want to accept them in conditional statements and light up my LED. Why?
At the same time, when I remap the virtual UART to its original pins (connected to USB, GPIO3 and GPIO1, and send the same messages via usb through the COM port view in the Arduino IDE, the LED turns on and off as I programmed it.
Do you have any ideas why this is happening?
By the way, I do not lower voltage coming from Arduino RX and TX pins from 5V to 3.3V, but since messages are recived coorectly, I don't think that this is causing a problem.
Here's my code:
Arduino:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("on");
delay(1000);
Serial.println("off");
delay(1000);
}
NodeMCU:
#include <SoftwareSerial.h>
SoftwareSerial s(D7,D8);//rx,tx
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
s.begin(9600);
pinMode(D4,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
String str = s.readStringUntil('\n');
Serial.println(str);
if(str == "on"){
digitalWrite(D4, HIGH);
}
if(str=="off"){
digitalWrite(D4, LOW);
}
}
Screenshot of COM4:
Screenshot of COM4:
UPD: I tried using sending 1 or 0 as int value via Serial.write() and s.read() and it works, maybe the prolem is in String type somehow
You Use İt Maybe Work
#include <SoftwareSerial.h>
String text = "";
SoftwareSerial s(D7,D8);//rx,tx
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
s.begin(9600);
pinMode(D4,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
String str = s.readStringUntil('\n');
text = str
Serial.println(text);
if(text == "on"){
digitalWrite(D4, HIGH);
}
if(text =="off"){
digitalWrite(D4, LOW);
}
}

use another serial to send data from arduino to processing

i need to use another serial to send data from arduino teensy to processing because default serial (Serial.begin(9600)) already used for big program
i try to read some reference about how maybe i can change from which serial i want to receive (https://processing.org/reference/libraries/serial/Serial.html), but i dont think it can be change
void setup() {
Serial.begin(115200); // already used
Serial2.begin(9600); // processing
}
void loop() {
Serial.println("...") //big code that i am not allow to change
Serial2.println("hello world");
delay(1000);
}
i expected to get "hello world" in my processing repeatly, but i really dont have any idea how to write the code so i can get value from Serial2 instead from Serial
It depends on what Teensy module you are using and how you're doing the wiring.
Please see the Teensy Using the Hardware Serial Ports article for more details.
If possible I'd try their UART/USB example:
// set this to the hardware serial port you wish to use
#define HWSERIAL Serial1
void setup() {
Serial.begin(9600);
HWSERIAL.begin(9600);
}
void loop() {
int incomingByte;
if (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.print("USB received: ");
Serial.println(incomingByte, DEC);
HWSERIAL.print("USB received:");
HWSERIAL.println(incomingByte, DEC);
}
if (HWSERIAL.available() > 0) {
incomingByte = HWSERIAL.read();
Serial.print("UART received: ");
Serial.println(incomingByte, DEC);
HWSERIAL.print("UART received:");
HWSERIAL.println(incomingByte, DEC);
}
}
If that hows with the same USB connection at the same time, negotiate with your colleague so you get to use the easier one that simply shows up as another Serial port in Processing.
If that is not an option:
double check the pinout the Serial Ports article above but also the logic level voltage (e.g. might be 3.3V, not 5V)
get a USB Serial converter (for the right logic level) - this will show up as a different Serial port using Processing's Serial.list()
connect Serial2's TX pin to the convertor's RX pin and read the data in Processing (similar to process to how you'd read Serial, just a different port name)

ESP8266 to ESP8266 i2C Communication

I am trying to get my ESP8266's connect and send messages over an i2c bus. I am using a NodeMcu Development Board. Pins D1,D2 and GND are connected to each other.
The code on my master is :
#include <Wire.h>
void setup() {
Wire.begin(D1,D2); // join i2c bus (address optional for master)
Serial.begin(115200);
}
byte x = 0;
void loop() {
Wire.beginTransmission(8);
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
Serial.println("Transmitted");
x++;
delay(500);
}
And the code on my slave ESP is:
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(115200); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
Serial.println("Received..");
/*
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
*/
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
Running this gives no output on the receiver chip.
As mentioned in the comments it doesn't look like I2C is supported, but you could use PJON
You just need to connect a single wire to enable communication between the two devices
I'm not sure but I would expect the Wire library from Arduino to use the hardware I2C controller for ATMega. The I2C driver in the firmware from Espressif seems to be doing I2C over GPIO, that would hint there is no hw controller on ESP (what are the odds they would be the same anyway). So you need to use something else than Wire.h, thus I would suggest - try downloading something that fakes I2C over GPIO for your Arduino IDE. Like this .. maybe, I haven't tried that out. I know not a complete solution, but maybe at least this helps.. good luck!
ESP8266(I2C Master) to ESP8266(I2C Slave) works from version 2.5.0. Check out my comments on the ESP8266 GitHub

ESP8266 connected to my Arduino Uno

I have the ESP8266 connected to my Arduino Uno. With a blank sketch I can use Serial Monitor to connect it to my wifi network using these commands
AT+IPR=9600
AT+CWMODE=1
AT+CWJAP="SSID_HERE",""
It get's an ip and everything. But now I want my sketch to just do this using this code
#include <SoftwareSerial.h>
#define SSID "SSID_HERE"
void setup(){
Serial.begin(9600);
Serial.setTimeout(5000);
delay(1000);
}
boolean connectWiFi()
{
// connect
Serial.println("AT+CWMODE=1");
Serial.println("AT+CWJAP=\"SSID_HERE\",\"\"");
delay(2000);
if(Serial.find("OK"))
{
Serial.println("AT+CIFSR");
Serial.flush();
delay(1000);
return true;
}
else
{
// Can not connect to the WiFi.
return false;
}
}
But it doesn't work.. The Serial.println shows up in the Serial Monitor, but the ESP8266 doesn't seem to respond. What am I missing?
AT -commands ends with carriage return, so you need to add '\r' to every command you print.
In your code lines looks like:
Serial.println("AT+CWMODE=1\r");
Serial.println("AT+CWJAP=\"SSID_HERE\",\"\"\r");
Serial.println("AT+CIFSR\r");
Reference: https://en.wikibooks.org/wiki/Serial_Programming/Modems_and_AT_Commands/Special_Commands_and_Character_Sequences
The problem here is that you are trying to use pins 0 & 1 for the serial comms, well its part of the problem.. Because the arduino uses serial as well, it for me is only really good to use pins 0 & 1 for serial when i've grounded the reset pin on the arduino. This turns the arduino into a dummy device.
You can use something like software serial and two different pins instead, this way you will not interfere with the hardware serial of the arduino.
Also just to note, the below example will barely work.. For some it will for others it wont.. The problem here is that software serial does not really work / run at 115200..
You can change baud rate via AT+UART_DEF=19200,8,1,0,0 which will also disable flow control, then use software serial with a different speed mySerial.begin(19200)
Using Serial.println("TEXT") will send the line returns for you, so no need to add them unless you use Serial.print("TEXT\r\n")
DO NOT USE: AT+IPR= as this will brick it and require a reflash
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 10); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
pinMode(11, INPUT);
pinMode(10, OUTPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("ARDUINO: Starting");
mySerial.begin(115200);
Serial.println("ARDUINO: Sending AT Command");
mySerial.println("AT");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}

Communicating serially through arduino xbeeshield

I have a ladyada xbee adapter on the computer side and an arduino xbeeshield which I am trying to communicate with over wireless. Both xbees are configured correctly in that I can receive data from the xbeeshield to the computer. However it doesn't work the other way i.e. xbeeshield does not echo a byte sent from the computer serially. Any idea what I might be doing wrong? (Note: When I connect the arduino board to the computer using USB cable, the echo program works just fine. It seems to be a problem in wireless mode only)
processing code
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
Serial.print((char) Serial.read());
delay(10);
}
}
I am just sending keystrokes from computer and waiting for a reply. I am not getting any.
I use the code I answered to the following question in regards to sending serial bytes from PC to Xbee/Arduino. It's been working fine for months. Ensure you've configured both your Xbee modules on the PC and Arduino side. Ensure your PAN ID's are the same as well.
Arduino making decision according to a packet received from serial port
What version of the Xbee modules are you using? My code works with Series 1 but should work with newer versions as well.
Try using softwareSerial library and connecting Tx and Rx to pin 4 and 2. Run the following sketch and tell me what happens. Change the Baudrate value to match your own
#include <SoftwareSerial.h>
uint8_t pinRx = 2 , pinTx = 4; // the pin on Arduino
long BaudRate = 57600; // Please set your Baudrate. It should match the one in XC-TU
char GotChar, getData;
// Xbee SoftwareSerial initialization
SoftwareSerial xbee(pinRx, pinTx); // RX, TX
void setup()
{
Serial.begin(9600);
Serial.println( "Welcome to the XBee Communication Test" );
Serial.print("BaudRate:");
Serial.println(BaudRate);
Serial.print(" Rx Pin#");
Serial.println(pinRx,DEC);
Serial.print(" Tx Pin#");
Serial.println(pinTx,DEC);
// set the data rate for the SoftwareSerial port
xbee.begin( BaudRate );
xbee.println("Setup Completed!");
}
void loop()
{
if (Serial.available())
{
GotChar = Serial.read();
xbee.print(GotChar);
Serial.print(GotChar);
}
while (xbee.available()>0)
{
Serial.println("Ohohoh");
getData = xbee.read();
Serial.print(" Received: );
Serial.print(getData);
Serial.println();
if(getData == 'a')
{
Serial.println(" sbam");
}
else if(getData == 'b')
{
Serial.println(" sbo");
}
}
}
Upload the program and open the serial monitor. Do you get the 'Setup completed' message on the computer? What happens if you send 'a' or 'b' from the Pc to the Arduino?

Resources