Compare commits

7 Commits

15 changed files with 120 additions and 240 deletions

7
.gitignore vendored
View File

@@ -1,8 +1 @@
# Artifacts
*_secrets.h
_build
version.h
# Binaries
*.FCStd1
*.stl

View File

@@ -1,5 +1,6 @@
{
"sketch": "window_control/window_control.ino",
"configuration": "BoardModel=primo,xtal=80,vt=flash,exception=legacy,ssl=all,eesz=4M2M,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=115200",
"board": "esp8266:esp8266:arduino-esp8266"
"board": "esp8266:esp8266:arduino-esp8266",
"port": "/dev/ttyUSB0"
}

5
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,5 @@
{
"files.associations": {
"sstream": "cpp"
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,116 +0,0 @@
# Kitsune Scientific
# What project to build
PROJ := firmware
# What board to build for and its core
CORE ?= esp8266:esp8266
FQBN ?= esp8266:esp8266:generic
# Tools, their links and versions
ARDUINO_CLI_VERSION :=
ARDUINO_CLI_URL := https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh
ARDUINO_LINT_VERSION :=
ARDUINO_LINT_URL := https://raw.githubusercontent.com/arduino/arduino-lint/main/etc/install.sh
# The version and time we're compiling from
GIT_VERSION := "$(shell git describe --abbrev=4 --dirty --always --tags)"
COMPILED_DATE := "$(shell date)"
# Treat all warnings as errors.
BUILDPROP := compiler.warning_flags.all='-Wall -Wextra -Werror'
# Locations
ROOT := $(PWD)
BINDIR := $(ROOT)/_build/bin
BUILDDIR := $(ROOT)/_build
# What port to build on
ifndef SERIAL_DEV
ifneq (,$(wildcard /dev/ttyUSB0))
SERIAL_DEV = /dev/ttyUSB0
else ifneq (,$(wildcard /dev/ttyACM0))
SERIAL_DEV = /dev/ttyACM0
else
SERIAL_DEV = unknown
endif
endif
.PHONY: all help build upload version release requirements
# Do everything
all: tools requirements version build
help:
@echo
@echo "Targets:"
@echo " lint De-lint the software."
@echo " build Build the software."
@echo " upload Upload the software."
@echo " requirements Fetch all requirements."
@echo
# Set VERBOSE=1 to not run in --silent
ifndef VERBOSE
.SILENT:
endif
# Only build the code
build: version $(PROJ).ino
$(BINDIR)/arduino-cli core install $(CORE)
$(BINDIR)/arduino-cli compile -b $(FQBN) $(PROJ)
# Make binaries
bin: tools requirements version $(PROJ).ino
$(BINDIR)/arduino-cli core install $(CORE)
$(BINDIR)/arduino-cli compile -b $(FQBN) $(PROJ) -e
# Clean and rename (could be optimized)
mv build/* _build
rm -rv build
# Only upload the software
upload: version build $(PROJ).ino
$(BINDIR)/arduino-cli upload -b $(FQBN) $(PROJ) -p $(SERIAL_DEV)
# Only update the version
version:
echo "#define VERSION \"$(GIT_VERSION)\"" > version.h
echo "#define COMPILED_ON \"$(COMPILED_DATE)\"" >> version.h
# Generate an artifact
release: build $(PROJ).ino
$(BINDIR)/arduino-cli compile -b $(FQBN) $(PROJ) --output-dir $(BUILDDIR)
# Only fetch the requirements
requirements:
@if [ -e requirements.txt ]; \
then while read -r i ; do echo ; \
echo "---> Installing " '"'$$i'"' ; \
$(BINDIR)/arduino-cli lib install "$$i" ; \
done < requirements.txt ; \
else echo "---> MISSING requirements.txt file"; \
fi
# Monitor the serial (debug) output.
# Requires minicom
monitor: upload
minicom -D $(SERIAL_DEV) -b 115200
lint: tools
$(BINDIR)/arduino-lint --compliance strict
# Fetches all the tools required
tools:
mkdir -p $(BINDIR)
curl -fsSL $(ARDUINO_CLI_URL) | BINDIR=$(BINDIR) sh -s $(ARDUINO_CLI_VERSION)
curl -fsSL $(ARDUINO_LINT_URL) | BINDIR=$(BINDIR) sh -s $(ARDUINO_LINT_VERSION)
# Messy, come back and fix?
clean:
rm -rv _build/arduino.avr.mega

View File

@@ -1,8 +0,0 @@
# To install
You will have to add `https://arduino.esp8266.com/stable/package_esp8266com_index.json` to your arduino-cli.yaml
```shell
arduino-cli config init
vim ~/.arduino15/arduino-cli.yaml
```

View File

@@ -1,105 +0,0 @@
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// WiFi
const char *ssid = "internet_of_insecure_things";
const char *wifi_password = "iamnotacrook";
// MQTT
const char *clientID = "window_0"; // Unique to this device
const char *mqtt_server = "mqtt.kitsunehosting.net";
const char *mqtt_topic = "home/upstairs_lab/actuators/window_0";
const char *mqtt_username = "admin";
const char *mqtt_password = "iamnotacrook";
// Initialise the WiFi and MQTT Client objects
WiFiClient wifiClient;
PubSubClient client(mqtt_server, 1883, wifiClient); // 1883 is the listener port for the Broker
void setup()
{
// pinMode(ledPin, OUTPUT);
// Switch the on-board LED off to start with
// digitalWrite(ledPin, HIGH);
// Begin Serial on 115200
// Remember to choose the correct Baudrate on the Serial monitor!
// This is just for debugging purposes
Serial.begin(115200);
Serial.print("Connecting to ");
Serial.println(ssid);
// Connect to the WiFi
WiFi.begin(ssid, wifi_password);
// Wait until the connection has been confirmed before continuing
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Configure client callback
client.setCallback(callback);
}
void reconnect()
{
/* Reconnects to the MQTT Server */
if (client.connect(clientID, mqtt_username, mqtt_password))
{
Serial.println("Connected to MQTT Broker!");
}
else
{
Serial.println("Connection to MQTT Broker failed...");
}
client.publish("test", "0");
// What to listen for
client.subscribe("test");
}
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
}
void loop()
{
delay(600);
// digitalWrite(ledPin, HIGH);
if (client.publish(mqtt_topic, "1"))
{
Serial.println("Button pushed and message sent!");
}
else
{
Serial.println("Message failed to send. Reconnecting to MQTT Broker and trying again");
reconnect();
delay(10);
client.publish(mqtt_topic, "-1");
}
tone(5, 1200, 300);
client.loop(); // Runs cleanup, etc on client
}

View File

@@ -1,2 +0,0 @@
#define WINDOW_CLOSE = 5
#define WINDOW_OPEN = 6

View File

@@ -1 +0,0 @@
PubSubClient

View File

@@ -0,0 +1,98 @@
// Include the esp wifi lib
#include <ESP8266WiFi.h>
// Include the mqtt client lib
#include <PubSubClient.h>
// Include the settings.h.
#include "window_control_settings.h"
#define buzzer 2
#define motorForward 1
#define motorReverse 0
#define sense 3
WiFiClient espClient;
PubSubClient client;
void setup() {
setup_wifi();
client.setClient(espClient);
client.setServer(MQTT_SERVER, MQTT_PORT);
client.setCallback(callback);
// Initalize some pins!
// Initalize buzzer pin
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
WiFi.hostname("ESP-" WINDOW_NAME);
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(950);
tone(buzzer, 1200, 50);
Serial.println("Not yet connected.. Waiting 1s to check again..");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
char receivedChar = (char)payload[i];
Serial.print(receivedChar);
if (receivedChar == '0')
//digitalWrite(in_led, LOW);
if (receivedChar == '1')
//digitalWrite(in_led, HIGH);
}
Serial.println();
}
void loop() {
// If we ever disconnect, reconnect.
if (!client.connected()) {
reconnect();
}
// Run the client loop
client.loop();
// Publishes a random 0 and 1 like someone switching off and on randomly (random(2))
client.publish(MQTT_ROOT "/random", String(random(2)).c_str(), true);
delay(1000);
client.subscribe(MQTT_ROOT "/in");
tone(buzzer, 2000, 500);
delay(1000);
}

View File

@@ -0,0 +1,10 @@
#define WIFI_SSID "muner"
#define WIFI_PASS "apollo11"
#define MQTT_SERVER "mqtt.kitsunehosting.net"
#define MQTT_PORT 1883
#define MQTT_USER "device"
#define MQTT_PASS "iamnotacrook"
#define WINDOW_NAME "window_0"
#define MQTT_ROOT "home/upstairs_lab/actuators/" WINDOW_NAME

View File

@@ -0,0 +1,5 @@
#define SECRET_SSID "secret_ssid"
#define SECRET_PASS ""Ah ah ah, you didn't say the magic word.""
#define SECRET_MQTT_USER "mqtt_user"
#define SECRET_MQTT_PASS "mqtt_pass"