Skip to content

Commit dce1f2d

Browse files
committed
SDK
0 parents  commit dce1f2d

File tree

8 files changed

+994
-0
lines changed

8 files changed

+994
-0
lines changed

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# BitFlash_Client
2+
An SDK for enabling simultaneous firmware deployment to multiple IoT devices at once.
3+
4+
## Features
5+
- Background firmware updates
6+
- Automatic WiFi connection management
7+
- Version checking against remote server
8+
- Progress tracking and status callbacks
9+
- Configurable update check intervals
10+
11+
## Installation
12+
1. Download the ZIP file of this repository
13+
2. In Arduino IDE: Sketch -> Include Library -> Add .ZIP Library
14+
3. Select the downloaded ZIP file
15+
16+
## Dependencies
17+
- ArduinoJson (>= 6.21.3)
18+
- ESP32 board support
19+
20+
## Usage
21+
```cpp
22+
#include <BitFlash_Client.h>
23+
24+
BitFlash_Client::Config config = {
25+
.ssid = "your_wifi_ssid",
26+
.password = "your_wifi_password",
27+
.currentVersion = "1.0.0",
28+
.jsonEndpoint = "https://your-server.com/firmware/version.json",
29+
.checkInterval = 5000, // Check every 5 seconds
30+
.autoConnect = true
31+
};
32+
33+
BitFlash_Client updater(config);
34+
35+
void setup() {
36+
Serial.begin(115200);
37+
updater.begin();
38+
}
39+
40+
void loop() {
41+
updater.handle();
42+
// Your code here
43+
}
44+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <BitFlash_Client.h>
2+
3+
void updateCallback(const char* status, int progress) {
4+
Serial.printf("Update Status: %s", status);
5+
if (progress >= 0) {
6+
Serial.printf(" (%d%%)", progress);
7+
}
8+
Serial.println();
9+
}
10+
11+
BitFlash_Client::Config config = {
12+
.ssid = "your_wifi_ssid",
13+
.password = "your_wifi_password",
14+
.currentVersion = "1.0.0",
15+
.jsonEndpoint = "https://your-server.com/firmware/version.json",
16+
.checkInterval = 5000, // Check every 5 seconds
17+
.autoConnect = true
18+
};
19+
20+
BitFlash_Client updater(config);
21+
22+
void setup() {
23+
Serial.begin(115200);
24+
Serial.println("BitFlash Client Example");
25+
26+
updater.setCallback(updateCallback);
27+
updater.begin();
28+
}
29+
30+
void loop() {
31+
updater.handle();
32+
33+
// Your application code here
34+
delay(1000);
35+
}

keywords.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
BitFlash_Client KEYWORD1
2+
begin KEYWORD2
3+
handle KEYWORD2
4+
checkForUpdate KEYWORD2
5+
setCheckInterval KEYWORD2
6+
setCallback KEYWORD2
7+
connectWiFi KEYWORD2
8+
disconnectWiFi KEYWORD2
9+
isWiFiConnected KEYWORD2
10+
Config KEYWORD3

library.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "BitFlash_Client",
3+
"version": "1.0.0",
4+
"description": "OTA firmware update library for ESP32",
5+
"keywords": "esp32, ota, fota, firmware, update",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/AryanBhirud/BitFlash_Client.git"
9+
},
10+
"authors": [{
11+
"name": "Aryan Bhirud",
12+
"email": "[email protected]"
13+
},
14+
{
15+
"name": "Prakriti Bhattacharya",
16+
"email": "[email protected]"
17+
}],
18+
"license": "GNU GPL-3.0",
19+
"frameworks": "arduino",
20+
"platforms": "espressif32",
21+
"dependencies": {
22+
"bblanchon/ArduinoJson": "^6.21.3"
23+
}
24+
}

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=BitFlash_Client
2+
version=1.0.0
3+
author=Aryan Bhirud, Prakriti Bhattacharya
4+
maintainer=Aryan Bhirud <[email protected]>, Prakriti Bhattacharya <[email protected]>
5+
sentence=OTA firmware update library for ESP32
6+
paragraph=Enables automatic background firmware updates for ESP32 devices. Handles WiFi connection, version checking, and firmware installation.
7+
category=Device Control
8+
url=https://github.com/AryanBhirud/BitFlash_Client
9+
architectures=esp32
10+
depends=ArduinoJson

src/BitFlash_Client.cpp

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#include "BitFlash_Client.h"
2+
3+
BitFlash_Client::BitFlash_Client(const Config& config)
4+
: _config(config), _lastCheck(0), _updateInProgress(false) {
5+
}
6+
7+
void BitFlash_Client::begin() {
8+
if (_config.autoConnect) {
9+
connectWiFi();
10+
}
11+
}
12+
13+
void BitFlash_Client::handle() {
14+
if (!_updateInProgress && millis() - _lastCheck >= _config.checkInterval) {
15+
checkForUpdate();
16+
_lastCheck = millis();
17+
}
18+
}
19+
20+
void BitFlash_Client::checkForUpdate() {
21+
if (!isWiFiConnected() && !connectWiFi()) {
22+
notifyCallback("WiFi connection failed");
23+
return;
24+
}
25+
26+
if (checkVersion()) {
27+
notifyCallback("Update available");
28+
}
29+
}
30+
31+
bool BitFlash_Client::checkVersion() {
32+
WiFiClientSecure client;
33+
client.setInsecure();
34+
35+
HTTPClient https;
36+
https.begin(client, _config.jsonEndpoint);
37+
38+
int httpCode = https.GET();
39+
if (httpCode != HTTP_CODE_OK) {
40+
notifyCallback("Failed to fetch version info");
41+
https.end();
42+
return false;
43+
}
44+
45+
StaticJsonDocument<512> doc;
46+
DeserializationError error = deserializeJson(doc, https.getString());
47+
https.end();
48+
49+
if (error) {
50+
notifyCallback("Failed to parse version info");
51+
return false;
52+
}
53+
54+
const char* latestVersion = doc["version"];
55+
const char* firmwareUrl = doc["firmware_url"];
56+
57+
if (strcmp(latestVersion, _config.currentVersion) > 0) {
58+
_updateInProgress = true;
59+
performUpdate(firmwareUrl);
60+
return true;
61+
}
62+
63+
return false;
64+
}
65+
66+
void BitFlash_Client::performUpdate(const char* firmwareUrl) {
67+
WiFiClientSecure *client = new WiFiClientSecure;
68+
client->setInsecure();
69+
70+
HTTPClient https;
71+
https.begin(*client, firmwareUrl);
72+
73+
int httpCode = https.GET();
74+
if (httpCode == HTTP_CODE_OK) {
75+
int contentLength = https.getSize();
76+
if (contentLength > 0 && Update.begin(contentLength)) {
77+
WiFiClient * stream = https.getStreamPtr();
78+
size_t written = 0;
79+
uint8_t buff[1024] = { 0 };
80+
81+
while (https.connected() && (written < contentLength)) {
82+
size_t size = stream->available();
83+
if (size) {
84+
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
85+
written += Update.write(buff, c);
86+
int progress = (written * 100) / contentLength;
87+
notifyCallback("Downloading update", progress);
88+
}
89+
yield();
90+
}
91+
92+
if (written == contentLength && Update.end()) {
93+
notifyCallback("Update complete, restarting...");
94+
ESP.restart();
95+
} else {
96+
notifyCallback("Update failed");
97+
}
98+
}
99+
}
100+
101+
https.end();
102+
delete client;
103+
_updateInProgress = false;
104+
}
105+
106+
bool BitFlash_Client::connectWiFi() {
107+
if (isWiFiConnected()) return true;
108+
109+
WiFi.begin(_config.ssid, _config.password);
110+
111+
int attempts = 0;
112+
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
113+
delay(500);
114+
attempts++;
115+
}
116+
117+
if (isWiFiConnected()) {
118+
setClock();
119+
return true;
120+
}
121+
122+
return false;
123+
}
124+
125+
void BitFlash_Client::disconnectWiFi() {
126+
WiFi.disconnect();
127+
}
128+
129+
bool BitFlash_Client::isWiFiConnected() {
130+
return WiFi.status() == WL_CONNECTED;
131+
}
132+
133+
void BitFlash_Client::setClock() {
134+
configTime(0, 0, "pool.ntp.org");
135+
time_t now = time(nullptr);
136+
while (now < 8 * 3600 * 2) {
137+
delay(500);
138+
now = time(nullptr);
139+
}
140+
}
141+
142+
void BitFlash_Client::setCheckInterval(uint32_t interval) {
143+
_config.checkInterval = interval;
144+
}
145+
146+
void BitFlash_Client::setCallback(std::function<void(const char* status, int progress)> callback) {
147+
_callback = callback;
148+
}
149+
150+
void BitFlash_Client::notifyCallback(const char* status, int progress) {
151+
if (_callback) {
152+
_callback(status, progress);
153+
}
154+
}

src/BitFlash_Client.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include <WiFi.h>
4+
#include <HTTPClient.h>
5+
#include <WiFiClientSecure.h>
6+
#include <Update.h>
7+
#include <time.h>
8+
#include <ArduinoJson.h>
9+
#include <functional>
10+
11+
class BitFlash_Client {
12+
public:
13+
struct Config {
14+
const char* ssid;
15+
const char* password;
16+
const char* currentVersion;
17+
const char* jsonEndpoint;
18+
uint32_t checkInterval; // In milliseconds
19+
bool autoConnect; // Whether to auto-connect to WiFi
20+
};
21+
22+
BitFlash_Client(const Config& config);
23+
24+
void begin();
25+
void handle();
26+
void checkForUpdate();
27+
void setCheckInterval(uint32_t interval);
28+
void setCallback(std::function<void(const char* status, int progress)> callback);
29+
bool connectWiFi();
30+
void disconnectWiFi();
31+
bool isWiFiConnected();
32+
33+
private:
34+
Config _config;
35+
unsigned long _lastCheck;
36+
std::function<void(const char* status, int progress)> _callback;
37+
bool _updateInProgress;
38+
39+
void setClock();
40+
bool checkVersion();
41+
void performUpdate(const char* firmwareUrl);
42+
void notifyCallback(const char* status, int progress = -1);
43+
};

0 commit comments

Comments
 (0)