Skip to content

Commit

Permalink
Merge pull request #135 from maxbundscherer/develop
Browse files Browse the repository at this point in the history
v.0.3.6
  • Loading branch information
maxbundscherer authored Oct 10, 2022
2 parents c6ef526 + 6fcb70a commit f84159b
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This repository is about a device called **Independer**. An Open-Source Project.

**Encrypted messaging & data sharing** via LoRaS & inexpensive hardware (ESP32). **Independent from mobile network & WIFI**. We use cheap *ESP32s*, *LoRa antennas*, *3D prototyping*, *mini keyboards* and develop our own encrypted *protocol* together with *software*.

**This page is intended for developers** - please visit the [official website](https://a-sdr.org/independer) for more information. Also visit [project presentation (PDF)](http://a-sdr.org/independer/independer.pdf). If you want to **join, or have an idea**, feel free to [write us](mailto:[email protected]) or [start a discussion](https://github.com/maxbundscherer/independer-loras/discussions). The current state of development can be viewed on the [project board](https://github.com/maxbundscherer/independer-loras/projects/2).
**This page is intended for developers** - please visit the [official website](https://a-sdr.org/independer) for more information. If you want to **join, or have an idea**, feel free to [write us](mailto:[email protected]) or [start a discussion](https://github.com/maxbundscherer/independer-loras/discussions). The current state of development can be viewed on the [project board](https://github.com/maxbundscherer/independer-loras/projects/2).

Presentation | Prototype-Video
:-------------------------:|:-------------------------:
Expand All @@ -26,7 +26,7 @@ This repository describes the concept, the project structure, the encrypted Lora
- **Actor to Gateway (via LoRaS)**: Send messages via Gateway. Actors can query their messages from Gateways.
- **Actor to Actor (via WIFI/Internet)**: Send messages via Internet. Actors can query their messages from WIFI/Internet.
- **Who is near me?** Scan the environment to find other Independers.
- **Many other functions**: Various test functions (Reachability Check, Reception & Transmission Evaluation), Update via WIFI, Notification LED, Deep Sleep Mode, Background-Sync, Battery Status, Send Quota & Gain, Contacts...
- **Many other functions**: Various test functions (Reachability Check, Reception & Transmission Evaluation), Update via WIFI, Notification LED, Deep Sleep Mode, Background-Sync, Battery Status, Send Quota & Gain, NTP-Time-Sync, Contacts...

### Specifications

Expand Down
Binary file modified docs/presentation/Independer.pptx
Binary file not shown.
4 changes: 2 additions & 2 deletions independer-app/src/device/mb-gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ void gui_msg_static(String msg_title, String msg)
display.display();
}

void gui_msg_static_gateway(String msg_title, String msg, int global_tx_time)
void gui_msg_static_gateway(String msg_title, String msg, int tx_time)
{
display.clear();
display.setFont(ArialMT_Plain_10);
Expand All @@ -408,7 +408,7 @@ void gui_msg_static_gateway(String msg_title, String msg, int global_tx_time)
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 10 * 2 + 2 + 4, msg);

int progress = (((float)global_tx_time - 0) / ((float)36000 - 0)) * (float)100;
int progress = (((float)tx_time - 0) / ((float)(C_LORA_QUOTA_CONTINGENT_SECONDS * 1000) - 0)) * (float)100;
if (progress > 100)
progress = 100;
if (progress < 0)
Expand Down
18 changes: 3 additions & 15 deletions independer-app/src/device/mb-lora.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,6 @@ long i_adapter_lora_string_hash(String msg)
* Trans Section
* ####################################
*/
uint64_t state_du_global_tx_time = 0;

int lora_get_global_tx_time_millis()
{
return state_du_global_tx_time / 1000;
}

int lora_get_global_tx_time_seconds()
{
return lora_get_global_tx_time_millis() / 1000;
}

void i_lora_trans_encrypt(String msg, int sendGain)
{

Expand All @@ -109,7 +97,7 @@ void i_lora_trans_encrypt(String msg, int sendGain)
LoRa.beginPacket();
LoRa.print(crypt_encrypt(msg));
LoRa.endPacket();
state_du_global_tx_time += (esp_timer_get_time() - du_start);
time_lora_quota_add(esp_timer_get_time() - du_start);
}
else
{
Expand All @@ -119,10 +107,10 @@ void i_lora_trans_encrypt(String msg, int sendGain)
LoRa.beginPacket();
LoRa.print(crypt_encrypt(msg));
LoRa.endPacket();
state_du_global_tx_time += (esp_timer_get_time() - du_start);
time_lora_quota_add(esp_timer_get_time() - du_start);
}

Serial.println("Send Duration " + String(lora_get_global_tx_time_millis()) + " millis " + String(lora_get_global_tx_time_seconds()) + " seconds");
Serial.println("Send Duration " + String(time_lora_quota_update_get_millis()) + " millis ");

digitalWrite(LED, LOW);
}
Expand Down
148 changes: 139 additions & 9 deletions independer-app/src/device/mb-time.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <UnixTime.h>
#include <ESP32Time.h>

unsigned long C_LORA_QUOTA_CLEAN_SECONDS = 3600; // 1h
unsigned long C_LORA_QUOTA_CONTINGENT_SECONDS = 36; // 36s

WiFiUDP ntpUDP;

// By default 'pool.ntp.org' is used with 60 seconds update interval and
Expand All @@ -12,16 +15,45 @@ NTPClient timeClient(ntpUDP);
UnixTime stamp(2); // UTC+2
ESP32Time rtc(0); // Offset is already in stamp (else 3600 * 2)

unsigned long i_time_get_current_unix_time()
{
return rtc.getEpoch();
}

String i_time_convert_current_time_to_string()
{
stamp.getDateTime(rtc.getEpoch());
stamp.getDateTime(i_time_get_current_unix_time());
return String(stamp.year) + "-" + String(stamp.month) + "-" + String(stamp.day) + " " + String(stamp.hour) + ":" + String(stamp.minute) + ":" + String(stamp.second);
}

String time_sync_get_ntp()
/**
* @return boolean update success
*/
boolean i_time_update()
{
timeClient.begin();
timeClient.update();

if (timeClient.getEpochTime() < 500)
{
Serial.println("Could not update time");
return false;
}
else
{
Serial.println("Could update time");
rtc.setTime(timeClient.getEpochTime());
// rtc.offset = 3600 * 2; //Offset is already in stamp
return true;
}

timeClient.end();
}

String time_sync_get_ntp_and_connect()
{

String ret = "";
String ret = "(Fehler)";

Serial.println("(Sync NTP now)");

Expand All @@ -41,12 +73,10 @@ String time_sync_get_ntp()
Serial.println("");
Serial.println("WiFi connected.");

timeClient.begin();
timeClient.update();
rtc.setTime(timeClient.getEpochTime());
// rtc.offset = 3600 * 2; //Offset is already in stamp
ret = i_time_convert_current_time_to_string();
timeClient.end();
if (i_time_update())
{
ret = i_time_convert_current_time_to_string();
}

// disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
Expand All @@ -65,4 +95,104 @@ String time_get_from_local()
void time_debug_console_output()
{
Serial.println("Current Time: '" + i_time_convert_current_time_to_string() + "'");
}

// TODO: Speicherüberlaufschutz
#define BOOT_STATE_LORA_QUOTA_DATA 100
RTC_DATA_ATTR char boot_state_lora_quota_data[BOOT_STATE_LORA_QUOTA_DATA] = "";

void time_lora_quota_add(int microseconds)
{
DynamicJsonDocument doc(1024 * 10);

deserializeJson(doc, String(boot_state_lora_quota_data));

doc[String(i_time_get_current_unix_time())] = microseconds;

String data = "";
serializeJson(doc, data);
strcpy(boot_state_lora_quota_data, data.c_str());
}

int time_lora_quota_update_get_millis()
{
DynamicJsonDocument doc(1024 * 10);

deserializeJson(doc, String(boot_state_lora_quota_data));

JsonObject root = doc.as<JsonObject>();

unsigned long tx_duration_microseconds = 0;

for (JsonPair kv : root)
{

const char *orgKey = kv.key().c_str();
char *ptr;

unsigned long t = strtoul(orgKey, &ptr, 10);
int d = kv.value().as<int>();

// Serial.println("----");
// Serial.println("1 '" + String(i_time_get_current_unix_time()) + "'");
// Serial.println("2 '" + String(t) + "'");
// Serial.println("3 '" + String(d) + "'");

if (t < (i_time_get_current_unix_time() - C_LORA_QUOTA_CLEAN_SECONDS) and (i_time_get_current_unix_time() > C_LORA_QUOTA_CLEAN_SECONDS))
{
// Is not in range
// Serial.println("Filter out " + String(t));
doc.remove(orgKey);
}
else
{
// Is in range
// Serial.println("Take a look at " + String(t) + ": " + d);
tx_duration_microseconds += d;
}

String data = "";
serializeJson(doc, data);
strcpy(boot_state_lora_quota_data, data.c_str());

// Serial.println("----");
// Serial.println();
}

return tx_duration_microseconds / 1000;
}

void time_update_time_and_quota_connected()
{
Serial.println("Update Time Start");
if (i_time_update())
{

DynamicJsonDocument doc(1024 * 10);

deserializeJson(doc, String(boot_state_lora_quota_data));

JsonObject root = doc.as<JsonObject>();

for (JsonPair kv : root)
{

const char *orgKey = kv.key().c_str();
char *ptr;

unsigned long t = strtoul(orgKey, &ptr, 10);
int d = kv.value().as<int>();

if (t >= i_time_get_current_unix_time())
{
// Is not in range
doc.remove(orgKey);
}

String data = "";
serializeJson(doc, data);
strcpy(boot_state_lora_quota_data, data.c_str());
}
}
Serial.println("Update Time End");
}
5 changes: 5 additions & 0 deletions independer-app/src/device/mb-wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ S_WIFI_REGISTER wifi_register_device_actor(String device_id, String secret, Stri
else
{
Serial.println("Connected to server!");
time_update_time_and_quota_connected();

DynamicJsonDocument doc(1024 * 10);

Expand Down Expand Up @@ -204,6 +205,7 @@ boolean wifi_register_device_gateway(String my_id, String gateway_id, String ser
else
{
Serial.println("Connected to server!");
time_update_time_and_quota_connected();

DynamicJsonDocument doc(1024 * 10);

Expand Down Expand Up @@ -292,6 +294,7 @@ boolean wifi_send_chat_message(String receiver, String author, String msg, Strin
else
{
Serial.println("Connected to server!");
time_update_time_and_quota_connected();

DynamicJsonDocument doc(1024 * 10);

Expand Down Expand Up @@ -381,6 +384,7 @@ String wifi_get_chat_messages(String myId, String serverUrl, int serverPort, int
else
{
Serial.println("Connected to server!");
time_update_time_and_quota_connected();

DynamicJsonDocument doc(1024 * 10);

Expand Down Expand Up @@ -465,6 +469,7 @@ boolean wifi_clear_message(String myId, String serverUrl, int serverPort, int se
else
{
Serial.println("Connected to server!");
time_update_time_and_quota_connected();

DynamicJsonDocument doc(1024 * 10);

Expand Down
6 changes: 2 additions & 4 deletions independer-app/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ boolean c_actor_mode = false;
* ####################################
*/
// Product Config
String c_product_version = "v.0.3.5";
String c_product_version = "v.0.3.6";

/*
* ####################################
Expand All @@ -50,6 +50,7 @@ int state_wifi_server_port = 5001; // saved in db (fixed)
int state_wifi_server_timeout = 10000; // saved in db
String state_wifi_server_device_token = ""; // saved in db and INIT CONFIG

#include "device/mb-time.h"
#include "device/mb-base64.h"
#include "device/mb-gui.h"
#include "device/mb-crypt.h"
Expand All @@ -65,9 +66,6 @@ String state_wifi_server_device_token = ""; // saved in db and INIT CO
// Database
#include "device/mb-database.h"

// Time
#include "device/mb-time.h"

// OTA
#include "device/mb-ota.h"

Expand Down
4 changes: 2 additions & 2 deletions independer-app/src/workflow/workflow-actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ void i_actor_functions_status_function_menu()
if (selected_wrapper.success and selected_wrapper.value == 0)
gui_display_prg_animated("Batterie Status (mV)", utils_get_battery(), 1950, 3100, C_GUI_DELAY_MSG_SHORT_I);
else if (selected_wrapper.success and selected_wrapper.value == 1)
gui_display_prg_animated("Sendekontingent (millis)", lora_get_global_tx_time_millis(), 0, 36000, C_GUI_DELAY_MSG_SHORT_I);
gui_display_prg_animated("Sendekontingent (millis)", time_lora_quota_update_get_millis(), 0, (C_LORA_QUOTA_CONTINGENT_SECONDS * 1000), C_GUI_DELAY_MSG_SHORT_I);
else
fin_flag = true;
}
Expand Down Expand Up @@ -323,7 +323,7 @@ void i_actor_functions_test_function_menu()
else if (selected_wrapper.success and selected_wrapper.value == 3)
{
gui_msg_static("Hinweis", "Frage Zeit\nab ...");
String r = time_sync_get_ntp();
String r = time_sync_get_ntp_and_connect();
gui_msg_animated("Zeit", "Empfangen\n'" + r + "'", C_GUI_DELAY_MSG_MIDDLE_I);
}
else if (selected_wrapper.success and selected_wrapper.value == 4)
Expand Down
2 changes: 1 addition & 1 deletion independer-app/src/workflow/workflow-gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void workflow_gateway_main()

if (state_gateway_has_sth_changed)
{
gui_msg_static_gateway("Independer Gateway", "Pakete: " + String(state_gateway_received_packets) + " Nachrichten: " + String(state_gateway_received_messages) + "\nDatenbank Einträge: " + String(state_gateway_db_items), lora_get_global_tx_time_millis());
gui_msg_static_gateway("Independer Gateway", "Pakete: " + String(state_gateway_received_packets) + " Nachrichten: " + String(state_gateway_received_messages) + "\nDatenbank Einträge: " + String(state_gateway_db_items), time_lora_quota_update_get_millis());
state_gateway_has_sth_changed = false;
}

Expand Down
5 changes: 0 additions & 5 deletions independer-app/src/workflow/workflow-independer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ char *c_cipher_key = "kjew50fkjriowdj6";

// Boot State
RTC_DATA_ATTR int boot_state_count = 0;
#define BOOT_STATE_MSG_COUNT 20
RTC_DATA_ATTR char boot_state_msg[BOOT_STATE_MSG_COUNT] = "Boot-Msg ";

/**
*
Expand Down Expand Up @@ -50,9 +48,6 @@ boolean workflow_independer_init(boolean isActor, String productVersion, boolean
rtc_gpio_deinit(GPIO_NUM_0);
++boot_state_count;
Serial.println("Boot number: " + String(boot_state_count));
// String(String(boot_state_msg) + String(boot_state_count)).toCharArray(boot_state_msg, BOOT_STATE_MSG_COUNT);
char *boot_state_msg = const_cast<char *>(String(boot_state_count).c_str());
Serial.println("Boot msg: " + String(boot_state_msg));

Serial.println("- Wake up reason");
utils_print_wakeup_reason();
Expand Down

0 comments on commit f84159b

Please sign in to comment.