添加Raspberry Pico 433MHZ转发模块看门狗功能

This commit is contained in:
2023-04-19 16:28:55 +08:00
parent bd18dc786a
commit e2ecbe2fb7
4 changed files with 117 additions and 47 deletions

View File

@@ -15,7 +15,11 @@ add_compile_options(-Wall
target_link_libraries(forward
pico_stdlib
hardware_adc
pico_multicore)
pico_multicore
pico_runtime
hardware_sleep
hardware_rtc
)
# enable usb output, disable uart output
pico_enable_stdio_usb(forward 1)

View File

@@ -5,21 +5,20 @@
#include "pico/multicore.h"
#include <map> // map
#include <time.h>
#include "hardware/watchdog.h"
#define BUFFER_SIZ 270
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
const uint RADIO_TRANSMIT_PIN = 16; // 433发射模块引脚
const uint RADIO_RECEIVER_PIN = 17; // 433接收模块引脚
const uint PASSWD_LEN = 3; // 随机数几位
const uint QUEUES_NUM = 3; // 队列数量, 满了发送
const uint QUEUES_WAIT = 60; // 队列等待时间, 单位秒, 队列未满时
const uint RADIO_TRANSMIT_PIN = 16; // 433发射模块引脚
const uint RADIO_RECEIVER_PIN = 17; // 433接收模块引脚
const uint PASSWD_LEN = 3; // 随机数几位
const uint QUEUES_NUM = 3; // 队列数量, 满了发送
const uint QUEUES_WAIT = 4; // 队列等待时间, 单位秒, 队列未满时
char ins[] = "55";
char outs = '1';
// 闪烁LED
static void light()
{
@@ -29,8 +28,8 @@ static void light()
sleep_ms(100);
gpio_put(LED_PIN, 0);
sleep_ms(100);
return ;
return;
}
static const char pool[] = {
@@ -58,11 +57,11 @@ static int RAND()
static void SEND(const int ID)
{
int RANDOM = 500;
const int LOOP_NUM = 1; // 循环发送次数
const uint PULSE_LENGTH = 169; // set this to PULSELENGTH RECIEVED
const uint REPEAT_TRANSMIT = 5; // set this to whatever works best for you. // 重复发送
const uint PROTOCOL = 1; // set this to PROTOCOL RECIEVED
const uint BIT_LENGTH = 24; // set this to BIT LENGTH RECIEVED
const int LOOP_NUM = 1; // 循环发送次数
const uint PULSE_LENGTH = 169; // set this to PULSELENGTH RECIEVED
const uint REPEAT_TRANSMIT = 5; // set this to whatever works best for you. // 重复发送
const uint PROTOCOL = 1; // set this to PROTOCOL RECIEVED
const uint BIT_LENGTH = 24; // set this to BIT LENGTH RECIEVED
gpio_init(RADIO_TRANSMIT_PIN);
RCSwitch mySwitch = RCSwitch();
@@ -71,25 +70,23 @@ static void SEND(const int ID)
mySwitch.setPulseLength(PULSE_LENGTH);
mySwitch.setRepeatTransmit(REPEAT_TRANSMIT);
for (int i = 0; i <= LOOP_NUM; i++) {
for (int i = 1; i <= LOOP_NUM; i++) {
RANDOM = RAND();
light(); // 灯闪烁
light(); // 灯闪烁
sleep_ms(RANDOM * 2 / 3); // 等待随机时间
sleep_ms(RANDOM * 2 / 3); // 等待随机时间
mySwitch.send(ID, BIT_LENGTH); // 发射
sleep_ms(100);
mySwitch.send(ID, BIT_LENGTH); // 发射
sleep_ms(120);
}
return ;
return;
}
static int int_string(int val, char *inchars, int SIZE, int *inchars_len, char *outchars, int *outchars_len)
{
memset(inchars, 0, SIZE);
memset(outchars, 0, SIZE);
snprintf(inchars, SIZE, "%d", val);
*inchars_len = strlen(inchars);
@@ -118,24 +115,18 @@ static void core1_main()
int inchars_len;
int outchars_len;
while (true)
{
if (rcSwitch.available())
{
light(); // 灯闪烁
while (true) {
if (rcSwitch.available()) {
light(); // 灯闪烁
uint32_t val = rcSwitch.getReceivedValue();
int_string(val, inchars, BUFFER_SIZ, &inchars_len, outchars, &outchars_len);
if (val != 0)
{
if (inchars[0] == ins[0] && inchars[1] == ins[1])
{
if (val != 0) {
if (inchars[0] == ins[0] && inchars[1] == ins[1]) {
multicore_fifo_push_blocking(atoi(outchars));
}
else
{
} else {
rcSwitch.resetAvailable();
val = 0;
continue;
@@ -147,30 +138,40 @@ static void core1_main()
val = 0;
}
sleep_ms(100);
sleep_ms(120);
}
return ;
return;
}
int main(void)
{
stdio_init_all();
sleep_ms(7000);
std::map < int, int >idcode;
int count=0;
uint32_t i=0;
multicore_reset_core1();
multicore_launch_core1(core1_main);
if (watchdog_caused_reboot()) {
printf("Rebooted by Watchdog!\n");
} else {
printf("Clean boot\n");
}
watchdog_enable(8000, 1);
while (1)
{
watchdog_update();
if (multicore_fifo_rvalid())
{
i = multicore_fifo_pop_blocking(); // 读取核心1发送来的数据
@@ -186,8 +187,8 @@ int main(void)
{
for (auto it: idcode)
{
printf("核心0转发433MHZ %u\n", it.first);
watchdog_update();
printf("核心0转发433MHZ 1bin %u\n", it.first);
SEND(it.first);
}
@@ -197,11 +198,12 @@ int main(void)
{
count++; // 计数
if (count == QUEUES_WAIT*10) // 秒
if (count == (QUEUES_WAIT*10)) // 秒
{
for (auto it: idcode)
{
printf("核心0转发433MHZ %u\n", it.first);
watchdog_update();
printf("核心0转发433MHZ 2bin %u\n", it.first);
SEND(it.first);
}
@@ -210,10 +212,10 @@ int main(void)
}
}
sleep_ms(10);
sleep_ms(100);
}
return 0;
}