Skip to content

Commit 3870dce

Browse files
author
Jim Lindblom
committed
Merge pull request #3 from sparkfun/feat002-example_for_using_Bridge_widget_to_demo_board_to_board_actuation
Feat002 example for using bridge widget to demo board to board actuation
2 parents f0ca0e6 + f01c3df commit 3870dce

File tree

3 files changed

+331
-0
lines changed

3 files changed

+331
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/******************************************************************************
2+
Bridge_Example_1.ino
3+
Brent Wilkins @ SparkFun Electronics
4+
March 11, 2016
5+
https://github.com/sparkfun/Blynk_Board_ESP8266/Firmware
6+
This file, is example code for the SparkFun Blynk Board which demonstrates the
7+
functionality of the Bridge widget. Using two of these boards to send signals
8+
from one board to the other, or from one connected mobile device to either
9+
Blynk Board or another mobile device if available. Simple wireless buttons and
10+
a more complex chat app are part of this demo.
11+
Resources:
12+
Blynk Arduino Library: https://github.com/blynkkk/blynk-library/releases/tag/v0.3.3
13+
License:
14+
This is released under the MIT license (http://opensource.org/licenses/MIT).
15+
Please see the included LICENSE.md for more information.
16+
Development environment specifics:
17+
Arduino IDE 1.6.7
18+
SparkFun BlynkBoard - ESP8266
19+
******************************************************************************/
20+
21+
// Comment next line out to disable serial prints and save space
22+
#define BLYNK_PRINT Serial // Enables Serial Monitor MUST be before #includes...
23+
#include <ESP8266WiFi.h>
24+
#include <BlynkSimpleEsp8266.h>
25+
26+
char auth[] = "LocalAuthToken"; // Put your Auth Token here
27+
char remoteAuth[] = "RemoteAuthToken"; // Auth token of bridged device
28+
29+
// Physical Pins:
30+
#define BUTTON_PIN 0 // GPIO0
31+
#define LED_PIN 5 // GPIO5
32+
33+
// Virtual Pins:
34+
#define TERMINAL 0 // V0
35+
#define LOCAL_LED_BUTTON 1 // V1
36+
#define REMOTE_LED_BUTTON 2 // V2
37+
#define TERMINAL_RECEIVE 3 // V3
38+
#define BRIDGE 4 // V4
39+
#define LOCAL_LED_RECIEVE 5 // V5
40+
#define REMOTE_LED_STATUS_UPDATE 6 // V6
41+
42+
volatile bool wasButtonPressed = false;
43+
bool ledState = LOW;
44+
bool remoteLedState = LOW;
45+
46+
// Attach virtual serial terminal to TERMINAL Virtual Pin
47+
WidgetTerminal terminal(TERMINAL);
48+
49+
// Configure bridge on LOCAL_BRIDGE virtual pin
50+
WidgetBridge bridge(BRIDGE);
51+
52+
void setup()
53+
{
54+
Serial.begin(9600); // See the connection status in Serial Monitor
55+
56+
// Here your Arduino connects to the Blynk Cloud.
57+
Blynk.begin(auth, "SSID", "PASSWORD");
58+
59+
// TODO: Set defaults after weird state changes
60+
// Wait until connected
61+
while (Blynk.connect() == false);
62+
63+
bridge.setAuthToken(remoteAuth);
64+
65+
// Setup the onboard button
66+
pinMode(BUTTON_PIN, INPUT_PULLUP);
67+
// Attach BUTTON_PIN interrupt to our handler
68+
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), onButtonPress, FALLING);
69+
70+
// Setup the onboard blue LED
71+
pinMode(LED_PIN, OUTPUT);
72+
73+
// This will print Blynk Software version to the Terminal Widget when
74+
// your hardware gets connected to Blynk Server
75+
terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
76+
terminal.println("-------------");
77+
terminal.println("Start chatting!");
78+
terminal.flush();
79+
}
80+
81+
// Interrupt service routine to capture button press event
82+
void onButtonPress()
83+
{
84+
// Invert state, since button is "Active LOW"
85+
wasButtonPressed = !digitalRead(BUTTON_PIN);
86+
}
87+
88+
// Virtual button on connected app was used to change local LED.
89+
// Update LED, and tell remote devices about it.
90+
BLYNK_WRITE(LOCAL_LED_BUTTON)
91+
{
92+
ledState = param.asInt(); // Update local state to match app state
93+
BLYNK_LOG("LED state is now %s", ledState ? "HIGH" : "LOW");
94+
digitalWrite(LED_PIN, ledState); // Set state of virtual button to LED
95+
// Send updated status to remote board.
96+
bridge.virtualWrite(REMOTE_LED_STATUS_UPDATE, ledState);
97+
}
98+
99+
// Virtual button on connected app was used to change remote LED.
100+
// Tell remote devices about the change.
101+
BLYNK_WRITE(REMOTE_LED_BUTTON)
102+
{
103+
remoteLedState = param.asInt(); // Update state with info from remote
104+
BLYNK_LOG("New remote LED state: %s", param.asInt() ? "HIGH" : "LOW");
105+
// Send updated status to remote board.
106+
bridge.virtualWrite(LOCAL_LED_RECIEVE, remoteLedState);
107+
}
108+
109+
// You can send commands from Terminal to your hardware. Just use
110+
// the same Virtual Pin as your Terminal Widget
111+
BLYNK_WRITE(TERMINAL)
112+
{
113+
// Send from this terminal to remote terminal
114+
bridge.virtualWrite(TERMINAL_RECEIVE, param.asStr());
115+
}
116+
117+
// Receive a string on LOCAL_RECEIVE and write that value to the connected
118+
// phone's terminal
119+
BLYNK_WRITE(TERMINAL_RECEIVE)
120+
{
121+
terminal.println(param.asStr()); // Write received string to local terminal
122+
terminal.flush();
123+
}
124+
125+
// Remote device triggered an LED change. Update the LED and app status.
126+
BLYNK_WRITE(LOCAL_LED_RECIEVE)
127+
{
128+
// This can be seen in the Serial Monitor
129+
BLYNK_LOG("Remote device triggered LED change");
130+
131+
ledState = param.asInt();
132+
// Turn on LED
133+
digitalWrite(LED_PIN, ledState);
134+
// Set state of virtual button to match remote LED
135+
Blynk.virtualWrite(LOCAL_LED_BUTTON, ledState);
136+
}
137+
138+
// Remote LED status changed. Show this in the app.
139+
BLYNK_WRITE(REMOTE_LED_STATUS_UPDATE)
140+
{
141+
remoteLedState = param.asInt();
142+
Blynk.virtualWrite(REMOTE_LED_BUTTON, remoteLedState);
143+
}
144+
145+
void loop()
146+
{
147+
Blynk.run(); // All the Blynk Magic happens here...
148+
149+
if (wasButtonPressed) {
150+
// This can be seen in the Serial Monitor
151+
BLYNK_LOG("Physical button was pressed.");
152+
// Toggle state
153+
remoteLedState ^= HIGH;
154+
// Send new state to remote board LED
155+
bridge.virtualWrite(LOCAL_LED_RECIEVE, remoteLedState);
156+
// Update state of button on local mobile app
157+
Blynk.virtualWrite(REMOTE_LED_BUTTON, remoteLedState);
158+
// Clear state variable set in ISR
159+
wasButtonPressed = false;
160+
}
161+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/******************************************************************************
2+
Bridge_Example_2.ino
3+
Brent Wilkins @ SparkFun Electronics
4+
March 11, 2016
5+
https://github.com/sparkfun/Blynk_Board_ESP8266/Firmware
6+
This file, is example code for the SparkFun Blynk Board which demonstrates the
7+
functionality of the Bridge widget. Using two of these boards to send signals
8+
from one board to the other, or from one connected mobile device to either
9+
Blynk Board or another mobile device if available. Simple wireless buttons and
10+
a more complex chat app are part of this demo.
11+
Resources:
12+
Blynk Arduino Library: https://github.com/blynkkk/blynk-library/releases/tag/v0.3.3
13+
License:
14+
This is released under the MIT license (http://opensource.org/licenses/MIT).
15+
Please see the included LICENSE.md for more information.
16+
Development environment specifics:
17+
Arduino IDE 1.6.7
18+
SparkFun BlynkBoard - ESP8266
19+
******************************************************************************/
20+
21+
// Comment next line out to disable serial prints and save space
22+
#define BLYNK_PRINT Serial // Enables Serial Monitor MUST be before #includes...
23+
#include <ESP8266WiFi.h>
24+
#include <BlynkSimpleEsp8266.h>
25+
26+
char auth[] = "LocalAuthToken"; // Put your Auth Token here
27+
char remoteAuth[] = "RemoteAuthToken"; // Auth token of bridged device
28+
29+
// Physical Pins:
30+
#define BUTTON_PIN 0 // GPIO0
31+
#define LED_PIN 5 // GPIO5
32+
33+
// Virtual Pins:
34+
#define TERMINAL 0 // V0
35+
#define LOCAL_LED_BUTTON 1 // V1
36+
#define REMOTE_LED_BUTTON 2 // V2
37+
#define TERMINAL_RECEIVE 3 // V3
38+
#define BRIDGE 4 // V4
39+
#define LOCAL_LED_RECIEVE 5 // V5
40+
#define REMOTE_LED_STATUS_UPDATE 6 // V6
41+
42+
volatile bool wasButtonPressed = false;
43+
bool ledState = LOW;
44+
bool remoteLedState = LOW;
45+
46+
// Attach virtual serial terminal to TERMINAL Virtual Pin
47+
WidgetTerminal terminal(TERMINAL);
48+
49+
// Configure bridge on LOCAL_BRIDGE virtual pin
50+
WidgetBridge bridge(BRIDGE);
51+
52+
void setup()
53+
{
54+
Serial.begin(9600); // See the connection status in Serial Monitor
55+
56+
// Here your Arduino connects to the Blynk Cloud.
57+
Blynk.begin(auth, "SSID", "PASSWORD");
58+
59+
// TODO: Set defaults after weird state changes
60+
// Wait until connected
61+
while (Blynk.connect() == false);
62+
63+
bridge.setAuthToken(remoteAuth);
64+
65+
// Setup the onboard button
66+
pinMode(BUTTON_PIN, INPUT_PULLUP);
67+
// Attach BUTTON_PIN interrupt to our handler
68+
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), onButtonPress, FALLING);
69+
70+
// Setup the onboard blue LED
71+
pinMode(LED_PIN, OUTPUT);
72+
73+
// This will print Blynk Software version to the Terminal Widget when
74+
// your hardware gets connected to Blynk Server
75+
terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
76+
terminal.println("-------------");
77+
terminal.println("Start chatting!");
78+
terminal.flush();
79+
}
80+
81+
// Interrupt service routine to capture button press event
82+
void onButtonPress()
83+
{
84+
// Invert state, since button is "Active LOW"
85+
wasButtonPressed = !digitalRead(BUTTON_PIN);
86+
}
87+
88+
// Virtual button on connected app was used to change local LED.
89+
// Update LED, and tell remote devices about it.
90+
BLYNK_WRITE(LOCAL_LED_BUTTON)
91+
{
92+
ledState = param.asInt(); // Update local state to match app state
93+
BLYNK_LOG("LED state is now %s", ledState ? "HIGH" : "LOW");
94+
digitalWrite(LED_PIN, ledState); // Set state of virtual button to LED
95+
// Send updated status to remote board.
96+
bridge.virtualWrite(REMOTE_LED_STATUS_UPDATE, ledState);
97+
}
98+
99+
// Virtual button on connected app was used to change remote LED.
100+
// Tell remote devices about the change.
101+
BLYNK_WRITE(REMOTE_LED_BUTTON)
102+
{
103+
remoteLedState = param.asInt(); // Update state with info from remote
104+
BLYNK_LOG("New remote LED state: %s", param.asInt() ? "HIGH" : "LOW");
105+
// Send updated status to remote board.
106+
bridge.virtualWrite(LOCAL_LED_RECIEVE, remoteLedState);
107+
}
108+
109+
// You can send commands from Terminal to your hardware. Just use
110+
// the same Virtual Pin as your Terminal Widget
111+
BLYNK_WRITE(TERMINAL)
112+
{
113+
// Send from this terminal to remote terminal
114+
bridge.virtualWrite(TERMINAL_RECEIVE, param.asStr());
115+
}
116+
117+
// Receive a string on LOCAL_RECEIVE and write that value to the connected
118+
// phone's terminal
119+
BLYNK_WRITE(TERMINAL_RECEIVE)
120+
{
121+
terminal.println(param.asStr()); // Write received string to local terminal
122+
terminal.flush();
123+
}
124+
125+
// Remote device triggered an LED change. Update the LED and app status.
126+
BLYNK_WRITE(LOCAL_LED_RECIEVE)
127+
{
128+
// This can be seen in the Serial Monitor
129+
BLYNK_LOG("Remote device triggered LED change");
130+
131+
ledState = param.asInt();
132+
// Turn on LED
133+
digitalWrite(LED_PIN, ledState);
134+
// Set state of virtual button to match remote LED
135+
Blynk.virtualWrite(LOCAL_LED_BUTTON, ledState);
136+
}
137+
138+
// Remote LED status changed. Show this in the app.
139+
BLYNK_WRITE(REMOTE_LED_STATUS_UPDATE)
140+
{
141+
remoteLedState = param.asInt();
142+
Blynk.virtualWrite(REMOTE_LED_BUTTON, remoteLedState);
143+
}
144+
145+
void loop()
146+
{
147+
Blynk.run(); // All the Blynk Magic happens here...
148+
149+
if (wasButtonPressed) {
150+
// This can be seen in the Serial Monitor
151+
BLYNK_LOG("Physical button was pressed.");
152+
// Toggle state
153+
remoteLedState ^= HIGH;
154+
// Send new state to remote board LED
155+
bridge.virtualWrite(LOCAL_LED_RECIEVE, remoteLedState);
156+
// Update state of button on local mobile app
157+
Blynk.virtualWrite(REMOTE_LED_BUTTON, remoteLedState);
158+
// Clear state variable set in ISR
159+
wasButtonPressed = false;
160+
}
161+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
These two example sketches are nearly identical. The key difference is the
2+
authentication tokens will be swapped in the two. This example code for the
3+
SparkFun Blynk Board demonstrates the functionality of the Bridge widget.
4+
Using two of these boards to send signals from one board to the other, or from
5+
one connected mobile device to either Blynk Board or another mobile device if
6+
available. Simple wireless buttons and a more complex chat app are part of this
7+
demo.
8+
9+
For more information please see the [tutorial](https://learn.sparkfun.com/tutorials/blynk-board-bridge-widget-demo/) on the SparkFun website.

0 commit comments

Comments
 (0)