简体   繁体   English

使用AWK和SED读取传感器,并将值输出到html文件

[英]Use AWK and SED to read a sensor, and output value to html file

I have a TP-Link WR703N flashed with openWRT, connected to a one wire DS18B20 temperature sensor. 我有一个用openWRT闪烁的TP-Link WR703N,已连接到单线DS18B20温度传感器。 My goal is to get the router to show the current temp. 我的目标是让路由器显示当前温度。 on a web page. 在网页上。 I am using digitemp to read the sensor 我正在使用digitemp来读取传感器

When i issue the following command: 当我发出以下命令时:

root@OpenWrt:~# digitemp_DS9097 -a root @ OpenWrt:〜#digitemp_DS9097 -a

The reply from digitemp is: digitemp的回复是:

DigiTemp v3.5.0 Copyright 1996-2007 by Brian C. Lane DigiTemp v3.5.0版权所有1996-2007 by Brian C.Lane
GNU Public License v2.0 - http://www.digitemp.com GNU公共许可证v2.0- http://www.digitemp.com
Feb 23 02:46:31 Sensor 0 C: 25.44 F: 77.79 2月23日02:46:31传感器0 C:25.44 F:77.79

I found this example , where a CRON-script with AWK and SED is used to read the c value, and update a HTML-file. 我找到了这个示例 ,其中带有AWK和SED的CRON脚本用于读取c值,并更新HTML文件。

When i try to paste the script to /etc/crontabs/root, some of the qoutation marks are replaced with ... in the VI editor. 当我尝试将脚本粘贴到/ etc / crontabs / root时,某些qoutation标记在VI编辑器中被替换为...。 I tried to use the Nano editor instead, but it just replaces the qoutation marks with ^?^?^. 我尝试改用Nano编辑器,但是它只是用^?^?^替换了qoutation标记。 I do not understand AWK as well as I wish, so i have trouble figuring outh wether the author of the script has used wrong qoutation-marks, or if there is another reason me not being able to input the right characters. 我对AWK的了解不尽如人意,因此我无法确定脚本的作者是否使用了错误的qoutation标记,或者是否还有其他原因无法输入正确的字符。

Any input would be greatly appreciated. 任何投入将不胜感激。

It's the formatting of that blog post, it's got non-ASCII quotes. 这是该博客文章的格式,带有非ASCII引号。 Just edit them to fix. 只需对其进行修复即可。

*/1 * * * * TEMP=$(digitemp_DS9097 -a | grep -i sensor | awk '{print $7}'); sed -i -r "14s,>[^<]*</,>${TEMP}</," /www/index.html

I'm not sure if the rest of the line is right though, my sed doesn't have that -r flag, and the script tries to change line 14 (hence 14s...) which is very picky on whether you copied the html from the blog exactly. 我不确定行的其余部分是否正确,我的sed没有-r标志,并且脚本尝试更改第14行(因此更改为14s ...),这对于您是否复制了完全来自博客的html。 I used this instead: 我改用这个:

*/1 * * * * TEMP=$(digitemp_DS9097 -a |grep -i sensor | awk '{print $7}');sed -i.bak "s,\\(66cc00.*\">\\)[^<]*</,\\1$TEMP</,"  /www/index.html

which matches on the colour number on the line instead. 它与行上的颜色编号匹配。 The extra \\\\(...\\\\) are capturing that so that I can use it again in the replacement as \\\\1 . 多余的\\\\(...\\\\)正在捕获该信息,因此我可以再次使用它替换为\\\\1

I've just read the manual for digitemp_DS9097 ( http://www.linuxcertif.com/man/1/digitemp_DS9097/ ) - you'd be better running it as digitemp_DS9097 -q -t 0 -O"%.2C" , which gives you the output directly as a single Centigrade number with no need for grep/awk. 我刚刚阅读了digitemp_DS9097的手册( http://www.linuxcertif.com/man/1/digitemp_DS9097/)-您最好将其作为digitemp_DS9097 -q -t 0 -O"%.2C" ,这样就可以直接将输出作为一个摄氏度编号,而无需grep / awk。 eg: 例如:

*/1 * * * * TEMP=$(digitemp_DS9097 -q -t 0 -O"%.2C");sed -i.bak "s,\\(66cc00.*\">\\)[^<]*</,\\1$TEMP</,"  /www/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM