Anas Mansour Anas Mansour - 2 years ago 196
HTTP Question

post-get from particle photon to django

i work on photon and django. i send request from photon (which can run .ino files). So i send send "POST" to my localhost ip and free host site's ip and am not able to receive "GET" the value in python/Django.
i think i can send successfully from photon but what i should do to get the value ?
my temp.ino is :

// 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);
}


my serializers.py:

from rest_framework import serializers

from staj.models import Info, SensorInfo

class MemberSerializer(serializers.ModelSerializer):
class Meta:
model = Info
fields = "__all__"
#fields = ('id', 'value', 'data', 'time')

class ApiStock(serializers.ModelSerializer):
info = MemberSerializer(many=True, read_only=True)

class Meta:
model = SensorInfo
#fields = ('id','username','firstName','lastName', 'info')
fields = "__all__"


my views.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)

Answer Source

You need to create an api in your django app that accepts GET request. Use django-rest-framework

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download