简体   繁体   English

HTTP请求从arduino更新Rails模型

[英]HTTP request to update rails model from arduino

I have a rails model called "barrel" that has an attribute "gallons". 我有一个名为“ barrel”的rails模型,其属性为“ gallons”。 I want to update the "gallons" attribute of certain barrels from an arduino (Boarduino v2.0) and Adafruit CC3000 WiFi module. 我想从arduino(Boarduino v2.0)和Adafruit CC3000 WiFi模块更新某些桶的“加仑”属性。

My rails app is living at port 3000 on my computer. 我的Rails应用程序位于计算机的3000端口上。 localhost:3000/barrels 本地主机:3000 /桶

I made a barrels scaffold, so it has a controller with an update method: 我做了一个桶形脚手架,所以它有一个带有更新方法的控制器:

# 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

On the arduino side I am sending an HTTP request. 在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

It says that it has been sent succesfully, but when I check the "gallons" attribute of barrel 1, it never changes. 它说已经成功发送了,但是当我检查桶1的“ gallons”属性时,它永远不会改变。 Am I formatting the HTTP request the wrong way? 我格式化HTTP请求的方式有误吗?

edit: 编辑:

I was getting an error from the server: 我从服务器收到错误消息:

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

In my actual .ino file (that I got from arduino samples), I noticed that I send a blank request. 在我实际的.ino文件(是从arduino样本获得的)中,我注意到我发送了一个空白请求。 Currently investigating whether removing this will resolve the WEBrick error. 当前正在研究是否删除它会解决WEBrick错误。

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

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

commenting out: client.println(F("")); 注释掉:client.println(F(“”)); got rid of this error. 摆脱了这个错误。 But the the updates still don't occur. 但是更新仍然不会发生。

Try updating using the gallons parameter instead of the barrel parameter. 尝试使用gallons参数而不是barrel参数进行更新。 This would break your normal update, so you should check the parameters for gallons and then only update the gallons if it is present: 这会中断您的常规更新,因此您应该检查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

I haven't tested this code, so try submitting both types of update and check that they both work. 我尚未测试此代码,因此请尝试同时提交两种更新类型并检查它们是否均有效。

Another option is as follows: 另一种选择如下:

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

But I'm not sure how well that PUT will exactly work. 但是我不确定PUT到底能发挥多大的作用。 Most web interfaces limit calls to GET and POST . 大多数Web界面将调用限制为GETPOST

I had to call this string in my Arduino code: 我必须在Arduino代码中调用此字符串:

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

and in my config/routes.rb I put: 然后在我的config / routes.rb文件中输入:

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

with DevicesController.rb containing: 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

I was facing the same problem with NODEMCU and changing arduino code from 我遇到了与NODEMCU相同的问题,并从更改了arduino代码

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

to

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

solved it :) 解决了:)

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

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