【问题标题】:HTTP request to update rails model from arduino从 arduino 更新 rails 模型的 HTTP 请求
【发布时间】:2017-11-19 08:43:48
【问题描述】:

我有一个名为“barrel”的导轨模型,它有一个属性“gallons”。我想从 arduino (Boarduino v2.0) 和 Adafruit CC3000 WiFi 模块更新某些桶的“加仑”属性。

我的 rails 应用程序位于计算机的 3000 端口。本地主机:3000/桶

我做了一个桶脚手架,所以它有一个带有更新方法的控制器:

# PUT /barrels/1
# PUT /barrels/1.json
def update
@barrel = Barrel.find(params[:id])
respond_to do |format|
  if @barrel.update_attributes(params[:barrel])
    format.html { redirect_to @barrel, notice: 'Barrel was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @barrel.errors, status: :unprocessable_entity }
  end
end
end

在 arduino 方面,我正在发送一个 HTTP 请求。

Connect to 123.456.7.8:3000  (my IP edited out) 
PUT /barrels/1?gallons=49 HTTP/1.0
Connected & Data sent
Closing connection

它说它已经发送成功,但是当我检查桶1的“加仑”属性时,它永远不会改变。我是否以错误的方式格式化 HTTP 请求?

编辑:

我从服务器收到一个错误:

[2013-11-30 14:47:45] ERROR WEBrick::HTTPStatus::LengthRequired

在我实际的 .ino 文件(我从 arduino 示例中获得)中,我注意到我发送了一个空白请求。目前正在调查删除它是否会解决 WEBrick 错误。

// Send request
if (client.connected()) {

  client.println(request);      
  client.println(F(""));
  Serial.println("Connected & Data sent");
} 

注释掉:client.println(F(""));摆脱了这个错误。但是更新仍然没有发生。

【问题讨论】:

    标签: ruby-on-rails arduino wifi


    【解决方案1】:

    尝试使用gallons 参数而不是barrel 参数进行更新。这会破坏您的正常更新,因此您应该检查gallons 的参数,然后仅在存在加仑时才更新:

    def update
      @barrel = Barrel.find(params[:id])
      respond_to do |format|
        if (params[:gallons] && @barrel.update_attribute(:gallons, params[:gallons]) || @barrel.update_attributes(params[:barrel])
          format.html { redirect_to @barrel, notice: 'Barrel was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render action: "edit" }
          format.json { render json: @barrel.errors, status: :unprocessable_entity }
        end
      end
    end
    

    我尚未测试此代码,因此请尝试提交两种类型的更新并检查它们是否都有效。

    【讨论】:

    • 谢谢!我已经更新了这段代码。我用我从服务器注意到的错误编辑了原始帖子。
    【解决方案2】:

    另一种选择如下:

     PUT /barrels/1?barrel[gallons]=49 HTTP/1.0
    

    但我不确定PUT 的效果如何。大多数 Web 界面将调用限制为 GETPOST

    我不得不在我的 Arduino 代码中调用这个字符串:

    POST /device_update/1?device[gauge]=240 HTTP/1.1
    

    并在我的 config/routes.rb 中输入:

    post "device_update/:id", to: "devices#update"
    

    DevicesController.rb 包含:

    class DevicesController < ApplicationController
      skip_before_filter :verify_authenticity_token 
        //Until I figure out how to send an authenticity token from my Arduino
      ...
      def update
        @device = Device.find(params[:id])
        if @device.update_attributes(device_params)
          render @device
        else
          render 'edit'
        end
      end
      ...
      private
        def device_params
          params.require(:device).permit( :gauge )
        end
    end
    

    【讨论】:

      【解决方案3】:

      我在使用 NODEMCU 和更改 arduino 代码时遇到了同样的问题

        Serial.println(String("GET /setLevel/" ) + value + " HTTP/1.1\r\n" +
                       "Host: " + host + "\r\n" +
                       "Connection: close\r\n" +
                       "\r\n"
                       );
      

      Serial.println(String("GET /setLevel/1/" + value )+ " HTTP/1.1\r\n" +
                       "Host: " + host + "\r\n" +
                       "Connection: close\r\n" +
                       "\r\n"
                       );
      

      解决了:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-31
        • 1970-01-01
        • 1970-01-01
        • 2011-04-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多