Skip to content

Contributed by WildboarG, organized by Ai-Thinker

[AI-WB2 Application] Dynamic Password Lock

AI-WB2 - Dynamic Password Lock


Description


Use the AI-WB2 to implement a dynamic password lock. The principle is simple: use a random number generator to generate a password, update it every ten minutes with a timer, start an HTTP server to display the dynamic password in sync, and with intranet penetration you can access and fetch the password from anywhere outside. Applied to a password lock, this makes a random dynamic password lock, provided for guests to use temporarily.

Startup Flow


  1. Boot up
  2. Connect to wifi
  3. Start the timer, interrupt once every 10 minutes
  4. Update the password in the interrupt callback
  5. Start the http server to display the password page, plus a countdown progress bar
  6. Visit the static page to get the password; when the progress bar reaches zero, the page auto-updates to keep the password real-time
  7. Enter the password to unlock

Code Example


  • Connect to wifi
Click to expand full code
c
static

void

wifi_sta_connect
(
char
 *ssid,
char
 *password)
 {

wifi_interface_t
 wifi_interface;
  wifi_interface = wifi_mgmr_sta_enable();
  wifi_mgmr_sta_connect(wifi_interface, ssid, password,
NULL
,
NULL
,
0
,
0
);
}
  • Timer task
Click to expand full code
c
void

flush_password
(
void
 *arg)
 {
  count--;

if
 (count <=
0
) {

if
 (Generate_password(lockpass) != SUCCESS) {

printf
(
"[password]:error\r\n"
);

return
;
    }

printf
(
"[password]:\t"
);

for
 (
int
 i =
0
; i <
6
; i++) {

printf
(
"%d\t"
, lockpass[i]);
    }

printf
(
"\r\n"
);
    count = UPDATE_CYCLE;
  }
}

// 更新锁任务

void

updatelock_task
(
void
 *pvParameters)
 {

// 初始化锁

if
 (Generate_password(lockpass) != SUCCESS) {

printf
(
"[LOCK]:init error"
);

return
;
  };
  timer_init(timer, flush_password);

printf
(
"[timer]:start\r\n"
);
  hosal_timer_start(&timer);

while
 (
1
) {

//

  }
}
  • Generate password
Click to expand full code
c
int

hosal_random_num_get
(
int
 range_min,
int
 range_max)
 {

int
 status;

uint32_t
 buf;
// 确保是 32 位整数

  status = hosal_random_num_read(&buf,
sizeof
(buf));

if
 (!status) {
    buf =
abs
(buf) % (range_max - range_min +
1
) + range_min;

return
 buf;
  }

return

-1
;
}

int

Generate_password
(
char
 *passwd)
 {

for
 (
int
 i =
0
; i <
6
; i++) {
    LOCK_PASSWORD[i] = hosal_random_num_get(
0
,
9
);

// printf("LOCK_PASSWORD[%d]= %d\n", i, LOCK_PASSWORD[i]);

  }

memcpy
(passwd, LOCK_PASSWORD,
6
);

return

0
;
}

LockState
Authentication
(
char
 *passwd)
 {

for
 (
int
 i =
0
; i <
6
; i++) {

if
 (passwd[i] != LOCK_PASSWORD[i]) {

return
 lock;
    }
  }

return
 unlock;
}
  • Start the http server
Click to expand full code
c
static

void

web_httpserver
(
struct
 netconn *conn)
 {

struct

netbuf
 *
inputbuf
;

char
 *buf;

uint16_t
 buflen;

err_t
 err;

  err = netconn_recv(conn, &inputbuf);

if
 (err != ERR_OK) {

return
;
  }
  netbuf_data(inputbuf, (
void
 **)&buf, &buflen);

// 处理 HTTP 请求

if
 (
strstr
(buf,
"GET / "
)) {

// netconn_write(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n",

//              44, NETCONN_NOCOPY);

    netconn_write(conn, html_page,
strlen
(html_page), NETCONN_NOCOPY);
  }
else

if
 (
strstr
(buf,
"GET /cgi-bin/get_password"
)) {

char
 json_response[
128
];

snprintf
(json_response,
sizeof
(json_response),

"HTTP/1.1 200 OK\r\n"

"Content-Type: application/json\r\n"

"Connection: close\r\n"

"\r\n"

"{\"password\":\"%d%d%d%d%d%d\",\"remainingTime\":%d}"
,
             lockpass[
0
], lockpass[
1
], lockpass[
2
], lockpass[
3
], lockpass[
4
],
             lockpass[
5
], count);

    netconn_write(conn, json_response,
strlen
(json_response), NETCONN_NOCOPY);
  }

  netconn_close(conn);
  netbuf_delete(inputbuf);
}

void

server_task
(
void
 *pvParameters)
 {

struct

netconn
 *
conn
, *
newconn
;

err_t
 err;
  conn = netconn_new(NETCONN_TCP);

  netconn_bind(conn,
NULL
, SERVER_PORT);
  netconn_listen(conn);

while
 (
1
) {
    err = netconn_accept(conn, &newconn);

if
 (err != ERR_OK) {
      netconn_close(conn);
      netconn_delete(conn);

break
;
    }

    web_httpserver(newconn);
    netconn_delete(newconn);
  }
}

Demo


For a quick demo, the password here is set to auto-update every 50s

Released under the MIT License. Build Time 2026-09-11 14:52:23