Particle Pushing data to Blynk

Particle.publish() & Webhook

You can push data from your Boron/Argon/Electron/Photon to the Blynk Cloud by using the particle.publish() function and a Webhook.   This is the most secure, and data efficent method.  

Update Datastream Value

You can update the value of one or more Blynk datastreams with a single HTTP GET.   The GET request format for a non-location datastream (virtual pin) is:


https://{server_address}/external/api/update?token={token}&{pin}={value}

You may append additional '&{pin}={value}' arguments to the GET to update additional datastreams.   All datastreams updated with this GET will have the same timestamp.  

The {server_address} can be found here: HTTPS API server addresses.   The {token} is your device 'BLYNK_AUTH_TOKEN' (see Blynk.Console Search -> Devices -> Device Info).  

 

For a location datastream, the HTTP GET is formatted as follows:


https://{server_address}/external/api/update?token={token}&dataStreamId={id}&value=lon&value=lat
HOWEVER: It is impossible to create a Particle webhook with the duplicate query parameter required for latitude & longitude (new Blynk API format).   The second duplicate query parameter will be automatically deleted in both the Particle console, and from the CLI.   But, you can post location datastream data using the old API format of:   &V7=-74.123,40.321

https://ny3.blynk.cloud/external/api/batch/update?token=AbcdefgHIJklmnopqrstuvWxyZabcdeF&V7=-74.123,40.321

 

The example that follows will show how to send data for the data types of floating point, integer, string (character array), and location (GPS latitude & longitude) from a Particle device to a webhook that will then pass the data to the Particle cloud.   This webhook is valid for the Blynk product/service known as 'Blynk IoT' released in 2021.  

Particle Boron/Argon/Electron/Photon Code

#include "Particle.h"

void BuildRandomChar(char *arr, int len){
  byte j = 0;
  for (int i=0; i<len-1; i++) {
    switch (j){
      case 0:
        arr[i] = char(random(65,91));
        j = 1;
        break;
      case 1:
        arr[i] = char(random(97,123));
        j = 2;
        break;
      default:
        arr[i] = char(random(48,58));
        j = 0;
        break;
    } // switch
  }
  arr[len-1] = 0x00;
} // BuildRandomChar()


String sGetRandomStr(int length) {
  String sStr;
  for (int i=0; i<length; i=i+2) {
    sStr += char(random(97,123));
    sStr += char(random(65,91));
  }
  return sStr;
} // sGetRandomStr()


float fGetLgSignedRandFloat() {
  uint16_t myLong = random(100000000, 999999999);
  uint32_t divisor = 10;
  uint16_t expnt = random(2,6);
  switch (expnt) {
    case 2:
      divisor = 100;
      break;
    case 3:
      divisor = 1000;
      break;
    case 4:
      divisor = 10000;
      break;
    case 5:
      divisor = 100000;
      break;
    default:
      divisor = 10;
      break;
  }
  float f = (float)myLong / (float)divisor;
  int sign = random(10);
  if (sign <= 5) {
    f = f * (-1.0);
  }
  return f;
} // fGetLgSignedRandFloat()


int16_t iGetRandSignedInt() {
  //int  (2 bytes)
  //  signed -32768 to 32767
  //  unsigned  (uint8_t):   
  int16_t myInt = random(32767);
  uint16_t sign = random(10);
  if (sign <= 5) {
    myInt = myInt * (-1);
  }
  return myInt;
}

const uint32_t TIMER_INTERVAL_MS = 60000;
uint32_t last_publish_ms = 0;
float gps_offset = 0.0;

void publishTimer() {

  if (last_publish_ms > millis())  last_publish_ms = millis();
  if (millis() - last_publish_ms >= TIMER_INTERVAL_MS) {
    digitalWrite(D7, HIGH);
    char data[90]; // See Serial.printlnf() for exact size. 
    float f = fGetLgSignedRandFloat();
    int16_t i = iGetRandSignedInt();
    char c[5]; BuildRandomChar(c,5);
    float lat = 40.7254182 + gps_offset; 
    float lon = -73.9879909 + gps_offset;
    gps_offset = gps_offset + 0.01;
    snprintf(data, sizeof(data), "{\"f\":%f,\"i\":%d,\"c\":\"%s\",\"lat\":%f,\"lon\":%f}", f, i, c, lat, lon);
    Serial.printlnf("sending request '%s' with size of %u bytes", data, strlen(data));
    bool pub_result = Particle.publish("blynk_set_datastream_vals", data, PRIVATE);
   if (pub_result){
      digitalWrite(D7, LOW);
    } else {
      Serial.println("ERROR: Particle.publish()");
    }
    Log.info("published %s", data);
    last_publish_ms = millis();
  }

} // publishTimer()


void setup() {

  pinMode(D7, OUTPUT);
  digitalWrite(D7, LOW);

  Serial.begin(9600);
  waitFor(Serial.isConnected, 30000);
  delay(1000);
  Serial.printlnf("System version: %s", System.version().c_str());
  Serial.printlnf("Free RAM %lu", System.freeMemory());


  randomSeed(millis());
} // setup()


void loop() {

  publishTimer();

} // loop()

 

Particle Webhook

 

Blynk.Console (dashboard)

 

 


Do you need help developing or customizing a IoT product for your needs?   Send me an email requesting a free one hour phone / web share consultation.  

 

The information presented on this website is for the author's use only.   Use of this information by anyone other than the author is offered as guidelines and non-professional advice only.   No liability is assumed by the author or this web site.