Tell me what you want, What you really, really want.

Moderator: Mmiscool

User avatar
By livetv
#49318
forlotto wrote:}
else if ( fname == F("temp") && num_args > 0 ) {
// function temp(sensor #)
// set return value
sensors.requestTemperatures();
*value = sensors.getTempCByIndex(args[0]);
return PARSER_TRUE;
}


@forlotto: This is so AWESOME!!! I figured out the library and what it is doing. Most of my assumptions were correct (except that there is no device code look-up table as it's derived with each call by brute force). It's no mystery to me at all now. I would propose addition of two functions. One to return all device codes on the wire (in a space separated string for easy handling with word()) and one to retrieve temperature readings by device code. Please understand that I'm very rusty with the particular C like dialects in play here (spending most of my time with PHP and Javascript). These won't run as written, but you'll get the idea.

Code: Select allvoid function getDevCodes()
{
   DeviceAddress deviceAddress;
   uint8_t index;
   char* strptr;
   // here we have to initialize strptr to the start of a character buffer
   for (index=0; getAddress(deviceAddress,index); index++)
   {
      uint8ToHex(strptr,deviceAddress);   // Fictitious function to convert 8 byte device code to 16 byte string and advance strptr
      (*strptr++)=" ";         // Add a space separator
   }
   if (index>0) strptr--;            // Don't need a trailing space
   (*strptr)="\0";               // Add 0 terminator
}                  // result is returned in character buffer as space separated device codes


function getTempByDevCode((string*)devCode)
{
   DeviceAddress deviceAddress;      // create 8 byte storage area for device code
   hexToUint8( deviceAddress, devCode );   // Fictitious function to convert 16 byte hex character string to 8 byte address
   return getTempC(deviceAddress);
}


If these are written on the library level, they look different than if written as the BASIC interpreter code (which would use "sensor."). I haven't figured out how the arg[] array works yet or exactly how I should handle strings or if my "fictitious" conversion functions actually exist elsewhere already, but if I had more time, I think I could figure out how to do this straight in the BASIC code. I'm on a roll but 4:00am came too quickly.
User avatar
By forlotto
#49347 Yes quite the shortcut I presume. Now I must state I am not an expert so to speak but that looks as if it closely resembles what you talk of but if you read through the library it would appear that there appears to be several ways already integrated on how to call for sensors within the lib file.

I am unsure of the specifics as I just briefly skimmed through the library but I recall methods to address a specific sensor etc...

so possibly you could do like what they did with the tft for instance and have commands like so:
temp.cel
temp.far
temp.genrct 'generate all roms list in a table with needed info then save to a file
temp.readtrom ' for any time you put a new sensor into service you can hook it up and read the rom and it would also read all roms on the bus.
temp.read1("romcode") 'read a specific sensor based off of the table on what it is addressed to.
temp.all 'read all temps

It should be possible I dunno what your plans are but you seem fairly effective I am sure you could provide a workable solution after a bit more study that would suit everyone.

Anyhow glad to see you are a leap closer to reaching your goal. Wish I was able to feed yah with a spoon and just integrate it maybe sometime down the line but direction always helps I suppose. Sooner or later I will develop a better working knowledge of things in due time baby steps I suppose ;)
User avatar
By livetv
#49365 Ok, so I'm burning time I don't have on this, but what follows is code that might actually work if plugged into the interpreter:

Code: Select all// the following 3 declarations need to be made somewhere to have them available for both functions.
byte devAddr[8];   // create 8 byte storage area for device code
String junk="";      // Need a place to compile results or process code "halves"
byte index;      // Just any old counter

// The rest can be plugged into the interpreter code right after the temp() function handler:

 else if ( fname == F("temp_ids") && num_args == 0 ) {
    // function temp_ids()
    // Return all connected sensor ROM codes as space-separated string (16 characters each)
    junk="";
    for (index=0; sensors.getAddress(deviceAddr,index); index++)   // Loop while devices are found
    {
        if (index>0) junk.concat(" ");               // If not the first device, add a separator space
        junk.concat(String(*((uint32_t *)devAddr), HEX);    // Convert & append the first 4 bytes of device code
        junk.concat(String(*((uint32_t *)(&devAddr[4])), HEX);   // Convert and append the last 4 bytes of device code
    }
    *value_str = junk;
    return PARSER_TRUE;
  }


 else if ( fname == F("temp_by_id")  && num_args == 1 ) {
    // function temp_by_id( Rom Code )   Rom code is a 16 character hexadecimal string
    // Return all connected sensor ROM codes as space-separated string
    sensors.requestTemperatures();   // Tell all sensors to convert analog to digital
    junk=args_str[0].substring(0,7);   // Get first 8 characters and...
    junk.strtoul(devAddr, NULL, 16);   //  ... convert them to the first 4 code bytes
    junk=args_str[0].substring(8);   // Do the same with the remaining 8 bytes
    junk.strtoul(&devAddr[4], NULL, 16);
    *value = sensors.getTempC(devAddr);   // Perform the temperature read by device code
    return PARSER_TRUE;
  }


Now what? I don't know what to do with this. I guess it gets compiled and flashed or something but that's beyond my feeble brain.

I can see why this was not done in the first place. The device codes are 8 byte values and they don't represent easily in what appears to be a 32 bit system with handy conversion functions that only work with 32 rather than 64 bits. Even if we can declare 64 bit unsigned integers, we'll probably want to express them as character strings to save in flash based files or something. If the device rom codes were 32 bits, this would have been so much easier!

Fascinating exercise! I learned a little about how the interpreter is written. I'd have used tokenization to eliminate variable and function lookups but the writers did a great job of keeping the code organized and functional. Check line 50 though:

if (Name == F("D8")) PinReturnValue = 13;

I think that should be 15, not 13.
User avatar
By forlotto
#49366 mmiscool or cicciocb or roger may be able to help you here look at the contributors to espbasic on the github I am really not sure how it all gets compiled and turned into binary form

my only guess is to make changes and save all files and compile with an arduino compiler maybe? I really do not know.

time for bed lol falling asleep at keyboard....