Compare commits

..

3 Commits

Author SHA1 Message Date
Kenwood f3d60b9adb force update 2021-12-17 18:09:45 -05:00
Kenwood 5f26731a3f Make a functioning firmware 2021-12-15 01:10:25 -05:00
Kenwood 7c6177cd40 Import all 2021-12-14 18:28:39 -05:00
16 changed files with 240 additions and 309 deletions

7
.gitignore vendored
View File

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

View File

@ -1,6 +1,5 @@
{
"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",
"port": "/dev/ttyUSB0"
"board": "esp8266:esp8266:arduino-esp8266"
}

View File

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

View File

@ -1,157 +0,0 @@
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
// Replace the next variables with your SSID/Password combination
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// Add your MQTT Broker IP address, example:
//const char* mqtt_server = "192.168.1.144";
const char* mqtt_server = "YOUR_MQTT_BROKER_IP_ADDRESS";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
//uncomment the following lines if you're using SPI
/*#include <SPI.h>
#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
float temperature = 0;
float humidity = 0;
// LED Pin
const int ledPin = 4;
void setup() {
Serial.begin(115200);
// default settings
// (you can also pass in a Wire library object like &Wire2)
//status = bme.begin();
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(ledPin, OUTPUT);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
// Changes the output state according to the message
if (String(topic) == "esp32/output") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin, LOW);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Subscribe
client.subscribe("esp32/output");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 5000) {
lastMsg = now;
// Temperature in Celsius
temperature = bme.readTemperature();
// Uncomment the next line to set temperature in Fahrenheit
// (and comment the previous temperature line)
//temperature = 1.8 * bme.readTemperature() + 32; // Temperature in Fahrenheit
// Convert the value to a char array
char tempString[8];
dtostrf(temperature, 1, 2, tempString);
Serial.print("Temperature: ");
Serial.println(tempString);
client.publish("esp32/temperature", tempString);
humidity = bme.readHumidity();
// Convert the value to a char array
char humString[8];
dtostrf(humidity, 1, 2, humString);
Serial.print("Humidity: ");
Serial.println(humString);
client.publish("esp32/humidity", humString);
}
}

Binary file not shown.

View File

@ -0,0 +1,116 @@
# 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

@ -0,0 +1,8 @@
# 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

@ -0,0 +1,105 @@
#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

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

View File

@ -0,0 +1 @@
PubSubClient

View File

@ -1,130 +0,0 @@
// 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);
// Initalize motor directions
pinMode(motorReverse, OUTPUT);
digitalWrite(motorReverse, LOW);
pinMode(motorForward, OUTPUT);
digitalWrite(motorForward, LOW);
// Initalize Sense Pin
pinMode(sense, INPUT);
// use "digitalRead(sense);" to read
}
void setup_wifi() {
delay(10);
// Set our hostname
WiFi.hostname("ESP-" WINDOW_NAME);
// Connect to the wifi!
WiFi.begin(WIFI_SSID, WIFI_PASS);
// For as long as we're not connected..
while (WiFi.status() != WL_CONNECTED) {
delay(950); // Wait...
tone(buzzer, 1200, 50); // Beep...
//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* message, unsigned int length) {
//Serial.print("Message arrived on topic: ");
//Serial.print(topic);
//Serial.print(". Message: ");
String messageString;
// Reconstruct a string from the mqtt contents.
for (int i = 0; i < length; i++) {
messageString += (char)message[i];
}
if (String(topic) == MQTT_ROOT "/request_state") {
//Serial.print("Changing output to ");
if(messageString == "open"){
//Serial.println("Opening window.");
digitalWrite(motorForward, HIGH);
digitalWrite(motorReverse, LOW);
tone(buzzer, 2000, 200);
delay(200);
tone(buzzer, 2000, 200);
delay(200);
tone(buzzer, 2000, 200);
delay(200);
client.publish(MQTT_ROOT "/current_state", "open", true);
}
else if(messageString == "close"){
//Serial.println("Closing window");
digitalWrite(motorForward, LOW);
digitalWrite(motorReverse, HIGH);
tone(buzzer, 2000, 200);
delay(200);
tone(buzzer, 2000, 200);
delay(200);
tone(buzzer, 2000, 200);
delay(200);
client.publish(MQTT_ROOT "/current_state", "close", true);
}
}
}
void loop() {
// If we ever disconnect, reconnect.
if (!client.connected()) {
reconnect();
}
// Run the client loop
client.loop();
client.subscribe(MQTT_ROOT "/request_state");
delay(1000);
}

View File

@ -1,10 +0,0 @@
#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

@ -1,5 +0,0 @@
#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"