Skip to content

Commit

Permalink
Added motion cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
kanflo committed Jan 21, 2016
1 parent 60a4233 commit de0d98d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ When flashing has completed, open a serial terminal to learn the IP number of yo

Start the python script ```server.py``` and type ```upload:<your IP>``` to capture, upload and display an image on your computer.

If you connect a [PIR module](http://www.ebay.com/sch/i.html?_trksid=PIR+module.TRS0&_nkw=PIR+module&_sacat=0) [eBay] to the JST connector you can use the command ```motion:on:<your ip>``` to have the board capture and upload an image when motion is detected.

###Limitations

This is work in progress and there is currently very little error handling. Simultaneous clients will break the camera demo.
Expand Down
35 changes: 17 additions & 18 deletions capture.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@


//#define CONFIG_NO_WIFI
#define CONFIG_NO_PIR
//#define CONFIG_NO_PIR

#define ARDUCAM_PWR (15) // Arducam Mini power enable

Expand All @@ -65,28 +65,27 @@ void pir_task(void *p)
printf("PIR task\n");

uint16_t threshold = 500;
// uint8_t pir_pin = 16;
bool old_state = false;
// gpio_enable(pir_pin, GPIO_OUTPUT);
// old_state = gpio_read(pir_pin);
// __pinMode(pir_pin, INPUT);

while(1) {
uint32_t adc = sdk_system_adc_read();
bool new_state = adc > threshold;
if (new_state != old_state) {
printf("%s [%u]\n", new_state ? "Motion" : "No motion", adc);
old_state = new_state;
if (cli_motion_enabled()) {
uint32_t adc = sdk_system_adc_read();
bool new_state = adc > threshold;
if (new_state != old_state) {
if (new_state) {
printf("Motion detected!\n");
if (arducam_capture()) {
arudcam_upload_fifo(cli_motion_upload_ip(), 8000);
} else {
printf("Error: capture failed\n");
}
}
old_state = new_state;
}
} else {
old_state = false;
}
delay_ms(10);

#if 0
// gpio_write(pir_pin, old_state);
__digitalWrite(pir_pin, old_state);
printf("%s\n", old_state ? "On" : "Off");
old_state = !old_state;
delay_ms(5000);
#endif
}
}
#endif // CONFIG_NO_PIR
Expand Down
36 changes: 36 additions & 0 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,41 @@
#include <task.h>
#include <arducam.h>
#include <http_upload.h>
#include "cli.h"
#include "camdriver.h"


#define MAX_ARGC (10)

static bool motion_enabled = false;
static char upload_ip[16];

bool cli_motion_enabled(void)
{
return motion_enabled;
}

char* cli_motion_upload_ip(void)
{
return (char*) upload_ip;
}

static void cmd_motion(uint32_t argc, char *argv[])
{
if (argc >= 2) {
motion_enabled = strcmp(argv[1], "on") == 0;
if (motion_enabled) {
if (argc == 3)
strcpy(upload_ip, argv[2]);
else
printf("Error: missing upload ip number.\n");
}
} else {
printf("Error: wrong number of parameters.\n");
}

}

static void cmd_capture(uint32_t argc, char *argv[])
{
if (arducam_capture()) {
Expand Down Expand Up @@ -123,15 +153,20 @@ static void cmd_off(uint32_t argc, char *argv[])

static void cmd_help(uint32_t argc, char *argv[])
{
printf("motion:<on|off>[:<ip number>] Enable/disable image upload on motion detection");
printf("size:<size> Set JPEG size (see below)\n");
printf("upload:<hostname> Capture and upload image to host\n");
printf("capture Capture image to '/dev/null'\n");
printf("on:<gpio number>[:<gpio number>]+ Set gpio to 1\n");
printf("off:<gpio number>[:<gpio number>]+ Set gpio to 0\n");
printf("\nExample:\n");

printf(" motion:on:172.16.3.107<enter> when motion is detected, captures and uploads image to host running server.py\n");
printf(" motion:off<enter> disable motion detection\n");
printf(" upload:172.16.3.107<enter> captures and uploads image to host running server.py\n");
printf(" on:0<enter> switches on gpio 0\n");
printf(" on:0:2:4<enter> switches on gpios 0, 2 and 4\n");
printf("\n");
printf("Valid JPEG sizes are:\n");
printf(" 160x120\n");
printf(" 320x240\n");
Expand Down Expand Up @@ -160,6 +195,7 @@ static void handle_command(char *cmd)

if (strlen(argv[0]) > 0) {
if (strcmp(argv[0], "help") == 0) cmd_help(argc, argv);
else if (strcmp(argv[0], "motion") == 0) cmd_motion(argc, argv);
else if (strcmp(argv[0], "size") == 0) cmd_set_size(argc, argv);
else if (strcmp(argv[0], "capture") == 0) cmd_capture(argc, argv);
else if (strcmp(argv[0], "upload") == 0) cmd_capture_upload(argc, argv);
Expand Down
4 changes: 4 additions & 0 deletions cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#ifndef _CLI_H_
#define _CLI_H_

#include <stdbool.h>

void cli_init(void);
bool cli_motion_enabled(void);
char* cli_motion_upload_ip(void);

#endif // _CLI_H_

0 comments on commit de0d98d

Please sign in to comment.