Skip to content

Commit e12af4d

Browse files
author
wamisnet
committed
カメラのサンプルコード追加
1 parent dbd6043 commit e12af4d

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
#include <Nefry.h>
2+
#include <WiFiClientSecure.h>
3+
HardwareSerial Serial1(1);
4+
5+
6+
#define PIC_PKT_LEN 128 //data length of each read, dont set this too big because ram is limited
7+
#define PIC_FMT_VGA 7
8+
#define PIC_FMT_CIF 5
9+
#define PIC_FMT_OCIF 3
10+
#define CAM_ADDR 0
11+
#define PIC_FMT PIC_FMT_VGA
12+
13+
const byte cameraAddr = (CAM_ADDR << 5); // addr
14+
unsigned long picTotalLen = 0; // picture length
15+
unsigned long uptime;
16+
const char* host = "192.168.0.2";
17+
const char* page = "/nefry/pic.php";
18+
const char* full = "https://api.imgur.com/3/image";
19+
//const char* clid = "44f1d313889227a";
20+
21+
22+
23+
24+
/*********************************************************************/
25+
void setup() {
26+
/*
27+
Nefry.setStoreTitle("Host", 0);
28+
Nefry.setStoreTitle("Page", 1);
29+
String hostS = Nefry.getStoreStr(0);
30+
String pageS = Nefry.getStoreStr(1);
31+
hostS.toCharArray(host, length(host));
32+
pageS.toCharArray(page, length(page));
33+
*/
34+
Nefry.print(F("POST to "));
35+
Nefry.print(host);
36+
Nefry.println(page);
37+
38+
39+
Serial1.begin(115200, SERIAL_8N1, 19, 18);
40+
initialize();
41+
preCapture();
42+
Nefry.enableSW();
43+
}
44+
/*********************************************************************/
45+
void loop() {
46+
Nefry.setLed(0, 255, 0);
47+
if (Nefry.readSW()) {
48+
delay(500);
49+
Capture();
50+
GetData();
51+
}
52+
}
53+
/*********************************************************************/
54+
void clearRxBuf() {
55+
while (Serial1.available()) {
56+
Serial1.read();
57+
}
58+
}
59+
/*********************************************************************/
60+
void sendCmd(char cmd[], int cmd_len) {
61+
for (int i = 0; i < cmd_len; i++) Serial1.print(cmd[i]);
62+
}
63+
/*********************************************************************/
64+
void initialize() {
65+
char cmd[] = {0xaa, 0x0d | cameraAddr, 0x00, 0x00, 0x00, 0x00};
66+
unsigned char resp[6];
67+
68+
Nefry.print("initializing camera...");
69+
70+
Serial1.setTimeout(500);
71+
while (true) {
72+
sendCmd(cmd, 6);
73+
if (Serial1.readBytes((char *)resp, 6) != 6) {
74+
Nefry.print(".");
75+
continue;
76+
}
77+
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x0d && resp[4] == 0 && resp[5] == 0) {
78+
if (Serial1.readBytes((char *)resp, 6) != 6) continue;
79+
if (resp[0] == 0xaa && resp[1] == (0x0d | cameraAddr) && resp[2] == 0 && resp[3] == 0 && resp[4] == 0 && resp[5] == 0) break;
80+
}
81+
}
82+
cmd[1] = 0x0e | cameraAddr;
83+
cmd[2] = 0x0d;
84+
sendCmd(cmd, 6);
85+
Nefry.println("\nCamera initialization done.");
86+
}
87+
/*********************************************************************/
88+
void preCapture() {
89+
char cmd[] = { 0xaa, 0x01 | cameraAddr, 0x00, 0x07, 0x00, PIC_FMT };
90+
unsigned char resp[6];
91+
92+
while (true) {
93+
clearRxBuf();
94+
sendCmd(cmd, 6);
95+
if (Serial1.readBytes((char *)resp, 6) != 6) continue;
96+
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x01 && resp[4] == 0 && resp[5] == 0) break;
97+
}
98+
}
99+
void Capture() {
100+
char cmd[] = { 0xaa, 0x06 | cameraAddr, 0x08, PIC_PKT_LEN & 0xff, (PIC_PKT_LEN>>8) & 0xff ,0};
101+
unsigned char resp[6];
102+
103+
while (true) {
104+
clearRxBuf();
105+
sendCmd(cmd, 6);
106+
if (Serial1.readBytes((char *)resp, 6) != 6) continue;
107+
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x06 && resp[4] == 0 && resp[5] == 0) break;
108+
}
109+
cmd[1] = 0x05 | cameraAddr;
110+
cmd[2] = 0;
111+
cmd[3] = 0;
112+
cmd[4] = 0;
113+
cmd[5] = 0;
114+
while (true) {
115+
clearRxBuf();
116+
sendCmd(cmd, 6);
117+
if (Serial1.readBytes((char *)resp, 6) != 6) continue;
118+
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x05 && resp[4] == 0 && resp[5] == 0) break;
119+
}
120+
cmd[1] = 0x04 | cameraAddr;
121+
cmd[2] = 0x1;
122+
while (true) {
123+
clearRxBuf();
124+
sendCmd(cmd, 6);
125+
if (Serial1.readBytes((char *)resp, 6) != 6) continue;
126+
if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x04 && resp[4] == 0 && resp[5] == 0) {
127+
Serial1.setTimeout(1000);
128+
if (Serial1.readBytes((char *)resp, 6) != 6) continue;
129+
if (resp[0] == 0xaa && resp[1] == (0x0a | cameraAddr) && resp[2] == 0x01) {
130+
picTotalLen = (resp[3]) | (resp[4] << 8) | (resp[5] << 16);
131+
Nefry.print("picTotalLen:");
132+
Nefry.println(picTotalLen);
133+
break;
134+
}
135+
}
136+
}
137+
}
138+
/*********************************************************************/
139+
void GetData() {
140+
unsigned int pktCnt = (picTotalLen) / (PIC_PKT_LEN - 6);
141+
if ((picTotalLen % (PIC_PKT_LEN-6)) != 0) pktCnt += 1;
142+
143+
char cmd[] = { 0xaa, 0x0e | cameraAddr, 0x00, 0x00, 0x00, 0x00 };
144+
unsigned char pkt[PIC_PKT_LEN];
145+
146+
147+
148+
149+
/*********************************************************************/
150+
151+
WiFiClient client;
152+
//client.setCACert(test_root_ca);
153+
reconnect:
154+
Nefry.setLed(255, 0, 0);
155+
Nefry.println("Try to connect..");
156+
if (!client.connect(host, 80)) {
157+
Nefry.setLed(0, 0, 0);
158+
client.stop();
159+
delay(1000);
160+
goto reconnect;
161+
}
162+
//
163+
Nefry.println("SendPict()..");
164+
Nefry.setLed(255, 0, 0);
165+
/*
166+
client.print(String("GET ") + page + F(" HTTP/1.1\n") +
167+
F("Host: ") + host + F("\n") +
168+
F("Connection: close\n\n"));
169+
*/
170+
client.print(String("POST ") + page + F(" HTTP/1.1\n") +
171+
F("Host: ") + host + F("\n") +
172+
//F("Authorization: Client-ID ") + String(clid) + F("\n") +
173+
F("Content-Type: image/jpeg\n") +
174+
F("Content-Length: ") + String(picTotalLen) + F("\n") +
175+
F("Connection: close\n\n"));
176+
Nefry.print(F("Content-Length: ")); Nefry.println(picTotalLen);
177+
Nefry.print(F("HTTP Sending..... "));
178+
179+
180+
181+
182+
183+
184+
185+
Serial1.setTimeout(1000);
186+
for (unsigned int i = 0; i < pktCnt; i++) {
187+
Nefry.print(".");
188+
cmd[4] = i & 0xff;
189+
cmd[5] = (i >> 8) & 0xff;
190+
191+
int retry_cnt = 0;
192+
retry:
193+
delay(10);
194+
clearRxBuf();
195+
sendCmd(cmd, 6);
196+
uint16_t cnt = Serial1.readBytes((char *)pkt, PIC_PKT_LEN);
197+
unsigned char sum = 0;
198+
for (int y = 0; y < cnt - 2; y++) {
199+
sum += pkt[y];
200+
}
201+
if (sum != pkt[cnt-2]) {
202+
if (++retry_cnt < 100) goto retry;
203+
else break;
204+
}
205+
206+
client.write((const uint8_t *)&pkt[4], cnt-6);
207+
client.flush();
208+
//if (cnt != PIC_PKT_LEN) break;
209+
}
210+
cmd[4] = 0xf0;
211+
cmd[5] = 0xf0;
212+
sendCmd(cmd, 6);
213+
214+
215+
216+
delay(1000);
217+
if (client.available()) {
218+
String line = client.readStringUntil('\r');
219+
Nefry.print(F("Responce: ")); Nefry.println(line);
220+
}
221+
client.stop();
222+
Nefry.println("Picture sent.");
223+
}
224+
225+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
if ($_SERVER["REQUEST_METHOD"] == "GET") {
3+
$file_list = scandir("./pic", 1);
4+
foreach ($file_list as $ff) { if(is_file("./pic/".$ff)) echo $ff." (".filesize("./pic/".$ff)." bytes)<br>\r\n".'<img src="./pic/'.$ff.'">'."<br>\r\n"; }
5+
} elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
6+
$data = file_get_contents("php://input");
7+
$newfile = "./pic/".date("Ymd_His").".jpg";
8+
file_put_contents($newfile, $data);
9+
}

0 commit comments

Comments
 (0)