[SOLVED] Get Value to Switch a Load

Hi everyone,
can I send and get values at the same sketch? I´m trying to get a value to switch a relay and at same time send values to ubidots. I have been having many problems to do this, so is there anybody that could help me?
thanks.

Hello @Marcelo
Yes you can!
By the way, we could help you if you post here your code :smiley:

Best regards,
Metavix

Teste_Esp12.ino (6.5 KB)

Hi Metavix,
i´m using this sketch from ACROBOTIC, working very well by the way, send every values to ubidots. In my project, i will put on a switch to command a relay into my greenhouse. Here is my problem, i don´t know how to write my sketch to get the value from ubidots to command this relay. Can you help me to write this code?
Best Regards

@Marcelo use this funcion in your sketch.

float getValue(char* id){
  float num;
  String response;
  uint8_t bodyPosinit;
  uint8_t bodyPosend;
  if (client.connect("things.ubidots.com", 80)){
        Serial.println(F("Geting your variable"));
        // Make a HTTP request:
        client.print(F("GET /api/v1.6/variables/"));
        client.print(id);
        client.println(F("/values?page_size=1 HTTP/1.1"));
        client.println(F("Host: things.ubidots.com"));
        client.println(F("User-Agent: NodeMCU/1.0")); 
        client.print(F("X-Auth-Token: "));
        client.println(TOKEN);
        client.println(F("Connection: close"));
        client.println();
    }
    int timeout = 0;
    while (!client.available() && timeout < 5000) {
        timeout++;
    }
    while (client.available()){
        response = client.readString();
    }
    Serial.println(response);
    bodyPosinit = 4 + response.indexOf("\r\n\r\n");
    response = response.substring(bodyPosinit);
    Serial.println(response);
    bodyPosinit = 9 + response.indexOf("\"value\":");
    bodyPosend = response.indexOf(", \"timestamp\"");
    num = response.substring(bodyPosinit,bodyPosend).toFloat();
    client.flush();
    client.stop();
    return num;
}

Please before to use this function change TOKEN for your personal token
Best regards,
Metavix

Hi Metavix,
I succeed in putting the devices to work!!! Now i´m getting and sending values to ubidots well. I have one more question: Do you know if it´s possible increase the time to send and get the values?

@Marcelo
to be faster? well with ESP is very slower. I think that it max speed.

Best regards,
Metavix

what dose it mean

bodyPosinit = 4 + response.indexOf("\r\n\r\n");
bodyPosinit = 9 + response.indexOf("\"value\":");
bodyPosend = response.indexOf(", \"timestamp\"");

what should i change this to get value of timestamp

bodyPosinit = 4 + response.indexOf("\r\n\r\n");
bodyPosinit = 9 + response.indexOf("\"timestamp\":");
bodyPosend = response.indexOf(", \"context\"");

what are the numbers means bodyPosinit = 4 + response.indexOf ?

regards
Juma

@Juma that is to parse the response of the server, it send you a byte every time when you call “serial.read()” function.

Best regards,
Metavix

what changes should I make so i may phrase timestamp ?

regards
Juma

@Juma do you need timestamp of the variable? Well i need to change the library for that if you want it i can do it

Best regards,
Metavix

Yes please i will be so much happy as i am unable to phrase the time stamp in .h file (C noop).
Its good as for other side to see if the sender went off line ( or micro controller got board :).

Juma

@Juma
Okay i will do that i think that it will be done for tomorrow.

Best regards,
Metavix

Thanks a lot Metavix.

Regards
Juma