Skip to content

Commit 35e2d58

Browse files
committed
Update esp8266 example
1 parent 0f338cb commit 35e2d58

File tree

1 file changed

+89
-49
lines changed

1 file changed

+89
-49
lines changed

examples/esp8266/esp8266.ino

Lines changed: 89 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <ESP8266WiFi.h>
2-
#include <WebSocketClient.h>
2+
#include <ESP8266WiFiMulti.h>
3+
#include <WebSocketsClient.h>
34
#include <Spacebrew.h>
45

56
const char* ssid = "wifi network";
@@ -9,8 +10,12 @@ char host[] = "sandbox.spacebrew.cc";
910
char clientName[] = "esp8266";
1011
char description[] = "";
1112

12-
WiFiClient wifiClient;
13-
Spacebrew spacebrewConnection;
13+
ESP8266WiFiMulti wifi;
14+
Spacebrew sb;
15+
16+
// Update sine wave 10 times per second
17+
unsigned int waveUpdateRate = 1000.0 / 10.0;
18+
unsigned long waveTimer;
1419

1520
// Declare handlers for various Spacebrew events
1621
// These get defined below!
@@ -24,93 +29,128 @@ void onRangeMessage(char *name, int value);
2429
void setup() {
2530
Serial.begin(115200);
2631

27-
//
32+
// Set up built-in LEDs for output
33+
pinMode(0, OUTPUT);
34+
pinMode(2, OUTPUT);
35+
digitalWrite(0, LOW);
36+
digitalWrite(2, LOW);
37+
2838
// Connect to wifi network
29-
//
30-
WiFi.begin(ssid, password);
31-
while (WiFi.status() != WL_CONNECTED) {
39+
Serial.println("");
40+
wifi.addAP(ssid, password);
41+
while (wifi.run() != WL_CONNECTED) {
3242
delay(500);
3343
Serial.print(".");
3444
}
45+
3546
Serial.println("");
36-
Serial.println("WiFi connected");
47+
Serial.println("WiFi connected");
3748
Serial.println("IP address: ");
3849
Serial.println(WiFi.localIP());
39-
50+
4051
// Bind Spacebrew connection event handlers
41-
spacebrewConnection.onOpen(onOpen);
42-
spacebrewConnection.onClose(onClose);
43-
spacebrewConnection.onError(onError);
52+
sb.onOpen(onOpen);
53+
sb.onClose(onClose);
54+
sb.onError(onError);
4455

4556
// Bind Spacebrew message event handlers
46-
spacebrewConnection.onBooleanMessage(onBooleanMessage);
47-
spacebrewConnection.onStringMessage(onStringMessage);
48-
spacebrewConnection.onRangeMessage(onRangeMessage);
49-
57+
sb.onBooleanMessage(onBooleanMessage);
58+
sb.onStringMessage(onStringMessage);
59+
sb.onRangeMessage(onRangeMessage);
60+
5061
// Register publishers and subscribers
51-
spacebrewConnection.addPublish("Analog", SB_RANGE);
52-
spacebrewConnection.addPublish("Button", SB_BOOLEAN);
53-
spacebrewConnection.addPublish("Parrot", SB_STRING);
54-
spacebrewConnection.addSubscribe("Blink LED", SB_BOOLEAN);
55-
spacebrewConnection.addSubscribe("Fade LED", SB_RANGE);
56-
spacebrewConnection.addSubscribe("Parrot", SB_STRING);
57-
62+
63+
// Messages received on the Parrot subscribers will be
64+
// sent back on the Parrot publishers
65+
sb.addSubscribe("Parrot String", SB_STRING);
66+
sb.addSubscribe("Parrot Range", SB_RANGE);
67+
sb.addSubscribe("Parrot Boolean", SB_BOOLEAN);
68+
69+
sb.addPublish("Parrot String", SB_STRING);
70+
sb.addPublish("Parrot Range", SB_RANGE);
71+
sb.addPublish("Parrot Boolean", SB_BOOLEAN);
72+
73+
// Send a sine wave
74+
sb.addPublish("Sine Wave", SB_RANGE);
75+
76+
// Control the built-in LEDs
77+
sb.addSubscribe("Red LED", SB_BOOLEAN);
78+
sb.addSubscribe("Blue LED", SB_BOOLEAN);
79+
5880
// Connect to the spacebrew server
59-
spacebrewConnection.connect(&wifiClient, host, clientName, description);
60-
61-
// pinMode(digitalLedPin, OUTPUT);
62-
// pinMode(analogLedPin, OUTPUT);
63-
// pinMode(buttonPin, INPUT);
81+
sb.connect(host, clientName, description);
6482

6583
}
6684

6785
void loop() {
68-
// Put your main code here, to run repeatedly:
69-
spacebrewConnection.monitor();
86+
sb.monitor();
87+
88+
unsigned long now = millis();
89+
if (now - waveTimer > waveUpdateRate) {
90+
waveTimer = now;
91+
92+
// sin() generates values between -1 to 1
93+
float sine = sin(millis() / 1000.0);
94+
95+
// change those values to be between 0 - 1023
96+
sine = ((sine + 1) / 2) * 1023;
97+
98+
sb.send("Sine Wave", (int) sine);
99+
}
70100
}
71101

72-
void onBooleanMessage(char *name, bool value){
73-
//turn the 'digital' LED on and off based on the incoming boolean
74-
// digitalWrite(digitalLedPin, value ? HIGH : LOW);
102+
void onBooleanMessage(char *name, bool value) {
75103
Serial.print("bool: ");
76104
Serial.print(name);
77105
Serial.print(" :: ");
78106
Serial.println(value);
79-
}
80107

81-
void onStringMessage(char *name, char* message){
82108
//repeat back whatever was sent
83-
spacebrewConnection.send("Parrot", message);
109+
if (strcmp(name, "Parrot Boolean") == 0) {
110+
sb.send("Parrot Boolean", value);
111+
}
112+
113+
//turn the LEDs on and off based on the incoming boolean
114+
if (strcmp(name, "Red LED") == 0) {
115+
digitalWrite(0, value ? HIGH : LOW);
116+
}
117+
if (strcmp(name, "Blue LED") == 0) {
118+
digitalWrite(2, value ? HIGH : LOW);
119+
}
120+
}
84121

122+
void onStringMessage(char *name, char* message) {
85123
Serial.print("string: ");
86124
Serial.print(name);
87125
Serial.print(" :: ");
88126
Serial.println(message);
127+
128+
//repeat back whatever was sent
129+
if (strcmp(name, "Parrot String") == 0) {
130+
sb.send("Parrot String", message);
131+
}
89132
}
90133

91-
void onRangeMessage(char *name, int value){
92-
//use the range input to control the brightness of the 'analog' LED
93-
// analogWrite(analogLedPin, map(value, 0, 1024, 0, 255));
134+
void onRangeMessage(char *name, int value) {
94135
Serial.print("range: ");
95136
Serial.print(name);
96137
Serial.print(" :: ");
97138
Serial.println(value);
139+
140+
//repeat back whatever was sent
141+
if (strcmp(name, "Parrot Range") == 0) {
142+
sb.send("Parrot Range", value);
143+
}
98144
}
99145

100-
void onOpen(){
101-
Serial.println("Spacebrew opened");
146+
void onOpen() {
102147
//send a message when we get connected!
103-
spacebrewConnection.send("Parrot", "Hello Spacebrew");
148+
sb.send("Parrot String", "Hello Spacebrew");
104149
}
105150

106-
void onClose(){
107-
Serial.println("Spacebrew closed");
108-
//turn everything off if we get disconnected
109-
// analogWrite(analogLedPin, 0);
110-
// digitalWrite(digitalLedPin, LOW);
151+
void onClose() {
111152

112153
}
113154

114-
void onError(char* message){
155+
void onError(char* message) {
115156
}
116-

0 commit comments

Comments
 (0)