簡體   English   中英

HTTP請求從arduino更新Rails模型

[英]HTTP request to update rails model from arduino

我有一個名為“ barrel”的rails模型,其屬性為“ 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的“ gallons”屬性時,它永遠不會改變。 我格式化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"
                 );

解決了:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM