M5StickC典型应用示例
本帖最后由 vany5921 于 2020-6-2 15:37 编辑1.WiFiClient设置RTC时间
#include <M5StickC.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include "time.h"
const char *ssid = "RTC";
const char *password = "";
WiFiServer server(80);
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
int lastDrawTime = 0;
void setup() {
M5.begin();
M5.Lcd.setRotation(3);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextSize(1);
M5.Lcd.setCursor(40, 0, 2);
M5.Lcd.println("RTC AP TEST");
Serial.println("Configuring access point...");
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
M5.Lcd.print("AP : " );
M5.Lcd.println(ssid);
M5.Lcd.print("IP : " );
M5.Lcd.println(myIP);
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("New Client.");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html; charset=utf-8;");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\" />");
client.println("<script>");
client.println("function clickBtn(){");
client.println("var jikan= new Date();");
client.println("document.time.year.value = jikan.getFullYear();");
client.println("document.time.mon.value= jikan.getMonth()+1;");
client.println("document.time.day.value= jikan.getDate();");
client.println("document.time.week.value= jikan.getDay();");
client.println("document.time.hour.value = jikan.getHours();");
client.println("document.time.min.value= jikan.getMinutes();");
client.println("document.time.sec.value= jikan.getSeconds();");
client.println("}");
client.println("</script>");
client.println("</head>");
client.println("<body>");
client.println("<form method=\"get\" name=\"time\">");
client.println("<table>");
client.println("<tr><th>year</th><td><input type=\"text\" name=\"year\" value=\"1900\" />1990-2099</td></tr>");
client.println("<tr><th>mon</th><td><input type=\"text\" name=\"mon\" value=\"1\" />1-12</td></tr>");
client.println("<tr><th>day</th><td><input type=\"text\" name=\"day\" value=\"1\" />1-31</td></tr>");
client.println("<tr><th>week</th><td><input type=\"text\" name=\"week\" value=\"0\" />0-6</td></tr>");
client.println("<tr><th>hour</th><td><input type=\"text\" name=\"hour\" value=\"0\" />0-23</td></tr>");
client.println("<tr><th>min</th><td><input type=\"text\" name=\"min\" value=\"0\" />0-59</td></tr>");
client.println("<tr><th>sec</th><td><input type=\"text\" name=\"sec\" value=\"0\" />0-59</td></tr>");
client.println("<tr><th></th><td><input type=\"button\" value=\"设置浏览器时间\" onclick=\"clickBtn()\" /></td></tr>");
client.println("<tr><th></th><td><input type=\"submit\" value=\"更新\" /></td></tr>");
client.println("</table>");
client.println("</form>");
client.println("</body>");
client.println("</html>");
Serial.println("html Rendering");
break;
} else if (currentLine.indexOf("GET /?") == 0) {
int pos1 = 0;
int pos2 = 0;
int val = 0;
// Set RTC time
RTC_TimeTypeDef TimeStruct;
RTC_DateTypeDef DateStruct;
// year
pos1 = currentLine.indexOf('year=', pos2);
pos2 = currentLine.indexOf('&', pos1);
val = currentLine.substring(pos1 + 1, pos2).toInt();
DateStruct.Year = val;
pos1 = currentLine.indexOf('mon=', pos2);
pos2 = currentLine.indexOf('&', pos1);
val = currentLine.substring(pos1 + 1, pos2).toInt();
DateStruct.Month = val;
pos1 = currentLine.indexOf('day=', pos2);
pos2 = currentLine.indexOf('&', pos1);
val = currentLine.substring(pos1 + 1, pos2).toInt();
DateStruct.Date = val;
pos1 = currentLine.indexOf('week=', pos2);
pos2 = currentLine.indexOf('&', pos1);
val = currentLine.substring(pos1 + 1, pos2).toInt();
DateStruct.WeekDay = val;
pos1 = currentLine.indexOf('hour=', pos2);
pos2 = currentLine.indexOf('&', pos1);
val = currentLine.substring(pos1 + 1, pos2).toInt();
TimeStruct.Hours = val;
pos1 = currentLine.indexOf('min=', pos2);
pos2 = currentLine.indexOf('&', pos1);
val = currentLine.substring(pos1 + 1, pos2).toInt();
TimeStruct.Minutes = val;
pos1 = currentLine.indexOf('sec=', pos2);
pos2 = currentLine.indexOf(' ', pos1);
val = currentLine.substring(pos1 + 1, pos2).toInt();
TimeStruct.Seconds = val;
M5.Rtc.SetTime(&TimeStruct);
M5.Rtc.SetData(&DateStruct);
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html; charset=utf-8;");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\" />");
client.println("</head>");
client.println("<body>");
client.println("更新<br />");
client.println("[<a href=\"/\">返回</a>]<br />");
Serial.println("RTC Update");
Serial.println("html Rendering");
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
client.stop();
Serial.println("Client Disconnected.");
}
if( lastDrawTime + 500 < millis() ){
static const char *wd = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"};
M5.Rtc.GetTime(&RTC_TimeStruct);
M5.Rtc.GetData(&RTC_DateStruct);
M5.Lcd.setCursor(0, 16 * 2);
M5.Lcd.printf("Data: %04d-%02d-%02d(%s)\n", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date, wd);
M5.Lcd.printf("Time: %02d : %02d : %02d\n", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);
lastDrawTime = millis();
}
}
2.电源联动自动关机
#include <M5StickC.h>
// 最后确认电压的时间
int lastVinTime = 0;
// 关闭AXP的电源
void axp_halt(){
Wire1.beginTransmission(0x34);
Wire1.write(0x32);
Wire1.endTransmission();
Wire1.requestFrom(0x34, 1);
uint8_t buf = Wire1.read();
Wire1.beginTransmission(0x34);
Wire1.write(0x32);
Wire1.write(buf | 0x80); // halt bit
Wire1.endTransmission();
}
void setup() {
M5.begin();
M5.Lcd.fillScreen(WHITE);
}
void loop() {
if( (M5.Axp.GetVinData()*1.7) < 3.0 ){
if( lastVinTime + 5000 < millis() ){
// 3.0V以下5秒以上关闭电源
axp_halt();
}
} else {
// 最终电压确认时间更新
lastVinTime = millis();
}
delay(500);
}
3.PPT蓝牙遥控器
#include <M5StickC.h>
#include <BleKeyboard.h>
BleKeyboard bleKeyboard("M5StickC BLE Presenter");
// 电池更新
unsigned long nextVbatCheck = 0;
// 计时器
unsigned long startTimer = 0;
// 剩余电量
int getVlevel() {
float vbat = M5.Axp.GetBatVoltage();
int vlevel = ( vbat - 3.2 ) / 0.8 * 100;
if ( vlevel < 0 ) {
vlevel = 0;
}
if ( 100 < vlevel ) {
vlevel = 100;
}
return vlevel;
}
void setup() {
M5.begin();
M5.Axp.ScreenBreath(9);
setCpuFrequencyMhz(80);
M5.Lcd.setRotation(3);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(0, 16);
M5.Lcd.println("BLE Presenter");
M5.Lcd.println(" BtnA :Next");
M5.Lcd.println(" BtnB :Prev");
M5.Lcd.println(" BtnP :Reset");
// 只能在启动时设定电池余量
bleKeyboard.setBatteryLevel(getVlevel());
bleKeyboard.begin();
}
void loop() {
// 按钮状态更新
M5.update();
// 按键操作
if (bleKeyboard.isConnected()) {
if ( M5.BtnA.wasPressed() ) {
// 使用主页按钮推进幻灯片
bleKeyboard.write(KEY_RIGHT_ARROW);
}
if ( M5.BtnB.wasPressed() ) {
// 用右键恢复幻灯片
bleKeyboard.write(KEY_LEFT_ARROW);
}
if ( M5.Axp.GetBtnPress() != 0 ) {
// 用电源按钮进行计时器复位
startTimer = millis();
}
} else {
// 没有连接的情况下不能进行计时器
startTimer = millis();
}
// 电池余量更新(每1分钟)
if (nextVbatCheck < millis()) {
M5.Lcd.setCursor(112, 0);
M5.Lcd.printf("%3d%%", getVlevel());
nextVbatCheck = millis() + 60000;
}
// 计时器显示
int min = ( millis() - startTimer ) / 1000 / 60;
int sec = ( millis() - startTimer ) / 1000 % 60;
M5.Lcd.setCursor(0, 0);
M5.Lcd.printf("%5d:%02d", min, sec);
// 等待
delay(100);
}
4.蓝牙串口工具
#include <M5StickC.h>
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
uint64_t chipid;
char chipname;
void setup() {
chipid = ESP.getEfuseMac();
sprintf( chipname, "SerialBT_%04X", (uint16_t)(chipid >> 32));
M5.begin();
M5.Lcd.setRotation(3);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.printf("Bluetooth: %s\n", chipname);
M5.Lcd.printf("Ver: %s %s\n", __DATE__, __TIME__);
M5.Lcd.println();
M5.Lcd.printf("Val:");
SerialBT.begin(chipname);
}
void loop() {
int val = analogRead(33);
Serial.println(val);
SerialBT.println(val);
M5.Lcd.setCursor(8*4, 8*3);
M5.Lcd.printf("%4d", val);
delay(500);
}
这2个蓝牙的库有吗?
页:
[1]