Skip to content

Commit

Permalink
Beginning of the common helper tools
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunorman committed May 27, 2018
1 parent be39b0e commit 8e6a867
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 0 deletions.
15 changes: 15 additions & 0 deletions RadDevice.cpp
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;
}
5 changes: 5 additions & 0 deletions RadDevice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <Arduino.h>

String get_device_id();
23 changes: 23 additions & 0 deletions RadHelpers.cpp
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);
}
}
}
10 changes: 10 additions & 0 deletions RadHelpers.h
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);
20 changes: 20 additions & 0 deletions RadLED.cpp
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);
}
}
5 changes: 5 additions & 0 deletions RadLED.h
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);
29 changes: 29 additions & 0 deletions RadWifi.cpp
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());
}
7 changes: 7 additions & 0 deletions RadWifi.h
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);

0 comments on commit 8e6a867

Please sign in to comment.