-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Beginning of the common helper tools
- Loading branch information
1 parent
be39b0e
commit 8e6a867
Showing
8 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <Arduino.h> | ||
#include <RadDevice.h> | ||
#include <EEPROM.h> | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <Arduino.h> | ||
|
||
String get_device_id(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <Arduino.h> | ||
#include <PubSubClient.h> | ||
#include <RadConfig.h> | ||
|
||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
#include <Arduino.h> | ||
#include <PubSubClient.h> | ||
#include <RadConfig.h> | ||
#include <RadLED.h> | ||
|
||
String float_to_string(float value, int decimals); | ||
|
||
void mqtt_reconnect(PubSubClient &client, String dev_id, RadConfig config); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <Arduino.h> | ||
#include <RadLED.h> | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include <Arduino.h> | ||
|
||
void LED_flash(int pin, int no_times, int on_ms, int off_ms); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <Arduino.h> | ||
|
||
#include <RadConfig.h> | ||
#include <ESP8266WiFi.h> | ||
#include <RadLED.h> | ||
|
||
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
#include <Arduino.h> | ||
#include <RadConfig.h> | ||
#include <ESP8266WiFi.h> | ||
|
||
void setup_wifi(String device_id, RadConfig config); |