Particle - control with Blynk
How to Control a Particle Device with Blynk
Hardware
Firmware
/*
Project blynk_to_particle.io
Author: Mark W Kiehl / Mechatronic Solutions LLC
Date: April 2023
Respond to a remote Blynk request to turn on or off the
blue LED on D7. An HTTP POST to the Particle API calls
a Particle function that responds to an argument with
a value of 'on' or 'off' corresponding to what should
happen to the LED.
Returns the value 1 if the LED has been turned on,
and 0 if turned off, -1 if an error.
Hardware:
Particle Boron 404x
Software:
Standard Particle firmware and this script.
*/
#include "Particle.h"
const char* firmware_version = "0.0.0";
uint8_t led_state = LOW;
uint8_t led_state_last = LOW;
// Register the Particle cloud function
int blynkLED(String on_or_off);
void setup() {
pinMode(D7, OUTPUT);
digitalWrite(D7, HIGH);
Serial.begin(9600);
waitFor(Serial.isConnected, 30000);
delay(1000);
Serial.printlnf("Device OS v%s", System.version().c_str());
Serial.printlnf("Free RAM %lu bytes", System.freeMemory());
Serial.printlnf("Firmware version v%s", firmware_version);
// register the Particle cloud function (funcKey, funcName)
Particle.function("blynk_led", blynkLED);
randomSeed(millis());
digitalWrite(D7, LOW);
Serial.println("Setup complete");
} // setup()
void loop() {
digitalWrite(D7, led_state);
} // loop()
int blynkLED(String on_off) {
// Custom Particle cloud function that changes the state of the built-in LED
// on D7 in response to an instruction from Blynk calling this
// custom cloud function.
// Returns the value 1 if the LED has been turned on, and 0 if turned off,
// -1 if an unexpected on_off value is received.
// Cloud functions must return int and take one String argument
// curl https://api.particle.io/v1/devices/{your 25 char device id}/blynk_led
// -d access_token={your 40 char access token}
// -d "args=on/off"
if (on_off == "on" || on_off == "1") {
led_state_last = led_state;
led_state = HIGH;
return 1;
} else if (on_off == "off" || on_off == "0") {
led_state_last = led_state;
led_state = LOW;
return 0;
} else {
Serial.print("Unexpected on_off value of: '"); Serial.print(on_off); Serial.println("'");
}
return -1;
}Particle API





Blynk Datastreams
Blynk Datastream Definitions:
Virtual Pin
Name
Data Type
Description
Blynk Device Template


Blynk Web Dashboard

Blynk Mobile App
Blynk Device Activation
Blynk Webhook


Full System Test
Wrap Up
Last updated
Was this helpful?







