Can you help me get this code to work? Thank you. I have not included all of the files because there is a space limit here. I am open adding more if necessary.
I am using an ESP32 camera with a microwave sensor and a fan. I am trying to upload my Arduino sketch using a FTDI232 via USB. I paid someone off of Fiverr to write the code because I was not able to do it myself and could not find anyone locally. My smartphone is iOS XR. I am trying to use BlynkEdgent so that when I take my device to a new location I do not have upload a sketch. Here is the error message I received when I tried to upload the sketch.
Arduino: 1.8.15 (Windows 10), Board: "AI Thinker ESP32-CAM, 240MHz (WiFi/BT), QIO, 80MHz"
Sketch uses 2921798 bytes (92%) of program storage space. Maximum is 3145728 bytes.
Global variables use 61044 bytes (18%) of dynamic memory, leaving 266636 bytes for local variables. Maximum is 327680 bytes.
esptool.py v3.0-dev
Serial port COM3
Connecting........_____....._____....._____.
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 94:b9:7e:c3:78:34
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 8192 bytes to 47...
Writing at 0x0000e000... (100 %)
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.0 seconds (effective 4681.2 kbit/s)...
A fatal error occurred: Invalid head of packet (0x78)
A fatal error occurred: Invalid head of packet (0x78)
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Combined_v1
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""
#include "src/OV2640.h"
#include <WiFi.h>
#include <WebServer.h>
#include <WiFiClient.h>
#include "esp_camera.h"
//blynk
//#include <BlynkSimpleEsp32.h>
#define BLYNK_FIRMWARE_TYPE "none"
#define BLYNK_FIRMWARE_VERSION "0.1.4"
#define BLYNK_PRINT Serial
#define APP_DEBUG
#include "BlynkEdgent.h"
//camera
#define CAMERA_MODEL_AI_THINKER
#include "camera_pins.h"
OV2640 cam;
//picture notification
String my_Local_IP;
void startCameraServer();
//wifi
#define SSID1 ""
#define PWD1 ""
//microwave sensor
#define RADAR 15
//fan
#define fanPin 4
unsigned long previousMillis = 0;
const long interval = 10000; // 30 minutes --> 30min * 60sec/min * 1000ms/sec = 1800000ms
//blynk -> for line notification
char auth[] = "";
//WebServer server(80);
const char HEADER[] = "HTTP/1.1 200 OK\r\n" \
"Access-Control-Allow-Origin: *\r\n" \
"Content-Type: multipart/x-mixed-replace; boundary=123456789000000000000987654321\r\n";
const char BOUNDARY[] = "\r\n--123456789000000000000987654321\r\n";
const char CTNTTYPE[] = "Content-Type: image/jpeg\r\nContent-Length: ";
const int hdrLen = strlen(HEADER);
const int bdrLen = strlen(BOUNDARY);
const int cntLen = strlen(CTNTTYPE);
void notifyMotion() {
Serial.println("Sent notification");
Blynk.notify("Motion Detected open to view picture and stream!");
}
void capture(){
uint32_t number = random(40000000);
Blynk.notify("Here is the image that was captured..");
Serial.println("image captured");
Serial.println("http://"+my_Local_IP+"/capture?_cb="+ (String)number);
Blynk.setProperty(V1, "urls", "http://"+my_Local_IP+"/capture?_cb="+(String)number);
delay(1000);
}
void handle_jpg_stream(void)
{
char buf[32];
int s;
WiFiClient client = server.client();
client.write(HEADER, hdrLen);
client.write(BOUNDARY, bdrLen);
while (true)
{
if (!client.connected()) break;
cam.run();
s = cam.getSize();
client.write(CTNTTYPE, cntLen);
sprintf( buf, "%d\r\n\r\n", s );
client.write(buf, strlen(buf));
client.write((char *)cam.getfb(), s);
client.write(BOUNDARY, bdrLen);
}
}
const char JHEADER[] = "HTTP/1.1 200 OK\r\n" \
"Content-disposition: inline; filename=capture.jpg\r\n" \
"Content-type: image/jpeg\r\n\r\n";
const int jhdLen = strlen(JHEADER);
void handle_jpg(void)
{
WiFiClient client = server.client();
cam.run();
if (!client.connected()) return;
client.write(JHEADER, jhdLen);
client.write((char *)cam.getfb(), cam.getSize());
}
void handleNotFound()
{
String message = "Server is running!\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
server.send(200, "text / plain", message);
}
void setup()
{
pinMode(fanPin, OUTPUT);
Serial.begin(115200);
delay(100);
//while (!Serial); //wait for serial connection.
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.pixel_format = PIXFORMAT_JPEG;
// Frame parameters
// config.frame_size = FRAMESIZE_UXGA;
config.frame_size = FRAMESIZE_QVGA;
config.jpeg_quality = 12;
config.fb_count = 2;
cam.init(config);
BlynkEdgent.begin();
IPAddress ip;
WiFi.mode(WIFI_STA);
WiFi.begin(SSID1, PWD1);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(F("."));
}
ip = WiFi.localIP();
my_Local_IP = ip.toString();
Serial.println(F("WiFi connected"));
Serial.println("");
Serial.println(ip);
Serial.print("Stream Link: http://");
Serial.print(ip);
Serial.println("/mjpeg/1");
server.on("/mjpeg/1", HTTP_GET, handle_jpg_stream);
server.on("/jpg", HTTP_GET, handle_jpg);
server.onNotFound(handleNotFound);
server.begin();
//camera server
startCameraServer();
Serial.print("Camera Ready! Use 'http://");
Serial.print(ip);
Serial.println("' to connect");
Blynk.begin(auth, SSID1, PWD1);
}
void switchFan() {
digitalWrite(fanPin, HIGH);
delay(2000);
digitalWrite(fanPin, LOW);
}
void loop()
{
unsigned long currentMillis = millis();
BlynkEdgent.run();
//Blynk.run();
//server.handleClient();
if (currentMillis - previousMillis >= interval) {
// save the last time you turned on the fan
previousMillis = currentMillis;
Serial.println("Switching fan on");
switchFan();
notifyMotion();
}
if (digitalRead(RADAR) == LOW) {
//notifyMotion();
//capture();
}
}
Sketch tab named BlynkEdgent.h
extern "C" {
void app_loop();
void eraseMcuConfig();
void restartMCU();
}
#include "Settings.h"
#include <BlynkSimpleEsp32_SSL.h>
#ifndef BLYNK_NEW_LIBRARY
#error "Old version of Blynk library is in use. Please replace it with the new one."
#endif
#if !defined(BLYNK_TEMPLATE_ID) || !defined(BLYNK_DEVICE_NAME)
#error "Please specify your BLYNK_TEMPLATE_ID and BLYNK_DEVICE_NAME"
#endif
#include "BlynkState.h"
#include "ConfigStore.h"
#include "ResetButton.h"
#include "ConfigMode.h"
#include "Indicator.h"
#include "OTA.h"
#include "Console.h"
inline
void BlynkState::set(State m) {
if (state != m && m < MODE_MAX_VALUE) {
DEBUG_PRINT(String(StateStr[state]) + " => " + StateStr[m]);
state = m;
// You can put your state handling here,
// i.e. implement custom indication
}
}
void printDeviceBanner()
{
Blynk.printBanner();
DEBUG_PRINT("--------------------------");
DEBUG_PRINT(String("Product: ") + BLYNK_DEVICE_NAME);
DEBUG_PRINT(String("Firmware: ") + BLYNK_FIRMWARE_VERSION " (build " __DATE__ " " __TIME__ ")");
if (configStore.getFlag(CONFIG_FLAG_VALID)) {
DEBUG_PRINT(String("Token: ...") + (configStore.cloudToken+28));
}
DEBUG_PRINT(String("Device: ") + BLYNK_INFO_DEVICE + " @ " + ESP.getCpuFreqMHz() + "MHz");
DEBUG_PRINT(String("MAC: ") + WiFi.macAddress());
DEBUG_PRINT(String("Flash: ") + ESP.getFlashChipSize() / 1024 + "K");
DEBUG_PRINT(String("ESP sdk: ") + ESP.getSdkVersion());
DEBUG_PRINT(String("Chip rev: ") + ESP.getChipRevision());
DEBUG_PRINT(String("Free mem: ") + ESP.getFreeHeap());
DEBUG_PRINT("--------------------------");
}
void runBlynkWithChecks() {
Blynk.run();
if (BlynkState::get() == MODE_RUNNING) {
if (!Blynk.connected()) {
if (WiFi.status() == WL_CONNECTED) {
BlynkState::set(MODE_CONNECTING_CLOUD);
} else {
BlynkState::set(MODE_CONNECTING_NET);
}
}
}
}
class Edgent {
public:
void begin()
{
WiFi.persistent(false);
WiFi.enableSTA(true); // Needed to get MAC
indicator_init();
button_init();
config_init();
console_init();
printDeviceBanner();
if (configStore.getFlag(CONFIG_FLAG_VALID)) {
BlynkState::set(MODE_CONNECTING_NET);
} else if (config_load_blnkopt()) {
DEBUG_PRINT("Firmware is preprovisioned");
BlynkState::set(MODE_CONNECTING_NET);
} else {
BlynkState::set(MODE_WAIT_CONFIG);
}
}
void run() {
app_loop();
switch (BlynkState::get()) {
case MODE_WAIT_CONFIG:
case MODE_CONFIGURING: enterConfigMode(); break;
case MODE_CONNECTING_NET: enterConnectNet(); break;
case MODE_CONNECTING_CLOUD: enterConnectCloud(); break;
case MODE_RUNNING: runBlynkWithChecks(); break;
case MODE_OTA_UPGRADE: enterOTA(); break;
case MODE_SWITCH_TO_STA: enterSwitchToSTA(); break;
case MODE_RESET_CONFIG: enterResetConfig(); break;
default: enterError(); break;
}
}
};
Edgent BlynkEdgent;
BlynkTimer edgentTimer;
void app_loop() {
edgentTimer.run();
edgentConsole.run();
}
BlynkState.h
enum State {
MODE_WAIT_CONFIG,
MODE_CONFIGURING,
MODE_CONNECTING_NET,
MODE_CONNECTING_CLOUD,
MODE_RUNNING,
MODE_OTA_UPGRADE,
MODE_SWITCH_TO_STA,
MODE_RESET_CONFIG,
MODE_ERROR,
MODE_MAX_VALUE
};
#if defined(APP_DEBUG)
const char* StateStr[MODE_MAX_VALUE+1] = {
"WAIT_CONFIG",
"CONFIGURING",
"CONNECTING_NET",
"CONNECTING_CLOUD",
"RUNNING",
"OTA_UPGRADE",
"SWITCH_TO_STA",
"RESET_CONFIG",
"ERROR",
"INIT"
};
#endif
namespace BlynkState
{
volatile State state = MODE_MAX_VALUE;
State get() { return state; }
bool is (State m) { return (state == m); }
void set(State m);
};