This commit is contained in:
威勝 張 2024-04-19 11:11:09 +08:00
commit d9b7eebc13
5 changed files with 221 additions and 0 deletions

View File

@ -0,0 +1,50 @@
bool LEDState = HIGH;
void setup() {
// initialize digital pin LED_BUILTIN as an output.
Serial.begin(115200);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(12, HIGH);
}
void motor_run(){
digitalWrite(12, LOW); // 方向
for (int i = 0; i < 200; i++) {
digitalWrite(13, HIGH); // 将引脚设置为高电平
delay(10); // 等待10毫秒
digitalWrite(13, LOW); // 将引脚设置为低电平
delay(10); // 等待10毫秒
}
}
void motor_36_run(){
digitalWrite(12, LOW); // 方向
for (int i = 0; i < 20; i++) {
digitalWrite(13, HIGH); // 将引脚设置为高电平
delay(10); // 等待10毫秒
digitalWrite(13, LOW); // 将引脚设置为低电平
delay(10); // 等待10毫秒
}
}
// the loop function runs over and over again forever
void loop() {
if (Serial.available() > 0) { // 检查是否有可用的串行数据
char receivedData[64]; // 用于存储接收到的数据
int dataSize = Serial.readBytesUntil('\n', receivedData, sizeof(receivedData)); // 读取串行数据直到遇到换行符为止
receivedData[dataSize] = '\0'; // 添加字符串结束符
Serial.print("Received: "); // 打印接收到的消息
Serial.println(receivedData); // 打印接收到的数据
if(strcmp(receivedData, "move_all") == 0) {
motor_run();
Serial.println("run_all"); // 打印接收到的数据
}
if(strcmp(receivedData, "move_36") == 0) {
motor_36_run();
Serial.println("run_36"); // 打印接收到的数据
}
}
delay(10);
}

View File

@ -0,0 +1,51 @@
int out_1_pin = 26;
int out_2_pin = 27;
bool LEDState = HIGH;
void setup() {
// initialize digital pin LED_BUILTIN as an output.
Serial.begin(115200);
pinMode(out_1_pin, OUTPUT);
pinMode(out_2_pin, OUTPUT);
}
void motor_run(){
digitalWrite(out_1_pin, LOW); // 方向
for (int i = 0; i < 200; i++) {
digitalWrite(out_2_pin, HIGH); // 将引脚设置为高电平
delay(10); // 等待10毫秒
digitalWrite(out_2_pin, LOW); // 将引脚设置为低电平
delay(10); // 等待10毫秒
}
}
void motor_36_run(){
digitalWrite(out_1_pin, LOW); // 方向
for (int i = 0; i < 20; i++) {
digitalWrite(out_2_pin, HIGH); // 将引脚设置为高电平
delay(10); // 等待10毫秒
digitalWrite(out_2_pin, LOW); // 将引脚设置为低电平
delay(10); // 等待10毫秒
}
}
// the loop function runs over and over again forever
void loop() {
if (Serial.available() > 0) { // 检查是否有可用的串行数据
char receivedData[64]; // 用于存储接收到的数据
int dataSize = Serial.readBytesUntil('\n', receivedData, sizeof(receivedData)); // 读取串行数据直到遇到换行符为止
receivedData[dataSize] = '\0'; // 添加字符串结束符
Serial.print("Received: "); // 打印接收到的消息
Serial.println(receivedData); // 打印接收到的数据
if(strcmp(receivedData, "move_all") == 0) {
motor_run();
Serial.println("run_all"); // 打印接收到的数据
}
if(strcmp(receivedData, "move_36") == 0) {
motor_36_run();
Serial.println("run_36"); // 打印接收到的数据
}
}
delay(10);
}

View File

@ -0,0 +1,103 @@
#include <Wire.h>
#include <WiFi.h>
#include <WebServer.h>
#include <WiFiClient.h>
#include <HTTPClient.h>
int out_1_pin = 26;
int out_2_pin = 27;
const char *ssid = "leo"; // Put your SSID here
const char *password = "00000000";
unsigned long previousMillis =0;
unsigned long interval = 30000;
WebServer server(80);
//WiFi連線
void WifiConnecte() {
//Set your Static IP address
IPAddress local_IP(192, 168,1 , 185);
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
if (!WiFi.config(local_IP, gateway, subnet)) {
Serial.println("STA Failed to configure");
}
//開始WiFi連線
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi連線成功");
Serial.print("IP Address:");
Serial.println(WiFi.localIP());
}
void handleNotFound() {
//digitalWrite(LED,LOW);//LED
String message = "Server is running!\n\n";
//server.send(200, "text/plain", message);
}
//Web_chaek
void web_check(void){
WiFiClient client = server.client();
String response = "HTTP/1.1 200 OK\r\n";
response += "Content-Type: multipart/x-mixed-replace; boundary=frame\r\n\r\n";
server.sendContent(response);
}
void motor_run(){
digitalWrite(out_1_pin, LOW); // 方向
for (int i = 0; i < 200; i++) {
digitalWrite(out_2_pin, HIGH); // 将引脚设置为高电平
delay(10); // 等待10毫秒
digitalWrite(out_2_pin, LOW); // 将引脚设置为低电平
delay(10); // 等待10毫秒
}
WiFiClient client = server.client();
server.send(200, "text/plain","run end");
}
void motor_36_run(){
digitalWrite(out_1_pin, LOW); // 方向
for (int i = 0; i < 20; i++) {
digitalWrite(out_2_pin, HIGH); // 将引脚设置为高电平
delay(10); // 等待10毫秒
digitalWrite(out_2_pin, LOW); // 将引脚设置为低电平
delay(10); // 等待10毫秒
}
WiFiClient client = server.client();
server.send(200, "text/plain","run end");
}
void setup() {
// put your setup code here, to run once:
pinMode(out_1_pin, OUTPUT);
pinMode(out_2_pin, OUTPUT);
Serial.begin(115200);
Serial.println("Hello!");
WifiConnecte();
server.on("/motor_run",HTTP_GET,motor_run);
server.on("/motor_36_run",HTTP_GET,motor_36_run);
server.on("/web_check",HTTP_GET,web_check);
server.onNotFound(handleNotFound);
server.begin();
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMills = millis();
if ((WiFi.status() != WL_CONNECTED)&& (currentMills - previousMillis >= interval)) {
Serial.print(millis());
Serial.print("wifi正在連線中... ");
WiFi.disconnect();//断开连接
WiFi.reconnect();
WifiConnecte();
}
server.handleClient();
}

View File

@ -0,0 +1,17 @@
# -*- coding: UTF-8 -*-
import serial, time
import sys
print(sys.getfilesystemencoding())
#port = '/dev/ttyTHS0'
port = 'COM4'
#ser = serial.Serial(port, baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=1)
ser = serial.Serial("COM4",115200,bytesize=8, parity='N', stopbits=1, timeout=1)
time.sleep(2)
while True:
# 向串口发送数据
command = "move_36" # 发送给Arduino的命令
ser.write(command.encode()) # 将命令转换为字节并发送到串口
print(ser.readline())
time.sleep(2)

BIN
PPT/MCU_1.pptx Normal file

Binary file not shown.