Need help in multiple serial communication?

Hi friends,

I am on a project where i have 8 flow meters(using seed studio g1/2" flow sensor interfaced with arduino), this is my code for flow meter

// include the library code:
#include <LiquidCrystal.h>


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);


byte statusLed    = 13;


byte sensorInterrupt = 0;  // 0 = digital pin 2
byte sensorPin       = 2;


// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 4.5;


volatile byte pulseCount;  


float flowRate;
unsigned int flowMilliLitres;
float totalMilliLitres;


unsigned long oldTime;


void setup()
{
  
  // Initialize a serial connection for reporting values to the host
  Serial.begin(9600);
  lcd.begin(16, 2);
   
  // Set up the status LED line as an output
  pinMode(statusLed, OUTPUT);
  digitalWrite(statusLed, HIGH);  // We have an active-low LED attached
  
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);


  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;


  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}


/**
 * Main program loop
 */
void loop()
{
   
   if((millis() - oldTime) > 1000)    // Only process counters once per second
  { 
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    detachInterrupt(sensorInterrupt);
        
    // Because this loop may not complete in exactly 1 second intervals we calculate
    // the number of milliseconds that have passed since the last execution and use
    // that to scale the output. We also apply the calibrationFactor to scale the output
    // based on the number of pulses per second per units of measure (litres/minute in
    // this case) coming from the sensor.
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    
    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    oldTime = millis();
    
    // Divide the flow rate in litres/minute by 60 to determine how many litres have
    // passed through the sensor in this 1 second interval, then multiply by 1000 to
    // convert to millilitres.
    flowMilliLitres = (flowRate / 60) * 1000;
    
    // Add the millilitres passed in this second to the cumulative total
    totalMilliLitres += flowMilliLitres;
      
    unsigned int frac;
    
    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(int(flowRate));  // Print the integer part of the variable
    Serial.print(".");             // Print the decimal point
    // Determine the fractional part. The 10 multiplier gives us 1 decimal place.
    frac = (flowRate - int(flowRate)) * 10;
    Serial.print(frac, DEC) ;      // Print the fractional part of the variable
    Serial.print("L/min");
    
    lcd.setCursor(0,0);
    lcd.print("F/R: ");
    lcd.print(int(flowRate));
    lcd.print(".");
    frac = (flowRate - int(flowRate)) *10;
    lcd.print(frac, DEC);
    lcd.print("L/min");
    // Print the number of litres flowed in this second
    Serial.print("  Current Liquid Flowing: ");             // Output separator
    Serial.print(flowMilliLitres);
    Serial.print("mL/Sec");


    // Print the cumulative total of litres flowed since starting
    Serial.print("  Output Liquid Quantity: ");             // Output separator
    Serial.print(totalMilliLitres/1000);
    Serial.println("L"); 
    lcd.setCursor(0,1);
    lcd.print("OLQ: ");
    lcd.print(totalMilliLitres/1000);
    lcd.print("L");


    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;
    
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  }
}


/*
Insterrupt Service Routine
 */
void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

now it is fixed at different places in my three floor building and I need to see the readings of all 8 flow meters in the monitor which is at the ground floor, what I have planned is to connect everything using wires because xbee's are costly, but arduino has only one serial communication interface and arduino mega has 4 serial communication interface, cant I use a single arduino board and connect all flow meters to one board and see the readings in serial monitor?

I tried by connecting one arduino with the other and I have taken a pic, I also tried by connecting receiver of two arduino boards sending to other board's tx pin, and tx of two arduino boards and sending to the rx of other board , Arduino got confused probably? didn't work, Can you just guide me through this. Dono what to do ? What i need is to have a single arduino board in the ground floor connected to the pc wherein I should be able to see the readings of all 8 flow meters?

Logeswari12:
Hi friends,

I am on a project where i have 8 flow meters(using seed studio g1/2" flow sensor interfaced with arduino),

cant I use a single arduino board and connect all flow meters to one board and see the readings in serial monitor?

Probably not. The serial ports are not the issue, it's the interrupt pins for the sensors. I believe the Uno has two, but the Mega has five. I therefore believe you can do this job with two Megas - one handling say four flow sensors which feeds the data to the second Mega. This second Mega handles the remaining sensors and sends all the data to the PC.

I believe there is a thread nearby on using two flow sensors.

That picture.. looks like Rx<->Rx and Tx<->TX !? (this is NOT correct)

Are you are saying that you want serial communications from several slave Arduinos to be received in the same serial port in a Master Arduino?

That should be possible, but will need some organization - definitely at the software level and possibly at the hardware level.

You need to ensure that only one device can transmit at any one time. The easiest way to do this is for the master to send a message with an ID number and the slave with that ID will then reply. Then the master will ask the next slave to respond.

I don't know what will happen electrically if you connect the Tx pins from several slaves to the single Rx pin for the master. It may be that the signal from any slave will be absorbed by the other connections and will not be strong enought when it reaches the master. The same risk exists in the other direction if you connect a single Tx pin from the master to the Rx pins of several slaves. These problems may be worsened if the cable lengths are long.

If there is an electrical problem of that sort you will need to install buffer ICs between the slaves and the master.

...R

If you want to stick with serial then you should bump up to RS-485. TTL serial doesn't like long distances.

If you want to simplify the communication then look at the EasyTransfer library. I'd suggest creating a SEND_DATA_STRUCTURE of eight ints representing the count from each flow meter; when the master sends out a message and one of the counts is zero -- only one -- that signifies to the respective slave that it needs to return its incremented count (the number of pulses from the flowmeter since the last message was sent). That way you won't have everyone trying to jabber on the line at the same time.

If you want to move to a wireless solution then the ESP8266 is the best bet. It has quite a learning curve getting them running though.

I don't know what will happen electrically if you connect the Tx pins from several slaves to the single Rx pin for the master. It may be that the signal from any slave will be absorbed by the other connections and will not be strong enought when it reaches the master.

I use a small signal diode with the band side connected to the slave tx pins. This keeps the slave tx pins from keeping the combined tx pins going to master rx pin high. The slave tx pin then only has to pull the master rx pin low and not be interfered with from the other slave tx pins being maintained high.

zoomkat:
I use a small signal diode with the band side connected to the slave tx pins. This keeps the slave tx pins from keeping the combined tx pins going to master rx pin high. The slave tx pin then only has to pull the master rx pin low and not be interfered with from the other slave tx pins being maintained high.

Very good. I had forgotten that the system idles HIGH.

...R

Probably not. The serial ports are not the issue, it's the interrupt pins for the sensors. I believe the Uno has two, but the Mega has five. I therefore believe you can do this job with two Megas - one handling say four flow sensors which feeds the data to the second Mega. This second Mega handles the remaining sensors and sends all the data to the PC.

I have a seperate arduino uno board for each flow meter which process the code and gives out flow quantity, just I need to transmit the data to ground floor for which I am searching for some kind of communication, so interrupt shouldn't be a problem,

Even though pondering about your idea, I should take that into " ideas to discuss" thanks, where I could just have the sensors on the required place and remaining process could be done at the ground floor.

Are you are saying that you want serial communications from several slave Arduinos to be received in the same serial port in a Master Arduino?

That should be possible, but will need some organization - definitely at the software level and possibly at the hardware level.

You need to ensure that only one device can transmit at any one time. The easiest way to do this is for the master to send a message with an ID number and the slave with that ID will then reply. Then the master will ask the next slave to respond.

I don't know what will happen electrically if you connect the Tx pins from several slaves to the single Rx pin for the master. It may be that the signal from any slave will be absorbed by the other connections and will not be strong enought when it reaches the master. The same risk exists in the other direction if you connect a single Tx pin from the master to the Rx pins of several slaves. These problems may be worsened if the cable lengths are long.

If there is an electrical problem of that sort you will need to install buffer ICs between the slaves and the master.

do you mean I need to change the code of flow meter? and what kind of hardware changes do you suggest?

Logeswari12:
I have a seperate arduino uno board for each flow meter which process the code and gives out flow quantity, just I need to transmit the data to ground floor for which I am searching for some kind of communication, so interrupt shouldn't be a problem,

I didn't realise you already had a swag of Arduinos and I think you might have jumped the gun there, but it certainly removes any problems you might have with running more than one flow meter. With that in mind, not to mention the price of cable, you might check the practicality of a cheapo NRF24 network. It only costs about $5 to test the worst case. You might need a more powerful version at the base station, but only one, and they are very reasonably priced too. You might also consider connecting the base station to the PC via bluetooth, which should widen the options about where they are located.

just I need to transmit the data to ground floor for which I am searching for some kind of communication

The simple thing to do is have the master arduino request a status report from the slave arduinos with the sensors. That way there are no communication conflicts. The slave arduinos only provide data to the master when requested.

Logeswari12:
do you mean I need to change the code of flow meter?

I don't think I said anything about that so I don't know what was in your mind when you posed the question.

and what kind of hardware changes do you suggest?

At the moment the only thing I have in mind is what @Zooomkat said in Reply #5.

There is also the point made by @Chagrin in Reply #4.

I would start by getting the system to work when all the devices are close together (on the kitchen table) and only then figure out what is needed to extend the range. There will be too many complexities if you start with long range transmission

...R

The simple thing to do is have the master arduino request a status report from the slave arduinos with the sensors. That way there are no communication conflicts. The slave arduinos only provide data to the master when requested.

May I have a pseudo code to establish this? I am beginner in coding.

Thanks

Logeswari12:
May I have a pseudo code to establish this? I am beginner in coding.

Thanks

Basic arduino to arduino test code. Don't connect the tx/rx/gnd between the arduinos until the code is loaded on them. Note a small signal diode may be needed as previously mentioned.

//zoomkat 3-5-12 simple delimited ',' string tx/rx 
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin. 
//Connect the arduino grounds together. 
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >0) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

FWIW, since u already looked at Zigbee's - meaning u had an interest to go RF except for price.
After much research I found the HopeRF modules which should give u wireless at much lower cost than Zigbees.
The RFM69W module is a transceiver and the packet structure allows u to define a unique node address. You can therefore set u 2-way comms.
Sorry can't be more help, still struggling with the implementation myself, but know they should work.

Basic arduino to arduino test code. Don't connect the tx/rx/gnd between the arduinos until the code is loaded on them. Note a small signal diode may be needed as previously mentioned.

on my way towards it, let me get you the updates asap.

thanks