Skip to content

Commit 607f917

Browse files
committed
add rs485 example
1 parent 36b7ab7 commit 607f917

File tree

6 files changed

+223
-24
lines changed

6 files changed

+223
-24
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"files.associations": {
3+
"array": "cpp",
4+
"deque": "cpp",
5+
"string": "cpp",
6+
"unordered_map": "cpp",
7+
"vector": "cpp",
8+
"string_view": "cpp",
9+
"initializer_list": "cpp"
10+
}
11+
}

examples/Basics/Button/Button.ino

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,41 @@
1212
*/
1313
#include <M5Stack.h>
1414
/* After M5Core is started or reset
15-
the program in the setUp () function will be run, and this part will only be run once.
16-
在 M5Core 启动或者复位后,即会开始执行setup()函数中的程序,该部分只会执行一次。 */
15+
the program in the setUp () function will be run, and this part will only be
16+
run once. 在 M5Core
17+
启动或者复位后,即会开始执行setup()函数中的程序,该部分只会执行一次。 */
1718
void setup() {
18-
M5.begin(); //Init M5Core. 初始化 M5Core
19-
M5.Power.begin(); //Init Power module. 初始化电源模块
20-
M5.Lcd.setTextColor(YELLOW); //Set the font color to yellow. 设置字体颜色为黄色
21-
M5.Lcd.setTextSize(2); //Set the font size. 设置字体大小为2
22-
M5.Lcd.setCursor(65, 10); //Move the cursor position to (65, 10). 移动光标位置到 (65, 10) 处
23-
M5.Lcd.println("Button example"); //The screen prints the formatted string and wraps the line. 输出格式化字符串并换行
24-
M5.Lcd.setCursor(3, 35);
25-
M5.Lcd.println("Press button B for 700ms");
26-
M5.Lcd.println("to clear screen.");
27-
M5.Lcd.setTextColor(RED);
19+
M5.begin(); // Init M5Core. 初始化 M5Core
20+
M5.Power.begin(); // Init Power module. 初始化电源模块
21+
M5.Lcd.setTextColor(
22+
YELLOW); // Set the font color to yellow. 设置字体颜色为黄色
23+
M5.Lcd.setTextSize(2); // Set the font size. 设置字体大小为2
24+
M5.Lcd.setCursor(65, 10); // Move the cursor position to (65, 10).
25+
// 移动光标位置到 (65, 10) 处
26+
M5.Lcd.println(
27+
"Button example"); // The screen prints the formatted string and wraps
28+
// the line. 输出格式化字符串并换行
29+
M5.Lcd.setCursor(3, 35);
30+
M5.Lcd.println("Press button B for 700ms");
31+
M5.Lcd.println("to clear screen.");
32+
M5.Lcd.setTextColor(RED);
2833
}
2934

3035
/* After the program in setup() runs, it runs the program in loop()
3136
The loop() function is an infinite loop in which the program runs repeatedly
3237
在setup()函数中的程序执行完后,会接着执行loop()函数中的程序
3338
loop()函数是一个死循环,其中的程序会不断的重复运行 */
3439
void loop() {
35-
M5.update(); //Read the press state of the key. 读取按键 A, B, C 的状态
36-
if (M5.BtnA.wasReleased() || M5.BtnA.pressedFor(1000, 200)) {
37-
M5.Lcd.print('A');
38-
} else if (M5.BtnB.wasReleased() || M5.BtnB.pressedFor(1000, 200)) {
39-
M5.Lcd.print('B');
40-
} else if (M5.BtnC.wasReleased() || M5.BtnC.pressedFor(1000, 200)) {
41-
M5.Lcd.print('C');
42-
} else if (M5.BtnB.wasReleasefor(700)) {
43-
M5.Lcd.clear(WHITE); // Clear the screen and set white to the background color. 清空屏幕并将白色设置为底色
44-
M5.Lcd.setCursor(0, 0);
45-
}
40+
M5.update(); // Read the press state of the key. 读取按键 A, B, C 的状态
41+
if (M5.BtnA.wasReleased() || M5.BtnA.pressedFor(1000, 200)) {
42+
M5.Lcd.print('A');
43+
} else if (M5.BtnB.wasReleased() || M5.BtnB.pressedFor(1000, 200)) {
44+
M5.Lcd.print('B');
45+
} else if (M5.BtnC.wasReleased() || M5.BtnC.pressedFor(1000, 200)) {
46+
M5.Lcd.print('C');
47+
} else if (M5.BtnB.wasReleasefor(700)) {
48+
M5.Lcd.clear(WHITE); // Clear the screen and set white to the
49+
// background color. 清空屏幕并将白色设置为底色
50+
M5.Lcd.setCursor(0, 0);
51+
}
4652
}
Binary file not shown.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
Description: Start the Web Server in the local network and the user can obtain the page response by accessing the assigned IP address.
3+
Please install library before compiling:
4+
Ethernet2: file in M5stack lib examples -> modules -> W5500 -> Ethernet2.zip (unzip the lib zip file to the Arduino Lib path)
5+
*/
6+
#include <M5Stack.h>
7+
#include <SPI.h>
8+
#include <Ethernet2.h>
9+
#define SCK 18
10+
#define MISO 19
11+
#define MOSI 23
12+
#define CS 26
13+
14+
// 01 05 00 01 02 00 9d 6a
15+
char uart_buffer[8] = {0x01, 0x05, 0x00, 0x01, 0x02, 0x00, 0x9d, 0x6a};
16+
char uart_rx_buffer[8] = {0};
17+
18+
char Num = 0;
19+
char stringnum = 0;
20+
unsigned long W5500DataNum = 0;
21+
unsigned long Send_Num_Ok = 0;
22+
unsigned long Rec_Num = 0;
23+
unsigned long Rec_Num_Ok = 0;
24+
25+
26+
// Enter a MAC address and IP address for your controller below.
27+
// The IP address will be dependent on your local network:
28+
byte mac[] = {
29+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
30+
};
31+
IPAddress ip(192, 168, 1, 177);
32+
33+
// Initialize the Ethernet server library
34+
// with the IP address and port you want to use
35+
// (port 80 is default for HTTP):
36+
EthernetServer server(80);
37+
38+
void setup() {
39+
// Open serial communications and wait for port to open:
40+
M5.begin(true, false, true);
41+
M5.Power.begin();
42+
while (!Serial) {
43+
; // wait for serial port to connect. Needed for Leonardo only
44+
}
45+
SPI.begin(SCK, MISO, MOSI, -1);
46+
Ethernet.init(CS);
47+
// start the Ethernet connection and the server:
48+
Ethernet.begin(mac, ip);
49+
server.begin();
50+
Serial.print("server is at ");
51+
Serial.println(Ethernet.localIP());
52+
53+
M5.Lcd.println("M5Stack W5500 Test");
54+
M5.Lcd.println(" ");
55+
M5.Lcd.print(Ethernet.localIP());
56+
}
57+
58+
59+
void loop() {
60+
// listen for incoming clients
61+
EthernetClient client = server.available();
62+
if (client) {
63+
Serial.println("new client");
64+
// an http request ends with a blank line
65+
boolean currentLineIsBlank = true;
66+
while (client.connected()) {
67+
if (client.available()) {
68+
char c = client.read();
69+
Serial.write(c);
70+
// if you've gotten to the end of the line (received a newline
71+
// character) and the line is blank, the http request has ended,
72+
// so you can send a reply
73+
if (c == '\n' && currentLineIsBlank) {
74+
// send a standard http response header
75+
client.println("HTTP/1.1 200 OK");
76+
client.println("Content-Type: text/html");
77+
client.println("Connection: close"); // the connection will be closed after completion of the response
78+
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
79+
client.println();
80+
client.println("<!DOCTYPE HTML>");
81+
client.println("<html>");
82+
83+
client.println("<body>");
84+
client.println("<h1>M5Stack W5500 Test</h1>");
85+
client.println("<br />");
86+
client.println("<p>Please click here</p>");
87+
client.println("<a href=\"http://www.M5Stack.com\">M5Stack</a>");
88+
client.println("<br />");
89+
client.println("<br />");
90+
client.println("<br />");
91+
92+
93+
client.print("W5500 Counter Num :");
94+
client.print(W5500DataNum);
95+
client.println("<br />");
96+
client.println("<br />");
97+
W5500DataNum ++;
98+
99+
client.print("Rec_Num_Ok Counter :");
100+
client.print(Rec_Num_Ok);
101+
client.println("<br />");
102+
client.println("<br />");
103+
104+
client.print("Rec_Num Counter :");
105+
client.print(Rec_Num);
106+
client.println("<br />");
107+
client.println("<br />");
108+
109+
110+
111+
client.println("</body>");
112+
113+
client.println("</html>");
114+
break;
115+
}
116+
if (c == '\n') {
117+
// you're starting a new line
118+
currentLineIsBlank = true;
119+
}
120+
else if (c != '\r') {
121+
// you've gotten a character on the current line
122+
currentLineIsBlank = false;
123+
}
124+
}
125+
}
126+
// give the web browser time to receive the data
127+
delay(1);
128+
// close the connection:
129+
client.stop();
130+
Serial.println("client disconnected");
131+
}
132+
}
133+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
*******************************************************************************
3+
* Copyright (c) 2021 by M5Stack
4+
* Equipped with M5Core sample source code
5+
* 配套 M5Core 示例源代码
6+
* Visit the website for more information: https://docs.m5stack.com/en/core/gray
7+
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/gray
8+
*
9+
* Describe: RS485
10+
* Date: 2022/5/21
11+
*******************************************************************************
12+
*/
13+
#include <M5Stack.h>
14+
15+
void setup() {
16+
M5.begin(true, false, true, false); // Init M5Core. 初始化 M5Core
17+
M5.Power.begin(); // Init Power module. 初始化电源模块
18+
M5.Lcd.setTextColor(
19+
YELLOW); // Set the font color to yellow. 设置字体颜色为黄色
20+
M5.Lcd.setTextSize(2); // Set the font size. 设置字体大小为2
21+
M5.Lcd.drawString("RS485", 130, 10);
22+
M5.Lcd.drawString("Signal", 0, 60);
23+
Serial2.begin(115200, SERIAL_8N1, 5, 15);
24+
}
25+
26+
void loop() {
27+
if (M5.BtnA.wasPressed()) { // Press the buttonA to send a
28+
// message.按下按键A发送信息
29+
M5.Lcd.fillCircle(100, 65, 15,
30+
GREEN); // Set the light to Green. 设置灯为绿色
31+
M5.update();
32+
Serial2.write('a');
33+
delay(50);
34+
35+
} else {
36+
M5.Lcd.fillCircle(100, 65, 15, WHITE);
37+
}
38+
if (Serial2.available()) { // If the serial port receives a
39+
// message. 如果串口收到信息
40+
int ch = Serial2.read(); // Read the message. 读取信息
41+
Serial.write(ch);
42+
M5.Lcd.fillCircle(100, 65, 15, GREEN);
43+
M5.update();
44+
delay(50);
45+
} else {
46+
M5.Lcd.fillCircle(100, 65, 15, WHITE);
47+
}
48+
M5.update();
49+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ category=Device Control
88
url=https://github.com/m5stack/m5stack
99
architectures=esp32
1010
includes=M5Stack.h
11-
depends=M5GFX,ESP32CAN,UNIT_ENV,UNIT_4RELAY,ADXL345,FastLED,MODULE_GRBL13.2,Adafruit MCP4725,Adafruit TCS34725,Adafruit NeoPixel,MAX30100lib,MFRC522_I2C,M5_BM8563,M5_ADS1100,M5_ADS1115,M5_FPC1020A,HX711 Arduino Library,PCA9554,TinyGPSPlus-ESP32,Adafruit SGP30 Sensor,FFT,TFTTerminal,ClosedCube TCA9548A,M5GFX,ArduinoJson,M5_EzData,PubSubClient,UNIT_SONIC,PoE_CAM,M5_RoverC,UNIT_UHF_RFID,M5_JoyC,M5_BH1750FVI,ATOM_DTU_CAT1,TinyGSM
11+
depends=M5_JoyC,ATOM_DTU_CAT1,M5_BMM150,M5GFX,M5_BM8563,M5_BH1750FVI,M5_ADS1100,M5_ADS1115,UNIT_MQTT,M5_EzData,M5_FPC1020A,M5_RoverC,UNIT_ENV,UNIT_SONIC,UNIT_UHF_RFID,UNIT_4RELAY,PoE_CAM,FastLED,Adafruit MCP4725,Adafruit TCS34725,Adafruit NeoPixel,MAX30100lib,MFRC522_I2C,HX711 Arduino Library,PCA9554,TinyGPSPlus-ESP32,Adafruit SGP30 Sensor,FFT,TFTTerminal,ClosedCube TCA9548A,Ethernet2,ESP8266Audio,ArduinoJson,PubSubClient,TinyGSM
1212

0 commit comments

Comments
 (0)