Raspberry PI Model B+ (LCD显示CPU温度)

Title:Raspberry PI Model B+ (LCD显示CPU温度)  --2015-01-29 17:44

买了块连接Raspberry PI Model B+的LCD显示器,上面没写CPU温度,就加工了下。

修改后的pcd8544_rpi.c:

/*
=================================================================================
 Name        : pcd8544_rpi.c
 Version     : 0.1

 Copyright (C) 2012 by Andre Wussow, 2012, [email protected]

 Description :
     A simple PCD8544 LCD (Nokia3310/5110) for Raspberry Pi for displaying some system informations.
     Makes use of WiringPI-library of Gordon Henderson (https://projects.drogon.net/raspberry-pi/wiringpi/)

     Recommended connection (http://www.raspberrypi.org/archives/384):
     LCD pins      Raspberry Pi
     LCD1 - GND    P06  - GND
     LCD2 - VCC    P01 - 3.3V
     LCD3 - CLK    P11 - GPIO0
     LCD4 - Din    P12 - GPIO1
     LCD5 - D/C    P13 - GPIO2
     LCD6 - CS     P15 - GPIO3
     LCD7 - RST    P16 - GPIO4
     LCD8 - LED    P01 - 3.3V

================================================================================
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
================================================================================
 */
#include <wiringPi.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sysinfo.h>
#include "PCD8544.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define TEMP_FILE_PATH "/sys/class/thermal/thermal_zone0/temp"
#define MAX_SIZE 32 

// pin setup
int _din = 1;
int _sclk = 0;
int _dc = 2;
int _rst = 4;
int _cs = 3;

// lcd contrast
//may be need modify to fit your screen!  normal: 30- 90 ,default is:45 !!!maybe modify this value!
int contrast = 55; 

int main (void)
{
  // print infos
  printf("Raspberry Pi PCD8544 sysinfo display\n");
  printf("========================================\n");

  // check wiringPi setup
  if (wiringPiSetup() == -1)
  {
    printf("wiringPi-Error\n");
    exit(1);
  }

  // init and clear lcd
  LCDInit(_sclk, _din, _dc, _cs, _rst, contrast);
  LCDclear();

  // show logo
  LCDshowLogo();

  delay(2000);

  for (;;)
  {
      // clear lcd
      LCDclear();

      // get system usage / info
      struct sysinfo sys_info;
      if(sysinfo(&sys_info) != 0)
      {
        printf("sysinfo-Error\n");
      }

      // uptime
      char uptimeInfo[15];
      unsigned long uptime = sys_info.uptime / 60;
      sprintf(uptimeInfo, "Uptime %ld min.", uptime);

      // cpu info
      char cpuInfo[10];
      unsigned long avgCpuLoad = sys_info.loads[0] / 1000;
      sprintf(cpuInfo, "CPU %ld%%", avgCpuLoad);

      // ram info
      char ramInfo[10];
      unsigned long totalRam = sys_info.freeram / 1024 / 1024;
      sprintf(ramInfo, "RAM %ld MB", totalRam);

      // cpu Temperature
      char cputemperature[15];
      float cpu_temperature_Result = Get_Cpu_Temperature();
      sprintf(cputemperature, "TEMP %.2f ^C", cpu_temperature_Result);

      // build screen
      LCDdrawstring(0, 0, "Raspberry Pi:");
      LCDdrawline(0, 10, 83, 10, BLACK);
      LCDdrawstring(0, 12, uptimeInfo);
      LCDdrawstring(0, 20, cpuInfo);
      LCDdrawstring(0, 28, ramInfo);
      LCDdrawstring(0, 36, cputemperature);
      LCDdisplay();

      delay(1000);
  }

    //for (;;){
  //  printf("LED On\n");
  //  digitalWrite(pin, 1);
  //  delay(250);
  //  printf("LED Off\n");
  //  digitalWrite(pin, 0);
  //  delay(250);
  //}

  return 0;
}

int Get_Cpu_Temperature(void)
{
    int fd;
    double temp = 0;
    char buf[MAX_SIZE]; 

    // 打开/sys/class/thermal/thermal_zone0/temp
    fd = open(TEMP_FILE_PATH, O_RDONLY);
    if (fd < 0) {
        fprintf(stderr, "failed to open thermal_zone0/temp\n");
        return -1;
    } 

    // 读取内容
    if (read(fd, buf, MAX_SIZE) < 0) {
        fprintf(stderr, "failed to read temp\n");
        return -1;
    } 

    // 转换为浮点数打印
    temp = atoi(buf) / 1000.0; 

    // 关闭文件
    close(fd);
    return temp;
}

  

编译执行下:

cc -o cpushow pcd8544_rpi.c PCD8544.c  -L/usr/local/lib -lwiringPi

./cpushow

因为℃跟°C显示有问题,就用^代替那个点了。

时间: 2024-10-11 06:53:14

Raspberry PI Model B+ (LCD显示CPU温度)的相关文章

树莓派(Raspberry Pi Model B+)无显示器和无线网卡上手指南

树莓派(Raspberry Pi Model B+)无显示器和无线网卡上手指南 最近买了一块树莓派板子(Raspberry Pi Model B+)平时作学习之用,只买了块裸板没有显示器和网卡(现在觉得完全没有必要去买显示器),在搭建环境的时候就遇到了一些问题,现在对在搭建树莓派环境遇到的问题做一个总结. 现在我知道的能够连接到树莓派的方法一共有四种,分别是串口终端操作.局域网.PC直连和手机USB网络共享,后面3种方法都是通过ssh的方式登陆树莓派(装好VNC-server后也可以用VNC查看

树莓派3代B型 Raspberry Pi Model 3 B 安装 centos7系统

板子类型: Raspberry Pi Model 3 B 搭配 32G的SD卡: 下载支持树莓派版本的centos7系统 https://buildlogs.centos.org/centos/7/isos/armhfp/ Mac上下载一个SD卡管理工具(格式化用) https://www.sdcard.org/downloads/formatter_4/eula_mac/index.html 格式化前: 格式化操作: 格式化后,发现在电脑上也能发现SD卡 先卸载掉外部磁盘 [email pro

raspberry pi 如何汉化显示中文

1 树莓派初装系统之后,首次启动会出现“raspi-config”工具,如下图:(若不是初次启动,在命令模式下,请输入 sudo raspi-config 命令,即可调出此界面.若在图形桌面下,打开桌面的 LXT 客户端,输入 sudo raspi-config 命令也可以调出这个工具. 2 1.如上图,通过键盘上下键,将蓝条,移动到第 4 步“Internationalisation Options”,回车进入,会出现下图: 3 2.如上图,“Change Locale“,就是汉化的选择项:选

Adding an On/Off switch to your Raspberry Pi

http://www.raspberry-pi-geek.com/Archive/2013/01/Adding-an-On-Off-switch-to-your-Raspberry-Pi#article_f5 Which Switch? Aaron Shaw Pulling the plug on your Pi without an orderly shutdown can corrupt the SD card. Also, many users prefer a convenient sw

Raspberry Pi 3 with Openwrt

https://wiki.openwrt.org/toh/raspberry_pi_foundation/raspberry_pi#boot_log_raspberry_pi_3 Table of Contents Supported Versions Hardware Specifications Raspberry Pi SoCs Raspberry Pi Models Using I2C and SPI Power Notes Serial Boot Logs Boot Log (Rasp

Raspberry Pi Kernel Compilation 内核编译官方文档

elinux.org/Raspberry_Pi_Kernel_Compilation#Use_the_provided_compiler Software & Distributions: Software - an overview. Distributions - operating systems and development environments for the Raspberry Pi. Kernel Compilation - advice on compiling a ker

Raspberry Pi B+ 定时向物联网yeelink上传CPU GPU温度

 Raspberry Pi B+ 定时向物联网yeelink上传CPU GPU温度 硬件平台: Raspberry Pi B+ 软件平台: 1  安装 requests 库 首先我们要先解决requests库,当我们向YEELINK POST 消息的时候会用到  : r = requests.post(apiurl, headers=apiheaders, data=json.dumps(payload)) 安装easy_install: <span style="line-heigh

在树莓派2代B型/3代 上安装Fedora23 - Installing Fedora 23 on Raspberry Pi 2 model B or Raspberry Pi 3

本文主要用于记录安装过程,以便日后查阅回顾. 之前在自己的树莓派上运行的一直是通过NOOB安装的Raspbian,但是本人平时更喜欢用Fedora作为开发和使用环境,而且Raspbian上的软件包通常更新比较缓慢.因为树莓派2代B型上市前,树莓派的芯片都是armv6或者更早的架构,只能运行ARM架构的Raspbian或者Fedora 18及更早版本.树莓派2代B型搭载的芯片架构是armv7,树莓派3代搭载的芯片架构是armv8,这两种芯片都能够运行ARM架构的Fedora 23,而仅运行Rasp

Raspberry Pi 3 Model B入手安装小记

1.买了最新出来的raspberry Pi 3,集成了wifi和蓝牙模块,省去了额外购买wifi模块和蓝牙模块的麻烦.为了保护脆弱的原件,特地买了一个专用的raspberry Pi盒子用来存放,考虑到新的博通1.2GHz四核处理器巨大的发热量,还准备了两块散热片和一块3*3的小电扇保证运行时不至于过热,准备工作一切就绪.图片为还未组装的raspberry Pi 3. 组装过程并不复杂,风扇插在外侧的2.3引脚上即可正常运行.组装完成后即为如图所示. 2.硬件组装完成以后,就要为raspberry