diff --git a/RadDevice.cpp b/RadDevice.cpp new file mode 100644 index 0000000..17778d1 --- /dev/null +++ b/RadDevice.cpp @@ -0,0 +1,15 @@ +#include +#include +#include + +String get_device_id() { + // device id is stored at pos 0 and is 6 bytes long. + EEPROM.begin(6); + String device_id; + for (int i=0 ; i < 6 ; i++) { + char f = EEPROM.read(i); + device_id += (f); + } + EEPROM.end(); + return device_id; +} \ No newline at end of file diff --git a/RadDevice.h b/RadDevice.h new file mode 100644 index 0000000..360889b --- /dev/null +++ b/RadDevice.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +String get_device_id(); diff --git a/RadHelpers.cpp b/RadHelpers.cpp new file mode 100644 index 0000000..d4fa903 --- /dev/null +++ b/RadHelpers.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +String float_to_string(float value, int decimals) { + char c_value[10]; + dtostrf(value, 1, decimals, c_value); + return c_value; +} + + +void mqtt_reconnect(PubSubClient &client, String dev_id, RadConfig config) { + Serial.println("[MQTT] Attempting MQTT connection..."); + while (!client.connected()) { + if (client.connect(dev_id.c_str())) { + Serial.println("[MQTT] Connected"); + } + else { + Serial.println("[MQTT] Failed, rc=" + String(client.state())); + delay(5000); + } + } +} \ No newline at end of file diff --git a/RadHelpers.h b/RadHelpers.h new file mode 100644 index 0000000..5c6579a --- /dev/null +++ b/RadHelpers.h @@ -0,0 +1,10 @@ +#pragma once + +#include +#include +#include +#include + +String float_to_string(float value, int decimals); + +void mqtt_reconnect(PubSubClient &client, String dev_id, RadConfig config); \ No newline at end of file diff --git a/RadLED.cpp b/RadLED.cpp new file mode 100644 index 0000000..9c8802d --- /dev/null +++ b/RadLED.cpp @@ -0,0 +1,20 @@ +#include +#include + +void LED_flash(int pin, int no_times, int on_ms, int off_ms) { + // When using the internal LED, the LOW and HIGH are around the wrong way. So: + // LOW = HIGH + // HIGH = LOW + int _LOW = LOW; + int _HIGH = HIGH; + if (pin == 2) { + _LOW = HIGH; + _HIGH = LOW; + } + for (int count=0; count < no_times; count++) { + digitalWrite(pin, LOW); + delay(on_ms); + digitalWrite(pin, HIGH); + delay(off_ms); + } +} \ No newline at end of file diff --git a/RadLED.h b/RadLED.h new file mode 100644 index 0000000..aa73360 --- /dev/null +++ b/RadLED.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +void LED_flash(int pin, int no_times, int on_ms, int off_ms); diff --git a/RadWifi.cpp b/RadWifi.cpp new file mode 100644 index 0000000..ff77d59 --- /dev/null +++ b/RadWifi.cpp @@ -0,0 +1,29 @@ +#include + +#include +#include +#include + +void setup_wifi(String device_id, RadConfig config) { + + // The devices last digits of the IP are based on the last digits of the device id + a starting point. + int last_digits = device_id.substring(4, 6).toInt(); + IPAddress this_ip(config.gateway[0], config.gateway[1], config.gateway[2], config.ip_start + last_digits); + + // Turn off broadcoasting. + WiFi.mode(WIFI_STA); + // Set static IP + WiFi.config(this_ip, config.gateway_ip(), config.gateway_ip()); + WiFi.hostname(device_id); + Serial.println("[WIFI] Connecting to '" + String(config.wifi_ssid)); + WiFi.begin(config.wifi_ssid, config.wifi_password); + + while (WiFi.status() != WL_CONNECTED) { + LED_flash(LED_BUILTIN, 3, 100, 100); + Serial.print(String(WiFi.status() + ".")); + } + Serial.println("\n[WIFI] Connected!"); + + Serial.print("[WIFI] IP Address: "); + Serial.println(WiFi.localIP()); +} \ No newline at end of file diff --git a/RadWifi.h b/RadWifi.h new file mode 100644 index 0000000..3c2a059 --- /dev/null +++ b/RadWifi.h @@ -0,0 +1,7 @@ +#pragma once + +#include +#include +#include + +void setup_wifi(String device_id, RadConfig config);