A substitute for bridge (for lazy people)

hi!

although we have the bridge widget, but to use it, one have to edit firmware on the target device(s) too… this is not very convenient for lazy people like me :slight_smile:

so, i created 2 functions:

  • one to read any pin on any device
  • one to write any pin on any device

without specific code needed on the target(s)!

#include <ESP8266HTTPClient.h>

void APIwriteDevicePin(String token, String pin, String value)
{
  HTTPClient http;

  // http://blynk-cloud.com/auth_token/update/v14?value=HH

  String url = "http://blynk-cloud.com/";  // url
  url += token;                            // blynk token
  url += "/update/";
  url += pin;                              // pin to update
  url += "?value=";
  url += value;                            // value to write

  http.begin(url);
  http.GET();
  delay(50);
  http.end();
  delay(10);
}

String APIreadDevicePin(String token, String pin)
{
  HTTPClient http;

  String payload = "request failed";

  // http://blynk-cloud.com/auth_token/get/pin

  String url = "http://blynk-cloud.com/";
  url += token;                                  // blynk token
  url += "/get/";
  url += pin;                                    // pin to read

  http.begin(url);
  
  int httpCode = http.GET();
  delay(50);

  if (httpCode > 0) {
	  payload = http.getString();                // get response payload as String
	  
	  payload.remove(0, 2);
      payload.remove(payload.length() - 2);      // strip [""]
    }
  else payload = payload + ", httpCode: " + httpCode;

  http.end();
  delay(10);

  return payload;
}

more info at: https://techtutorialsx.com/2016/07/17/esp8266-http-get-requests/

7 Likes

Only for virtual pins… bridge.virtualWrite(vPin)… for which which you would need Blynk functions for anyhow (on the receiving device), regardless of using that devices App project or Bridge from another devices App project… or API :wink:

So, you wrote code to avoid writing code :stuck_out_tongue:

If I understand your code, it is using the API… which is probably the underling function of Bridge anyhow… reading and toggling a physical pin from a remote device… without additional code on the receiving side :wink:

yeah, but i never ever used non-virtual pins in my projects.

for example, i have a project with 20 devices, installed in different places. all of them has a temp sensor which is displayed in the app. but i need to read and process this data from each device on another mcu.

to resolve this with bridge, i would have to edit the firmware on all 20 devices. but with the above function, i just have to edit 1 firmware, put it in a for loop and thats all.

exactely!
i had to write those functions only once, than i can re-use them (only 1 line of code) as many time as needed.

OK… you have a Blynk Function on V0 that does a bunch of different things each time you press a button from it’s App… Read a sensor, run some calculations, process an if() loop based on the results and send out updates to the App and email.

But now you also want another device (with it’s own App) to activate the same Blynk Function (easily done with bridgeX.virtualWrite(V0) - bridgeX having been setup with the AUTH token of the receiving device)…

So… using your method instead of bridgeX… how can you possibly do that without having written the initial Blynk Function in the first place?

PS, not trying to dump on your code… I guess I just don’t understand it’s need when Blynk Bridge is so simple as it is.

no stress!

probably i’ve run into a special use case or didn’t explain very well the scenario.

so, i have a client, who runs a hotel. in the building and surroundings there are lots of sonoffs with custom firmware, using blynk. each sonoff does a dedicate job, some have additional sensors, etc.

now, initially, the logic was designed as each unit was self sufficient for the task, with no interaction required between them. they all have display widgets and controls in the app. they were installed 8 months ago, and are working ok.

but the guy constantly has new needs / ideas to automate. so, sometimes to implement the new logic, i have to add new units or edit the firmware of existing devices. sometimes i need to read sensors or change values (like relays, etc) on the targets.

this is when the above functions come handy: i can access the targets without touching the existing firmware on them. i do not need new functions on the targets, just need the value of a temp sensor or to remotely change the state of a button. thats all.

thus, i just have to upload ota firmware for only one unit, the target can stay as is. he even didn’t knows that its being accessed (read / write) from outside.

actually, i save the time to edit a secondary firmware, compile the bin file, upload it to the server, init the update on the device, verify if the update was succesful, etc.

maybe it is not big deal, but if you have to do this for 10-20 devices or on a monthly basis, it becomes quite boring and time consuming. thats all :slight_smile:

no, this is not the case. i just need to read or write a value on the target and process it on the new device.

Nice and clean! IMHO better than bridge :stuck_out_tongue:
PS: … waiting for a MQTT “style” bridge…

thanks. i never used mqtt, so i’m not sure how the “mqtt bridge” should look like?

More or less like yours: You ask for sensor data “basement/temperature” (for example) and you got it! Any device is able to ask for it at any time, and any device at any time can send its data/command to server (broker). Very pleasant and friendly protocol, though still I have something to learn about it.

what is the mqtt advantage / disatvantage compared to the blynk ecosystem?

Hmmm… the best is the combination of two :smiley: MQTT has no friendly mobile dashboard and it seems for me the data storage is less optimal than in Blynk (slower data access)

Hmm… well I am clearly out of my depth here programming wise. I am still not sure how you can use one of these functions on an existing and independently running script without needing to modify something on it.

But on closer look it seems more a RestfullAPI thing… of which I have never learned beyond spelling API :blush: So I guess I will STFU and keep reading :stuck_out_tongue:

I read all of these Bridge type of topics and laugh to myself at how much trouble people can have getting remote devices to talk to each other.
I really like @wanek’s simple solution to update the Blynk server with values from one device that can then be read from another. But, MQTT and Node-Red would probably be a better solution for the Hotel scenario described.

I have a Raspberry Pi running Node-Red and Mosquitto (the MQTT) server software. My individual devices (Sonoff in some cases) don’t run any Blynk software, the just have basic Wi-Fi connection and MQTT client code, plus code that tells them what to do when an MQTT message arrives. This can include sending back an MQTT message of their own.
Node-Red is the glue that binds it all together, and interfaces with Blynk (I use the cloud server), Amazon Alexa etc. It provides a web-based drag and drop graphical programming environment with the ability to write your own functions.
Most of the time, when I want to make a change or add a new feature it’s done in Node-Red and deployed by the press of a single button. There are obviously times when code on individual devices needs to be tweaked, but if your base code is well designed that happens quite infrequently.
As your hotel system up and running then it’s obviously not worth changing to an MQTT/Node-Red system, but if you were starting a similar project in future you should take a serious look at it as a possible solution.

Pete.

1 Like

as i said earlier, you can’t. but this is not the scope of those functions. probably i can not explain better.

lets take the following scenario:

you have a device with his own app and tasks, which it works ok. the device has a temp sensor.

someday, you need the value of that temp sensor on another device. how would you proceed, without touching the code on the target device?

What i can see, is the … bridge! Yes, it is based on on HTTP API (not sure if it is the recent version, or the one being on withdrawal process), but the only thing needed to be done to “ask” for some data is to implement it on SLAVE device. MASTER does not need to know about it’s existence. It’s just “serving” data

it sounds interesting. will definetly take a look.
just a question:

what happens with the mqtt system, if the internet / server connection is down? can each device work on its own?

NO! just like with blynk, it is client-server

it is.

thanks for the explanation, probably you can express yourself better :slight_smile: i’m not very good at explaining things.

i know that. but what i mean, if i program a sonoff with some basic tasks, ie, if temp is lower than x start relay, etc. and i adapt the blynk routine to be non blocking, and if the server / network is down, the sonoff can continue to do this basic stuff in offline mode. (of course will not push data to server and one can not turn on the relay from phone, but at least the very basic functionality works).

this is true for mqtt?

Yes it is. However if you program sonoff’s (or other node) behavior into the node-red, the of course NO!

BUT it is possible (to offload some tasks to node-red)