|
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <ArduinoJson.h>
- #include <HTTPClient.h>
- #include <EEPROM.h>
- #include <Ticker.h>
- #include "DHT.h" //加載DHT11的庫(kù)
- #include "esp_http_client.h"
- #include "esp_camera.h"
- #include "configuration.h"
- #include "camera_pins.h"
- char config_flag = 0; //配網(wǎng)成功標(biāo)識(shí)
- //tcp客戶端相關(guān)
- String TcpClient_Buff = "";
- unsigned int TcpClient_BuffIndex = 0;
- unsigned long TcpClient_preTick = 0;
- unsigned long preHeartTick = 0;//心跳
- unsigned long preTCPStartTick = 0;//連接
- String over_int = "false";
- bool preTCPConnected = false;
- String UID = "c124bafd03ba4909b1f7794c3e38af32"; //巴法云私鑰
- String TOPIC = "mytrash"; //主題名字
- //圖像拍照上傳相關(guān)
- int capture_interval = 5 * 1000; // 20秒上傳一次
- const char* uid = "c124bafd03ba4909b1f7794c3e38af32"; //私鑰
- const char* topic = "mytrash"; //主題
- const char* wechatMsg = ""; //如果不為空,會(huì)推送到
- const char* wecomMsg = ""; //如果不為空,會(huì)推送到企業(yè),推送到企業(yè)的消息
- const char* urlPath = ""; //如果不為空,會(huì)生成自定義圖片鏈接,自定義圖片上傳后返回的圖片url,url前一部分為巴法云域名,第二部分:私鑰+主題名的md5值,第三部分為設(shè)置的圖片鏈接值。
- bool flashRequired = true; //閃光燈,true是打開閃光燈
- const int brightLED = 4; //閃光燈引腳
- const int sensorPin = 14; // 溢出檢測(cè)引腳
- unsigned char sendp = 0;
- int sensorValue = 0;
- int connect_time = 0;
- const char* post_url = "imagesbemfa/upload/v1/upimages.php"; // 默認(rèn)上傳地址
- static String httpResponseString; //接收服務(wù)器返回信息
- bool internet_connected = false;
- long current_millis;
- long last_capture_millis = 0;
- // Bright LED (Flash)
- const int ledFreq = 50; // PWM settings
- const int ledChannel = 15; // camera uses timer1
- const int ledRresolution = 8; // resolution (8 = from 0 to 255)
- struct config_type
- {
- char stassid[32];
- char stapsw[64];
- char cuid[40];
- char ctopic[32];
- uint8_t reboot;
- uint8_t magic;
- };
- config_type config;
- WiFiClient TCPclient;
- Ticker delayTimer;
- DHT dht(DHTPIN, DHTTYPE);//聲明 dht 函數(shù)
- /**
- * 存儲(chǔ)配網(wǎng)信息
- */
- void saveConfig()
- {
- int rand_key;
- uint8_t mac[6];
- WiFi.macAddress(mac);
- config.reboot = 0;
- EEPROM.begin(512);
- uint8_t *p = (uint8_t*)(&config);
- for (int i = 0; i < sizeof(config); i++)
- {
- EEPROM.write(i, *(p + i));
- }
- EEPROM.commit();
- }
-
- void delayRestart(float t)
- {
- delayTimer.attach(t, []()
- {
- ESP.restart();
- });
- }
- /**
- * airkiss配網(wǎng)
- */
- void doSmartconfig()
- {
- Serial.println("waiting for WeChat Config.....");
- Serial.print("*");
- Serial.print("*");
- Serial.print("*");
- Serial.print("*");
- Serial.print("*");
- Serial.println("*");
- WiFi.mode(WIFI_STA);
- WiFi.stopSmartConfig();
- WiFi.beginSmartConfig();
- int cnt = 0;
- bool flag_ok = false;
- while (!WiFi.smartConfigDone())
- {
- delay(300);
- if (flag_ok == true) continue;
- if (WiFi.smartConfigDone())
- {
- strcpy(config.stassid, WiFi.SSID().c_str());
- strcpy(config.stapsw, WiFi.psk().c_str());
- config.magic = MAGIC_NUMBER;
- saveConfig();
- flag_ok = true;
- }
- cnt++;
- if (cnt >= 600)
- {
- delayRestart(0);
- }
- }
- }
- /**
- * 設(shè)置SmartConfig
- */
- void setConfig()
- {
- String mac = WiFi.macAddress().substring(8);//取mac地址做主題用
- mac.replace(":", "");//去掉:號(hào)
- WiFiClient client_bemfa_WiFiClient;
- HTTPClient http_bemfa_HTTPClient;
- http_bemfa_HTTPClient.begin(client_bemfa_WiFiClient,"//probemfa/vv/setSmartConfig?version=1&user="+mac);
- int httpCode = http_bemfa_HTTPClient.GET();
- if (httpCode == 200)
- {
- Serial.println("wifi smartconfig ok");
- }
- http_bemfa_HTTPClient.end();
- }
- /**
- * 初始化wifi信息,并連接路由器網(wǎng)絡(luò)
- */
- void initWiFi()
- {
- char temp[32];
- uint8_t mac[6];
- WiFi.macAddress(mac);
- //sprintf(temp, "%s_%02X%02X%02X", HOST_NAME, mac[3], mac[4], mac[5]);
- WiFi.hostname(temp);
- if(WiFi.status() != WL_CONNECTED)
- {
- WiFi.disconnect();//斷開連接
- WiFi.mode(WIFI_STA);//STA模式
- WiFi.begin(config.stassid, config.stapsw);//連接路由器
- }
- Serial.print("Waiting for connect");
- while (WiFi.status() != WL_CONNECTED)
- {//檢查是否連接成功
- delay(500);
- Serial.print(".");
- connect_time++;
- if(connect_time >= 360)
- {
- connect_time = 0;
- Serial.println("wifi connect faild");
- }
- }
- if(config_flag == 1)
- {
- setConfig();
- }
- Serial.println("wifi is connected");
- //Serial.print("ssid:");
- //Serial.println(WiFi.SSID());
- //Serial.print("psw:");
- //Serial.println(WiFi.psk());
- WiFi.softAP(temp);
- }
- /**
- * 加載存儲(chǔ)的信息,并檢查是否進(jìn)行了反復(fù)5次重啟恢復(fù)出廠信息
- */
- uint8_t *p = (uint8_t*)(&config);
- void loadConfig()
- {
- uint8_t mac[6];
- WiFi.macAddress(mac);
- EEPROM.begin(512);
- for (int i = 0; i < sizeof(config); i++)
- {
- *(p + i) = EEPROM.read(i);
- }
- config.reboot = config.reboot + 1;
- if(config.reboot>=4)
- {
- restoreFactory();
- }
- if(config.magic != MAGIC_NUMBER)
- {
- config_flag = 1;
- }
- EEPROM.begin(512);
- for (int i = 0; i < sizeof(config); i++)
- {
- EEPROM.write(i, *(p + i));
- }
- EEPROM.commit();
- delay(2000);
- EEPROM.begin(512);
- config.reboot = 0;
- for (int i = 0; i < sizeof(config); i++)
- {
- EEPROM.write(i, *(p + i));
- }
- EEPROM.commit();
- delay(2000);
- }
- /**
- * 恢復(fù)出廠設(shè)置,清除存儲(chǔ)的wifi信息
- */
- void restoreFactory(){
- Serial.println("Restore Factory.......");
- config.magic = 0x00;
- strcpy(config.stassid, "");
- strcpy(config.stapsw, "");
- config.magic = 0x00;
- saveConfig();
- delayRestart(1);
- while (1)
- {
- delay(100);
- }
- }
- /**
- * 檢查是否需要airkiss配網(wǎng)
- */
- void waitKey()
- {
- if(config_flag == 1)
- {
- doSmartconfig();
- }
- }
- /*
- *發(fā)送數(shù)據(jù)到TCP服務(wù)器
- */
- void sendtoTCPServer(String p)
- {
- if (!TCPclient.connected())
- {
- //Serial.println("Client is not readly");
- return;
- }
- TCPclient.print(p);
- //Serial.println("[Send to TCPServer]:String");
- }
- /*
- *初始化和服務(wù)器建立連接
- */
- void startTCPClient()
- {
- if(TCPclient.connect(TCP_SERVER_ADDR, atoi(TCP_SERVER_PORT)))
- {
- //Serial.print("\nINFO:Connected to server:");
- //Serial.printf("%s:%d\r\n",TCP_SERVER_ADDR,atoi(TCP_SERVER_PORT));
- preTCPConnected = true;
- preHeartTick = millis();
- TCPclient.setNoDelay(true);
- sendtoTCPServer("cmd=1&uid="+UID+"&topic="+TOPIC+"\r\n");
- }
- else
- {
- //Serial.print("INFO:Failed connected to server:");
- //Serial.println(TCP_SERVER_ADDR);
- TCPclient.stop();
- preTCPConnected = false;
- }
- preTCPStartTick = millis();
- }
- /*
- *檢查數(shù)據(jù),發(fā)送數(shù)據(jù)
- */
- void doTCPClientTick()
- {
- //檢查是否斷開,斷開后重連
- if(WiFi.status() != WL_CONNECTED) return;
- if (!TCPclient.connected())
- {//斷開重連
- if(preTCPConnected == true)
- {
- preTCPConnected = false;
- preTCPStartTick = millis();
- Serial.println("TCP Client disconnected");
- TCPclient.stop();
- }
- else if(millis() - preTCPStartTick > 1*1000)//重新連接
- startTCPClient();
- }
- else
- {
- if (TCPclient.available())
- {//收數(shù)據(jù)
- char c =TCPclient.read();
- TcpClient_Buff +=c;
- TcpClient_BuffIndex++;
- TcpClient_preTick = millis();
- if(TcpClient_BuffIndex>=MAX_PACKETSIZE - 1)
- {
- TcpClient_BuffIndex = MAX_PACKETSIZE-2;
- TcpClient_preTick = TcpClient_preTick - 200;
- }
- preHeartTick = millis();
- }
- if(millis() - preHeartTick >= upDataTime)
- {//上傳數(shù)據(jù)
- preHeartTick = millis();
- float h = dht.readHumidity();//讀取濕度
- float t = dht.readTemperature();//讀取攝氏度
- if (isnan(h) || isnan(t))
- {
- Serial.println("Failed to read from DHT sensor!");
- return;
- } //檢查抓取是否成功
- /*********************數(shù)據(jù)上傳*******************/
- String upstr = "";
- String uart_str = "";
- upstr = "cmd=2&uid="+UID+"&topic="+TOPIC+"&msg=#"+over_int+"#"+t+"#"+h+"\r\n";
- sendtoTCPServer(upstr);
- uart_str = "DATA#"+over_int+"#"+t+"#"+h+"\r\n";
- Serial.print(uart_str);
- upstr = "";
- }
- }
- if((TcpClient_Buff.length() >= 1) && (millis() - TcpClient_preTick>=200))
- {//data ready
- TCPclient.flush();
- //Serial.print("Buff:");
- //Serial.println(TcpClient_Buff);
- TcpClient_Buff="";
- TcpClient_BuffIndex = 0;
- }
- }
- // change illumination LED brightness
- void brightLed(byte ledBrightness) {
- ledcWrite(ledChannel, ledBrightness); // change LED brightness (0 - 255)
- }
- // ----------------------------------------------------------------
- // set up PWM for the illumination LED (flash)
- // ----------------------------------------------------------------
- // note: I am not sure PWM is very reliable on the esp32cam - requires more testing
- void setupFlashPWM() {
- ledcSetup(ledChannel, ledFreq, ledRresolution);
- ledcAttachPin(brightLED, ledChannel);
- brightLed(0);
- }
- void Setint(){
- sendp = 1;
- }
- void setup() {
- Serial.begin(115200);
- pinMode(brightLED, OUTPUT); // flash LED
- pinMode(sensorPin, INPUT); // 將引腳設(shè)置為輸入模式
- digitalWrite(brightLED, LOW); // led off = Low
- attachInterrupt(2, Setint, FALLING); //當(dāng)電平發(fā)生變化時(shí),觸發(fā)中斷
- attachInterrupt(13, restoreFactory, FALLING); //當(dāng)電平發(fā)生變化時(shí),觸發(fā)中斷
- dht.begin(); //啟動(dòng)傳感器
- camera_config_t config;
- config.ledc_channel = LEDC_CHANNEL_0;
- config.ledc_timer = LEDC_TIMER_0;
- config.pin_d0 = Y2_GPIO_NUM;
- config.pin_d1 = Y3_GPIO_NUM;
- config.pin_d2 = Y4_GPIO_NUM;
- config.pin_d3 = Y5_GPIO_NUM;
- config.pin_d4 = Y6_GPIO_NUM;
- config.pin_d5 = Y7_GPIO_NUM;
- config.pin_d6 = Y8_GPIO_NUM;
- config.pin_d7 = Y9_GPIO_NUM;
- config.pin_xclk = XCLK_GPIO_NUM;
- config.pin_pclk = PCLK_GPIO_NUM;
- config.pin_vsync = VSYNC_GPIO_NUM;
- config.pin_href = HREF_GPIO_NUM;
- config.pin_sscb_sda = SIOD_GPIO_NUM;
- config.pin_sscb_scl = SIOC_GPIO_NUM;
- config.pin_pwdn = PWDN_GPIO_NUM;
- config.pin_reset = RESET_GPIO_NUM;
- config.xclk_freq_hz = 20000000;
- config.frame_size = FRAMESIZE_UXGA;
- config.pixel_format = PIXFORMAT_JPEG; // for streaming
- config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
- config.fb_location = CAMERA_FB_IN_PSRAM;
- config.jpeg_quality = 10;
- config.fb_count = 1;
- // if PSRAM IC present, init with UXGA resolution and higher JPEG quality
- // for larger pre-allocated frame buffer.
- if (config.pixel_format == PIXFORMAT_JPEG) {
- if (psramFound()) {
- config.jpeg_quality = 10;
- config.fb_count = 2;
- config.grab_mode = CAMERA_GRAB_LATEST;
- } else {
- // Limit the frame size when PSRAM is not available
- config.frame_size = FRAMESIZE_SVGA;
- config.fb_location = CAMERA_FB_IN_DRAM;
- }
- } else {
- // Best option for face detection/recognition
- config.frame_size = FRAMESIZE_240X240;
- #if CONFIG_IDF_TARGET_ESP32S3
- config.fb_count = 2;
- #endif
- }
- #if defined(CAMERA_MODEL_ESP_EYE)
- pinMode(13, INPUT_PULLUP);
- pinMode(14, INPUT_PULLUP);
- #endif
- // camera init
- esp_err_t err = esp_camera_init(&config);
- if (err != ESP_OK) {
- //Serial.printf("Camera init failed with error 0x%x", err);
- return;
- }
- sensor_t* s = esp_camera_sensor_get();
- // initial sensors are flipped vertically and colors are a bit saturated
- if (s->id.PID == OV3660_PID) {
- s->set_vflip(s, 1); // flip it back
- s->set_brightness(s, 1); // up the brightness just a bit
- s->set_saturation(s, -2); // lower the saturation
- }
- #if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
- s->set_vflip(s, 1);
- s->set_hmirror(s, 1);
- #endif
- #if defined(CAMERA_MODEL_ESP32S3_EYE)
- s->set_vflip(s, 1);
- #endif
- loadConfig();
- waitKey();
- initWiFi();
- //Serial.print("Camera Ready!:");
- //Serial.println(WiFi.localIP());
- setupFlashPWM(); // configure PWM for the illumination LED
- }
- /********http請(qǐng)求處理函數(shù)*********/
- esp_err_t _http_event_handler(esp_http_client_event_t* evt) {
- if (evt->event_id == HTTP_EVENT_ON_DATA) {
- httpResponseString.concat((char*)evt->data);
- }
- return ESP_OK;
- }
- static esp_err_t take_send_photo() {
- if (flashRequired) {
- brightLed(255);
- delay(300);
- }
- //Serial.println("take_send_photo...");
- camera_fb_t* fb = NULL;
- esp_err_t res = ESP_OK;
- fb = esp_camera_fb_get();
- if (flashRequired) brightLed(0); // change LED brightness back to previous state
- if (!fb) {
- //Serial.println("Camera capture failed...");
- return ESP_FAIL;
- }
- httpResponseString = "";
- esp_http_client_handle_t http_client;
- esp_http_client_config_t config_client = { 0 };
- config_client.url = post_url;
- config_client.event_handler = _http_event_handler;
- config_client.method = HTTP_METHOD_POST;
- http_client = esp_http_client_init(&config_client);
- esp_http_client_set_post_field(http_client, (const char*)fb->buf, fb->len); //設(shè)置http發(fā)送的內(nèi)容和長(zhǎng)度
- esp_http_client_set_header(http_client, "Content-Type", "image/jpg"); //設(shè)置http頭部字段
- esp_http_client_set_header(http_client, "Authorization", uid); //設(shè)置http頭部字段
- esp_http_client_set_header(http_client, "Authtopic", topic); //設(shè)置http頭部字段
- esp_http_client_set_header(http_client, "wechatmsg", wechatMsg); //設(shè)置http頭部字段
- esp_http_client_set_header(http_client, "wecommsg", wecomMsg); //設(shè)置http頭部字段
- esp_http_client_set_header(http_client, "picpath", urlPath); //設(shè)置http頭部字段
- esp_err_t err = esp_http_client_perform(http_client); //發(fā)送http請(qǐng)求
- if (err == ESP_OK) {
- //json數(shù)據(jù)解析
- StaticJsonDocument<200> doc;
- DeserializationError error = deserializeJson(doc, httpResponseString);
- if (error) {
- //Serial.print(F("deserializeJson() failed: "));
- //Serial.println(error.c_str());
- }
- }
- //Serial.println("Taking picture END");
- esp_camera_fb_return(fb);
- esp_http_client_cleanup(http_client);
- return res;
- }
- void loop()
- {
- sensorValue = digitalRead(sensorPin); // 讀取引腳的電平
- if (sensorValue == HIGH)
- {
- over_int = "true";
- }
- else
- {
- over_int = "false";
- }
- doTCPClientTick();
- // current_millis = millis();
- // if (current_millis - last_capture_millis > capture_interval)
- // {
- // last_capture_millis = millis();
- // take_send_photo();
- // }
- if (sendp == 1)
- { // Take another picture
- take_send_photo();
- sendp = 0;
- }
- }
復(fù)制代碼
|
-
圖片1.png
(688.86 KB, 下載次數(shù): 40)
下載附件
2024-5-6 21:22 上傳
-
-
esp32camv2.zip
2024-5-6 21:23 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
6.9 KB, 下載次數(shù): 9, 下載積分: 黑幣 -5
esp32-cam代碼
-
-
trash.zip
2024-5-6 21:24 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
7.63 KB, 下載次數(shù): 9, 下載積分: 黑幣 -5
小程序代碼
評(píng)分
-
查看全部評(píng)分
|