Two arduinos won"t communicate over serial!?

I started a weather station project and i found some code online that will read all of the sensor data and put it into variables. I bought an arduino uno wifi rev 2 to take the data produced by the code and send it to a web server but i couldn't get that code to run on it because if the different register addresses. So i decided it would be easier to run the sensor code on the a normal uno and let the uno wifi only deal with the internet stuff. I then wrote some code to turn the individual variables into one long string and send it to the uno wifi over serial to be sent to the internet. But when i tried to get the two arduinos to communicate over serial nothing happened and none of the tx or rx leds lit up at all. If you guys could help me find and fix the problem that would be great. Ill post the codes below
Thanks

Transmitter Code

#include <Wire.h>
#include <Adafruit_BME280.h>
#include <SoftwareSerial.h>




//Wind Direction
int show = 0;
int error = 0;
int ledPin = 13;
byte direct = 1;//begin with North
char* compass[] = {"N  ", "NNE", "NE ", "ENE", "E  ", "ESE", "SE ", "SSE", "S  ", "SSW", "SW ", "WSW", "W  ", "WNW", "NW ", "NNW","???"};
byte pointer = 0;
//CSV Converter Variables
String directf = "";//does not need to be converted to string
String strdir = "";
String speedf = "";
String tempf = "";
String pressf = "";
String rainf = ""; //no need for comma
String finalstr = "";
String randf = "";
int randoma = 22;
float windspeed = 0.0;


//Wind Speed
float radius = 0.065; //metres from center pin to middle of cup
volatile int seconds = 0;
volatile int revolutions = 0; //counted by interrupt
volatile int rps = -1; // read revs per second (5 second interval)
volatile int mps = 0;  //wind speed metre/sec

//Rainfall
volatile double rainfall = 0;

//Barometer and Thermometer
double  temperature   = 0.0;
double  barometer = 0.0;
Adafruit_BME280 bmp;

void setup() {
  //The angle of wind direction is given here for each pin on its own
  //if two opins are activated, ie magnet halfway between, the direction
  //is interpolated so that 16 angles are reported, not just the 8 shown.
  //you may need to experiment with the magnet to get this accurate and
  //reliable.  The alignment of the reed switches should not be critical.
  pinMode( 4, INPUT_PULLUP); //angle ==   0 degrees
  pinMode( 5, INPUT_PULLUP); //angle ==  45 degrees
  pinMode( 6, INPUT_PULLUP); //angle ==  90 degrees
  pinMode( 7, INPUT_PULLUP); //angle == 135 degrees
  pinMode( 8, INPUT_PULLUP); //angle == 180 degrees
  pinMode( 9, INPUT_PULLUP); //angle == 225 degrees
  pinMode(10, INPUT_PULLUP); //angle == 270 degrees
  pinMode(11, INPUT_PULLUP); //angle == 315 degrees
  pinMode(ledPin, OUTPUT);

  Serial.begin(9600);
  
  while (! Serial);
  

  
  
  show = 0;
  
  
  //Anemometer
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(0, rps_fan, FALLING);
  //Rainfall
  pinMode(3, INPUT_PULLUP);
  attachInterrupt(1, tipbuckets, FALLING);
  //Barometer
  bmp.begin();    //start the barometer and temp packages

  // Initialize Timer1 for a 1 second interrupt
  // Thanks to http://www.engblaze.com/ for this section, see their Interrupt Tutorial
  cli();          // disable global interrupts
  TCCR1A = 0;     // set entire TCCR1A register to 0
  TCCR1B = 0;     // same for TCCR1B
  // set compare match register to desired timer count:
  OCR1A = 15624;
  // turn on CTC mode:
  TCCR1B |= (1 << WGM12);
  // Set CS10 and CS12 bits for 1024 prescaler:
  TCCR1B |= (1 << CS10);
  TCCR1B |= (1 << CS12);
  // enable timer compare interrupt:
  TIMSK1 |= (1 << OCIE1A);
  // enable global interrupts:
  sei();
} // setup()

//Routine Driven by Interrupt, trap 1 second interrupts, and output every 5 seconds
ISR(TIMER1_COMPA_vect) {
  seconds++;
  if (seconds == 5) { //make 5 for each output
    seconds = 0;
    rps = revolutions;
    revolutions = 0;
  }
}

// executed every time the interrupt 0 (pin2) gets low, ie one rev of cups.
void rps_fan() {
  revolutions++;
}//end of interrupt routine

// executed every time the interrupt 1 (pin3) gets low, ie one tip of buckets.
void tipbuckets() {
  rainfall++;
 //this has not been calibrated as it will depend on your own printing dimensions
 //as a rough guide though, this system has 50 tips per 100ml, over a radius of 4.25cm
 //one tip is approx 2ml, or 2cmCubed. Area of catchment= pi*4.25*4.25=56.7 cm Squared
 //Rainfal per tip is 2/56.7=25th of a ml
}//end of interrupt routine


void loop() {
    
  while (true) {
    int pointer = -1;
    direct = (((PINB & B00001111) << 4) | ((PIND & B11110000) >> 4)) ^ B11111111;
    //chuck out any non readings where the magnet doesn't work
    switch (direct) {
      case 1:
        pointer = 0;
        break;
      case 3:
        pointer = 1;
        break;
      case 2:
        pointer = 2;
        break;
      case 6:
        pointer = 3;
        break;
      case 4:
        pointer = 4;
        break;
      case 12:
        pointer = 5;
        break;
      case 8:
        pointer = 6;
        break;
      case 24:
        pointer = 7;
        break;
      case 16:
        pointer = 8;
        break;
      case 48:
        pointer = 9;
        break;
      case 32:
        pointer = 10;
        break;
      case 96:
        pointer = 11;
        break;
      case 64:
        pointer = 12;
        break;
      case 192:
        pointer = 13;
        break;
      case 128:
        pointer = 14;
        break;
      case 129:
        pointer = 15;
        break;
      default:
        pointer = 16;
        // if nothing else matches, do the default
        // default 16, "???" mainly for debugging
        break;
    }
    //Serial.print(direct);
    //Serial.print("\t -> ");
    //Serial.print(pointer);
    //Serial.print("\t -> ");
    hexBinDump(direct); //working beautifully
    
    
    //Serial.println("Wind: ");
    if (rps != -1) { //Update every 5 seconds, this will be equal to reading frequency (Hz)x5.
      float kmph = rps * 3.1414 * 2 * radius * 12 * 60 / 1000;
      if (direct != 0) {
        //Serial.println(compass[pointer]);
      }
      
      //Serial.println("@ ");
      if (kmph < 10) {
        //Serial.println(" ");
      }
      //Serial.println(kmph, 1); //kilometres per hour to one decimal place
      //Serial.println("km/h ");
      windspeed = kmph;
    }
    //Barometer and Temperature
    temperature = (double)bmp.readTemperature();   //internal temperature
    barometer = (double)bmp.readPressure() / 100.0; //Pa reduced to mBar
    
    //Serial.println("P:");
      if (barometer < 1000) {
        //Serial.println(" ");
      }
    //Serial.println(barometer,0);
    //Serial.println("kPa ");
   //Serial.println(11, 2);
    //Serial.println("T:");
      if (temperature < 10) {
        //Serial.println(" ");
      }
    //Serial.println(temperature,1);
    //Serial.println("C ");
    //Rainfall 
    
    //Serial.println("Rainfall: ");
    //Serial.println(rainfall,1);
    //Serial.println("mm   ");
    
    
    strdir = compass[pointer]; //wind direction conversion
    directf = strdir + ",";

    String strspeed = String(windspeed,1); //wind speed conversion
    speedf = strspeed + ",";

    String strtemp = String(temperature,1); // temperature conversion
    tempf = strtemp + ",";

    String strpress = String(barometer,0); //pressure conversion
    pressf = strpress + ",";

    String strrain = String(rainfall,1); //rain conversion
    rainf = strrain + ",";

    String strrand = String(randoma);
    randf = strrand;

    finalstr = directf+speedf+tempf+pressf+rainf+randf;

    Serial.println(finalstr); //sends the finalstr variable over serial to uno wifi
    delay(2000);
  }
  
  
} // loop()

//Useful to invoke to debug the byte Array
void hexBinDump(int myNos) {
  //Serial.println("T A3 10100011 07 00000111 02 00000010 AA 10101010 F0 11110000 06 00000110 FF 11111111 07 00000111 33 00110011 60 01100000");
  byte mask = B10000000;
  if ((myNos & B11110000) == 0) { //alter if you need to change how many bytes you are checking
    //Serial.print("0");
  }
  //Serial.print(myNos, HEX);
  //Serial.print(" ");
  for (int k = 0; k < 8; k++) {
    if (myNos & mask) {
      //Serial.print("1");
    }
    else {
      //Serial.print("0");
    }
    mask = mask >> 1;
  }
  //Serial.println();
} 

Receiver /wifi code
ps. i haven't added the wifi part to this code yet. it only displays the first variable on a small oled display

#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <SoftwareSerial.h>

#define OLED_WIDTH 128
#define OLED_HEIGHT 64

#define OLED_ADDR   0x3C

Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);



void setup(){
  Serial.begin(9600);
  
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 17);
  
  

  display.display();

}
void loop(){
if(Serial.available()){
String rxString = "";
String strArr[6]; //Set the size of the array to equal the number of values you will be receiveing.
//Keep looping until there is something in the buffer.
while (Serial.available()) {
//Delay to allow byte to arrive in input buffer.
delay(2);
//Read a single character from the buffer.
char ch = Serial.read();
//Append that single character to a string.
rxString+= ch;
}
int stringStart = 0;
int arrayIndex = 0;
for (int i=0; i < rxString.length(); i++){
//Get character and check if it's our "special" character.
if(rxString.charAt(i) == ','){
//Clear previous values from array.
strArr[arrayIndex] = "";
//Save substring into array.
strArr[arrayIndex] = rxString.substring(stringStart, i);
//Set new string starting point.
stringStart = (i+1);
arrayIndex++;
}
}
//Put values from the array into the variables.
String winddir = strArr[0];
String windspeed = strArr[1];
String temperature = strArr[2];
String barometer = strArr[3];
String rain = strArr[4];
String other = strArr[5];
Serial.println(winddir);

//Convert string to int if you need it.
display.clearDisplay();
display.setCursor(0, 17);
display.print(winddir);
display.display();
}

  
}

Why use String and why turn the variables into one long String ?

I assume that you are using hardware Serial which is shared with the USB interface, certainly on the Uno

One of the most useful debugging tools on an Uno is Serial. Since you're already including it and running at a slow baud rate, use software serial for your communication and use Serial (at a much higher speed) to find out what's going wrong.

Right now when i plug in both arduinos and look at the rx and tx leds on both arduinos nothing lights up even though the transmitter works when it is only connected to my computer.

How are the two connected?

The rx on one is connected to the tx on the other and vice versa and the grounds are connected together and i have the 5v pin from one going to the vin pin on the other.

Which pins on the Uno WiFi are you using for the Serial interface ? If you are using pins 0 and 1 then you should be receiving data on Serial1

You seem to be trying to receive data on the Serial interface

Serial ports

The Arduino Uno WiFi Rev. 2 has 3 hardware serial ports. Serial is connected to the USB interface, Serial1 is connected to Pin 0 (RX) and 1 (TX), Serial 2 is connected to the u-blox NINA-W13 module. This allows the usage of pins 0 and 1 without issues: on the original Arduino UNO, the usage of Pins 0 and 1 disrupts the sketch upload.

i think that's my problem, i've been trying to send and receive on serial not serial1. Thank you for pointing this out. Do you know how i can send and receive over serial1, this is my first time using serial for arduino to arduino communication.

I suspect that UKHeliBob has nailed your issue, but powering one Uno from another seems questionable too.

Ok i will try serial1 and power them separately. Thanks

The Uno only has one serial interface named Serial. It is used by the USB interface and pins 0 and 1

To receive on Serial1 on the Uno WiFi use pins 0 and 1 and the Serial1 object instead of Serial. Don't forget to cross connect Rx/Tx between the boards and ensure that there is also a common GND connection

Thank You guys so much it is working perfectly now

You might want to consider using SofttwareSerial on the Uno leaving the hardware serial interface for debugging

That's a good idea ill take a look at how to hook that up. Thanks

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.