粒子光子到djang的postget

2024-06-16 09:16:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我研究光子和django。我从photon发送请求(它可以运行.ino文件)。因此,我将send“POST”发送到本地主机ip和免费主机站点的ip,但无法接收python/Django中的“GET”值。 我想我可以成功地从光子发送,但我该怎么做才能得到这个值? 我的温度输入是:

// This #include statement was automatically added by the Particle IDE.
#include <HttpClient.h>

#include "application.h"  

 HttpClient http;  

 http_header_t headers[] = {  
    { "Content-Type", "application/json" },  
    { NULL, NULL }   
 };  
 http_request_t request;  
 http_response_t response;  

 void setup() {  

   Serial.begin(9600);  
   //192.168.1.169:8080
   //my free host "heroku" site ip is: 23.23.197.77
   request.ip = IPAddress(23,23,197,77);  
   request.port = 8000;
 }  

 void printResponse(http_response_t &response) {  
   Serial.println("HTTP Response: ");  
   Serial.println(response.status);  
   Serial.println(response.body);  
 }  

 void getRequest() {  

   request.path = "/photon/time";  
   request.body = "";  

   http.get(request, response, headers);  
   printResponse(response);  
 }  


 void postRequest() {  
   // here i send value: 22345 to 23.23.197.77 . but cant get it.
   request.path = "/photon/measurements";  
   request.body = "{\"measurementType\":\"static\", \"value\": 22345}";  

   http.post(request, response, headers);  
   printResponse(response);

 }  


 void loop() {  

   getRequest();  
   postRequest();   
   delay(50);  
 }    

我的序列化程序.py公司名称:

^{pr2}$

我的视图.py公司名称:

# here by def "get" i can show my database to views
#but i cant creat a "get" for receive value which come from photon like we see in temp.ino
class SensorList(APIView):

    def get(self,request):
        sensors = SensorInfo.objects.all()
        serializers = ApiStock(sensors,many=True)
        #print(type(serializers.data))
        return Response(serializers.data)

Tags: iphttpgetincludevalueresponserequestserial