0

I am building an arduino project that requests status from raspberry pi server running django web application. The web app is simple, it stores status of one switch in database (True or False meaning On or Off). I have a view that returns simple string with the status in form "Status:1" or "Status:0".

The problem is that sometimes the response from the server is in a form of long string consisted of unicode characters. The characters are unknown to me when I copy-paste them from Arduino IDE Serial Monitor they look like this: '⸮'. The arduino code repeatly loads this page and checks for status to update the HW in my project. The length of this string is completely random and sometimes even infinite. The occurence is also random so I cannot predict when it happens therefore why it happens.

The problem it is causing me is that I am parsing the response from the server on my arduino and when this infinite string occurs the program on the board freezes because it is parsing it practicaly forever. That is simply unacceptable.

The code that reads the response from server on arduino is:

while(client.connected() || client.available()){
  char c = client.read();
  Serial.print(c);
}

It is not the code I use to parse the response, it is just code I used to find out what is the problem by outputing the response. I am attaching the screen of serial monitor window so you can see the problem (at the end of question). To parse response I use TextFinder.

The view code that creates the response in django:

def state(request, room_name):
  room = Light.objects.get(light_name=room_name)
  out = ""
  if room.light_status:
    out = "Status:1\t"
  else:
    out = "Status:0\t"
  return HttpResponse(out)

Do any of you have any idea what could be causing this problem?

I also have one more problem that might be connected to this one but I am not sure. Sometimes the response value is wrong which causes the LED to blink (just for one iteration at random time, this might be caused by delay from server who knows).

EDIT: I thought the ASCII value of the character could be of some use. It is 255 (the last ASCII character).

The screen of the response in Arduino Serial Monitor

Dominik
  • 5
  • 5

1 Answers1

0

First of all your out should be a dict i.e. out = {"Status":"1"} etc. Secondly, for your question you can remove all the non-ascii characters as Remove Non Ascii characters

Just Replace white-space with empty i.e ''.

Nikhil Rajawat
  • 121
  • 1
  • 4
  • The out is string intentionally, HttpResponse needs string to return the same way it takes html page in the form of string if I want full webpage. That's the problem, there are no non-ascii characters in the string I am outputing it is hard-coded string so there cannot be any. Which white-space do you mean? – Dominik Jun 03 '17 at 16:46
  • The link what I have given in that the guy is using a `white-space` for replacing the non-ascii characters, so in place of that you can use just an empty string `''`. – Nikhil Rajawat Jun 03 '17 at 17:15
  • Yeah, great! But it still does not explain why would it send non ascii character if I have hard-coded string as output that does not contain any non-ascii characters. I am responding with string in format "Status:1" where are there any non-ascii characters in that? – Dominik Jun 03 '17 at 18:01