[英]Ajax http request from Pebble to Raspberry Pi Python server fails
As a Pebble/Ajax/Java Script/Python newbie I am working on the very basic code in order to receive Raspberry Pi sensor readings on the Pebble watch: 作为Pebble / Ajax / Java Script / Python新手,我正在研究非常基本的代码,以便在Pebble手表上接收Raspberry Pi传感器的读数:
I am executing a simple Ajax request from Pebble to a Raspberry Pi Python server but fail to get a any reponse (Client/Server code is below). 我正在执行从Pebble到Raspberry Pi Python服务器的简单Ajax请求,但无法获得任何响应(下面是“客户端/服务器”代码)。
However, the Raspberry server is responding sucessfully when I open a web browser and and enter the local url to the Raspberry Pi, ex. 但是,当我打开Web浏览器并输入Raspberry Pi(例如)的本地URL时,Raspberry服务器响应成功。 192.168.1.80:9999: {"Pi response":"Hello World!}"
192.168.1.80:9999:{“ Pi响应”:“ Hello World!}”
Also the Pebble reponse is successful when I change the local URL from 192.168.1.80 to a very simple php-script on a remote web page ( http://if.christianbirch.dk/helloworld.php ): [PHONE] pebble-app.js:?: {"Web response":"Hello World!"} 当我将本地URL从192.168.1.80更改为远程网页上的一个非常简单的php-script( http://if.christianbirch.dk/helloworld.php )时,Pebble响应也会成功:[PHONE] pebble-app .js:?:{“ Web响应”:“ Hello World!”}
I've searched through many forums for a solution but without luck. 我已经在许多论坛中搜索了解决方案,但是没有运气。 However, I suspect that either the local URL or port number is somehow incorrectly defined in the Ajax script - or the Raspberry Pi Python script could be missing a header sentence.
但是,我怀疑本地URL或端口号在Ajax脚本中未正确定义-或Raspberry Pi Python脚本可能缺少标头语句。
Any experience re. 任何经验重新。 this issue?
这个问题? Thank you in advance!
先感谢您!
The Pebble JS script: Pebble JS脚本:
var ajax = require('ajax');
ajax(
{
url: 'http://192.168.1.80', // *** Valid reponse is received when changing this URL to http://if.christianbirch.dk/helloworld.php ***
method: 'get',
type: 'json',
port:'9999'
},
function(data) {
// Success!
console.log(JSON.stringify(data));
},
function(error) {
// Failure!
console.log('No response!');
}
);
The Python server script (Valid reponse received when called from web browser) https://docs.python.org/2/library/socketserver.html Python服务器脚本(从网络浏览器调用时收到有效响应) https://docs.python.org/2/library/socketserver.html
import SocketServer
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
self.reqdata = self.request.recv(1024).strip()
print self.reqdata
self.send('{"Pi Reponse":"Hello world!"}')
def send(self.content):
self.request.sendall('HTTP/1.0 200 OK\n\n{}.format(content))
if __name__ == "__main__":
HOST, PORT = "0.0.0.0", 9999
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()
As you seem to have figured out, in CloudPebble, the web-based emulator does not have local network access. 您似乎已经发现,在CloudPebble中,基于Web的仿真器没有本地网络访问权限。 To use CloudPebble along with local network app development, you need to set your Phone to be the compilation target.
要将CloudPebble与本地网络应用程序开发一起使用,您需要将Phone设置为编译目标。 I ran into this as well while also developing against my Pi.
我也遇到了这个问题,同时还针对我的Pi进行了开发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.