Prtg and json temperature sensor

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
mahnonsaprei
Posts: 12
Joined: Sun Sep 03, 2017 1:43 pm

Prtg and json temperature sensor

Post by mahnonsaprei » Sun Oct 01, 2017 11:28 am

Hi at all,

i'm studying this language and i'm going crazy to export my temperature sensor in json, i have a prtg server and i'd like to share sensors data with this program:

Below a part of code:

while True:
cl, addr = s.accept()
print ('Got connection from', addr)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
dict = {"prtg": {"result": [{"channel": "Channel 1 (float)", "value": ""}]}}
dict['prtg']['result'][0]['value'] = tem
print(dict)
encoded = ujson.dumps(dict)
cl.send(encoded)
cl.close()

The output is, (in html page, when connect with esp8266's ip address):
{"prtg": {"result": [{"channel": "Channel 1 (float)", "value": 25.1}]}}
It's correct but when i call json with a client like postman to test, there is no response.

it's as if the web server has problems to but if I log in to its ip in html i see the string.

Maybe the string json must be wrote in txt file in a pc?
I don't know.... :cry:

Thank you for all your support.

Marcello from italy :?:

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Prtg and json temperature sensor

Post by pythoncoder » Mon Oct 02, 2017 7:00 am

The JSON string is indeed correct (see below) so as far as I can see the fault is either in the format of the HTML page or in the way the client side software is extracting the JSON string from the HTML and interpreting it. One approach might be to send an absolute minimum JSON string and see if you can access that.

The following works under MicroPython and under Python 3.4.3

Code: Select all

>>> a = ujson.loads('{"prtg": {"result": [{"channel": "Channel 1 (float)", "value": 25.1}]}}')
>>> a['prtg']['result'][0]['value']
25.1
>>> 
Peter Hinch
Index to my micropython libraries.

mahnonsaprei
Posts: 12
Joined: Sun Sep 03, 2017 1:43 pm

Re: Prtg and json temperature sensor

Post by mahnonsaprei » Mon Oct 02, 2017 2:35 pm

pythoncoder wrote:The JSON string is indeed correct (see below) so as far as I can see the fault is either in the format of the HTML page or in the way the client side software is extracting the JSON string from the HTML and interpreting it. One approach might be to send an absolute minimum JSON string and see if you can access that.

The following works under MicroPython and under Python 3.4.3

Code: Select all

>>> a = ujson.loads('{"prtg": {"result": [{"channel": "Channel 1 (float)", "value": 25.1}]}}')
>>> a['prtg']['result'][0]['value']
25.1
>>> 
First, thank you of your support.

I'don't understand, i'm a newbie of python, you means that my code it's correct right?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Prtg and json temperature sensor

Post by pythoncoder » Mon Oct 02, 2017 4:38 pm

Your code is producing valid JSON strings which can be parsed by MicroPython and by regular Python 3.x. So therefore the error must be elsewhere than in your conversion to JSON.

I would examine the HTML code which embeds the JSON string. If that is also correct then the fault must lie on the client side. I'm no expert on this stuff but my inclination would be to look very carefully at the HTML you are generating: I would hope that the client side tools will have been thoroughly debugged.

If the HTML really is correct try alternative client side software to see if that can parse your HTML/JSON.
Peter Hinch
Index to my micropython libraries.

mahnonsaprei
Posts: 12
Joined: Sun Sep 03, 2017 1:43 pm

Re: Prtg and json temperature sensor

Post by mahnonsaprei » Wed Oct 04, 2017 6:26 am

I'm out for job. I'll reply html inspection when i go to back to home.
I checked html few days ago with chrome and i didn't found any trouble (for my knowledge).

Maybe the problem could be when mycropython generate a response with he's web server?

I don't know.

Thank you for all your support. You're great!

Post Reply