阿里云通过esp8266控制下位机控制RGB不同颜色,区分度255255255
1、项目简介
2、实现逻辑
#esp8266初始化,和阿里云进行连接(开定时器轮询),接收阿里云的数据
#esp8266通过串口将阿里云数据传送给单片机
#单片机控制RGB灯输出不同颜色
3、应用场景
#远程控制装饰灯
4、核心代码梳理
void RGB_LED_Reset(void)
{
RGB_LED_LOW;
HAL_Delay(80);
}
void RGB_LED_Write_Byte(uint8_t byte)
{
uint8_t i;
for(i=0; i<8; i++)
{
if(byte&0x80)
{
RGB_LED_Write1();
}
else
{
RGB_LED_Write0();
}
byte <<= 1;
}
}
void RGB_LED_Write_24Bits(uint8_t green,uint8_t red,uint8_t blue)
{
RGB_LED_Write_Byte(green);
RGB_LED_Write_Byte(red);
RGB_LED_Write_Byte(blue);
}
//亮灯颜色设定,其他颜色以此类推
void RGB_LED_Red(void)
{
uint8_t i;
//1个LED全彩灯
for(i=0; i<1; i++)
{
RGB_LED_Write_24Bits(0, 0xff, 0);
}
}
void RGB_LED_Green(void)
{
uint8_t i;
for(i=0; i<1; i++)
{
RGB_LED_Write_24Bits(0xff, 0, 0);
}
}
void RGB_LED_Blue(void)
{
uint8_t i;
for(i=0; i<1; i++)
{
RGB_LED_Write_24Bits(0, 0, 0xff);
}
}
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);//open uart1 RXNE
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_TogglePin(GPIOB, LED_Pin);
HAL_Delay(200);
// if(rx_ok)
{
//rx_order++;
//rx_ok = 0;
RGB_LED_Write_24Bits(gv, rv, bv);
}
}
/* USER CODE END 3 */
}
//lua脚本程序
R = 0
G = 0
B = 0
pin=4
gpio.mode(pin,gpio.OUTPUT)
cfg = {}
cfg.ssid = "esp8266"
cfg.pwd = "esp8xxest"
wifi.setmode(wifi.STATION)
wifi.sta.config(cfg)
wifi.sta.connect()
timer1 = tmr.create()
timer2 = tmr.create()
ProductKey = "xxxxxbwI"
DeviceName = "rgb-mqtt"
DeviceSecret = "77445179xxxxab61b7a03fe"
RegionId = "cxxxxai"
ESP8266ClientId = 202xxx10
SubTopic="/a1xxx5bwI/rgb-mqtt/user/R"
Pubtopic="/sys/a1hxxx5bwI/rgb-mqtt/thing/event/property/post"
BrokerAddress = ProductKey..".iot-as-mqtt."..RegionId..".aliyuncs.com"
BrokerPort = 1883
HmacData = "clientId"..ESP8266ClientId.."deviceName"..DeviceName.."productKey"..ProductKey
MQTTClientId = ESP8266ClientId.."|securemode=3,signmethod=hmacsha1|"
MQTTUserName = DeviceName.."&"..ProductKey
MQTTPassword = crypto.toHex(crypto.hmac("sha1",HmacData,DeviceSecret))
function ConnectWifi()
if wifi.sta.getip() == nil then
print("Connecting...")
gpio.write(pin,gpio.HIGH)
else
timer1:stop()
print("Connect AP success")
print(wifi.sta.getip())
MQTTClient = mqtt.Client(MQTTClientId, 120, MQTTUserName, MQTTPassword, false)
MQTTClient:connect(BrokerAddress, BrokerPort, 0, function(client)
timer1:stop()
-- print("MQTT connect success")
gpio.write(pin,gpio.LOW)
MQTTClient:subscribe(SubTopic,0,function(conm)
print("MQTT subscribe success")
MQTTOn()
end)
end,
function(client,reason)
print("MQTT connect fail:"..reason)
end)
end
end
function MQTTOn()
print("MQTT listen...")
MQTTClient:on("message",function(client,topic,data)
-- print("\n")
-- print(topic..":")
-- print(data)
local match_1 = string.find(data,"{", 2)
local match_2 = string.find(data,"}")
local match_3 = string.sub(data, match_1+5, match_2-1)
if(string.find(data,"R")) then
print(string.format("orderR %s F ",match_3))
R = match_3 + 0
MQTTPublish()
end
if(string.find(data,"G")) then
print(string.format("orderG %s F ",match_3))
G = match_3 + 0
MQTTPublish()
end
if(string.find(data,"B")) then
print(string.format("orderB %s F ",match_3))
B = match_3 + 0
MQTTPublish()
end
end)
end
function MQTTPublish()
data = {}
data.R = R
data.G = G
data.B = B
ok,json = pcall(sjson.encode, {params=data})
-- print("message:"..json)
MQTTClient:publish(Pubtopic, json, 0, 0, function(client)
-- print("Publish weather success")
end)
end
timer1:alarm(500, tmr.ALARM_AUTO, ConnectWifi)
5、部分参考资料
#阿里云官网资料
#将往期关于esp8266博客
#如下资料见资料包或者去对应官网和论坛查找
6、注意事项
#阿里云和onenet的连接方式有些区别,页面也有些区别
#esp8266运行lua脚本前,需要烧录可执行的脚本文件,具体步骤产看往期博客
#阿里云收费的,前期可能便宜点