Posting to emoncms via python from local pi

I was only referring specifically to the way the url is constructed and excusing my “quick fix” more than criticizing your original code.

I notice your code here

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)

won’t actually work as intended because the temp is read once before entering the loop, so the same temp will be sent every 5 mins without change, the reading of the temp needs to be in the loop eg

while True:

    temp=str(read_temp())

    url = "http://192.168.1.2/emoncms/input/post?node=1&apikey=187xxxxxxxxxxxxxxxxx120&json={'Temp':"

    url = url + temp + "}"

    urllib2.urlopen(url)

    time.sleep(300)

as for my comment about the url formatting, I was just thinking of using variables over hardcoding and avoiding casting and type errors by doing something like

host = "http://192.168.1.2/emoncms"
apikey = "187xxxxxxxxxxxxxxxxx120"
node = 1

while True:

    temp = read_temp()

    url = '{}/input/post?node={}&apikey={}&json={{"Temp":{}}}'.format(host,node,apikey,temp)

    urllib2.urlopen(url)

    time.sleep(300)

thanks for the two points… its my first ever bit of code!

I think not hardcoding is probably a good practice to get into :slight_smile:

I’ve always hated structured learning (probably a downside), everything I’ve learnt i’ve always just dived in

thanks again

thumbsup

Seconded.

6 months down the line, can you be certain that you’ve picked up all the occurrences of a value? What if two such things happen to have the same value now, but you find they need different values later? If they are not hardcoded, you don’t have to think back to know what the magic number was - it’s child’s play to update. If they’re hard coded, expect bugs.

Would you believe the only programming I’ve ever been taught formally was Algol at Uni and some years later a short course on Fortran 4?

I’d never heard of emoncms, and got it up and running using the instructions here which are out of date and had to google steps that didnt work … emoncms/LinuxInstall.md at master · emoncms/emoncms · GitHub

quickly read the page on “inputs” then moved onto the posting issue that is resolved here…

Just navigating around emoncms now … wow what a mature feature rich platform

My next goal is to use this cheap pi/dallas temp probe combo with emoncms to monitor temperature at a elderly relatives flat and send me notifications when the daily temprature pattern suddenly changes … i.e. they might have had a fall … got a bit of a uphill challenge i think, but love a challenge. Like to learn with a task in mind, so boring learning from a book/course notes chapter by chapter :slight_smile:

Tell us the exact details you found wrong and I’ll try to get it corrected. Thanks for pointing it out.

Hi, trying to remember …I think in the configure apache section the script resulted in a permission denied error, I had to sudo nano emoncms.conf and add the lines in manually (new installation of ubuntu LTS 32bit 16.04 server)

Was it definitely “emoncms.conf” or could it have been “apache2.conf”?

I just took a quick look and saw this line

sudo echo 'ServerName localhost' >> /etc/apache2/apache2.conf

which should be something like

printf "ServerName localhost" | sudo tee -a /etc/apache2/apache2.conf 1>&2

rather than trying to use sudo echo

this was edited back in Febuary from

sudo sh -c "echo 'ServerName localhost' >> /etc/apache2/apache2.conf"

which is another way of getting around a sudo echo. But the printf and tee is the preferred method now, it is also used earlier in the guide for setting up the Redis extn’ for PHP.

I’m unable to test the emoncms.conf bit right now, but the apache2.conf bit certainly looks wrong to me.

[edit] I just found the discussion related to that change

so I’m guessing the change from using sudo su was not fully tested, looks like the heredoc was, but possibly not the sudo echo.

Hi, on second thoughts, yes it was apache2.conf, i remember putting in ServerName localhost at the end of the file using nano

Thanks for confirming, now corrected.