1、先对环境进行搭建Arduino ,或者是python,驱动安装;
2、再实现简单的功能原理,用来验证环境与普通IO的功能实现,系统是否搭建成功;
3、实现板载RGB功能,再对灯光度进行调节;
4、实现串口功能,打印出传输的信息。
5、实现灯光色温的调节,通过智能照明系统可以控制灯光色温,以营造不同的氛围和效果。
6、实现灯光的定时开关,通过智能照明系统可以设置灯光的定时开关。
7、实现灯光的远程控制,通过智能照明系统可以实现灯光的远程控制,方便用户随时随地控制灯光和开关、不同场景应用。
8、实现MQTT功能。
由于开年来,比较繁忙,今天来个汇总贴,后面慢慢再一一发贴出来。
1、先对环境进行搭建Arduino ,或者是python,驱动安装;
先全用Arduino
先说问题点,这个一直都无法编译,把时间浪费了,搞了几天,今年被大佬搞定了,感谢大佬的指导。刚开始是装的arduino-ide_2.3.2_Windows_64bit这个版本,好像是欠他点什么,一直不给玩,后面经大佬指导装的1.8版本搞定了。下面我把代码贴上来.
2、再实现简单的功能原理,用来验证环境与普通IO的功能实现,系统是否搭建成功;
// RGB灯由PA12(R),PA13(B),PA14(G)
void setup() {
pinMode(PA12, OUTPUT);
// pinMode(PA13, OUTPUT);
// pinMode(PA14, OUTPUT);
digitalWrite(PA12, HIGH);
// digitalWrite(PA13, HIGH);
// digitalWrite(PA14, HIGH);
}
// 呼吸灯
void loop() {
for (int i = 0; i < 100; i++) {
analogWrite(PA12, i);
delay(500);
}
}
3、实现板载RGB功能,再对灯光度进行调节;
#include <Arduino.h>
void setup() {
// initialize digital pin's from all 3 colors as an output.
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT);
}
// blink RED, GREEN, BLUE
void blink() {
digitalWrite(LED_R, HIGH); // turn the RED LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_R, LOW); // h:\Evaluation\BW16 KIT\led\led\led.inoturn the RED LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(LED_G, HIGH); // turn the GREEN LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_G, LOW); // turn the GREEN LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(LED_B, HIGH); // turn the BLUE LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_B, LOW); // turn the BLUE LED off by making the voltage LOW
delay(1000); // wait for a second
}
// fade RED and BLUE
void fade() {
// fade RED in from min to max in increments of 5 points:
for (int fadeValue = 0; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(LED_R, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade RED out from max to min in increments of 5 points:
for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(LED_R, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade BLUE in from min to max in increments of 5 points:
for (int fadeValue = 0; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(LED_B, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade BLUE out from max to min in increments of 5 points:
for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(LED_B, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
// the loop function runs over and over again forever
void loop() {
blink(); // blink RED, GREEN, BLUE
fade(); // fade RED, BLUE
delay(1000); // wait for a second
}
4、实现串口功能,打印出传输的信息。
void setup() {
Serial.begin(9600); // 设置串口波特率为9600
}
void loop() {
// 发送数据
Serial.println("Please input somthing:");
// 等待接收数据
while (!Serial.available()) {} // 等待串口接收数据
delay(10); // 等待数据稳定
// 读取并打印接收到的数据
String receivedData = Serial.readString();
Serial.print("Received data: ");
Serial.println(receivedData);
delay(10); // 延时一段时间后重新发送数据
}
上图:
5、实现灯光色温的调节,通过智能照明系统可以控制灯光色温,以营造不同的氛围和效果。
void setup() {
pinMode(PA12, OUTPUT);
// pinMode(PA14, OUTPUT);
pinMode(PA13, OUTPUT);
Serial.begin(115200); // 初始化串口
}
int calculateColorValue(int colorTemp, int minVal, int maxVal) {
// 根据色温数值和最小最大值计算颜色亮度
int val = map(colorTemp, 0, 100, minVal, maxVal);
return val;
}
void loop() {
// 读取串口数据
if (Serial.available() > 0) {
int colorTemp = Serial.parseInt(); // 读取串口发送的色温数值
Serial.print("the color tmp is set as ");
Serial.print(colorTemp);
Serial.print("/r/n");
// 根据色温数值调整颜色亮度
// analogWrite(PA14, calculateColorValue(colorTemp, 0, 255));//这个引脚打开之后就出现问题,不开一切正常
analogWrite(PA12, calculateColorValue(colorTemp, 0, 255));
analogWrite(PA13, calculateColorValue(colorTemp, 0, 255));
}
}
再来个图:
6、实现灯光的定时开关,通过智能照明系统可以设置灯光的定时开关。
// bool isLightOn = false; // 灯光状态,默认关闭
unsigned long previousMillis = 0; // 上一次变换状态的时间
unsigned long interval = 1000; // 每个状态之间持续的时间间隔,默认1秒
unsigned int isLightOn=0; //变量定义位置吗,有点不懂
void setup() {
pinMode(PA13, OUTPUT);
Serial.begin(115200); // 初始化串口
// digitalWrite(PA13, true);
}
void loop() {
unsigned long currentMillis = millis(); // 获取当前运行时间
int setTime=0;
// Serial.println(currentMillis);
if(Serial.available() > 0) { // 如果串口有可用数据
setTime = Serial.parseInt(); // 从串口读取设置的定时时间
interval = setTime * 500; // 将设置的时间转换为秒
Serial.print("time set!\r\n");
}
if(currentMillis - previousMillis >= interval) { // 如果超过设定的时间间隔
previousMillis = currentMillis; // 更新上次状态变换的
Serial.print("led change!\r\n");
isLightOn +=1; // 切换灯光
if(isLightOn%2==0) {
digitalWrite(PA13, HIGH);
Serial.print("led on ");
} else {
digitalWrite(PA13, LOW); // 关闭灯光
Serial.print("led off ");
}
}
}
7、实现灯光的远程控制,通过智能照明系统可以实现灯光的远程控制,方便用户随时随地控制灯光和开关、不同场景应用。
#include <WiFi.h>
#include <WiFiServer.h>
#include <WiFiClient.h>
char* ssid = "mldesk"; // 你的Wi-Fi网络名称
const char* password = "12345671"; // 你的Wi-Fi密码
// const int ledPin = PA_13; // LED控制引脚
WiFiServer server(80);
String header;
void setup() {
pinMode(PA13, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println("Server started");
}
void loop() {
WiFiClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
header += c;
if (c == '\n') {
if (header.indexOf("ON") > 0) {
digitalWrite(PA13, HIGH);
} else if (header.indexOf("OFF") > 0) {
digitalWrite(PA13, LOW);
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head></head>");
client.println("<body>");
// client.println("LED is now " + (digitalRead(PA13) ? "ON" : "OFF"));
client.println("</body>");
client.println("</html>");
header = "";
client.stop();
}
}
}
}
}
上图:
8、实现MQTT功能。
#include <WiFi.h>
#include <PubSubClient.h>
char ssid[] = "mldesk"; // your network SSID (name)
char pass[] = "12345671"; // your network password
int status = WL_IDLE_STATUS; // Indicator of Wifi status
char mqttServer[] = "test.mosquitto.org";
char clientId[] = "amebaClient";
char publishTopic[] = "outTopic";
char publishPayload[] = "hello world";
char subscribeTopic[] = "inTopic";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
void callback(char* topic, byte* payload, unsigned int length) {
char payload_r_on[]="LED_R_ON";
char payload_g_on[]="LED_G_ON";
char payload_b_on[]="LED_B_ON";
char payload_all_off[]="LED_OFF";
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (unsigned int i = 0; i < length; i++) {
Serial.print((char)(payload[i]));
}
Serial.println();
Serial.print("payload len:");
Serial.println(length);
if(strcmp(topic,subscribeTopic)==0)
{
Serial.println("Receive subscribe.");
if(memcmp((char *)payload,payload_r_on,strlen(payload_r_on))==0)
{
Serial.println("Light the Led Red");
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, LOW);
digitalWrite(LED_B, LOW);
client.publish(publishTopic,payload_r_on);
}
if(memcmp((char *)payload,payload_g_on,strlen(payload_g_on))==0)
{
Serial.println("Light the Led Green");
digitalWrite(LED_R, LOW);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_B, LOW);
client.publish(publishTopic,payload_g_on);
}
if(memcmp((char *)payload,payload_b_on,strlen(payload_b_on))==0)
{
Serial.println("Light the Led Blue");
digitalWrite(LED_R, LOW);
digitalWrite(LED_G, LOW);
digitalWrite(LED_B, HIGH);
client.publish(publishTopic,payload_b_on);
}
if(memcmp((char *)payload,payload_all_off,strlen(payload_all_off))==0)
{
Serial.println("Light the Led Off");
digitalWrite(LED_R, LOW);
digitalWrite(LED_G, LOW);
digitalWrite(LED_B, LOW);
client.publish(publishTopic,payload_all_off);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!(client.connected())) {
Serial.print("\r\nAttempting MQTT connection...");
// Attempt to connect
if (client.connect(clientId)) {
Serial.println("connected");
//Once connected, publish an announcement and resubscribe
// client.publish(publishTopic, publishPayload);
client.subscribe(subscribeTopic);
} else {
Serial.println("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
//Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// initialize digital pin's from all 3 colors as an output.
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT);
digitalWrite(LED_R, LOW);
digitalWrite(LED_G, LOW);
digitalWrite(LED_B, LOW);
//Initialize serial and wait for port to open:
Serial.begin(115200);
// wait for serial port to connect.
while (!Serial) {
;
}
Serial.println("Init serial OK");
//Attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("\r\nAttempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
client.setServer(mqttServer, 1883);
client.setCallback(callback);
//Allow Hardware to sort itself out
delay(1500);
}
void loop() {
if (!(client.connected())) {
reconnect();
}
client.loop();
}
上图:
|