从 arduino 更新 Rails 模型的 HTTP 请求 [英] HTTP request to update rails model from arduino

查看:12
本文介绍了从 arduino 更新 Rails 模型的 HTTP 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a rails model called "barrel" that has an attribute "gallons". I want to update the "gallons" attribute of certain barrels from an arduino (Boarduino v2.0) and Adafruit CC3000 WiFi module.

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

My rails app is living at port 3000 on my computer. localhost:3000/barrels

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

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

在 arduino 端,我正在发送一个 HTTP 请求.

On the arduino side I am sending an HTTP request.

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 请求?

It says that it has been sent succesfully, but when I check the "gallons" attribute of barrel 1, it never changes. Am I formatting the HTTP request the wrong way?

我从服务器收到错误:

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

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

In my actual .ino file (that I got from arduino samples), I noticed that I send a blank request. Currently investigating whether removing this will resolve the WEBrick error.

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

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

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

commenting out: client.println(F("")); got rid of this error. But the the updates still don't occur.

推荐答案

我在使用 NODEMCU 时遇到了同样的问题,并从

I was facing the same problem with NODEMCU and changing arduino code from

  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"
                 );

解决了:)

这篇关于从 arduino 更新 Rails 模型的 HTTP 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆