HTTP请求从arduino更新rails模型

我有一个名为“桶”的铁轨模型,其属性为“加仑”。 我想从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(“”)); 摆脱了这个错误。 但是更新仍然没有发生。

尝试使用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 

我没有测试过这段代码,所以尝试提交两种类型的更新并检查它们是否都有效。

另一种选择如下:

  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 

我遇到了与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" ); 

解决了:)