I have created a simple Web socket application using Spring boot that's running on my local network on 192.168.1.50:8080
(I've enabled CORS).
You can see the Spring boot application code here.
The server is waiting for a call from the URL /arduino/api/v1/getArduinoResponse
and should forward the message to /arduino/subscribers
channel.
The WebSocket server is fully functional.
@RestController
@MessageMapping("/arduino/api/v1")
@RequestMapping("/arduino/api/v1")
public class ArduinoControllerV1 {
@Autowired
private SimpMessagingTemplate template;
@MessageMapping("/getArduinoResponse")
@SendTo("/arduino/subscribers")
public WebSocketResponseEntity getArduinoResponse(WebSocketResponseEntity message) throws Exception {
return message;
}
@PostMapping("/sendCommand")
public String sendCommand(@RequestBody CommandEntity commandEntity) throws Exception {
this.template.convertAndSend("/arduino/subscribers", commandEntity.getCommand());
return String.format("Sending to %s the command: %s ", commandEntity.getDeviceSerial(), commandEntity.getCommand());
}
}
Configuration file:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/arduino");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/arduino-websocket").setAllowedOriginPatterns("*").withSockJS();
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
};
}
}
I have created a sketch on my Arduino MEGA that suppose to subscribe to the broker that belongs to the Spring boot server.
But for some reason, I'm receiving a message that I am unable to connect to the WS server. I know that I'm missing the channel name in the Arduino code, and I don't know where to put it.
This is my Arduino code:
#include <Arduino.h>
#include <WebSocketsClient.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 178 }; // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 };
const char* ws_host = "192.168.1.50";
const int ws_port = 8080;
const char* stompUrl = "/arduino-websocket"; // don't forget the leading "/" !!!
WebSocketsClient webSocket;
void sendMessage(String & msg) {
webSocket.sendTXT(msg.c_str(), msg.length() + 1);
}
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
Serial.println("[WSc] Disconnected!\n");
break;
case WStype_CONNECTED:
{
Serial.print("[WSc] Connected to url: ");
Serial.println((char *)payload);
// send message to server when Connected
webSocket.sendTXT("Connected");
}
break;
case WStype_TEXT:
Serial.print("[WSc] get text: ");
Serial.println((char *)payload);
// send message to server
// webSocket.sendTXT("message here");
break;
case WStype_BIN:
Serial.print("[WSc] get binary length: ");
Serial.println(length);
// hexdump(payload, length);
// send data to server
// webSocket.sendBIN(payload, length);
break;
}
}
void setup() {
Serial.begin(115200);
Serial.println("Started");
while (!Serial) {
; // wait for serial port to connect.
}
Serial.println("Serial Started");
// connect to Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
while (!Ethernet.begin(mac)) {
Serial.println("failed. Retrying in 5 seconds.");
delay(5000);
Serial.print("Starting W5100...");
}
Serial.println("Ethernet Started");
Serial.print("I'm sitting on: ");
Serial.println(Ethernet.localIP());
// connect to websocket
webSocket.begin(ws_host, ws_port, stompUrl);
//webSocket.setExtraHeaders(); // remove "Origin: file://" header because it breaks the connection with Spring's default websocket config
// webSocket.setExtraHeaders("foo: I am so funny\r\nbar: not"); // some headers, in case you feel funny
webSocket.onEvent(webSocketEvent);
}
void loop() {
webSocket.loop();
}
Thanks for your help