簡體   English   中英

從Pebble到Raspberry Pi Python服務器的Ajax http請求失敗

[英]Ajax http request from Pebble to Raspberry Pi Python server fails

作為Pebble / Ajax / Java Script / Python新手,我正在研究非常基本的代碼,以便在Pebble手表上接收Raspberry Pi傳感器的讀數:

我正在執行從Pebble到Raspberry Pi Python服務器的簡單Ajax請求,但無法獲得任何響應(下面是“客戶端/服務器”代碼)。

但是,當我打開Web瀏覽器並輸入Raspberry Pi(例如)的本地URL時,Raspberry服務器響應成功。 192.168.1.80:9999:{“ Pi響應”:“ Hello World!}”

當我將本地URL從192.168.1.80更改為遠程網頁上的一個非常簡單的php-script( http://if.christianbirch.dk/helloworld.php )時,Pebble響應也會成功:[PHONE] pebble-app .js:?:{“ Web響應”:“ Hello World!”}

我已經在許多論壇中搜索了解決方案,但是沒有運氣。 但是,我懷疑本地URL或端口號在Ajax腳本中未正確定義-或Raspberry Pi Python腳本可能缺少標頭語句。

任何經驗重新。 這個問題? 先感謝您!


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!');
}
);

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() 

您似乎已經發現,在CloudPebble中,基於Web的仿真器沒有本地網絡訪問權限。 要將CloudPebble與本地網絡應用程序開發一起使用,您需要將Phone設置為編譯目標。 我也遇到了這個問題,同時還針對我的Pi進行了開發。

暫無
暫無

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

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