I have a newly installed emoncms running on ubuntu server (192.168.1.2) and a raspberry pi with a working dallas temperature sensor attached on the same network
On a PC on the network the following URL works in browser to update the temperature feed on the emoncms with an arbitary value (20 here)…
But testing from the Pi by running the following python script, i am getting a 200 OK message back from emoncms but the feed is not updating, any ideas please, I am stuck…
i was just replying to your thread, fair enough :). One question though if poss, is there a way of the logs showing everything (debug) just not errors?
I don’t use urlllib myself so I apologize if I’m wrong, but I think urlencode is expecting a list of key:value parameters so something along the lines of
will probably avoid the error you are currently seeing, but whether that works with emoncms is another thing all together.
Is there any particular reason for using “POST” rather than the usual “GET” requests? I know it seems logical to use “POST” to “post” data to emoncms, but data is prodominantly posted via standard “GET” requests as per your testing in the browser. I think emoncms will accept “POST” requests but I’m not at all familiar with that method.
This might be a FWIW type observation, but I noticed the post part of the URL you used with your browser
i.e. http://192.168.1.2/emoncms/input/post?node=1&json={‘Temp’:‘20’}&apikey=1871xxxxxxxxxxxxxxx120
differs from the post part of the URL you’re using in your connection request.
i.e. conn.request("POST", "/emoncms/input/post.json", params, headers)
(specifically the /post.json part)
I haven’t run emonCMS for some time, but I do remember seeing /post.json as one of the options in an
earlier version of emonCMS. You mentioned your installation was new, but didn’t say what the version
number is.
However, /post.json isn’t shown on the currect Input API page.
I don’t know if it’s simply not listed, or if it’s no longer usable.
@glyn.hudson? Is this an issue, or can it be ignored?
If I remember correctly, the .json part of the /post.json has been dropped and is just ignored as opposed to being blocked or erroneous, but as you say FWIW it definiatly worth dropping the .json to test that theory.
I spotted that too, there are 2 fundamental pieces of debugging info missing here. Not only is the test url different (so there is no confirmation that the used request format works), there is no printout of the formed request in the script to check what is actually being sent.
I guessed that might be because “POST” (request type, not api endpoint) was being used rather than “GET” requests, the “GET” url is both easier to print and to test in a browser (IMO - I know you can use postman or a similar utility).
Good call Brian. I use Python Requests to get data from my modbus instruments.
Just for the heck of it, I decided to load an instance of emonCMS and see if I could whip up something using Requests. Here’s a snippet of the “essential” part of the script that uses the CSV input method to send data
to version 9.8.31:
Here’s the script for reference: csv.py.txt (1.5 KB)
This script uses the node:key json (i.e. not the fulljson) method to send data to emonCMS:
(but is otherwise the same as the CSV version above) node-key.py.txt (1.5 KB)
Disclaimer:
Although these scripts do indeed work, I’m not a coder. The scripts were written as proof of concept and to give the OP an example. They have no error handling. The only thing they are guaranteed to do is take up storage space.
sorry, I’m only a beginner python programmer trying to get my head around some edited code i found online, been trying to follow the advice, but getting stuck with each edit
I saw another simpler example which worked and managed to update the feed using the following code…
only issue now is in my script the current dallas probe temperature is sent to the variable called read_temp() as a number, but if i specify it in following line …
url = url + read_temp() + "}"
I get the error: TypeError: cannot concatenate ‘str’ and ‘float’ objects ?
Edit - formatted code for readability - BT, Moderator
I am constrained to using this inelegant way as the dallas temperature sensors are running on a pi running libreelec which is a locked down “kodi os” missing loads of python libraries (I would have to recompile from source to add, way above my head)
now my pi libreelec media centres (used for watching netflix/youtube) are also temperature nodes at home, each dallas probe is under £2 on ebay!
for anyone interested this is my final working code (running on libreelec), now all I have to do is get my head around emoncms …
import os
import glob
import time
import urllib2
import httplib
import json
os.system('modprobe w1-gpio') # load one wire communication device kernel modules
os.system('modprobe w1-therm')
base_dir = '/sys/devices/w1_bus_master1/' # point to the address
device_folder = glob.glob(base_dir + '28*')[0] # find device with address starting from 28*
device_file = device_folder + '/w1_slave' # store the details
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines() # read the device details
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES': # ignore first line
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=') # find temperature in the details
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0 # convert to Celsius
return temp_c
temp=str(read_temp())
while True:
url = "http://192.168.1.2/emoncms/input/post?node=1&apikey=187xxxxxxxxxxxxxxxxx120&json={'Temp':"
url = url + temp + "}"
urllib2.urlopen(url)
time.sleep(300)