Communicating two devices with different tokens

So one thing you’ve not clarified is the criteria that you used in your automation. Was it “has changed”?

Are the values shown in your temperature gauge changing, and do they match those on the device that is taking the readings?

Is this screenshot of your AQUEASY VER2 dashboard?

I’ve bumped-up your privileges, on the forum so that limit shouldn’t apply now.

Pete.

thank you pete.

yes, they matched. I used ‘for any’ instead of ‘has changed’ . they synchvery well in live readings. switches are too, they can be used in one dashboard even when they are in a separate device.

The only problem was as I talked about earlier, it does not seem to save data history to each other. I checked the enabled data history thoroughly btw

I’ve used multiple automations for sensor values. Liked basically I have forwarded data to each of them to synch the values.


and this are the datastream pins. They have the same ones for each device like I have mentioned earlier.



this is my dashboard, both of devices’ dashboard are basically the same

Are you sure about this?

I’ve just done a very quick test with a rapidly changing datastream and the automation only updates the target device once every minute.

Pete.

You can change the limit for it to update per 1 second. That’s more than enough.

Maybe you would be better using the HTTP(S) API. Here’s an ESP32 example…

If you want to send multiple values from one device to another then I’d suggest that you don’t keep calling the api_bridge() function multiple times in rapid succession but instead modify it to use the batch update API…

and update multiple virtual pins at the same time.

Pete.

thank you sir. I’m gonna try this and get back to you.

So I’ve been testing it to send data to the other device.

#define BLYNK_PRINT Serial

#define BLYNK_TEMPLATE_ID "TMPL6AZyxyTL4"
#define BLYNK_TEMPLATE_NAME "HTTPS API TESTING"
#define BLYNK_AUTH_TOKEN "ks03Jtf3cctERNTZnu8ian2FNy2****"

#include <BlynkSimpleEsp32_SSL.h>
#include <HTTPClient.h>

char auth[] = "BLYNK_AUTH_TOKEN"; // Auth token for this device
char ssid[] = "TP-Link_Extender";
char pass[] = "";

String server_name = "http://sgp1.blynk.cloud/external/api/"; // <<< SEE COMMENTS

String bridge_token_1 = "hleI-MTUdjZQOGpFs3WLWRWjc-*****"; // token for the receiving device

float temperature = 10.0; // Used for testing, will be incremented in the "push_some_data" function

BlynkTimer timer;

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(10000L, push_some_data);
}

void loop()
{
  Blynk.run();
  timer.run();
}


void push_some_data()
{
  api_bridge(bridge_token_1,0,temperature); // Token for receiving device, virtual pin number, value to send

  // Increment the temperature value to simulate changing data...  
  temperature = temperature + 0.3;
  if (temperature >= 50)
  {
    temperature = 10.0;
  }
}


void api_bridge(String token, int virtual_pin, float value_to_send)
{
  HTTPClient http;

  String server_path = server_name + "update?token=" + token + "&pin=v" + String(virtual_pin) + "&value=" +  float(value_to_send);

  // Your Domain name with URL path or IP address with path
  http.begin(server_path.c_str());
  
  // Send HTTP GET request
  Serial.print("Sending ");
  Serial.print(value_to_send);
  Serial.print(" to pin V");
  Serial.println(virtual_pin); 
  
  long request_time = millis();
  int httpResponseCode = http.GET();
  
  if (httpResponseCode>0)
  {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    String payload = http.getString();
  }
  else
  {
    Serial.print("Error code: ");
    Serial.print(httpResponseCode);
    Serial.print(" <-----------------------------------");    
  }
 
  Serial.print("Response time = ");
  Serial.print(millis() - request_time);
  Serial.println(" milliseconds");
  Serial.println(); 
  
  // Free resources
  http.end();
}

this is the code I used and this is the result I get.

[381219] Connecting to blynk.cloud:443
[382167] Certificate OK
[382212] Invalid auth token

I am not very familiar yet with this method and a beginner when it comes to coding and this method is kind of complicated. So I don’t know what went wrong. Can you recommend what went wrong??

I particularly have a hard time getting this one.

void push_some_data()
{
  api_bridge(bridge_token_1,0,temperature); // Token for receiving device, virtual pin number, value to send

  // Increment the temperature value to simulate changing data...  
  temperature = temperature + 0.3;
  if (temperature >= 50)
  {
    temperature = 10.0;
  }
}


void api_bridge(String token, int virtual_pin, float value_to_send)
{
  HTTPClient http;

  String server_path = server_name + "update?token=" + token + "&pin=v" + String(virtual_pin) + "&value=" +  float(value_to_send);

  // Your Domain name with URL path or IP address with path
  http.begin(server_path.c_str());
  
  // Send HTTP GET request
  Serial.print("Sending ");
  Serial.print(value_to_send);
  Serial.print(" to pin V");
  Serial.println(virtual_pin); 
  
  long request_time = millis();
  int httpResponseCode = http.GET();
  
  if (httpResponseCode>0)
  {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    String payload = http.getString();
  }
  else
  {
    Serial.print("Error code: ");
    Serial.print(httpResponseCode);
    Serial.print(" <-----------------------------------");    
  }
 
  Serial.print("Response time = ");
  Serial.print(millis() - request_time);
  Serial.println(" milliseconds");
  Serial.println(); 
  
  // Free resources
  http.end();
}

Thank you for bearing with me.

This is telling you that this…

Isn’t a valid Blynk Auth token.

This is code to generate test data to send from one device to another. It is called by a timer and increments the variable temperature from 10 to 50 in 0.3° increments and sends it via the API to the receiving device.

The void api_bridge() function is the code that calls the API to send the data from the sending device (the one running this code) to the receiving device.

Pete.

Thank You, Pete.

Additional question is…

Do I still need to add codes for the receiving ESP or it will only run directly to Blynk?

I made it work.

#define BLYNK_TEMPLATE_ID "TMPL6-kTjpHlS"
#define BLYNK_TEMPLATE_NAME "AquaEasy Monitoring"
#define BLYNK_AUTH_TOKEN "n_nR6fFi1MIEU5bvtcOj1VE******"


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32_SSL.h>
#include <HTTPClient.h>

char auth[] = BLYNK_AUTH_TOKEN; // Auth token for this device
char ssid[] = "TP-Link_Extender";
char pass[] = "";

i just removed the " " in the

char auth[] = BLYNK_AUTH_TOKEN; // Auth token for this device

I will just need to figure out how to send multiple datastreams again.

Thank you, Pete.

It really depends exactly what you are trying to achieve.
The data will be pushed to the Blynk server whether or not you have the second device connected and online, so in theory no actual hardware is actually needed for the second device. But if that is the case, then why is the second device needed at all?

Most people take an approach of having multiple “slave” devices sending their data to a “master” device. This allows the data from all of these various slaves to be displayed on one main master dashboard.
You may wish to have widgets on the master dashboard that are intended to control pins on your slave devices - to activate relays for example, and in that case you would need your master device to send commands back to the slave device(s) also via the API (although you could potentially use automations for that).

Without having a better understanding of what you’re trying to achieve with your setup it’s unclear what the best approach would be in your situation.

If you have lots of slave devices then it may be simpler to move towards using MQTT and Node-Red to handle all of the master/slave communication via MQTT messaging, rather than via Blynk, just using Blynk for the dashboard. This is the approach I take in my home automation projects where I typically have 30+ devices that talk to Node-Red, then Node-Red acting as a single Blynk device with its associated dashboard.

Pete.

I have been using two esp32s because I have to separate two sensors because one causes signal interference to the other one if connected to the same ESP32. Particularly the Dissolved Oxygen Level Sensor and Ph level sensor. So we have gone to this route. I actually have been reading about node-red but we have invested this much to using the blynk iot system and have decided to stick with it for now.

Thank you for lots of help, Pete.

I’m gonna continue the project and update you about the progress sooner or later.

Sounds like you need to look at some sort of screening, probably using screened cable on the wiring runs.

I wasn’t suggesting that you stop using Blynk IoT, just using Node-Red and MQTT as your logic engine and Blynk as your UI. But, if you only have two devices then it’s probably not worth the effort.

Pete.

Sounds like you need to look at some sort of screening, probably using screened cable on the wiring runs.<

We actually tried it, and place them as far away to each other as possible but a lot of articles on internet recommends that we use some kind of signal isolator thing which cost a lot more than an esp32. I think they just don’t work well together in a common ground.

So here’s an update. I tried to send multiple data streams using the example you have given me and it actually fixed the problem I have been dealing with which is saving and displaying data history on a chart.

The issue is that calling the API multiple times in quick succession to send multiple datastreams, then repeating this again a few seconds later isn’t the best practice and could also result in you hitting the maximum number of API calls in a 24 hour period.

Blynk has an API to send multiple datastreams in a single API call, and I linked you to that in an earlier post.
You should look at using this API instead, by modifying the code to allow multiple pins and multiple pin values to be passed to the api_bridge function, and modifying the server_pathto include the batch keyword.

Pete.