forked from mshulman/signalK-fusion-volume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fusion_volume_control.ino
154 lines (121 loc) · 4.04 KB
/
Fusion_volume_control.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
25-Jul-2021
Subscribe to the SignalK server and get the volume control settings
for all zones
Eventually, display the volume on a local display, and allow changing
the volume with a rotary encoder
*/
#include <ArduinoHttpClient.h>
#include <WiFi101.h>
#include <ArduinoJson.h>
#include "arduino_secrets.h"
#include <Regexp.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// WiFi Settings ///////
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
char serverAddress[] = "192.168.1.250"; // server address
int port = 3000;
WiFiClient wifi;
WebSocketClient client = WebSocketClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
int count = 0;
void setUpSubscription() {
Serial.println("Setting up subscription");
client.begin("/signalk/v1/stream?subscribe=none");
while (!client.connected()) {
printDots();
delay(500);
; // wait to connect
}
Serial.println("connected");
client.beginMessage(TYPE_TEXT);
client.print("{\"context\":\"vessels.self\",\"subscribe\": [{\"path\": \"entertainment.device.fusion1.output.*\"}]}");
client.endMessage();
}
void setup() {
WiFi.setPins(8,7,4,2);
Serial.begin(9600);
Serial.println("Starting...");
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
Serial.print(". Password: ");
Serial.println(pass);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
if (status != WL_CONNECTED) {
delay(1000); // wait a bit and try again
}
}
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void printVolumeUpdates(String message) {
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, message);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
const char* context = doc["context"];
JsonObject updates_0 = doc["updates"][0];
JsonObject updates_0_source = updates_0["source"];
const char* updates_0_source_label = updates_0_source["label"]; // "fusion"
const char* updates_0_source_type = updates_0_source["type"]; // "NMEA2000"
long updates_0_source_pgn = updates_0_source["pgn"]; // 130820
const char* updates_0_source_src = updates_0_source["src"]; // "10"
const char* updates_0__source = updates_0["$source"]; // "fusion.10"
const char* updates_0_timestamp = updates_0["timestamp"]; // "2017-04-15T18:24:26.782Z"
const char* path = updates_0["values"][0]["path"];
int volume = updates_0["values"][0]["value"]; // 10
Serial.println();
char* completePath = const_cast<char*>(path);
MatchState ms;
ms.Target(completePath);
char result = ms.Match ("zone.");
if (result > 0) {
String zone = String(completePath).substring(ms.MatchStart, ms.MatchStart + ms.MatchLength);
Serial.print("Zone: ");
Serial.println(zone);
} else {
Serial.println ("No match.");
}
Serial.print("volume: ");
Serial.println(volume);
}
static void printDots() {
if (count > 50) {
Serial.println(".");
count = 0;
} else {
Serial.print(".");
count = count + 1;
}
}
void loop() {
setUpSubscription();
while (client.connected()) {
// check if a message is available to be received
int messageSize = client.parseMessage();
if (messageSize > 0) {
const String message = client.readString();
// look for entertainment.device.fusion1.output.zone1.volume.master
if (message.indexOf("volume") > -1) {
printVolumeUpdates(message);
} else {
printDots();
}
}
// wait 100 msec
delay(100);
}
Serial.println("client disconnected");
}