Skip to content

Commit a1f8654

Browse files
matteocrippaMatteo Crippa
andauthored
Feature/v0.0.5 (#11)
* fix esp8266 * fix unit tests * add screen boot status * add early setup for ota * add version script * added platform.device * add auto update logic * fix auto update code * force change version for ddebug * adjust logic for update * fix firmware version --------- Co-authored-by: Matteo Crippa <matteocrippa@github.users.com>
1 parent bf68510 commit a1f8654

File tree

11 files changed

+280
-26
lines changed

11 files changed

+280
-26
lines changed

.github/workflows/pr_version.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Check and Update Version
2+
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
8+
jobs:
9+
checkAndUpdateVersion:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v2
15+
16+
- name: Extract Version from leafminer.h
17+
id: extract_version
18+
run: |
19+
echo "::set-output name=version::$(grep -o '_VERSION "[^"]*' src/leafminer.h | cut -d'"' -f2)"
20+
echo "Extracted version: ${{ steps.extract_version.outputs.version }}"
21+
22+
- name: Read current version from version.json
23+
id: read_version_json
24+
run: |
25+
echo "Reading version from version.json"
26+
CURRENT_VERSION=$(jq -r '.current' version.json)
27+
echo "Current version in version.json: $CURRENT_VERSION"
28+
echo "::set-output name=current_version::$CURRENT_VERSION"
29+
30+
- name: Compare versions
31+
id: compare_versions
32+
run: |
33+
if [ "${{ steps.extract_version.outputs.version }}" != "${{ steps.read_version_json.outputs.current_version }}" ]; then
34+
echo "Versions are different. Updating version.json."
35+
sed -i 's/"current": "\(.*\)"/"current": "'${{ steps.extract_version.outputs.version }}'"/' version.json
36+
git config --global user.email "actions@github.com"
37+
git config --global user.name "GitHub Actions"
38+
git add version.json
39+
git commit -m "Update version to ${{ steps.extract_version.outputs.version }}"
40+
echo "Version updated in version.json. Creating commit."
41+
git push
42+
else
43+
echo "Versions are the same. No update needed."
44+
fi

CHANGELOG.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# v0.0.4
2-
- Restored random extra nonce 2 generator
3-
- Fixed extra nonce to be always > 0
4-
- Add restart ESP if stuck for > 5 min
5-
- LCD devices will show the boot screen if setup is active
6-
- enforce network check on listen
7-
- Show session identifier on screen
8-
- Reset to AP mode if not able to connect to WiFi or Stratum
1+
# v0.0.5
2+
- Fix ESP8266 stale crash
3+
- Fix unit tests
4+
- Add screen status handler on start
5+
- Add Github action to check version
6+
- Add version.json file
7+
- Add device value in platform
8+
9+
- Add auto OTA update check on start

TODO.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
# BACKLOG (prioritized)
22

3-
- [ ] Restore random nonce2
43
- [ ] Show miner ip (web setup option)
54
- [ ] Show clock (web setup option)
65
- [ ] Show Accepted/Rejected (web setup option)
7-
- [ ] Add optional logic to wait for response for miner.submit
86
- [ ] New mining ui
97
- [ ] Fix blinking
108
- [ ] Miner `from` value needs to be handled
119
- [ ] ESP32-S3 led
1210
- [ ] Led brightness
13-
- [ ] [GeekMagicClock SmallTV](https://github.com/GeekMagicClock/smalltv/issues/4)
1411
- [ ] Increase to, at least, 20kH/s for ESP8266
1512
- [ ] Add support for .66 inch OLED
16-
- [ ] Add OTA support via web
1713
- [ ] Add mDNS support to discover Main node
1814
- [ ] Introduce proxy mining Main + Nodes to split the nonce in count(nodes)
19-
- [ ] Add installer
2015
- [ ] Migrate to IDF
2116

2217
# DONE
@@ -74,3 +69,8 @@
7469
- [x] Fix ESP8266
7570
- [x] Add ota from web ESP32
7671
- [x] Add ota from web ESP8266
72+
- [x] Restore random nonce2
73+
- [x] [GeekMagicClock SmallTV](https://github.com/GeekMagicClock/smalltv/issues/4)
74+
- [x] Add installer
75+
- [x] Add OTA support via web
76+
- [x] Fix unit tests

platformio.ini

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ board = d1_mini
77
framework = arduino
88
monitor_speed = 115200
99
build_type = debug
10-
monitor_filters = esp8266_exception_decoder
10+
monitor_filters =
11+
log2file
12+
esp8266_exception_decoder
13+
time
1114
board_build.f_cpu = 160000000L
1215
board_build.filesystem = littlefs
1316
test_build_src = yes
@@ -17,6 +20,7 @@ build_src_filter =
1720
-<screen/*>
1821
build_flags =
1922
-O3
23+
-DESP8266_D=1
2024
-DLOG_LEVEL=0
2125
-fexceptions
2226
lib_deps =
@@ -51,7 +55,7 @@ build_flags =
5155
-DLOG_LEVEL=0
5256
-fexceptions
5357
-DHAS_LCD
54-
-DGEEKMAGICCLOCK_SMALLTV
58+
-DGEEKMAGICCLOCK_SMALLTV=1
5559
-DUSER_SETUP_LOADED
5660
-DST7789_2_DRIVER
5761
-DTFT_RGB_ORDER=TFT_RGB

src/leafminer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef LEAFMINER_H
22
#define LEAFMINER_H
33

4-
#define _VERSION "0.0.4"
4+
#define _VERSION "0.0.5"
55
#define DIFFICULTY 1e-4
66

77
// Mining

src/main.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "current.h"
1313
#include "utils/button.h"
1414
#include "storage/storage.h"
15+
#include "network/autoupdate.h"
1516
#if defined(ESP32)
1617
#include "freertos/task.h"
1718
#endif // ESP32
@@ -42,34 +43,37 @@ void setup()
4243

4344
force_ap = button_setup();
4445

45-
#if !defined(HAS_LCD)
46-
Blink::getInstance().setup();
47-
delay(500);
48-
Blink::getInstance().blink(BLINK_START);
49-
#else
50-
screen_setup();
51-
#endif // HAS_LCD
52-
5346
storage_load(&configuration);
5447
if (configuration.wifi_ssid == "" || force_ap)
5548
{
5649
accesspoint_setup();
5750
return;
5851
}
5952

53+
#if !defined(HAS_LCD)
54+
Blink::getInstance().setup();
55+
delay(500);
56+
Blink::getInstance().blink(BLINK_START);
57+
#else
58+
screen_setup();
59+
#endif // HAS_LCD
60+
6061
#if defined(ESP32)
6162
xTaskCreatePinnedToCore(currentTaskFunction, "checkStale", 1024, NULL, 4, NULL, 0);
6263
xTaskCreatePinnedToCore(mineTaskFunction, "mineTaskCore0", 12192, (void *)0, 1, NULL, 0);
6364
#if CORE == 2
6465
xTaskCreatePinnedToCore(mineTaskFunction, "mineTaskCore1", 12192, (void *)1, 2, NULL, 1);
6566
#endif // CORE == 2
6667
xTaskCreatePinnedToCore(buttonTaskFunction, "buttonTask", 2048, NULL, 4, NULL, 0);
67-
#if defined(HAS_LCD)
68+
#if defined(HAS_LCD)
6869
xTaskCreatePinnedToCore(screenTaskFunction, "screenTask", 4096, NULL, 3, NULL, 0);
6970
#endif
7071
#endif
7172

72-
if (network_getJob() == -1) {
73+
autoupdate();
74+
75+
if (network_getJob() == -1)
76+
{
7377
l_error(TAG_MAIN, "Failed to connect to network");
7478
l_info(TAG_MAIN, "Fallback to AP mode");
7579
force_ap = true;

src/model/job.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ Job::Job(const Notification &notification, const Subscribe &subscribe, double di
2323
job_id = notification.job_id;
2424
ntime = ntime_string;
2525

26+
#ifndef UNIT_TEST
2627
extranonce2 = generate_extra_nonce2(subscribe.extranonce2_size);
28+
#else
29+
extranonce2 = "00000002";
30+
#endif
2731
std::string coinbase = notification.coinb1 + subscribe.extranonce1 + extranonce2 + notification.coinb2;
2832
l_info(TAG_JOB, "[%s] - Coinbase: %s", job_id.c_str(), coinbase.c_str());
2933

34+
#if defined(ESP8266)
35+
ESP.wdtFeed();
36+
#endif
37+
3038
std::string coinbase_hash;
3139
generateCoinbaseHash(coinbase, coinbase_hash);
3240

src/network/autoupdate.cpp

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#if defined(ESP8266)
2+
#include <ESP8266WiFi.h>
3+
#include <Updater.h>
4+
#include <ESP8266HTTPClient.h>
5+
#else
6+
#include <WiFi.h>
7+
#include <Update.h>
8+
#include <HTTPClient.h>
9+
#endif // ESP8266
10+
#include <cJSON.h>
11+
#include <string>
12+
#include "leafminer.h"
13+
#include "autoupdate.h"
14+
#include "model/configuration.h"
15+
#include "utils/platform.h"
16+
#include "utils/log.h"
17+
18+
const std::string AUTOUPDATE_URL = "https://raw.githubusercontent.com/matteocrippa/leafminer/feature/v0.0.5/version.json"; //"https://raw.githubusercontent.com/matteocrippa/leafminer/main/version.json";
19+
const char TAG_AUTOUPDATE[] = "autoupdate";
20+
21+
#if defined(ESP8266_D)
22+
std::string DEVICE = "esp8266";
23+
#elif defined(GEEKMAGICCLOCK_SMALLTV)
24+
std::string DEVICE = "geekmagic-smalltv";
25+
#elif defined(ESP32_WROOM)
26+
std::string DEVICE = "esp32";
27+
#elif defined(ESP32_S2)
28+
std::string DEVICE = "esp32-s2";
29+
#elif defined(ESP32_S3)
30+
std::string DEVICE = "esp32-s3";
31+
#elif defined(LILYGO_T_S3)
32+
std::string DEVICE = "lilygo-t-display-s3";
33+
#else
34+
std::string DEVICE = "unknown";
35+
#endif
36+
37+
extern Configuration configuration;
38+
39+
void autoupdate()
40+
{
41+
l_info(TAG_AUTOUPDATE, "Connecting to %s...", configuration.wifi_ssid.c_str());
42+
while (WiFi.waitForConnectResult() != WL_CONNECTED)
43+
{
44+
WiFi.begin(configuration.wifi_ssid.c_str(), configuration.wifi_password.c_str());
45+
delay(800);
46+
}
47+
48+
HTTPClient http;
49+
http.begin(AUTOUPDATE_URL.c_str());
50+
int httpCode = http.GET();
51+
if (httpCode == HTTP_CODE_OK)
52+
{
53+
std::string payload = http.getString().c_str();
54+
l_debug(TAG_AUTOUPDATE, "payload: %s", payload.c_str());
55+
56+
cJSON *json = cJSON_Parse(payload.c_str());
57+
cJSON *versionItem = cJSON_GetObjectItem(json, "current");
58+
59+
// Check if the "version" field exists and is a string
60+
if (versionItem != NULL && cJSON_IsString(versionItem))
61+
{
62+
std::string version = cJSON_GetStringValue(versionItem);
63+
// Now you can safely use the 'version' string
64+
l_debug(TAG_AUTOUPDATE, "Version: %s", version.c_str());
65+
if (strcmp(version.c_str(), _VERSION) == 0)
66+
{
67+
l_debug(TAG_AUTOUPDATE, "No Updates, Version: %s", version.c_str());
68+
return;
69+
}
70+
else
71+
{
72+
l_debug(TAG_AUTOUPDATE, "New Version: %s", version.c_str());
73+
cJSON *url = cJSON_GetObjectItemCaseSensitive(json, "link");
74+
cJSON *device = cJSON_GetObjectItemCaseSensitive(json, "devices");
75+
76+
// Check if the device is supported
77+
bool isDeviceSupported = false;
78+
for (int i = 0; i < cJSON_GetArraySize(device); i++)
79+
{
80+
std::string deviceItem = cJSON_GetStringValue(cJSON_GetArrayItem(device, i));
81+
if (deviceItem == DEVICE)
82+
{
83+
isDeviceSupported = true;
84+
break;
85+
}
86+
}
87+
88+
if (!isDeviceSupported)
89+
{
90+
l_error(TAG_AUTOUPDATE, "Device not supported: %s", DEVICE.c_str());
91+
return;
92+
}
93+
else
94+
{
95+
l_debug(TAG_AUTOUPDATE, "Device supported: %s", DEVICE.c_str());
96+
97+
// Replace placeholders in the URL with actual values
98+
std::string downloadUrl = url->valuestring;
99+
size_t versionPos = downloadUrl.find("{{version}}");
100+
if (versionPos != std::string::npos)
101+
{
102+
downloadUrl.replace(versionPos, strlen("{{version}}"), version);
103+
}
104+
105+
size_t devicePos = downloadUrl.find("{{device}}");
106+
if (devicePos != std::string::npos)
107+
{
108+
downloadUrl.replace(devicePos, strlen("{{device}}"), DEVICE);
109+
}
110+
111+
l_debug(TAG_AUTOUPDATE, "Downloading: %s", downloadUrl.c_str());
112+
113+
http.begin(downloadUrl.c_str());
114+
http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
115+
116+
int httpCode = http.GET();
117+
if (httpCode >= 200 && httpCode <= 302)
118+
{
119+
l_debug(TAG_AUTOUPDATE, "Downloaded: %d", http.getSize());
120+
if (Update.begin(http.getSize()))
121+
{
122+
l_debug(TAG_AUTOUPDATE, "Begin Update");
123+
size_t written = Update.writeStream(http.getStream());
124+
if (written == http.getSize())
125+
{
126+
l_debug(TAG_AUTOUPDATE, "Written: %d", written);
127+
if (Update.end())
128+
{
129+
l_debug(TAG_AUTOUPDATE, "Update Success: %d", written);
130+
ESP.restart();
131+
}
132+
else
133+
{
134+
l_error(TAG_AUTOUPDATE, "Update Failed");
135+
}
136+
}
137+
else
138+
{
139+
l_error(TAG_AUTOUPDATE, "Written: %d", written);
140+
}
141+
}
142+
else
143+
{
144+
l_error(TAG_AUTOUPDATE, "Begin Update Failed");
145+
}
146+
}
147+
else
148+
{
149+
l_error(TAG_AUTOUPDATE, "httpCode: %d", httpCode);
150+
}
151+
}
152+
}
153+
}
154+
else
155+
{
156+
l_error(TAG_AUTOUPDATE, "Invalid or missing 'version' field in JSON");
157+
return;
158+
}
159+
}
160+
else
161+
{
162+
l_error(TAG_AUTOUPDATE, "httpCode: %d", httpCode);
163+
}
164+
}

src/network/autoupdate.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef AUTOUPDATE_H
2+
#define AUTOUPDATE_H
3+
void autoupdate();
4+
#endif // AUTOUPDATE_H

0 commit comments

Comments
 (0)