Go Down

Topic: Multiple 1wire device sorted table access (Read 762 times) previous topic - next topic

wvg_ca

Dec 23, 2016, 03:00 pm Last Edit: Dec 23, 2016, 03:08 pm by wvg_ca
The intent is to use a cable array of up to 24 seperate 1wire devices, in this case Maxim DS18B20 temperature sensors [non-parasitic mode], and a seperate reader with 1602 two line display ..

I would like to poll all existing sensors on the cable, return their individual addresses, and have them sorted by the last two digits of their address, and stored in a table by ascending order ..

The intent is to have the reader then use the table and return temperature readings from each sensor in ascending order, and when last sensor is read, return to first sensor [push button for sequencing]..

The reader will be used for several different cables, which may have differing numbers of sensors on each cable , which may be up to 35 feet from first sensor to the reader..

When the cable is assembled, the sensors will be installed in last wo digit ascending order ..

el_supremo

Quote
and have them sorted by the last two digits of their address, and stored in a table by ascending order
Why? Store them in the order that the OneWire addressing scheme uses. See the links in my msg #3 in this thread.
Also, what will you do if two or more of them happen to have the same last two digits?

35feet is a fair distance to drive 24 DS18B20. What sort of environment will they be in? An industrial environment, for example, with electrical noise from lots of heavy machinery could play havoc with the sensors.

What exactly are you asking for anyway? Software? Hardware? How much are you willing to pay?

Pete
Don't send me technical questions via Private Message.

wvg_ca

I didn't realize that they were normally stored already sorted, thank you ..

The application is to monitor stored product in a storage bins, rural environment ..

What I am looking for is software only, with readings starting from end of chain [bottom of storage bin]..

As I am new to Arduino's, and not a programmer beyond very basic stuff, I have no idea of time to implement, or associated costs involved, but I do realize that getting someone to do this would not be gratis ..

thanks

GoForSmoke

#3
Jan 01, 2017, 04:24 am Last Edit: Jan 01, 2017, 05:35 am by GoForSmoke
You can run 1wire directly from Arduino pins, here's the library reference for the I2C port..

And most any other 2 pins can work as an I2C port too. An Arduino Nano has 22 pins you can use as digital, 8 of those as analog too.
Note: I don't know how many mA to drive those sensors but Arduino pins total ouput max is 200mA. You may need transistors (use TTL level MOSFETs) to power the wires and use 5mA or less per pin.

Question is, how many I2C lines can one Arduino support? 1Wire is slow, a few at least. How much would a star layout with X number of sensors per shorten that 35ft length? I fixed software for a home greenhouse that had 1wire temperature sensors. The devices took a long time between Q & A regardless of how fast you clock the bits when they do, the load is very light.

The not really hard trick is to collect the incoming 1wire from each source into one list.

If you ever do need to sort arriving serial text, here is an example of a fast serial sort. Set your Serial Monitor to 250000 baud, lower right corner, and you won't get garbage.
You can enter up to 20 numbers as the top comments of the sketch specifies, it all goes in at 25000 cps in one gulp and the sort keeps up.

Code: [Select]
// GoForSmoke Arduino Forum Example: Index Sort Serial Entry Demo is Free to Use.
// Just don't say you own it, I gave it away, it's public domain.
// This demo reads serial input for up to 20 number values from -32768 to 32767
// and sorts not the data but indexes into the data on a per char arrived basis.
// The sort is finished when the last char is processed, when most sorts begin.
// Uncomment the program trace print lines to help figure out how the sort works.

// Copy (ctrl-c) any series of numbers and paste (ctrl-v) them into serial monitor
// up at the top in the single user entry line then hit enter. I used these last,
// 223 -55 87 -126 9999 21000 -863 7512 444 11 -1260 -5078 768 -15 -2555 5 4 3 2 1
// Or just enter numers of your own.

const byte maxElements = 20;
byte elements;
char entryState; // 8 bit signed, -128 to 127
byte index[ maxElements ];
int data[ maxElements ];
int accumulate;
char sign;
char ch;

byte i, j, k, flag;

void usage()
{
  Serial.println( F( "Serial input sort example using indexes." ));
  Serial.println( F( "Enter up to 20 whitespace separated ints." ));
  Serial.println( F( "Make sure that your monitor adds newline.\n" ));
}

void show_indexes( byte count )
{
  Serial.print( F( "indexes so far: " ));
  for ( byte x = 0; x <= count; x++ )
  {
    Serial.print( index[ x ] );
    if ( x < count )
    {
      Serial.print( F( "," ));
    }
  }
  Serial.println( F( "\n" ));
}


void setup()
{
  Serial.begin( 250000 );
  usage();

  elements = entryState = 0;
}

void loop()
{
  if ( Serial.available() )
  {
    ch = Serial.read();

    //    Serial.print( F( "state " ));
    //    Serial.println( entryState, DEC );

    switch ( entryState )
    {
      case -1 :
        if (( ch >= '0' ) && ( ch <= '9' ))
        {
          accumulate = ch - '0'; // initial value set
          entryState = 1; // get the rest of the digits
        }
        else
        {
          entryState = 2; // garbage following '-', ignore line
        }
        break;

      case 0 :
        sign = 1;
        if (( ch >= '0' ) && ( ch <= '9' ))
        {
          accumulate = ch - '0'; // initial value set
          entryState = 1; // get the rest of the digits
        }
        else if ( ch == '\n' )
        {
          entryState = 3; // all entries finished
        }
        else if ( ch == '-' )
        {
          sign = -1;
          entryState = -1;
        }
        break;

      case 1 :
        if (( ch >= '0' ) && ( ch <= '9' ))
        {
          if ( accumulate < 3275 ) // max int is 32768
          {
            accumulate *= 10;
            accumulate += ch - '0';
          }
        }
        else
        {
          accumulate *= int( sign );
          data[ elements ] = accumulate;
          elements++;
          if ( ch == '\n' )
          {
            entryState = 10; // last number, all entries finished
          }
          else if ( elements == maxElements )
          {
            entryState = 9; // data array filled, ignore entered chars until newline.
          }
          else
          {
            entryState = 5; // this entry finished
          }
        }
        break;

      case 2 : // looking for newline
        if ( ch == '\n' )
        {
          entryState = 0; // finished, start over
          usage();
        }
        break;
    }

    //      Serial.print( F( "entryState = " ));
    //      Serial.print( entryState, DEC );
    //      Serial.print( F( "  entered = " ));
    //      Serial.print( accumulate );
    //      Serial.print( F( "  elements = " ));
    //      Serial.println( elements );

    if ( entryState > 4 ) // sort the new entry
    {
      //      Serial.print( F( "entryState = " ));
      //      Serial.print( entryState, DEC );
      //      Serial.print( F( "  entered = " ));
      //      Serial.print( accumulate );
      //      Serial.print( F( "  elements = " ));
      //      Serial.println( elements );

      i = elements - 1;
      if ( elements > 0 )
      {
        flag = 0;
        for ( j = 0; j < elements; j++ )
        {
          if ( data[ i ] < data[ index[ j ]] ) // new number is less than indexed value
          {
            for ( k = i; k > j; k-- ) // move finished indexes up
            {
              index[ k ] = index[ k - 1 ];
            }
            index[ j ] = i;
            j = elements;
            flag = 1;
          }
          if ( flag == 0 )
          {
            index[ i ] = i;
          }
        }
        Serial.print( accumulate );
        Serial.print( F( " " ));
        show_indexes( i ); // see how the indexes get sorted as each data is added
      }
    }

    if ( entryState > 5 || entryState == 3 ) // report
    {
      Serial.println( F( "Data as entered." ));
      for ( i = 0; i < elements; i++ )
      {
        Serial.print( data[ i ] );
        if ( i < elements - 1 )
        {
          Serial.print( F( "," ));
        }
      }

      Serial.println( F( "\n\nData as sorted." ));
      for ( i = 0; i < elements; i++ )
      {
        Serial.print( data[ index[ i ] ] );
        if ( i < elements - 1 )
        {
          Serial.print( F( "," ));
        }
      }
      Serial.println( F( "\n\n" ));

      elements = 0;
      for ( i = 0; i < maxElements; i++ )
      {
        data[ i ] = 0;
        index[ i ] = 0;
      }
    }

    if ( entryState == 3 || entryState == 5 || entryState == 10 )
    {
      if ( entryState == 10 || entryState == 3 )
      {
        usage();
      }
      entryState = 0;
    }
    else if ( entryState == 9 )
    {
      entryState = 2;
    }

  } // end if ( Serial.available() )
}  // end loop()

// 223 -55 87 -126 9999 21000 -863 7512 444 11 -1260 -5078 768 -15 -2555 5 4 3 2 1
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

el_supremo

1wire is not I2C, and vice versa.

Quote
And most any other 2 pins can work as an I2C port too.
No. Almost any digital pin can be a 1wire port (best to avoid the built-in LED pin) but hardware-driven I2C can only be on the 2 pins reserved for that purpose.

Quote
If you ever do need to sort arriving serial text
That's not what the OP wanted sorted.

Pete
Don't send me technical questions via Private Message.

wvg_ca

I haven't done anything to date as I am waiting for the 1 wire sensors to come in ...

The cable length in total would be [worst case] about 35 feet, from two feet from the bottom inside the storage bin, out the top, and then down to about four feet off the ground  ..

There may be more than one cable in each storage bin in some cases, but the reader would be moved to each cable to do the readings, no connections are planned between the cables themselves ..The intent is to read the temperature from each sensor, spaced at consistent intervals, do determine if localized heating / spoilage is occurring..

If the 1wire devices addresses are stored in order as el_supremo had noted in other posts, then I assume that during cable assembly I would have to read each sensor individually, write down the address, and physically pre-sort them into the order that I want them read?

GoForSmoke

#6
Jan 01, 2017, 05:05 pm Last Edit: Jan 01, 2017, 05:09 pm by GoForSmoke
That's not what the OP wanted sorted.

Pete
I didn't say the OP did. First three words I used there are ... If .. you .. ever ..

The idea is about rearranging links to data rather than rearranging the data itself even as you collect the data.

So you can collect your temperature reads in sensor order and store them in an array or on an SD card?
At the same time you can make a set of links in temperature sorted order to show which sensors are hottest or coldest..




1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

Go Up