简体   繁体   English

如何从Arduino连接到Action Cable Websocket?

[英]How to connect into Action Cable websocket from Arduino?

I built an app in Python Flask which controls the LED light on an Arduino through broadcasting the color selected in a form to all members of the websocket channel. 我在Python Flask中构建了一个应用程序,该应用程序通过将以表格形式选择的颜色广播给websocket通道的所有成员来控制Arduino上的LED灯。 I am now rebuilding in Rails and trying to determine how my Arduino can specify which channel it would like to join. 我现在在Rails中重建并尝试确定Arduino如何指定要加入的通道。 I've already begun a connection to the WebSocket and seem to be getting the following back from Rails: [WSc] Received text: {"type":"ping","message":1544679171} . 我已经开始连接到WebSocket,并且似乎可以从Rails得到以下信息: [WSc] Received text: {"type":"ping","message":1544679171}

Now I just need to determine how I can send in a request to specifically stream from the ArduinoChannel, but I'm not sure how to go about it. 现在,我只需要确定如何发送请求以从ArduinoChannel专门发送流,但是我不确定该如何处理。 I've tried adding parameters to my webSocket.begin , but that doesn't seem to have any affect. 我尝试将参数添加到webSocket.begin ,但这似乎没有任何影响。

Below is my Arduino code for reference: 以下是我的Arduino代码供参考:

#include <ESP8266WiFi.h>
#include <WebSocketsClient.h>
#include <ArduinoJson.h>
#include <EEPROM.h>

// Initialize pins
int redpin = D0;
int greenpin = D2;
int bluepin = D4;

//// Connecting to the internet
const char* ssid = "**************";
const char* password = "******";

// Setting up the websocket client
WebSocketsClient webSocket;

// Set up the WiFi client;
WiFiClient client;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);


  delay(10);
  WiFi.begin(ssid, password);
  while(WiFi.status()!= WL_CONNECTED) {
    Serial.print(".");
    delay(500);  
  }
  Serial.println("");
  Serial.print("IP Address: ");
  Serial.print(WiFi.localIP() + "\n");
  Serial.print(WiFi.macAddress() + "\n");

  // Initializing the WS2812B communication
  setRgb(255,80,90);

  // Initializing the websocket connection

  webSocket.begin("192.168.1.93",3000, "/cable" );
//  webSocket.sendTXT('{"command":"subscribe","identifier":"{\"channel\":\"ArduinoChannel\"}"', 0);
  webSocket.onEvent(webSocketEvent);
  webSocket.setReconnectInterval(5);

}
void loop() {
  // put your main code here, to run repeatedly:
  webSocket.loop();

} 

void webSocketEvent(WStype_t type, uint8_t *payload, size_t length) {
  switch(type) {
    Serial.write(type);
    case WStype_DISCONNECTED:
      Serial.printf("[WSc] Disconnected!\n");
      break;
    case WStype_CONNECTED:
      Serial.printf("[WSc] Connected to url: %s\n", payload);
      break;
    case WStype_TEXT:
      Serial.printf("[WSc] Received text: %s\n", payload);
      DynamicJsonBuffer jBuffer;
      JsonObject &root = jBuffer.parseObject(payload);
      setRgb(root["r"],root["g"],root["b"]);
      break;
  }
}

  void setRgb(uint8_t r, uint8_t g, uint8_t b) {
  analogWrite(redpin, r);
  analogWrite(bluepin, b);
  analogWrite(greenpin, g);
  delay(10);
}

TL;DR: TL; DR:

how I can send in a request to specifically stream from the ArduinoChannel 我如何发送请求以专门从ArduinoChannel流

To receive streams from ArduinoChannel , you'll need to "subscribe" by sending through Websocket connection the following the String data from Arduino-client: 要从ArduinoChannel接收流,您需要通过Websocket连接发送以下来自Arduino-client的String数据来“订阅”:

"{\"command\":\"subscribe\",\"identifier\":\"{\\\"channel\\\":\\\"ArduinoChannel\\\"}\"}"

... which is almost the same as your commented out sendTXT code, but that probably you were just incorrectly "escaping" the double quotes. ...几乎与您注释掉的sendTXT代码相同,但是可能您只是在错误地“转义”了双引号。

References: 参考文献:

I traced from the JS-client version of ActionCable here 在此处跟踪了ActionCable的JS客户端版本

  1.  App.cable.subscriptions.create('ArduinoChannel') 
  2.  Subscriptions.prototype.create = function create(channelName, mixin) { var channel = channelName; var params = (typeof channel === "undefined" ? "undefined" : _typeof(channel)) === "object" ? channel : { channel: channel }; // params = { channel: "ArduinoChannel" } var subscription = new Subscription(this.consumer, params, mixin); return this.add(subscription); }; 
  3.  function Subscription(consumer) { var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var mixin = arguments[2]; classCallCheck(this, Subscription); this.consumer = consumer; this.identifier = JSON.stringify(params); extend(this, mixin); } 
  4.  Subscriptions.prototype.add = function add(subscription) { this.subscriptions.push(subscription); this.consumer.ensureActiveConnection(); this.notify(subscription, "initialized"); this.sendCommand(subscription, "subscribe"); return subscription; }; 
  5.  Subscriptions.prototype.sendCommand = function sendCommand(subscription, command) { var identifier = subscription.identifier; // command = "subscribe"; identifier = JSON.stringify(params) = '{"channel":"ArduinoChannel"}'; return this.consumer.send({ command: command, identifier: identifier }); }; 
  6.  Consumer.prototype.send = function send(data) { // data = { command: 'subscribe', identifier: '{"channel":"ArduinoChannel"}' } return this.connection.send(data); }; 
  7.  Connection.prototype.send = function send(data) { if (this.isOpen()) { // JSON.stringify(data) = '{"command":"subscribe","identifier":"{\\"channel\\":\\"ArduinoChannel\\"}"}' this.webSocket.send(JSON.stringify(data)); return true; } else { return false; } }; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM