-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpresense_Memory_Usage_Checker.ino
More file actions
144 lines (124 loc) · 4 KB
/
Spresense_Memory_Usage_Checker.ino
File metadata and controls
144 lines (124 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Spresense ESP8266ATLib Simple Camera Server
* Copyright 2019 Yoshino Taro
*
* This example demonstrates how to make a simple surveillance camera using ESP8266ATLib.
* Please note that this library is made for Spresense ESP8266 Wi-Fi Add-on board.
*
* After flushing this example to Spresense,
* access an IP address with cam.jpg like http://xxx.xxx.xxx.xxx/cam.jpg by a browser,
* then you can see a captured image on the page
*
* This example code is under LGPL v2.1
* (because Arduino library of Spresense is under LGPL v2.1)
*/
#include <Camera.h>
#include "ESP8266ATLib.h"
#include "MM-S50MV.h"
// #define SAP_MODE
#define BAUDRATE 115200
#define BUFSIZE 2048
#ifdef SAP_MODE
#define SSID "SprESP8266AP"
#define PASS "123123123"
#else
#define SSID "Chintaka-Lab"
#define PASS "65596357"
#endif
void setup() {
Serial.begin(115200);
theCamera.begin();
Serial.println("Set Auto white balance parameter");
theCamera.setAutoWhiteBalanceMode(CAM_WHITE_BALANCE_FLUORESCENT);
theCamera.setStillPictureImageFormat(320, 240, CAM_IMAGE_PIX_FMT_JPG);
MMS50MV.begin();
MMS50MV.skip(256);
delay(500);
MMS50MV.set(MMS50MV_CMD_MODE, MMS50MV_MODE_SYNC); // sync mode.
delay(500);
MMS50MV.skip(256);
MMS50MV.sync(); // sync.
MMS50MV.set(MMS50MV_CMD_MODE, MMS50MV_MODE_NOMAL); // normal mode.
delay(500);
MMS50MV.set(0x10, 0); // 256frames/s
delay(500);
MMS50MV.skip(256);
int32_t dis = MMS50MV.get();
printf("dis=%ld(mm)\n", dis);
dis = MMS50MV.get();
printf("dis=%ld(mm)\n", dis);
dis = MMS50MV.get();
printf("dis=%ld(mm)\n", dis);
esp8266at.begin(BAUDRATE);
#ifdef SAP_MODE
esp8266at.espStartAP(SSID, PASS);
#else
esp8266at.espConnectAP(SSID, PASS);
#endif
digitalWrite(LED0, HIGH);
esp8266at.setupTcpServer("80");
digitalWrite(LED1, HIGH);
Serial.println();
Serial.println();
Serial.println("---------------------------------------------");
Serial.println("Try to access the address below.");
Serial.println("http://" + esp8266at.getLocalIP() + "/cam.jpg");
Serial.println();
Serial.println("You can see a captured picture on the page");
Serial.println("---------------------------------------------");
Serial.println();
dis = MMS50MV.get();
printf("dis=%ld(mm)\n", dis);
// esp8266at.setWaitTime(100);
}
void loop() {
digitalWrite(LED2, LOW);
String linkID = "";
String s = esp8266at.espListenToClient(&linkID);
boolean result = false;
if (!(s.startsWith("+IPD") && s.indexOf("HTTP/1"))) return;
if (s.indexOf("GET") < 0) return; // only GET acceptable
Serial.println(s);
if (s.indexOf("cam.jpg") != -1) {
digitalWrite(LED2, HIGH);
int len = 0;
uint8_t* imgbuf = NULL;
while (len == 0) {
Serial.println("Taking a picture");
CamImage img = theCamera.takePicture();
len = img.getImgSize();
imgbuf = img.getImgBuff();
Serial.println("Image Size: " + String(len));
delay(10);
}
String msg = "HTTP/1.1 200 OK\r\n";
msg += "Content-Type: image/jpeg\r\n";
msg += "Content-Length: ";
msg += String(len) + "\r\n";
msg += "\r\n";
Serial.print(msg);
esp8266at.sendMessageToClient(linkID, msg);
for (; len > 0; imgbuf += BUFSIZE, len -= BUFSIZE) {
uint16_t sendDataSize = min(len, BUFSIZE);
Serial.println("data size: " + String(sendDataSize));
result = esp8266at.sendBinaryToClient(linkID, imgbuf, sendDataSize);
if (!result) {
Serial.println("Send data is fault");
break;
}
}
digitalWrite(LED2, LOW);
} else {
String uri = "http://" + esp8266at.getLocalIP() + "/cam.jpg";
String msg = "HTTP/1.1 200 OK\r\n";
msg += "Content-Type: text/html\r\n";
msg += "\r\n";
Serial.print(msg);
esp8266at.sendMessageToClient(linkID, msg);
msg = "<html>please access <a href=" + uri + ">" + uri + "</a></html>\r\n";
esp8266at.sendMessageToClient(linkID, msg);
}
Serial.println("Connection closed: " + linkID);
esp8266at.closeClientConnection(linkID);
delay(100);
}