硬件准备:ESP32-WROOM-32开发板
SSD1306显示器(不带驱动板)
嘉立创打板,工程文件地址:lanzouw.com/i9XhY0e6qhuj 密码:5ykd
下单板子并且根据工程文件下单元件。
焊接。
拿出ESP32-WROOM-32开发板,用Arduino.exe烧录程序。
#include "SSD1306.h"
#include <Arduino.h>
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
SSD1306 display(0x3c, 25, 26);
String str="";
RTC_DATA_ATTR int bootCount = 0;
const int potpin = 34;
int potValue = 0;
BluetoothSerial SerialBT;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
SerialBT.begin("ESP32test"); //Bluetooth device name
SerialBT.printf("ESP32 is restart now! It's the %d time\r\n", ++bootCount);
display.init();
display.setFont(ArialMT_Plain_16);
Serial.begin(115200); //set up serial library baud rate to 9600
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
str = "";
while (SerialBT.available() > 0)
{
display.clear();
str += char(SerialBT.read()); // read是剪切,而不是复制
display.drawString(15, 42 , "light: " + String(potValue));
delay(10); // 延时2
}
potValue = analogRead(potpin);
SerialBT.println(potValue);
display.drawString(15, 3, str);
display.drawRect(0, 0, 128, 64);
display.drawHorizontalLine(0, 21, 128);
display.drawHorizontalLine(0, 42, 128);
display.display();
digitalWrite(LED_BUILTIN, LOW);
delay(100);
}
烧录好之后打开PC蓝牙,连接“ESP32test”。
PC上运行以下python程序:
import serial
import time
import psutil
def get_cpu():
psutil.cpu_percent(None) # 第一次返回的结果是0
time.sleep(0.5)
return psutil.cpu_percent(None)
def get_ram():
return psutil.virtual_memory().percent
def poweroffSchedule(text):
ser = serial.Serial('com5', 115200, parity='E', stopbits=1, bytesize=8, timeout=0.5)
ser.write(text.encode("gbk"))
print('光照强度:' + ser.readline().decode('utf-8'))
# ser.close()
if __name__ == "__main__":
# for i in li:
while True:
poweroffSchedule('CPU:' + ' ' + str(get_cpu()) + '%\r\n' + 'RAM:' + ' ' + str(get_ram()) + '%')
time.sleep(1)
成品如下:
标签:__,ESP,32,get,PC,str,psutil,display,SerialBT From: https://www.cnblogs.com/ff888/p/16807426.html