Bridge.get / Bridge.put - Confusing code differences...

Hi,

I am reasonably new to Arduino and specifically the Yun. I have come across a weird issue when trying to code using Bridge.put and Bridge.get.

I am writing a multi PID heater controller with a web interface. I have followed others advice and split what I am try to accomplish down into individual modules and get each one working before tackling the whole.

This morning I have been working on passing variables between a web page written in PHP and the Yun using the PHP bridge class.

I have got this working now by using different code but I am trying to understand an issue that cropped up this morning.

In order to populate the web form with the current settings I have first written each of them to the Bridge register using Bridge.put. As there are 6 heaters with 4 settings each (24 total) I used a “for loop” to ease the amount of code required, as follows…

#include <Bridge.h>

float Op[6] = {45, 45, 10, 10, 10, 10};
float Kp[6] = {60, 60, 60, 60, 60, 60};
float Ki[6] = {0.03125, 0.03125, 0.03125, 0.03125, 0.03125, 0.03125};
float Kd[6] = {0, 0, 0, 0, 0, 0};

char bridge[7];
int Ht = 9;

String bridger;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Bridge.begin();
  delay(10000);
  populateKeys();
    
}

void populateKeys() 
{
    for(int i = 0; i < 6; i++)
  {
    bridger = "Op";
    bridger += i;
    Bridge.put(String(bridger), String(Op[i], 0));
    Serial.print(bridger);
    Serial.print(" ");
    Serial.println(String(Op[i], 0));
    
    bridger = "Kp";
    bridger += i;
    Bridge.put(String(bridger), String(Kp[i], 0));
    Serial.print(bridger);
    Serial.print(" ");
    Serial.println(String(Kp[i], 0));
    
    bridger = "Ki";
    bridger += i;
    Bridge.put(String(bridger), String(Ki[i], 5));
    Serial.print(bridger);
    Serial.print(" ");
    Serial.println(String(Ki[i], 5));
    
    bridger = "Kd";
    bridger += i;
    Bridge.put(String(bridger), String(Kd[i], 5));
    Serial.print(bridger);
    Serial.print(" ");
    Serial.println(String(Kd[i], 5));
  }
  Ht = 9;
  Bridge.put("Ht", String("9"));
}

Now that “for loop” works and Bridge.put is quite happy with processing the loop. The issue I have found comes when trying to do the same sort of thing using Bridge.get…

If I try and use a “for loop” to read the settings, as above but with Bridge.get instead:

bridger = "Op";
bridger += i;
Bridge.get(String(bridger), bridge, 7);

I get an error of _no known conversion for argument 1 from ‘String’ to ‘const char*’ _ which I am finding quite strange as Bridge.put lets me do it this way.

As stated at the top I am very new to Arduino. I understand from the error the Bridge.get doesn’t like the fact that the key has been provided as a String and not a const char* but can’t understand why Bridge.put is happy but Bridge.get isn’t.

Any advice would be appreciated.

Regards

Andy Hodges

That depends on the method implementation. If you look at the bridge library header file you will find this

class BridgeClass {
  public:
    BridgeClass(Stream &_stream);
    void begin();

    // Methods to handle key/value datastore
    void put(const char *key, const char *value);
    void put(const String &key, const String &value)[/b]
    {
      put(key.c_str(), value.c_str());
    }
    [b]unsigned int get(const char *key, uint8_t *buff, unsigned int size);
    unsigned int get(const char *key, char *value, unsigned int maxlen)
    {
      return get(key, reinterpret_cast<uint8_t *>(value), maxlen);
    }

As you can see there is no implementation of the method get with the String parameter while there is for the put method.
To solve this problem you can pass your string as bridger.c_str()

Bridge.get(bridger.c_str(), bridge, 7);

Thanks Angelo, that worked perfectly and cut the code down by over 100 lines :o :D

I had looked at the bridge library file and noticed the differences between bridge.put and bridge.get as you have listed them but alas, my limited knowledge of C meant I didn't know the solution.

Thanks again

Andy Hodges