1
1
#include < ESP8266WiFi.h>
2
- #include < WebSocketClient.h>
2
+ #include < ESP8266WiFiMulti.h>
3
+ #include < WebSocketsClient.h>
3
4
#include < Spacebrew.h>
4
5
5
6
const char * ssid = " wifi network" ;
@@ -9,8 +10,12 @@ char host[] = "sandbox.spacebrew.cc";
9
10
char clientName[] = " esp8266" ;
10
11
char description[] = " " ;
11
12
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;
14
19
15
20
// Declare handlers for various Spacebrew events
16
21
// These get defined below!
@@ -24,93 +29,128 @@ void onRangeMessage(char *name, int value);
24
29
void setup () {
25
30
Serial.begin (115200 );
26
31
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
+
28
38
// 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) {
32
42
delay (500 );
33
43
Serial.print (" ." );
34
44
}
45
+
35
46
Serial.println (" " );
36
- Serial.println (" WiFi connected" );
47
+ Serial.println (" WiFi connected" );
37
48
Serial.println (" IP address: " );
38
49
Serial.println (WiFi.localIP ());
39
-
50
+
40
51
// 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);
44
55
45
56
// 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
+
50
61
// 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
+
58
80
// 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);
64
82
65
83
}
66
84
67
85
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
+ }
70
100
}
71
101
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) {
75
103
Serial.print (" bool: " );
76
104
Serial.print (name);
77
105
Serial.print (" :: " );
78
106
Serial.println (value);
79
- }
80
107
81
- void onStringMessage (char *name, char * message){
82
108
// 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
+ }
84
121
122
+ void onStringMessage (char *name, char * message) {
85
123
Serial.print (" string: " );
86
124
Serial.print (name);
87
125
Serial.print (" :: " );
88
126
Serial.println (message);
127
+
128
+ // repeat back whatever was sent
129
+ if (strcmp (name, " Parrot String" ) == 0 ) {
130
+ sb.send (" Parrot String" , message);
131
+ }
89
132
}
90
133
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) {
94
135
Serial.print (" range: " );
95
136
Serial.print (name);
96
137
Serial.print (" :: " );
97
138
Serial.println (value);
139
+
140
+ // repeat back whatever was sent
141
+ if (strcmp (name, " Parrot Range" ) == 0 ) {
142
+ sb.send (" Parrot Range" , value);
143
+ }
98
144
}
99
145
100
- void onOpen (){
101
- Serial.println (" Spacebrew opened" );
146
+ void onOpen () {
102
147
// send a message when we get connected!
103
- spacebrewConnection .send (" Parrot" , " Hello Spacebrew" );
148
+ sb .send (" Parrot String " , " Hello Spacebrew" );
104
149
}
105
150
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 () {
111
152
112
153
}
113
154
114
- void onError (char * message){
155
+ void onError (char * message) {
115
156
}
116
-
0 commit comments