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

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

View File

@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.12)
# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)
include(pico_extras_import.cmake)
project(pico_examples C CXX ASM)
set(CMAKE_C_STANDARD 11)
@ -15,6 +16,7 @@ pico_sdk_init()
# Add blink example
add_subdirectory(examples/Transmit)
add_subdirectory(examples/Receive)
add_subdirectory(examples/Forward)

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()
{
@ -30,7 +29,7 @@ static void light()
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,18 +70,17 @@ 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)
@ -90,7 +88,6 @@ static int int_string(int val, char *inchars, int SIZE, int *inchars_len, char *
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,17 +138,17 @@ 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;
@ -168,9 +159,19 @@ int main(void)
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);
}
@ -211,9 +213,9 @@ int main(void)
}
sleep_ms(10);
sleep_ms(100);
}
return 0;
}

View File

@ -0,0 +1,62 @@
# This is a copy of <PICO_EXTRAS_PATH>/external/pico_extras_import.cmake
# This can be dropped into an external project to help locate pico-extras
# It should be include()ed prior to project()
if (DEFINED ENV{PICO_EXTRAS_PATH} AND (NOT PICO_EXTRAS_PATH))
set(PICO_EXTRAS_PATH $ENV{PICO_EXTRAS_PATH})
message("Using PICO_EXTRAS_PATH from environment ('${PICO_EXTRAS_PATH}')")
endif ()
if (DEFINED ENV{PICO_EXTRAS_FETCH_FROM_GIT} AND (NOT PICO_EXTRAS_FETCH_FROM_GIT))
set(PICO_EXTRAS_FETCH_FROM_GIT $ENV{PICO_EXTRAS_FETCH_FROM_GIT})
message("Using PICO_EXTRAS_FETCH_FROM_GIT from environment ('${PICO_EXTRAS_FETCH_FROM_GIT}')")
endif ()
if (DEFINED ENV{PICO_EXTRAS_FETCH_FROM_GIT_PATH} AND (NOT PICO_EXTRAS_FETCH_FROM_GIT_PATH))
set(PICO_EXTRAS_FETCH_FROM_GIT_PATH $ENV{PICO_EXTRAS_FETCH_FROM_GIT_PATH})
message("Using PICO_EXTRAS_FETCH_FROM_GIT_PATH from environment ('${PICO_EXTRAS_FETCH_FROM_GIT_PATH}')")
endif ()
if (NOT PICO_EXTRAS_PATH)
if (PICO_EXTRAS_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_EXTRAS_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_EXTRAS_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
PICO_EXTRAS
GIT_REPOSITORY https://github.com/raspberrypi/pico-extras
GIT_TAG master
)
if (NOT PICO_EXTRAS)
message("Downloading PICO EXTRAS")
FetchContent_Populate(PICO_EXTRAS)
set(PICO_EXTRAS_PATH ${PICO_EXTRAS_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
if (PICO_SDK_PATH AND EXISTS "${PICO_SDK_PATH}/../pico-extras")
set(PICO_EXTRAS_PATH ${PICO_SDK_PATH}/../pico-extras)
message("Defaulting PICO_EXTRAS_PATH as sibling of PICO_SDK_PATH: ${PICO_EXTRAS_PATH}")
else()
message(FATAL_ERROR
"PICO EXTRAS location was not specified. Please set PICO_EXTRAS_PATH or set PICO_EXTRAS_FETCH_FROM_GIT to on to fetch from git."
)
endif()
endif ()
endif ()
set(PICO_EXTRAS_PATH "${PICO_EXTRAS_PATH}" CACHE PATH "Path to the PICO EXTRAS")
set(PICO_EXTRAS_FETCH_FROM_GIT "${PICO_EXTRAS_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of PICO EXTRAS from git if not otherwise locatable")
set(PICO_EXTRAS_FETCH_FROM_GIT_PATH "${PICO_EXTRAS_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download EXTRAS")
get_filename_component(PICO_EXTRAS_PATH "${PICO_EXTRAS_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_EXTRAS_PATH})
message(FATAL_ERROR "Directory '${PICO_EXTRAS_PATH}' not found")
endif ()
set(PICO_EXTRAS_PATH ${PICO_EXTRAS_PATH} CACHE PATH "Path to the PICO EXTRAS" FORCE)
add_subdirectory(${PICO_EXTRAS_PATH} pico_extras)