Skip to content

Commit 7b6d868

Browse files
committed
Camera example added
1 parent c5a606a commit 7b6d868

File tree

2 files changed

+469
-0
lines changed

2 files changed

+469
-0
lines changed

examples/Camera/Camera.ino

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/*
2+
*
3+
* Example for how to use SinricPro Camera device:
4+
* - update the Sinric Pro ESP SDK to v2.5.1 or newer
5+
* - update the Sinric Pro App to v2.7.1 or newer
6+
* - setup a camera device
7+
* - handle request using callback (turn on/off builtin led indicating device power state)
8+
*
9+
* If you encounter any issues:
10+
* - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
11+
* - ensure all dependent libraries are installed
12+
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
13+
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
14+
* - open serial monitor and check whats happening
15+
* - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
16+
* - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
17+
*/
18+
19+
// Uncomment the following line to enable serial debug output
20+
//#define ENABLE_DEBUG
21+
22+
#ifdef ENABLE_DEBUG
23+
#define DEBUG_ESP_PORT Serial
24+
#define NODEBUG_WEBSOCKETS
25+
#define NDEBUG
26+
#endif
27+
28+
#include <WiFi.h>
29+
#include <WebServer.h>
30+
#include <WiFiClient.h>
31+
32+
#include "SinricPro.h"
33+
#include "SinricProSwitch.h"
34+
35+
#include "SimStreamer.h"
36+
#include "OV2640Streamer.h"
37+
#include "CRtspSession.h"
38+
39+
// Select your camera model
40+
41+
//#define CAMERA_MODEL_WROVER_KIT
42+
#define T_Camera_V17_VERSION
43+
//#define CAMERA_MODEL_M5STACK_PSRAM
44+
//#define CAMERA_MODEL_M5STACK_WIDE
45+
//#define CAMERA_MODEL_AI_THINKER
46+
47+
#include "select_pins.h"
48+
49+
#define WIFI_SSID "YOUR-WIFI-SSID"
50+
#define WIFI_PASSWD "YOUR-WIFI-PASSWORD"
51+
52+
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx". Get it from https://portal.sinric.pro/ -> Credentials
53+
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" . Get it from https://portal.sinric.pro/ -> Credentials
54+
#define CAMERA_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx". Get it from https://portal.sinric.pro/ -> Devices
55+
56+
OV2640 cam;
57+
CStreamer *streamer;
58+
59+
WiFiServer rtspServer(8554);
60+
61+
// Optional but important for stability.
62+
IPAddress local_IP(192, 168, 1, 124);
63+
IPAddress gateway(192, 168, 1, 1);
64+
IPAddress subnet(255, 255, 255, 0);
65+
IPAddress primaryDNS(8, 8, 8, 8); //optional
66+
IPAddress secondaryDNS(8, 8, 4, 4); //optional
67+
68+
69+
bool onPowerState(const String &deviceId, bool &state) {
70+
Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off");
71+
return true; // request handled properly
72+
}
73+
74+
75+
// setup function for SinricPro
76+
void setupSinricPro() {
77+
// add device to SinricPro
78+
SinricProSwitch& mySwitch = SinricPro[CAMERA_ID];
79+
80+
// set callback function to device
81+
mySwitch.onPowerState(onPowerState);
82+
83+
// setup SinricPro
84+
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); });
85+
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
86+
SinricPro.begin(APP_KEY, APP_SECRET);
87+
}
88+
89+
void setupWiFi() {
90+
IPAddress ip;
91+
92+
// Configures static IP address
93+
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
94+
Serial.println("Failed to configure IP");
95+
}
96+
97+
WiFi.begin(WIFI_SSID, WIFI_PASSWD);
98+
while (WiFi.status() != WL_CONNECTED) {
99+
delay(500);
100+
Serial.print(".");
101+
}
102+
103+
Serial.println("");
104+
Serial.println("WiFi connected");
105+
Serial.println("IP address: ");
106+
Serial.println(WiFi.localIP());
107+
}
108+
109+
void setupCamera() {
110+
camera_config_t config;
111+
config.ledc_channel = LEDC_CHANNEL_0;
112+
config.ledc_timer = LEDC_TIMER_0;
113+
config.pin_d0 = Y2_GPIO_NUM;
114+
config.pin_d1 = Y3_GPIO_NUM;
115+
config.pin_d2 = Y4_GPIO_NUM;
116+
config.pin_d3 = Y5_GPIO_NUM;
117+
config.pin_d4 = Y6_GPIO_NUM;
118+
config.pin_d5 = Y7_GPIO_NUM;
119+
config.pin_d6 = Y8_GPIO_NUM;
120+
config.pin_d7 = Y9_GPIO_NUM;
121+
config.pin_xclk = XCLK_GPIO_NUM;
122+
config.pin_pclk = PCLK_GPIO_NUM;
123+
config.pin_vsync = VSYNC_GPIO_NUM;
124+
config.pin_href = HREF_GPIO_NUM;
125+
config.pin_sscb_sda = SIOD_GPIO_NUM;
126+
config.pin_sscb_scl = SIOC_GPIO_NUM;
127+
config.pin_pwdn = PWDN_GPIO_NUM;
128+
config.pin_reset = RESET_GPIO_NUM;
129+
config.xclk_freq_hz = 20000000;
130+
config.pixel_format = PIXFORMAT_JPEG; //PIXFORMAT_YUV422 PIXFORMAT_GRAYSCALE PIXFORMAT_RGB565 PIXFORMAT_JPEG
131+
132+
133+
/*
134+
FRAMESIZE_UXGA (1600 x 1200)
135+
FRAMESIZE_QVGA (320 x 240)
136+
FRAMESIZE_CIF (352 x 288)
137+
FRAMESIZE_VGA (640 x 480)
138+
FRAMESIZE_SVGA (800 x 600)
139+
FRAMESIZE_XGA (1024 x 768)
140+
FRAMESIZE_SXGA (1280 x 1024)
141+
*/
142+
143+
if(psramFound()){
144+
Serial.println("psram found");
145+
config.frame_size = FRAMESIZE_UXGA;
146+
config.jpeg_quality = 40; //10-63 lower number means higher quality
147+
config.fb_count = 2;
148+
} else {
149+
Serial.println("psram not found");
150+
config.frame_size = FRAMESIZE_VGA;
151+
config.jpeg_quality = 12;
152+
config.fb_count = 1;
153+
}
154+
155+
cam.init(config);
156+
}
157+
158+
void setupStreaming() {
159+
rtspServer.begin();
160+
//streamer = new SimStreamer(true); // our streamer for UDP/TCP based RTP transport
161+
streamer = new OV2640Streamer(cam); // our streamer for UDP/TCP based RTP transport
162+
}
163+
164+
void handleStreaming() {
165+
uint32_t msecPerFrame = 100;
166+
static uint32_t lastimage = millis();
167+
168+
// If we have an active client connection, just service that until gone
169+
streamer->handleRequests(0); // we don't use a timeout here,
170+
// instead we send only if we have new enough frames
171+
uint32_t now = millis();
172+
if(streamer->anySessions()) {
173+
if(now > lastimage + msecPerFrame || now < lastimage) { // handle clock rollover
174+
streamer->streamImage(now);
175+
lastimage = now;
176+
177+
// check if we are overrunning our max frame rate
178+
now = millis();
179+
if(now > lastimage + msecPerFrame) {
180+
printf("warning exceeding max frame rate of %d ms\n", now - lastimage);
181+
}
182+
}
183+
}
184+
185+
WiFiClient rtspClient = rtspServer.accept();
186+
if(rtspClient) {
187+
Serial.print("client: ");
188+
Serial.print(rtspClient.remoteIP());
189+
Serial.println();
190+
streamer->addSession(rtspClient);
191+
}
192+
}
193+
194+
void setup()
195+
{
196+
Serial.begin(115200);
197+
while (!Serial); //wait for serial connection.
198+
199+
setupCamera();
200+
setupWiFi();
201+
setupSinricPro();
202+
setupStreaming();
203+
}
204+
205+
void loop()
206+
{
207+
SinricPro.handle();
208+
handleStreaming();
209+
}

0 commit comments

Comments
 (0)