如何从 Arduino 连接到 Action Cable websocket?

How to connect into Action Cable websocket from Arduino?

我在 Python Flask 中构建了一个应用程序,它通过向 websocket 通道的所有成员广播表单中选择的颜色来控制 Arduino 上的 LED 灯。我现在正在 Rails 重建并试图确定我的 Arduino 如何指定它想加入哪个频道。我已经开始连接到 WebSocket,并且似乎从 Rails 得到了以下信息:[WSc] Received text: {"type":"ping","message":1544679171}.

现在我只需要确定如何发送请求以专门从 ArduinoChannel 流式传输,但我不确定如何去做。我已经尝试将参数添加到我的 webSocket.begin,但这似乎没有任何影响。

下面是我的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);
}

长话短说:

how I can send in a request to specifically stream from the ArduinoChannel

要从 ArduinoChannel 接收流,您需要 "subscribe" 通过 Websocket 连接发送以下来自 Arduino 客户端的字符串数据:

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

... 这与您注释掉的 sendTXT 代码几乎相同,但您可能只是错误地 "escaping" 双引号。

参考文献:

我从 ActionCable here

的 JS-client 版本追踪
  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;
      }
    };