Ollifi
Posts: 13
Joined: Sun Dec 22, 2013 6:59 pm

Temperature

Sun Dec 22, 2013 7:06 pm

So I would like to send raspberry pi internal temp sensor data to my server, this way :

Between 30 Min. ,would raspberry open this web page:
http://myserver.com/temp.php?temp=[temp]
[temp] would be replaced with temp. celcius value.
Then, my webpage would save the data to MySql db, for example

Is this possible? Could it even send CPU & Mem.usage, UPTIME (and any other interesting information) ?

Thanks for helping!

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Temperature

Mon Dec 23, 2013 1:48 am

That's a terrible design.

Write a script to read your temp sensor, uptime and other info and update MySQL directly. It's not difficult to do that in python or perl. You can use a web server with php to extract data from your database to display it or graph it, but not for data collection.

Search on here for DHT22
https://www.google.com/search?q=dht22+s ... errypi.org
there's lots of threads on data collection using that device (some of it going directly to Google Docs).
http://www.raspberrypi.org/phpBB3/viewtopic.php?t=62467
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: Temperature

Mon Dec 23, 2013 2:06 am

Hi,
Ollifi wrote:http://myserver.com/temp.php?temp=[temp]
Is this possible?
Yes.
Since you haven't asked anything about obtaining & combining temperature (and CPU, uptime, memory, etc.) data into the URL string, I suppose that you're confident with this...
You can use wget to send this composed URL string to the server. Response can also be parsed to get a success/fail status. If not needed, redirect the response to /dev/null.


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

Ollifi
Posts: 13
Joined: Sun Dec 22, 2013 6:59 pm

Re: Temperature

Mon Dec 23, 2013 5:47 am

Thanks for advice. I don't want to install MySQL into my raspberry, since it would consume too much memory/CPU there. It's also easiest for me to do this way.

Could you give me direct example how to program this kind of activity?
http://myserver.com/temp.php?temp=&mem=&cpu=&uptime=

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Temperature

Mon Dec 23, 2013 6:27 am

Here is an example Bash script that will gather some information and include it in a URL.

Code: Select all

#!/bin/bash

# Get various system parameters

# 1. CPU Temperature in 1/1000's degree C
cputemp=$( cat /sys/devices/virtual/thermal/thermal_zone0/temp )

# 2. Uptime in Seconds
up=$( cat /proc/uptime ) ;# Get uptime values in seconds
uptime=${up%%\.*} ;# Remove all except the integer part of the first value

# Add any more values here..

# Push to server
wget -q "http://myserver.com/temp.php?temp=$cputemp&uptime=$uptime"
Use "cron" to trigger it at set intervals. (Read up "man 5 crontab" on the crontab format and "man 1 crontab" on how to edit it).

I suppose you could do something similar to the above in Python (or any other language) if you prefer, but for simple things like this I prefer good old Bash.

Ollifi
Posts: 13
Joined: Sun Dec 22, 2013 6:59 pm

Re: Temperature

Mon Dec 23, 2013 7:09 am

Thank you! For memory usage, is there any other possibility instead of this:

Code: Select all

mem=$( cat /proc/meminfo ) ;
and then parsing via PHP? I think there should be better option.

Ollifi
Posts: 13
Joined: Sun Dec 22, 2013 6:59 pm

Re: Temperature

Mon Dec 23, 2013 7:20 am

So I found out this,

Code: Select all

memFree = $( cat /proc/meminfo | grep MemFree ) ;
memUsed = $( cat /proc/meminfo | grep MemUsed ) ;
,but when run, it would output error:

Code: Select all

./LOG: line 14: memFree: command not found
./LOG: line 15: memUsed: command not found
Any solution? Thanks!


EDIT:And what command would be good for CPU usage?

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Temperature

Mon Dec 23, 2013 7:26 am

You could parse the output of the "free" command with sed in your script.

Here's an example I just hacked together

Code: Select all

meminfo=$( free | sed -n "s/^Mem: *[0-9]* *\([0-9]*\) *\([0-9]*\) .*$/memused=\1\&memfree=\2/p" )
which sets meminfo to "memused=nnnnn&memfree=nnnnn".

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Temperature

Mon Dec 23, 2013 7:35 am

Ollifi wrote:So I found out this,

Code: Select all

memFree = $( cat /proc/meminfo | grep MemFree ) ;
memUsed = $( cat /proc/meminfo | grep MemUsed ) ;
,but when run, it would output error:

Code: Select all

./LOG: line 14: memFree: command not found
./LOG: line 15: memUsed: command not found
Any solution? Thanks!
You have spaces around the "=" signs. That isn't allowed. As "memFree" isn't immediately followed by an "=" Bash is assuming it is a command instead of a variable name and trying to execute it. Obviously there is no such command as "memFree".
EDIT:And what command would be good for CPU usage?
I can't think of an easy one at the moment. You could try parsing the output of "top -n 1". I'm sure there is an easier way...

Ollifi
Posts: 13
Joined: Sun Dec 22, 2013 6:59 pm

Re: Temperature

Mon Dec 23, 2013 3:25 pm

Ok. One more question: so my achievement is to be able to reboot pi from different computer. So how can I make my pi to go to web page http://myserver.com/reboot.php, between 1 min,for example , should that file have content "reboot" , then pi should execute: "sudo reboot". Is this possible?

That would be useful in crash situations ?

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Temperature

Mon Dec 23, 2013 4:34 pm

Ollifi wrote:Ok. One more question: so my achievement is to be able to reboot pi from different computer. So how can I make my pi to go to web page http://myserver.com/reboot.php, between 1 min,for example , should that file have content "reboot" , then pi should execute: "sudo reboot". Is this possible?
Yes, quite possible.
You can fire off a script every minute with cron, but that is a bit frequent.
Something like

Code: Select all

#!/bin/bash

if [ "$( wget -q -O - http://myserver.com/reboot.php )" = "reboot" ]
then
  sudo /sbin/reboot
fi
That would be useful in crash situations ?
I don't think so. If the Pi has crashed it (probably) won't run anything at all.

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: Temperature

Mon Dec 23, 2013 5:07 pm

Hi,
Ollifi wrote:That would be useful in crash situations ?
No, as already said... you'd need a watchdog.

Watchdog is a device (or just a tiny block inside a processor, I saw even it as just a piece of software) that needs to be periodicaly kicked by monitored device. If not, it performs its reset.
The software watchdog is not really very reliable, because if the system crashes, then the watchdog is also out of order... so, I preffer hardware-based watchdogs.

Here hasn't been much debate about watchdogs so far... RasPi is obviously reliable enough. ;-)

If you're planning to install RasPi to a remote location, where conditions would not be very friendly ("low quality" power supply, for example), then the watchdog might be one of the options to make the overall system more resilient and with shorter downtimes. But then the watchdog is definitelly not the first and the only one thing required.


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

Jooohan
Posts: 2
Joined: Wed Jan 01, 2014 9:56 pm

Re: Temperature

Wed Jan 01, 2014 10:00 pm

Hi, if interested here's a guide how I set up my weather station using a Raspberry Pi and a DS18B20 digital temperature sensor posting reading to a Tomcat web server which displays the information in charts and diagrams on a web page.
http://macgyverdev.blogspot.se/2014/01/ ... ry-pi.html

Return to “Beginners”