Initial Commit.

Nothing works here.
This commit is contained in:
Chris Giacofei 2023-01-25 21:00:25 -05:00
commit 0a7fa301ed
46 changed files with 3454 additions and 0 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
* text eol=lf

12
.gitignore vendored Executable file
View File

@ -0,0 +1,12 @@
.DS_Store
bin/*/
*build-*
*build*
*~
build-tmp/
Makefile
doc/
debug.txt
*.geany
*.tags
*.bin

21
LICENSE Executable file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Chris Giacofei
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

76
MakefileGlobal.mk Normal file
View File

@ -0,0 +1,76 @@
BASE_DIR := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
LIBRARY_DIR := $(BASE_DIR)/lib
ifndef MONITOR_PORT
ifneq (,$(wildcard /dev/ttyUSB0))
MONITOR_PORT = /dev/ttyUSB0
else ifneq (,$(wildcard /dev/ttyACM0))
MONITOR_PORT = /dev/ttyACM0
else
MONITOR_PORT = unknown
endif
endif
ESPTOOL := $(BASE_DIR)/hardware/esp8266/tools/esptool/esptool
ESPOTA := $(BASE_DIR)/hardware/esp8266/tools/espota.py
TARGET_DIR := $(BASE_DIR)/$(subst :,.,build/$(FQBN))
BIN_DIR := $(BASE_DIR)/bin
SRC := $(wildcard $(SKETCH_DIR)/*.ino)
HDRS := $(wildcard $(SKETCH_DIR)/*.h)
BIN := $(notdir $(SRC)).bin
$(info FQBN is [${FQBN}])
$(info ESP_IP is [${ESP_IP}])
$(info OTA_SERVER is [${OTA_SERVER}])
$(info OTA_PASSWD is [${OTA_PASSWD}])
$(info MONITOR_PORT is [${MONITOR_PORT}])
$(info TARGET_DIR is [${TARGET_DIR}])
$(info BIN_DIR is [${BIN_DIR}])
$(info SRC is [${SRC}])
$(info HDRS is [${HDRS}])
$(info BIN is [${BIN}])
$(info SERIAL_DEV is [${SERIAL_DEV}])
all: $(BIN_DIR)/$(BIN) upload
compile: $(BIN_DIR)/$(BIN)
upload: $(BIN_DIR)/$(BIN)
$(ESPTOOL) -v -cd nodemcu -b 115200 -p $(MONITOR_PORT) -ca 0x00000 -cf $(TARGET_DIR)/$(SKETCH).bin
ota: $(BIN_DIR)/$(BIN)
$(ESPOTA) -d -r -i $(ESP_IP) -I $(OTA_SERVER) -p 8266 -P 8266 -a $(OTA_PASSWD) -f $(TARGET_DIR)/$(SKETCH).bin
clean:
rm -rf $(TARGET_DIR)
monitor:
screen $(MONITOR_PORT) 115200
new-library:
$(eval NEW_LIB = $(LIBRARY_DIR)/$(LIB))
mkdir -p $(NEW_LIB)
$(eval UC = $(shell echo '$(LIB)' | tr '[:lower:]' '[:upper:]'))
@ echo "#ifndef $(UC)_h" > $(NEW_LIB)/$(LIB).h
@ echo "#define $(UC)_h" >> $(NEW_LIB)/$(LIB).h
@ echo "" >> $(NEW_LIB)/$(LIB).h
@ echo "#endif" >> $(NEW_LIB)/$(LIB).h
.PHONY: all compile test library upload ota clean monitor
$(TARGET_DIR)/$(BIN): $(SRC) $(HDRS)
@ echo $(FQBN)
@ mkdir -p $(TARGET_DIR)
arduino-cli compile -logger=machine \
--fqbn=$(FQBN) \
--library "$(subst $(space),$(sep),$(strip $(sort $(dir $(wildcard $(LIBRARY_DIR)/*/)))))" \
--build-path "$(TARGET_DIR)" \
--warnings=none \
--verbose \
"$(SKETCH_DIR)/$(SKETCH)"
$(BIN_DIR)/$(BIN): $(TARGET_DIR)/$(BIN)
@ echo "$(BIN) $(BIN_DIR)/"
@ mv $(TARGET_DIR)/$(BIN) $(BIN_DIR)/

2
README.md Executable file
View File

@ -0,0 +1,2 @@
# Code for the Damn Brewery

BIN
bin/FermController.ino.bin Normal file

Binary file not shown.

View File

@ -0,0 +1,343 @@
#!/usr/bin/env python3
#
# Original espota.py by Ivan Grokhotkov:
# https://gist.github.com/igrr/d35ab8446922179dc58c
#
# Modified since 2015-09-18 from Pascal Gollor (https://github.com/pgollor)
# Modified since 2015-11-09 from Hristo Gochkov (https://github.com/me-no-dev)
# Modified since 2016-01-03 from Matthew O'Gorman (https://githumb.com/mogorman)
#
# This script will push an OTA update to the ESP
# use it like: python3 espota.py -i <ESP_IP_address> -I <Host_IP_address> -p <ESP_port> -P <Host_port> [-a password] -f <sketch.bin>
# Or to upload SPIFFS image:
# python3 espota.py -i <ESP_IP_address> -I <Host_IP_address> -p <ESP_port> -P <HOST_port> [-a password] -s -f <spiffs.bin>
#
# Changes
# 2015-09-18:
# - Add option parser.
# - Add logging.
# - Send command to controller to differ between flashing and transmitting SPIFFS image.
#
# Changes
# 2015-11-09:
# - Added digest authentication
# - Enhanced error tracking and reporting
#
# Changes
# 2016-01-03:
# - Added more options to parser.
#
from __future__ import print_function
import socket
import sys
import os
import optparse
import logging
import hashlib
import random
# Commands
FLASH = 0
SPIFFS = 100
AUTH = 200
PROGRESS = False
# update_progress() : Displays or updates a console progress bar
## Accepts a float between 0 and 1. Any int will be converted to a float.
## A value under 0 represents a 'halt'.
## A value at 1 or bigger represents 100%
def update_progress(progress):
if (PROGRESS):
barLength = 60 # Modify this to change the length of the progress bar
status = ""
if isinstance(progress, int):
progress = float(progress)
if not isinstance(progress, float):
progress = 0
status = "error: progress var must be float\r\n"
if progress < 0:
progress = 0
status = "Halt...\r\n"
if progress >= 1:
progress = 1
status = "Done...\r\n"
block = int(round(barLength*progress))
text = "\rUploading: [{0}] {1}% {2}".format( "="*block + " "*(barLength-block), int(progress*100), status)
sys.stderr.write(text)
sys.stderr.flush()
else:
sys.stderr.write('.')
sys.stderr.flush()
def serve(remoteAddr, localAddr, remotePort, localPort, password, filename, command = FLASH):
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (localAddr, localPort)
logging.info('Starting on %s:%s', str(server_address[0]), str(server_address[1]))
try:
sock.bind(server_address)
sock.listen(1)
except Exception:
logging.error("Listen Failed")
return 1
# Check whether Signed Update is used.
if ( os.path.isfile(filename + '.signed') ):
filename = filename + '.signed'
file_check_msg = 'Detected Signed Update. %s will be uploaded instead.' % (filename)
sys.stderr.write(file_check_msg + '\n')
sys.stderr.flush()
logging.info(file_check_msg)
content_size = os.path.getsize(filename)
f = open(filename,'rb')
file_md5 = hashlib.md5(f.read()).hexdigest()
f.close()
logging.info('Upload size: %d', content_size)
message = '%d %d %d %s\n' % (command, localPort, content_size, file_md5)
# Wait for a connection
logging.info('Sending invitation to: %s', remoteAddr)
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
remote_address = (remoteAddr, int(remotePort))
sock2.sendto(message.encode(), remote_address)
sock2.settimeout(10)
try:
data = sock2.recv(128).decode()
except Exception:
logging.error('No Answer')
sock2.close()
return 1
if (data != "OK"):
if(data.startswith('AUTH')):
nonce = data.split()[1]
cnonce_text = '%s%u%s%s' % (filename, content_size, file_md5, remoteAddr)
cnonce = hashlib.md5(cnonce_text.encode()).hexdigest()
passmd5 = hashlib.md5(password.encode()).hexdigest()
result_text = '%s:%s:%s' % (passmd5 ,nonce, cnonce)
result = hashlib.md5(result_text.encode()).hexdigest()
sys.stderr.write('Authenticating...')
sys.stderr.flush()
message = '%d %s %s\n' % (AUTH, cnonce, result)
sock2.sendto(message.encode(), remote_address)
sock2.settimeout(10)
try:
data = sock2.recv(32).decode()
except Exception:
sys.stderr.write('FAIL\n')
logging.error('No Answer to our Authentication')
sock2.close()
return 1
if (data != "OK"):
sys.stderr.write('FAIL\n')
logging.error('%s', data)
sock2.close()
sys.exit(1)
return 1
sys.stderr.write('OK\n')
else:
logging.error('Bad Answer: %s', data)
sock2.close()
return 1
sock2.close()
logging.info('Waiting for device...')
try:
sock.settimeout(10)
connection, client_address = sock.accept()
sock.settimeout(None)
connection.settimeout(None)
except Exception:
logging.error('No response from device')
sock.close()
return 1
received_ok = False
try:
f = open(filename, "rb")
if (PROGRESS):
update_progress(0)
else:
sys.stderr.write('Uploading')
sys.stderr.flush()
offset = 0
while True:
chunk = f.read(1460)
if not chunk: break
offset += len(chunk)
update_progress(offset/float(content_size))
connection.settimeout(10)
try:
connection.sendall(chunk)
if connection.recv(32).decode().find('O') >= 0:
# connection will receive only digits or 'OK'
received_ok = True
except Exception:
sys.stderr.write('\n')
logging.error('Error Uploading')
connection.close()
f.close()
sock.close()
return 1
sys.stderr.write('\n')
logging.info('Waiting for result...')
# libraries/ArduinoOTA/ArduinoOTA.cpp L311 L320
# only sends digits or 'OK'. We must not not close
# the connection before receiving the 'O' of 'OK'
try:
connection.settimeout(60)
received_ok = False
received_error = False
while not (received_ok or received_error):
reply = connection.recv(64).decode()
# Look for either the "E" in ERROR or the "O" in OK response
# Check for "E" first, since both strings contain "O"
if reply.find('E') >= 0:
sys.stderr.write('\n')
logging.error('%s', reply)
received_error = True
elif reply.find('O') >= 0:
logging.info('Result: OK')
received_ok = True
connection.close()
f.close()
sock.close()
if received_ok:
return 0
return 1
except Exception:
logging.error('No Result!')
connection.close()
f.close()
sock.close()
return 1
finally:
connection.close()
f.close()
sock.close()
return 1
# end serve
def parser(unparsed_args):
parser = optparse.OptionParser(
usage = "%prog [options]",
description = "Transmit image over the air to the esp8266 module with OTA support."
)
# destination ip and port
group = optparse.OptionGroup(parser, "Destination")
group.add_option("-i", "--ip",
dest = "esp_ip",
action = "store",
help = "ESP8266 IP Address.",
default = False
)
group.add_option("-I", "--host_ip",
dest = "host_ip",
action = "store",
help = "Host IP Address.",
default = "0.0.0.0"
)
group.add_option("-p", "--port",
dest = "esp_port",
type = "int",
help = "ESP8266 ota Port. Default 8266",
default = 8266
)
group.add_option("-P", "--host_port",
dest = "host_port",
type = "int",
help = "Host server ota Port. Default random 10000-60000",
default = random.randint(10000,60000)
)
parser.add_option_group(group)
# auth
group = optparse.OptionGroup(parser, "Authentication")
group.add_option("-a", "--auth",
dest = "auth",
help = "Set authentication password.",
action = "store",
default = ""
)
parser.add_option_group(group)
# image
group = optparse.OptionGroup(parser, "Image")
group.add_option("-f", "--file",
dest = "image",
help = "Image file.",
metavar="FILE",
default = None
)
group.add_option("-s", "--spiffs",
dest = "spiffs",
action = "store_true",
help = "Use this option to transmit a SPIFFS image and do not flash the module.",
default = False
)
parser.add_option_group(group)
# output group
group = optparse.OptionGroup(parser, "Output")
group.add_option("-d", "--debug",
dest = "debug",
help = "Show debug output. And override loglevel with debug.",
action = "store_true",
default = False
)
group.add_option("-r", "--progress",
dest = "progress",
help = "Show progress output. Does not work for ArduinoIDE",
action = "store_true",
default = False
)
parser.add_option_group(group)
(options, args) = parser.parse_args(unparsed_args)
return options
# end parser
def main(args):
# get options
options = parser(args)
# adapt log level
loglevel = logging.WARNING
if (options.debug):
loglevel = logging.DEBUG
# end if
# logging
logging.basicConfig(level = loglevel, format = '%(asctime)-8s [%(levelname)s]: %(message)s', datefmt = '%H:%M:%S')
logging.debug("Options: %s", str(options))
# check options
global PROGRESS
PROGRESS = options.progress
if (not options.esp_ip or not options.image):
logging.critical("Not enough arguments.")
return 1
# end if
command = FLASH
if (options.spiffs):
command = SPIFFS
# end if
return serve(options.esp_ip, options.host_ip, options.esp_port, options.host_port, options.auth, options.image, command)
# end main
if __name__ == '__main__':
sys.exit(main(sys.argv))
# end if

View File

@ -0,0 +1,339 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.

View File

@ -0,0 +1,21 @@
# esptool.py
A Python-based, open-source, platform-independent utility to communicate with the ROM bootloader in Espressif chips.
[![Test esptool](https://github.com/espressif/esptool/actions/workflows/test_esptool.yml/badge.svg?branch=master)](https://github.com/espressif/esptool/actions/workflows/test_esptool.yml) [![Build esptool](https://github.com/espressif/esptool/actions/workflows/build_esptool.yml/badge.svg?branch=master)](https://github.com/espressif/esptool/actions/workflows/build_esptool.yml)
## Documentation
Visit the [documentation](https://docs.espressif.com/projects/esptool/) or run `esptool.py -h`.
## Contribute
If you're interested in contributing to esptool.py, please check the [contributions guide](https://docs.espressif.com/projects/esptool/en/latest/contributing.html).
## About
esptool.py was initially created by Fredrik Ahlberg (@[themadinventor](https://github.com/themadinventor/)), and later maintained by Angus Gratton (@[projectgus](https://github.com/projectgus/)). It is now supported by Espressif Systems. It has also received improvements from many members of the community.
## License
This document and the attached source code are released as Free Software under GNU General Public License Version 2 or later. See the accompanying [LICENSE file](https://github.com/espressif/esptool/blob/master/LICENSE) for a copy.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

22
lib/Button/Button.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef button_h
#define button_h
#include "Arduino.h"
class Button
{
private:
uint8_t btn;
uint16_t state;
public:
void begin(uint8_t button) {
btn = button;
state = 0;
pinMode(btn, INPUT_PULLUP);
}
bool pressed() {
state = (state<<1) | digitalRead(btn) | 0xfe00;
return (state == 0xff00);
}
};
#endif

View File

@ -0,0 +1,109 @@
#include "communicator.h"
Communicator::Communicator(WiFiClient& network, void (*cmd)(char *topic, byte *payload, unsigned int length)) {
this->_net = network;
this->mqttCallback = cmd;
this->_mqtt_client.setBufferSize(1536);
}
void Communicator::loop() {
_mqtt_client.loop();
}
bool Communicator::ConnectMQTT(const String &server, const String &name, const String &user, const String &password) {
_mqtt_client.setClient(_net);
_mqtt_client.setServer(server.c_str(), 1883);
_mqtt_client.setCallback(mqttCallback);
byte i = 0;
if (_mqtt_client.connected()) return true;
while (!_mqtt_client.connected() && (i < 3)) {
Serial.println("Attempt MQTT Connection.");
boolean ret;
ret = _mqtt_client.connect(name.c_str(), user.c_str(), password.c_str());
if (ret) {
Serial.println("Connected to MQTT");
return true;
} else {
int Status = _mqtt_client.state();
switch (Status)
{
case -4:
Serial.println(F("Connection timeout"));
break;
case -3:
Serial.println(F("Connection lost"));
break;
case -2:
Serial.println(F("Connect failed"));
break;
case -1:
Serial.println(F("Disconnected"));
break;
case 1:
Serial.println(F("Bad protocol"));
break;
case 2:
Serial.println(F("Bad client ID"));
break;
case 3:
Serial.println(F("Unavailable"));
break;
case 4:
Serial.println(F("Bad credentials"));
break;
case 5:
Serial.println(F("Unauthorized"));
break;
}
}
Serial.print(".");
i++;
delay(5000);
}
return false;
}
void Communicator::mqtt_discovery(const String topic, StaticJsonDocument<1536> &entity) {
String payload;
serializeJson(entity, payload);
bool response = ConnectMQTT(MQTT_BROKER.toString(), MQTT_NAME, MQTT_USER, MQTT_PASSWORD);
if (response)
{
Serial.print("Sending discovery payload to ");
Serial.println(topic);
Serial.println("");
Serial.println(payload);
Serial.println("");
_mqtt_client.publish(topic.c_str(), payload.c_str(), false);
if (entity.containsKey("mode_cmd_t")) _mqtt_client.subscribe(entity["mode_cmd_t"]);
if (entity.containsKey("temp_cmd_t")) _mqtt_client.subscribe(entity["temp_cmd_t"]);
if (entity.containsKey("temp_hi_cmd_t")) _mqtt_client.subscribe(entity["temp_hi_cmd_t"]);
if (entity.containsKey("temp_lo_cmd_t")) _mqtt_client.subscribe(entity["temp_lo_cmd_t"]);
_mqtt_client.loop();
}
}
void Communicator::publish_data(String topic, String value) {
_mqtt_client.publish(topic.c_str(), value.c_str(), false);
}

View File

@ -0,0 +1,25 @@
#ifndef MyClass_h
#define MyClass_h
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include "secrets.h"
class Communicator {
public:
Communicator(WiFiClient&, void (*mqttCallback)(char*, byte*, unsigned int));
void (*mqttCallback)(char*, byte*, unsigned int);
bool ConnectMQTT(const String&, const String&, const String&, const String&);
void mqtt_discovery(const String, StaticJsonDocument<1536>&);
void publish_data(String, String);
void loop();
private:
PubSubClient _mqtt_client;
WiFiClient _net;
};
#endif

358
lib/Device/Device.cpp Normal file
View File

@ -0,0 +1,358 @@
#include "Device.h"
Device::Device(const uint8_t _pin_cool, const uint8_t _pin_heat, const uint8_t _pin_wire) {
this->_pin_cool = _pin_cool;
this->_pin_heat = _pin_heat;
this->_pin_wire = _pin_wire;
}
void Device::CoolSet(bool set_state) {
// Set pin state
if (set_state) {
Serial.println("Starting cooling.");
strcpy(_curr_action, ACTION_COOLING);
SendState(ACTION_TPC, _curr_action);
digitalWrite(_pin_heat, LOW); // Just to be sure.
digitalWrite(_pin_cool, HIGH);
} else {
Serial.println("Stopping cooling.");
strcpy(_curr_action, ACTION_IDLE);
SendState(ACTION_TPC, _curr_action);
digitalWrite(_pin_cool, LOW);
digitalWrite(_pin_heat, LOW);
}
}
void Device::HeatSet(bool set_state) {
if (set_state) {
Serial.println("Starting heat.");
strcpy(_curr_action, ACTION_HEATING);
SendState(ACTION_TPC, _curr_action);
digitalWrite(_pin_cool, LOW); // Just to be sure.
digitalWrite(_pin_heat, HIGH);
} else {
Serial.println("Stopping heat.");
strcpy(_curr_action, ACTION_IDLE);
SendState(ACTION_TPC, _curr_action);
digitalWrite(_pin_cool, LOW);
digitalWrite(_pin_heat, LOW);
}
}
void Device::Hyst(float new_value){
_hyst = new_value;
}
float Device::Hyst(){
return _hyst;
}
void Device::Update(){
float currentMillis = millis();
//Read temp sensor.
if (currentMillis - _lastSensor >= _sensor_period) {
//Read temperature from DS18b20
float tempC = _sensors.getTempC(_ds_serial);
if (tempC == DEVICE_DISCONNECTED_C) {
Serial.println("Error: Could not read temperature data");
} else {
_curr_temp = DallasTemperature::toFahrenheit(tempC);
}
}
// Some helpful variables.
bool heating = _curr_action==ACTION_HEATING;
bool cooling = _curr_action==ACTION_COOLING;
bool running = (heating || cooling);
// Adjust cool/heat on or off
if (_mode == "cool"){
if (_curr_temp > _temp + _hyst && !cooling) {
CoolSet(true);
} else if (_curr_temp <= _temp && cooling) {
CoolSet(false);
}
} else if (_mode == "heat"){
if (_curr_temp < _temp - _hyst && !heating) {
HeatSet(true);
} else if (_curr_temp >= _temp && heating) {
HeatSet(false);
}
} else if (_mode == "auto"){
if ((_curr_temp < _temp_lo - _hyst) && !heating) {
HeatSet(true);
} else if ((_curr_temp > _temp_hi + _hyst) && !cooling) {
CoolSet(true);
} else if (running && (_curr_temp >= _temp_lo) && (_curr_temp <= _temp_hi)) {
if (heating) HeatSet(false);
if (cooling) CoolSet(false);
}
} else {
// IS OFF
if (heating) HeatSet(false);
if (cooling) CoolSet(false);
}
//Send Data to broker
if (currentMillis - _lastSend >= _send_period) {
// Time's up, send data
char temp[7];
dtostrf(_curr_temp, 6, 2, temp);
SendState(TEMP_CURRENT, temp);
_lastSend = currentMillis;
}
_mqtt_client.loop();
}
void Device::AttachNet(WiFiClient &network) {
this->_net = network;
this->_mqtt_client.setClient(_net);
this->_mqtt_client.setBufferSize(DOC_SIZE);
}
void Device::AttachSensor(DallasTemperature &sensors, uint8_t serial[8]){
_sensors = sensors;
_ds_serial = serial;
}
//void Device::topicRoot(const String &root) { _device_prefix = root; }
//byte* Device::topicRoot() { return _device_prefix; }
void Device::SendConfig(char* broker, char* name, String &chipid, bool multimode) {
String name_slug = slugify(name);
String CMD_TPL = "{{ value }}";
String STAT_TPL = "{{ value_json }}";
_topic_root = ROOT + name_slug;
String _config_topic = CONFIG_ROOT + name_slug + "_" + chipid + "/config";
StaticJsonDocument<DOC_SIZE> payload_doc;
payload_doc["uniq_id"] = chipid + "_" + name_slug;
payload_doc["name"] = name;
payload_doc["temp_unit"] = "F";
payload_doc["max_temp"] = _max_temp;
payload_doc["min_temp"] = _min_temp;
payload_doc["initial"] = _init_temp;
// Action Topic
payload_doc["action_topic"] = _topic_root + ACTION_TPC;
payload_doc["action_template"] = STAT_TPL;
// Mode setup
payload_doc["mode_cmd_t"] = _topic_root + MODE_SET;
payload_doc["mode_cmd_tpl"] = CMD_TPL;
payload_doc["mode_stat_t"] = _topic_root + MODE_STATE;
payload_doc["mode_stat_tpl"] = STAT_TPL;
JsonArray modes = payload_doc.createNestedArray("modes");
modes.add("off");
modes.add("cool");
payload_doc["temp_cmd_t"] = _topic_root + TEMP_SET;
payload_doc["temp_cmd_tpl"] = CMD_TPL;
payload_doc["temp_stat_t"] = _topic_root + TEMP_STATE;
payload_doc["temp_stat_tpl"] = STAT_TPL;
payload_doc["curr_temp_t"] = _topic_root + TEMP_CURRENT;
payload_doc["curr_temp_tpl"] = CMD_TPL;
if (multimode) {
payload_doc["temp_hi_cmd_t"] = _topic_root + TEMP_HI_SET;
payload_doc["temp_hi_cmd_tpl"] = CMD_TPL;
payload_doc["temp_hi_stat_t"] = _topic_root + TEMP_HI_STATE;
payload_doc["temp_hi_stat_tpl"] = STAT_TPL;
payload_doc["temp_lo_cmd_t"] = _topic_root + TEMP_LO_SET;
payload_doc["temp_lo_cmd_tpl"] = CMD_TPL;
payload_doc["temp_lo_stat_t"] = _topic_root + TEMP_LO_STATE;
payload_doc["temp_lo_stat_tpl"] = STAT_TPL;
modes.add("heat");
modes.add("auto");
}
// Attach Device
JsonObject dev = payload_doc.createNestedObject("dev");
dev["name"] = DEVICE_NAME;
dev["mdl"] = DEVICE_MDL;
dev["sw"] = String(version);
dev["mf"] = DEVICE_MF;
JsonArray ids = dev.createNestedArray("ids");
ids.add(chipid);
String payload;
serializeJson(payload_doc, payload);
bool response = ConnectMQTT(broker, MQTT_NAME, MQTT_USER, MQTT_PASSWORD);
if (response) {
_mqtt_client.publish(_config_topic.c_str(), payload.c_str(), MSG_RETAIN);
_mqtt_client.loop();
}
_mqtt_client.subscribe((_topic_root + MODE_SET).c_str());
_mqtt_client.subscribe((_topic_root + TEMP_SET).c_str());
if (multimode) {
_mqtt_client.subscribe((_topic_root + TEMP_HI_SET).c_str());
_mqtt_client.subscribe((_topic_root + TEMP_LO_SET).c_str());
}
_mqtt_client.loop();
}
void Device::_Temp(byte value){
Serial.print("Set Temp");
_temp = value;
SendState(TEMP_STATE, (char*)value);
}
void Device::_Mode(char* value){
Serial.print("Set Mode");
strcpy(_mode, value);
SendState(MODE_STATE, (char*)value);
}
void Device::_TempHi(byte value){
Serial.print("Set High Temp");
_temp_hi = value;
SendState(TEMP_HI_STATE, (char*)value);
}
void Device::_TempLo(byte value){
Serial.print("Set Low Temp");
_temp_lo = value;
SendState(TEMP_LO_STATE, (char*)value);
}
void Device::SendState(String suffix, String payload){
String topic = _topic_root + suffix;
_mqtt_client.publish(topic.c_str(), payload.c_str(), MSG_RETAIN);
}
/** Callback function for MQTT client.
Looks up a command function based on topic and executes it.
@param topic
@param payload
@param length
*/
void Device::_mqttCallback(char *topic, uint8_t *payload, unsigned int length) {
char data [16] = {'\0'};
// Remove root from the incoming topic.
char suffix[100] = topic[_topic_root.length()];
Serial.print("Incoming topic -> ");
Serial.print(suffix);
Serial.print(": ");
int i;
for (i; i < length; i++)
{
data[i] = ((char)payload[i]);
Serial.print(data[i]);
}
data[i] = '\0';
switch (suffix) {
case MODE_SET:
_Mode(data);
break;
case TEMP_SET:
_Temp(atoi(data));
break;
case TEMP_LO_SET:
_TempLo(atoi(data));
break;
case TEMP_HI_SET:
_TempHi(atoi(data));
break;
default:
Serial.println("Command function not found for " + String(topic));
}
}
/** Connect to MQTT broker.
@param server IP address string of the server.
@param name the name used for this connection
@param user MQTT broker username
@param password MQTT broker password
@return boolean indicating success or failure of connection.
*/
bool Device::ConnectMQTT(const String &server, const String &name, const String &user, const String &password) {
_mqtt_client.setServer((server.c_str(), 1883);
_mqtt_client.setCallback(&Device::_mqttCallback);
byte i = 0;
if (_mqtt_client.connected()) return true;
while (!_mqtt_client.connected() && (i < 3)) {
Serial.println("Attempt MQTT Connection.");
boolean ret;
ret = _mqtt_client.connect(name.c_str(), user.c_str(), password.c_str());
if (ret) {
Serial.println("Connected to MQTT");
return true;
} else {
int Status = _mqtt_client.state();
switch (Status)
{
case -4:
Serial.println(F("Connection timeout"));
break;
case -3:
Serial.println(F("Connection lost"));
break;
case -2:
Serial.println(F("Connect failed"));
break;
case -1:
Serial.println(F("Disconnected"));
break;
case 1:
Serial.println(F("Bad protocol"));
break;
case 2:
Serial.println(F("Bad client ID"));
break;
case 3:
Serial.println(F("Unavailable"));
break;
case 4:
Serial.println(F("Bad credentials"));
break;
case 5:
Serial.println(F("Unauthorized"));
break;
}
}
Serial.print(".");
i++;
delay(5000);
}
return false;
}

81
lib/Device/Device.h Normal file
View File

@ -0,0 +1,81 @@
#ifndef DEVICE_H
#define DEVICE_H
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <Tools.h>
#include <secrets.h>
const char version[] = "0.0.1";
#define ACTION_TPC "action/state"
#define MODE_SET "mode/set"
#define MODE_STATE "mode/state"
#define TEMP_SET "temp/set"
#define TEMP_STATE "temp/state"
#define TEMP_CURRENT "temp/current"
#define TEMP_HI_SET "temp_hi/set"
#define TEMP_HI_STATE "temp_hi/state"
#define TEMP_LO_SET "temp_lo/set"
#define TEMP_LO_STATE "temp_lo/state"
#define ACTION_OFF "off"
#define ACTION_HEATING "heating"
#define ACTION_COOLING "cooling"
#define ACTION_IDLE "idle"
#ifndef DEVICE_NAME
#define DEVICE_NAME "MQTT Thermostat"
#endif
#ifndef DEVICE_MDL
#define DEVICE_MDL "Thermostat"
#endif
#ifndef DEVICE_MF
#define DEVICE_MF ""
#endif
#ifndef ROOT
#define ROOT "thermostat/"
#endif
#ifndef CONFIG_ROOT
#define CONFIG_ROOT "homeassistant/climate/"
#endif
#ifndef MSG_RETAIN
#define MSG_RETAIN true
#endif
#define TOPIC_LIMIT 4
//COMMAND_SIGNATURE void (*commandFunction)(uint8_t*) // https://forum.arduino.cc/t/assignment-of-function/528949/3
//CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
const size_t DOC_SIZE = JSON_OBJECT_SIZE(29) + JSON_ARRAY_SIZE(4);
struct CommandTopic {
void (*cmd)(byte*);
String CmdTopic;
};
struct ControlDevice {
String name; // "Glycol Chiller"
String topic_root; // "brewhouse/"
CommandTopic command_topics[TOPIC_LIMIT];
};
void (*cmd)(byte*) CmdLookup(String command_topic, ControlDevice devices[], int device_count) {
for (int i=0;i<device_count;i++) {
ControlDevice this_device = devices[i];
for (int j=0;j<TOPIC_LIMIT;j++) {
if (this_device.command_topics[j]->CmdTopic == command_topic) return this_device.command_topics[j]->cmd;
}
}
}
#endif

11
lib/Device/globals.h Normal file
View File

@ -0,0 +1,11 @@
TOPIC_ROOT + suffix;
TOPIC_ROOT + suffix;
TOPIC_ROOT + "mode/set";
TOPIC_ROOT + "mode/state";
TOPIC_ROOT + "temp/set";
TOPIC_ROOT + "temp/state";
TOPIC_ROOT + "temp/current";
TOPIC_ROOT + "temp_hi/set";
TOPIC_ROOT + "temp_hi/state";
TOPIC_ROOT + "temp_lo/set";
TOPIC_ROOT + "temp_lo/state";

17
lib/Global/Global.h Normal file
View File

@ -0,0 +1,17 @@
/* This file is all the stuff I want to be
able to change without having to update the
source repository.
*/
#ifndef GLOBAL_H
#define GLOBAL_H
#define DEVICE_SW "0.0.1"
auto chipid = String(ESP.getChipId(), HEX);
#define DEVICE_NAME "Glycol Chiller"
#define DEVICE_MDL "Chillenator v0.1"
#define DEVICE_MF "Damn Yankee Brewing"
#define FERMENTER_COUNT 2
#endif

28
lib/README.md Executable file
View File

@ -0,0 +1,28 @@
This directory is intended for the project specific (private) libraries.
The source code of each library should be placed in separate directory, like
"lib/private_lib/[here are source files]".
For example, see how can be organized `Foo` and `Bar` libraries:
|--lib
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |- readme.txt --> THIS FILE
|--src
|- main.c
Then in `src/main.c` you should use:
#include <Foo.h>
#include <Bar.h>
// rest H/C/CPP code

41
lib/SlowPWM/SlowPWM.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef SLOWPWM_h
#define SLOWPWM_h
#include <Arduino.h>
class slowPWM {
private:
byte outputPin;
unsigned long period;
unsigned long lastSwitchTime;
byte outputState;
public:
void begin(byte pin, unsigned long per) {
outputPin = pin;
period = per;
lastSwitchTime = 0;
outputState = LOW;
pinMode(pin, OUTPUT);
Serial.println("Setup PWM");
}
void compute(byte duty) {
unsigned long onTime = (duty * period) / 100;
unsigned long offTime = period - onTime;
unsigned long currentTime = millis();
if (duty == 0) {
outputState = LOW;
} else if (outputState == HIGH && (currentTime - lastSwitchTime >= onTime)) {
lastSwitchTime = currentTime;
outputState = LOW;
} else if (outputState == LOW && (currentTime - lastSwitchTime >= offTime)) {
lastSwitchTime = currentTime;
outputState = HIGH;
}
digitalWrite(outputPin, outputState);
}
};
#endif

27
lib/Tools/Tools.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef TOOLS_h
#define TOOLS_h
/** Convert String to slug by making lowercase and
* and replacing spaces with `_`.
*/
String slugify(String input) {
input.toLowerCase();
input.replace(" ", "_");
return input;
}
/** Convert char array to slug by making lowercase and
* and replacing spaces with `_`.
char* slugify(char* input) {
char* output;
strcpy(output, input);
strlwr(output);
for (uint8_t i=0; i<strlen(output); i++)
if (output[i] == ' ') output[i] = '_';
return output;
}
*/
#endif

22
lib/button/button.h Executable file
View File

@ -0,0 +1,22 @@
#ifndef button_h
#define button_h
#include "Arduino.h"
class Button
{
private:
uint8_t btn;
uint16_t state;
public:
void begin(uint8_t button) {
btn = button;
state = 0;
pinMode(btn, INPUT_PULLUP);
}
bool pressed() {
state = (state<<1) | digitalRead(btn) | 0xfe00;
return (state == 0xff00);
}
};
#endif

View File

@ -0,0 +1,109 @@
#include "communicator.h"
Communicator::Communicator(WiFiClient& network, void (*cmd)(char *topic, byte *payload, unsigned int length)) {
this->_net = network;
this->mqttCallback = cmd;
this->_mqtt_client.setBufferSize(1536);
}
void Communicator::loop() {
_mqtt_client.loop();
}
bool Communicator::ConnectMQTT(const String &server, const String &name, const String &user, const String &password) {
_mqtt_client.setClient(_net);
_mqtt_client.setServer(server.c_str(), 1883);
_mqtt_client.setCallback(mqttCallback);
byte i = 0;
if (_mqtt_client.connected()) return true;
while (!_mqtt_client.connected() && (i < 3)) {
Serial.println("Attempt MQTT Connection.");
boolean ret;
ret = _mqtt_client.connect(name.c_str(), user.c_str(), password.c_str());
if (ret) {
Serial.println("Connected to MQTT");
return true;
} else {
int Status = _mqtt_client.state();
switch (Status)
{
case -4:
Serial.println(F("Connection timeout"));
break;
case -3:
Serial.println(F("Connection lost"));
break;
case -2:
Serial.println(F("Connect failed"));
break;
case -1:
Serial.println(F("Disconnected"));
break;
case 1:
Serial.println(F("Bad protocol"));
break;
case 2:
Serial.println(F("Bad client ID"));
break;
case 3:
Serial.println(F("Unavailable"));
break;
case 4:
Serial.println(F("Bad credentials"));
break;
case 5:
Serial.println(F("Unauthorized"));
break;
}
}
Serial.print(".");
i++;
delay(5000);
}
return false;
}
void Communicator::mqtt_discovery(const String topic, StaticJsonDocument<1536> &entity) {
String payload;
serializeJson(entity, payload);
bool response = ConnectMQTT(MQTT_BROKER.toString(), MQTT_NAME, MQTT_USER, MQTT_PASSWORD);
if (response)
{
Serial.print("Sending discovery payload to ");
Serial.println(topic);
Serial.println("");
Serial.println(payload);
Serial.println("");
_mqtt_client.publish(topic.c_str(), payload.c_str(), false);
if (entity.containsKey("mode_cmd_t")) _mqtt_client.subscribe(entity["mode_cmd_t"]);
if (entity.containsKey("temp_cmd_t")) _mqtt_client.subscribe(entity["temp_cmd_t"]);
if (entity.containsKey("temp_hi_cmd_t")) _mqtt_client.subscribe(entity["temp_hi_cmd_t"]);
if (entity.containsKey("temp_lo_cmd_t")) _mqtt_client.subscribe(entity["temp_lo_cmd_t"]);
_mqtt_client.loop();
}
}
void Communicator::publish_data(String topic, String value) {
_mqtt_client.publish(topic.c_str(), value.c_str(), false);
}

View File

@ -0,0 +1,25 @@
#ifndef MyClass_h
#define MyClass_h
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include "secrets.h"
class Communicator {
public:
Communicator(WiFiClient&, void (*mqttCallback)(char*, byte*, unsigned int));
void (*mqttCallback)(char*, byte*, unsigned int);
bool ConnectMQTT(const String&, const String&, const String&, const String&);
void mqtt_discovery(const String, StaticJsonDocument<1536>&);
void publish_data(String, String);
void loop();
private:
PubSubClient _mqtt_client;
WiFiClient _net;
};
#endif

48
lib/config/config.h Executable file
View File

@ -0,0 +1,48 @@
/* This file is all the stuff I want to be
able to change without having to update the
source repository.
*/
#ifndef CONFIG_H
#define CONFIG_H
// Pin Definitions
#define I_CLK 2
#define I_DT 3
#define c 4
#define O_PWM 5
#define I_CS1 47
#define I_CS2 48
// MQTT Topic Definitions
#define TOPIC_ROOT "brewery/"
#define BOIL_SETPOINT_TOPIC "setpoint/boil"
#define MASH_SETPOINT_TOPIC "setpoint/boil"
#define BOIL_ACTUAL_TOPIC "sensor/boil"
#define MASH_ACTUAL_TOPIC "sensor/mash"
// The value of the Rref resistor. Use 430.0 for PT100 and 4300.0 for PT1000
static const double RREF_KETTLE = 430.0;
static const double RREF_MASH = 430.0;
// The 'nominal' 0-degrees-C resistance of the sensor
// 100.0 for PT100, 1000.0 for PT1000
static const double RNOMINAL_KETTLE = 100.0;
static const double RNOMINAL_MASH = 100.0;
#define MQTT_NAME "brewhouse"
#define MQTT_PASSWORD "4SutKhR2ZEET2IU0PNhH"
#define MQTT_USER "mqtt_user"
static const IPAddress MQTT_BROKER(192, 168, 1, 198);
static const byte mac[] = { 0xA6, 0x61, 0x0A, 0xAE, 0x89, 0xDE }; //physical mac address
static const IPAddress ip(192,168,1,177);
static const int PeriodPWM = 2000;
static const double ThreshPWR = 5; // Float stored as int, last digit is decimal place
static const double Hysteresis = 1; //
EthernetClient _net;
#endif

17
lib/global/global.h Normal file
View File

@ -0,0 +1,17 @@
/* This file is all the stuff I want to be
able to change without having to update the
source repository.
*/
#ifndef GLOBAL_H
#define GLOBAL_H
#define DEVICE_SW "0.0.1"
auto chipid = String(ESP.getChipId(), HEX);
#define DEVICE_NAME "Glycol Chiller"
#define DEVICE_MDL "Chillenator v0.1"
#define DEVICE_MF "Damn Yankee Brewing"
#define FERMENTER_COUNT 2
#endif

253
lib/mqtt/mqtt.cpp Executable file
View File

@ -0,0 +1,253 @@
#include "mqtt.h"
void mqttHA::merge(JsonVariant &dst, JsonVariantConst src)
{
if (src.is<JsonObjectConst>())
{
for (JsonPairConst kvp : src.as<JsonObjectConst>())
{
if (dst[kvp.key()])
merge(dst[kvp.key()], kvp.value());
else
dst[kvp.key()] = kvp.value();
}
}
else
{
dst.set(src);
}
}
String mqttHA::slugify(String input) {
return input.toLowerCase().replace(" ", "_");
}
void mqttHA::publishTemperature(DynamicJsonDocument &device, String &vessle) {
name_slug = slugify(device["name"]);
vessel_slug = slugify(vessel);
String topic = "homeassistant/sensor/" + name_slug + "_" + chipid + "/" + vessel_slug + "_temp/config";
DynamicJsonDocument entity(200);
entity["uniq_id"] = chipid + "_" + vessel_slug + "_temp";
entity["dev_cla"] = "temperature";
entity["name"] = vessle + " Temperature";
entity["unit_of_meas"] = "°" + TEMP_UNIT;
entity["val_tpl"] = "{{ value_json }}";
entity["stat_t"] = "damn_yankee/" + name_slug + "/" + vessel_slug + "_temp";
merge(entity, device)
String payload;
serializeJson(entity, payload)
mqtt_discovery(topic, payload);
}
void mqttHA::mqtt_discovery(const String topic, const String payload) {
bool response = ConnectMQTT(MQTT_BROKER, MQTT_NAME, MQTT_USER, MQTT_PASSWORD);
if (response)
{
mqtt_client.setBufferSize(512);
mqtt_client.publish(topic.c_str(), payload.c_str(), true);
/*
// Passive Sensors
mqtt_client.publish((topic + "boil_temperature/config").c_str(),
("{\"uniq_id\": \"" + chipid + "_boil_temp\"," +
"\"dev_cla\": \"temperature\"," +
"\"name\": \"Boil Temperature\"," +
"\"unit_of_meas\": \"°" + TEMP_UNIT + "\"," +
"\"val_tpl\": \"{{ value_json }}\"," +
"\"stat_t\": \"brewhouse/" + DEVNAME + "/boil_temperature\"," +
device + "}")
.c_str(),
true);
mqtt_client.publish((topic + "mash_temperature/config").c_str(),
("{\"uniq_id\": \"" + chipid + "_mash_temp\"," +
"\"dev_cla\": \"temperature\"," +
"\"name\": \"Mash Temperature\"," +
"\"unit_of_meas\": \"°" + TEMP_UNIT + "\"," +
"\"val_tpl\": \"{{ value_json }}\"," +
"\"stat_t\": \"brewhouse/" + DEVNAME + "/mash_temperature\"," +
device + "}")
.c_str(),
true);
// User Changeable Settings
topic = "homeassistant/number/brewhouse_" + chipid + "/";
mqtt_client.publish((topic + "mash_setpoint/config").c_str(),
("{\"uniq_id\": \"" + chipid + "_mash_setpoint\"," +
"\"dev_cla\": \"temperature\"," +
"\"name\": \"mash Setpoint\"," +
"\"val_tpl\": \"{{ value_json }}\"," +
"\"cmd_tpl\": \"{{ value_json }}\"," +
"\"stat_t\": \"brewhouse/" + DEVNAME + "/mash_setpoint\"," +
"\"cmd_t\": \"brewhouse/" + DEVNAME + "/mash_set_temp\"," +
device + "}")
.c_str(),
true);
mqtt_client.publish((topic + "boil_setpoint/config").c_str(),
("{\"uniq_id\": \"" + chipid + "_boil_setpoint\"," +
"\"dev_cla\": \"temperature\"," +
"\"name\": \"Boil Setpoint\"," +
"\"val_tpl\": \"{{ value_json }}\"," +
"\"cmd_tpl\": \"{{ value_json }}\"," +
"\"stat_t\": \"brewhouse/" + DEVNAME + "/boil_setpoint\"," +
"\"cmd_t\": \"brewhouse/" + DEVNAME + "/boil_set_temp\"," +
device + "}")
.c_str(),
true);
mqtt_client.publish((topic + "boil_pwm/config").c_str(),
("{\"uniq_id\": \"" + chipid + "_boil_pwm\"," +
"\"dev_cla\": \"temperature\"," +
"\"name\": \"Boil pwm\"," +
"\"val_tpl\": \"{{ value_json }}\"," +
"\"cmd_tpl\": \"{{ value_json }}\"," +
"\"stat_t\": \"brewhouse/" + DEVNAME + "/boil_pwm\"," +
"\"cmd_t\": \"brewhouse/" + DEVNAME + "/boil_set_pwm\"," +
device + "}")
.c_str(),
true);
topic = "homeassistant/select/brewhouse_" + chipid + "/";
mqtt_client.publish((topic + "boil_mode/config").c_str(),
("{\"uniq_id\": \"" + chipid + "_boil_mode\"," +
"\"name\": \"Boil Mode\"," +
"\"ops\": [\"PWM\",\"PID\"]," +
"\"val_tpl\": \"{{ value_json }}\"," +
"\"cmd_tpl\": \"{{ value_json }}\"," +
"\"stat_t\": \"brewhouse/" + DEVNAME + "/boil_mode\"," +
"\"cmd_t\": \"brewhouse/" + DEVNAME + "/boil_set_mode\"," +
device + "}")
.c_str(),
true);
*/
mqtt_client.loop();
}
bool mqttHA::ConnectMQTT(const String &server, const String &name, const String &user, const String &password) {
Serial.println("Setup MQTT client.");
mqtt_client.begin(server.c_str(), _net);
mqtt_client.onMessage(MessageReceived);
Serial.println("connecting MQTT...");
byte i = 0;
while (!mqtt_client.connected() && (i < 3)) {
boolean ret;
ret = mqtt_client.connect(name.c_str(), user.c_str(), password.c_str())
if (ret) {
Serial.println("Connected to MQTT");
mqtt_client.subscribe("brewery/setpoint/bk");
return true;
} else {
int Status = mqtt_client.state();
switch (Status)
{
case -4:
Serial.println(F("Connection timeout"));
break;
case -3:
Serial.println(F("Connection lost"));
break;
case -2:
Serial.println(F("Connect failed"));
break;
case -1:
Serial.println(F("Disconnected"));
break;
case 1:
Serial.println(F("Bad protocol"));
break;
case 2:
Serial.println(F("Bad client ID"));
break;
case 3:
Serial.println(F("Unavailable"));
break;
case 4:
Serial.println(F("Bad credentials"));
break;
case 5:
Serial.println(F("Unauthorized"));
break;
}
}
Serial.print(".");
i++;
delay(5000);
}
return false
}
void mqttHA::MessageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
/** JSON Parser Setup */
StaticJsonDocument<200> doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, payload);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
char buf[30];
strcpy(buf,TOPIC_PREFIX);
strcat(buf,BOIL_SETPOINT_TOPIC);
if (topic == buf) {
// Update PWM setpoint.
String name = doc["entity"];
String setting = doc["setpoint"];
KettleDuty = setting.toInt();
String unit = doc["units"];
Serial.println("Updating setpoint for " + name + " to " + setting + " " + unit);
}
}
static void mqttHA::SendSensorData() {
Serial.println("Sending data...");
// NOTE: max message length is 250 bytes.
StaticJsonDocument<200> doc;
doc["entity"] = "boil_kettle";
doc["setpoint"] = KettleDuty;
doc["units"] = "%";
String jstr;
serializeJson(doc, jstr);
String topic = TOPIC_PREFIX;
topic += "sensor/boil_kettle";
mqtt_client.publish(topic, jstr);
}

17
lib/mqtt/mqtt.h Executable file
View File

@ -0,0 +1,17 @@
#include <ArduinoJson.h>
#include <MQTT.h>
#include "config.h"
class mqttHA {
public:
void mqtt_discovery(const String publish_topic, const String publish_payload);
bool ConnectMQTT(const String &server, const String &name, const String &user, const String &password);
void MessageReceived(String &topic, String &payload);
static void SendSensorData();
void publishTemperature(String &name, String &device_name, String &device );
private:
MQTTClient mqtt_client;
}

24
lib/secrets/secrets.h Normal file
View File

@ -0,0 +1,24 @@
/* This file is all the stuff I want to be
able to change without having to update the
source repository.
*/
#ifndef SECRETS_H
#define SECRETS_H
#define I_CS1 47
#define I_CS2 48
// NETWORK CREDENTIALS
#define WIFI_SSID "gia_home"
#define WIFI_PASSWORD "uolwqzmvwhdetviw"
// MQTT BROKER
#define MQTT_NAME "brewhouse"
#define MQTT_PASSWORD "4SutKhR2ZEET2IU0PNhH"
#define MQTT_USER "mqtt_user"
static const IPAddress MQTT_BROKER(192, 168, 1, 198);
static const double Hysteresis = 1; //
#endif

42
lib/slowPWM/slowPWM.h Executable file
View File

@ -0,0 +1,42 @@
#ifndef SLOWPWM_h
#define SLOWPWM_h
#include <Arduino.h>
class slowPWM {
private:
byte outputPin;
unsigned long period;
unsigned long lastSwitchTime;
byte outputState;
public:
void begin(byte pin, unsigned long per) {
outputPin = pin;
period = per;
lastSwitchTime = 0;
outputState = LOW;
pinMode(pin, OUTPUT);
Serial.println("Setup PWM");
}
byte compute(byte duty) {
unsigned long onTime = (duty * period) / 100;
unsigned long offTime = period - onTime;
unsigned long currentTime = millis();
if (duty == 0) {
outputState = LOW;
} else if (outputState == HIGH && (currentTime - lastSwitchTime >= onTime)) {
lastSwitchTime = currentTime;
outputState = LOW;
} else if (outputState == LOW && (currentTime - lastSwitchTime >= offTime)) {
lastSwitchTime = currentTime;
outputState = HIGH;
}
return outputState;
}
};
#endif

583
makeEspArduino.mk Normal file
View File

@ -0,0 +1,583 @@
#====================================================================================
# makeESPArduino
#
# A makefile for ESP8286 and ESP32 Arduino projects.
#
# License: LGPL 2.1
# General and full license information is available at:
# https://github.com/plerup/makeEspArduino
#
# Copyright (c) 2016-2021 Peter Lerup. All rights reserved.
#
#====================================================================================
START_TIME := $(shell date +%s)
__THIS_FILE := $(abspath $(lastword $(MAKEFILE_LIST)))
__TOOLS_DIR := $(dir $(__THIS_FILE))tools
OS ?= $(shell uname -s)
# Include possible operating system specfic settings
-include $(dir $(__THIS_FILE))/os/$(OS).mk
# Include possible global user settings
CONFIG_ROOT ?= $(if $(XDG_CONFIG_HOME),$(XDG_CONFIG_HOME),$(HOME)/.config)
-include $(CONFIG_ROOT)/makeEspArduino/config.mk
# Include possible project specific settings
-include $(firstword $(PROJ_CONF) $(dir $(SKETCH))config.mk)
# Build threads, default is using all the PC cpus
BUILD_THREADS ?= $(shell nproc)
MAKEFLAGS += -j $(BUILD_THREADS)
# Build verbosity, silent by default
ifndef VERBOSE
MAKEFLAGS += --silent
endif
# ESP chip family type
CHIP ?= esp8266
UC_CHIP := $(shell perl -e "print uc $(CHIP)")
IS_ESP32 := $(if $(filter-out esp32,$(CHIP)),,1)
# Serial flashing parameters
UPLOAD_PORT_MATCH ?= /dev/ttyU*
UPLOAD_PORT ?= $(shell ls -1tr $(UPLOAD_PORT_MATCH) 2>/dev/null | tail -1)
# Monitor definitions
MONITOR_SPEED ?= 115200
MONITOR_PORT ?= $(UPLOAD_PORT)
MONITOR_PAR ?= --rts=0 --dtr=0
MONITOR_COM ?= $(if $(NO_PY_WRAP),python3,$(PY_WRAP)) -m serial.tools.miniterm $(MONITOR_PAR) $(MONITOR_PORT) $(MONITOR_SPEED)
# OTA parameters
OTA_ADDR ?=
OTA_PORT ?= $(if $(IS_ESP32),3232,8266)
OTA_PWD ?=
OTA_ARGS = --progress --ip="$(OTA_ADDR)" --port="$(OTA_PORT)"
ifneq ($(OTA_PWD),)
OTA_ARGS += --auth="$(OTA_PWD)"
endif
# HTTP update parameters
HTTP_ADDR ?=
HTTP_URI ?= /update
HTTP_PWD ?= user
HTTP_USR ?= password
HTTP_OPT ?= --progress-bar -o /dev/null
# Output directory
BUILD_ROOT ?= $(lastword $(MAKEFILE_LIST))
BUILD_DIR ?= build
# File system and corresponding disk directories
FS_TYPE ?= spiffs
MK_FS_MATCH = mk$(shell perl -e "print lc $(FS_TYPE)")
FS_DIR ?= $(dir $(SKETCH))data
FS_RESTORE_DIR ?= $(BUILD_DIR)/file_system
# Utility functions
git_description = $(shell git -C $(1) describe --tags --always --dirty 2>/dev/null || echo Unknown)
time_string = $(shell date +$(1))
find_files = $(shell find $2 | awk '/.*\.($1)$$/')
# ESP Arduino directories
ifndef ESP_ROOT
# Location not defined, find and use possible version in the Arduino IDE installation
ARDUINO_ROOT ?= $(HOME)/.arduino15
ARDUINO_ESP_ROOT = $(ARDUINO_ROOT)/packages/$(CHIP)
ESP_ROOT := $(if $(ARDUINO_HW_ESP_ROOT),$(ARDUINO_HW_ESP_ROOT),$(lastword $(wildcard $(ARDUINO_ESP_ROOT)/hardware/$(CHIP)/*)))
ifeq ($(ESP_ROOT),)
$(error No installed version of $(CHIP) Arduino found)
endif
ARDUINO_LIBS ?= $(shell grep -o "sketchbook.path=.*" $(ARDUINO_ROOT)/preferences.txt 2>/dev/null | cut -f2- -d=)/libraries
ESP_ARDUINO_VERSION := $(notdir $(ESP_ROOT))
# Find used version of compiler and tools
COMP_PATH := $(lastword $(wildcard $(ARDUINO_ESP_ROOT)/tools/xtensa-*/*))
MK_FS_PATH := $(lastword $(wildcard $(ARDUINO_ESP_ROOT)/tools/$(MK_FS_MATCH)/*/$(MK_FS_MATCH)))
PYTHON3_PATH := $(lastword $(wildcard $(ARDUINO_ESP_ROOT)/tools/python3/*))
else
# Location defined, assume that it is a git clone
ESP_ARDUINO_VERSION = $(call git_description,$(ESP_ROOT))
MK_FS_PATH := $(lastword $(wildcard $(ESP_ROOT)/tools/$(MK_FS_MATCH)/$(MK_FS_MATCH)))
PYTHON3_PATH := $(wildcard $(ESP_ROOT)/tools/python3)
endif
ESP_ROOT := $(abspath $(ESP_ROOT))
ESP_LIBS = $(ESP_ROOT)/libraries
SDK_ROOT = $(ESP_ROOT)/tools/sdk
TOOLS_ROOT = $(ESP_ROOT)/tools
# The esp8266 tools directory contains the python3 executable as well as some modules
# Use these to avoid additional python installation requirements here
PYTHON3_PATH := $(if $(PYTHON3_PATH),$(PYTHON3_PATH),$(dir $(shell which python3 2>/dev/null)))
PY_WRAP = $(PYTHON3_PATH)/python3 $(__TOOLS_DIR)/py_wrap.py $(TOOLS_ROOT)
NO_PY_WRAP ?= $(if $(IS_ESP32),1,)
# Validate the selected version of ESP Arduino
ifeq ($(wildcard $(ESP_ROOT)/cores/$(CHIP)),)
$(error $(ESP_ROOT) is not a vaild directory for $(CHIP))
endif
# Validate the file system type
ifeq ($(wildcard $(MK_FS_PATH)),)
$(error Invalid file system: "$(FS_TYPE)")
endif
# Set possible default board variant and validate
BOARD_OP = perl $(__TOOLS_DIR)/board_op.pl $(ESP_ROOT)/boards.txt "$(CPU)"
ifeq ($(BOARD),)
BOARD := $(if $(IS_ESP32),esp32,generic)
else ifeq ($(shell $(BOARD_OP) $(BOARD) check),)
$(error Invalid board: $(BOARD))
endif
# Handle esptool variants
ESPTOOL_EXT = $(if $(IS_ESP32),,.py)
ESPTOOL ?= $(if $(NO_PY_WRAP),$(ESP_ROOT)/tools/esptool/esptool$(ESPTOOL_EXT),$(PY_WRAP) esptool)
ESPTOOL_COM ?= $(ESPTOOL) --baud=$(UPLOAD_SPEED) --port $(UPLOAD_PORT) --chip $(CHIP)
ifeq ($(IS_ESP32),)
# esp8266, use esptool directly instead of via tools/upload.py in order to avoid speed restrictions currently implied there
UPLOAD_COM = $(ESPTOOL_COM) $(UPLOAD_RESET) write_flash 0x00000 $(BUILD_DIR)/$(MAIN_NAME).bin
FS_UPLOAD_COM = $(ESPTOOL_COM) $(UPLOAD_RESET) write_flash $(SPIFFS_START) $(FS_IMAGE)
endif
# Detect if the specified goal involves building or not
GOALS := $(if $(MAKECMDGOALS),$(MAKECMDGOALS),all)
BUILDING := $(if $(filter $(GOALS), monitor list_boards list_flash_defs list_lwip set_git_version install help tools_dir preproc info),,1)
# Sketch (main program) selection
ifeq ($(BUILDING),)
SKETCH = /dev/null
endif
ifdef DEMO
SKETCH := $(if $(IS_ESP32),$(ESP_LIBS)/WiFi/examples/WiFiScan/WiFiScan.ino,$(ESP_LIBS)/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino)
endif
SKETCH ?= $(abspath $(wildcard *.ino *.pde))
ifeq ($(SKETCH),)
$(error No sketch specified or found. Use "DEMO=1" for testing)
endif
ifeq ($(wildcard $(SKETCH)),)
$(error Sketch $(SKETCH) not found)
endif
SRC_GIT_VERSION := $(call git_description,$(dir $(SKETCH)))
# Main output definitions
SKETCH_NAME := $(basename $(notdir $(SKETCH)))
MAIN_NAME ?= $(SKETCH_NAME)
MAIN_EXE ?= $(BUILD_DIR)/$(MAIN_NAME).bin
FS_IMAGE ?= $(BUILD_DIR)/FS.bin
# Build file extensions
OBJ_EXT = .o
DEP_EXT = .d
# Special tool definitions
OTA_TOOL ?= python $(TOOLS_ROOT)/espota.py
HTTP_TOOL ?= curl
# Core source files
CORE_DIR = $(ESP_ROOT)/cores/$(CHIP)
CORE_SRC := $(call find_files,S|c|cpp,$(CORE_DIR))
CORE_OBJ := $(patsubst %,$(BUILD_DIR)/%$(OBJ_EXT),$(notdir $(CORE_SRC)))
CORE_LIB = $(BUILD_DIR)/arduino.ar
USER_OBJ_LIB = $(BUILD_DIR)/user_obj.ar
# Find project specific source files and include directories
_LIBS = $(LIBS)
ifdef EXPAND_LIBS
_LIBS := $(call find_files,S|c|cpp,$(_LIBS))
endif
SRC_LIST = $(BUILD_DIR)/src_list.mk
FIND_SRC_CMD = $(__TOOLS_DIR)/find_src.pl
$(SRC_LIST): c $(FIND_SRC_CMD) | $(BUILD_DIR)
$(if $(BUILDING),echo "- Finding all involved files for the build ...",)
perl $(FIND_SRC_CMD) "$(EXCLUDE_DIRS)" $(SKETCH) "$(CUSTOM_LIBS)" "$(_LIBS)" $(ESP_LIBS) $(ARDUINO_LIBS) >$(SRC_LIST)
-include $(SRC_LIST)
ifeq ($(suffix $(SKETCH)),.ino)
# Use sketch copy with correct C++ extension
SKETCH_CPP = $(BUILD_DIR)/$(notdir $(SKETCH)).cpp
USER_SRC := $(subst $(SKETCH),$(SKETCH_CPP),$(USER_SRC))
endif
USER_OBJ := $(patsubst %,$(BUILD_DIR)/%$(OBJ_EXT),$(notdir $(USER_SRC)))
USER_DIRS := $(sort $(dir $(USER_SRC)))
# Use first flash definition for the board as default
FLASH_DEF ?= $(shell $(BOARD_OP) $(BOARD) first_flash)
# Same method for LwIPVariant
LWIP_VARIANT ?= $(shell $(BOARD_OP) $(BOARD) first_lwip)
# Handle possible changed state i.e. make command line parameters or changed git versions
CMD_LINE ?= $(shell tr "\0" " " </proc/$$PPID/cmdline)
CMD_LINE := $(CMD_LINE)
IGNORE_STATE ?= $(if $(BUILDING),,1)
ifeq ($(IGNORE_STATE),)
STATE_LOG := $(BUILD_DIR)/state.txt
STATE_INF := $(strip $(foreach par,$(CMD_LINE),$(if $(findstring =,$(par)),$(par),))) \
$(SRC_GIT_VERSION) $(ESP_ARDUINO_VERSION)
# Ignore port and speed changes
STATE_INF := $(patsubst UPLOAD_%,,$(STATE_INF))
PREV_STATE_INF := $(if $(wildcard $(STATE_LOG)),$(shell cat $(STATE_LOG)),$(STATE_INF))
ifneq ($(PREV_STATE_INF),$(STATE_INF))
$(info * Build state has changed, doing a full rebuild *)
$(shell rm -rf "$(BUILD_DIR)")
endif
STATE_SAVE := $(shell mkdir -p $(BUILD_DIR) ; echo '$(STATE_INF)' >$(STATE_LOG))
endif
# The actual build commands are to be extracted from the Arduino description files
ARDUINO_MK = $(BUILD_DIR)/arduino.mk
OS_NAME ?= linux
ARDUINO_DESC := $(shell find -L $(ESP_ROOT) -maxdepth 1 -name "*.txt" | sort)
$(ARDUINO_MK): $(ARDUINO_DESC) $(MAKEFILE_LIST) $(__TOOLS_DIR)/parse_arduino.pl | $(BUILD_DIR)
$(if $(BUILDING),echo "- Parsing Arduino configuration files ...",)
perl $(__TOOLS_DIR)/parse_arduino.pl '$(ESP_ROOT)' '$(ARDUINO_ESP_ROOT)' $(BOARD) '$(FLASH_DEF)' '$(OS_NAME)' '$(LWIP_VARIANT)' $(ARDUINO_EXTRA_DESC) $(ARDUINO_DESC) >$(ARDUINO_MK)
-include $(ARDUINO_MK)
# Compilation directories and path
INCLUDE_DIRS += $(CORE_DIR) $(ESP_ROOT)/variants/$(INCLUDE_VARIANT) $(BUILD_DIR)
C_INCLUDES := $(foreach dir,$(INCLUDE_DIRS) $(USER_INC_DIRS),-I$(dir))
VPATH += $(shell find $(CORE_DIR) -type d) $(USER_DIRS)
# Automatically generated build information data source file
# Makes the build date and git descriptions at the time of actual build event
# available as string constants in the program
BUILD_INFO_H = $(BUILD_DIR)/buildinfo.h
BUILD_INFO_CPP = $(BUILD_DIR)/buildinfo.c++
BUILD_INFO_OBJ = $(BUILD_INFO_CPP)$(OBJ_EXT)
BUILD_DATE = $(call time_string,"%Y-%m-%d")
BUILD_TIME = $(call time_string,"%H:%M:%S")
$(BUILD_INFO_H): | $(BUILD_DIR)
@echo "typedef struct { const char *date, *time, *src_version, *env_version; } _tBuildInfo; extern _tBuildInfo _BuildInfo;" >$@
# Use ccache if it is available and not explicitly disabled (USE_CCACHE=0)
USE_CCACHE ?= $(if $(shell which ccache 2>/dev/null),1,0)
ifeq ($(USE_CCACHE),1)
C_COM_PREFIX = ccache
CPP_COM_PREFIX = $(C_COM_PREFIX)
endif
# Generated header files
GEN_H_FILES += $(BUILD_INFO_H)
# Build output root directory
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
# Create a C++ file from the sketch
$(SKETCH_CPP): $(SKETCH)
echo "#include <Arduino.h>" >$@
cat $(abspath $<) >>$@
# Build rules for the different source file types
$(BUILD_DIR)/%.cpp$(OBJ_EXT): %.cpp $(ARDUINO_MK) | $(GEN_H_FILES)
@echo $(<F)
$(CPP_COM) $(CPP_EXTRA) $($(<F)_CFLAGS) $(abspath $<) -o $@
$(BUILD_DIR)/%.c$(OBJ_EXT): %.c $(ARDUINO_MK) | $(GEN_H_FILES)
@echo $(<F)
$(C_COM) $(C_EXTRA) $($(<F)_CFLAGS) $(abspath $<) -o $@
$(BUILD_DIR)/%.S$(OBJ_EXT): %.S $(ARDUINO_MK) | $(GEN_H_FILES)
@echo $(<F)
$(S_COM) $(S_EXTRA) $(abspath $<) -o $@
$(CORE_LIB): $(CORE_OBJ)
@echo Creating core archive
rm -f $@
$(CORE_LIB_COM) $^
$(USER_OBJ_LIB): $(USER_OBJ)
@echo Creating object archive
rm -f $@
$(LIB_COM) $@ $^
# Possible user specific additional make rules
ifdef USER_RULES
include $(USER_RULES)
endif
# Putting the object files in a libarary minimizes the memory usage in the executable
ifneq ($(NO_USER_OBJ_LIB),)
USER_OBJ_DEP = $(USER_OBJ)
else
USER_OBJ_DEP = $(USER_OBJ_LIB)
endif
# Linking the executable
$(MAIN_EXE): $(CORE_LIB) $(USER_LIBS) $(USER_OBJ_DEP)
@echo Linking $(MAIN_EXE)
$(PRELINK)
@echo " Versions: $(SRC_GIT_VERSION), $(ESP_ARDUINO_VERSION)"
@echo '#include <buildinfo.h>' >$(BUILD_INFO_CPP)
@echo '_tBuildInfo _BuildInfo = {"$(BUILD_DATE)","$(BUILD_TIME)","$(SRC_GIT_VERSION)","$(ESP_ARDUINO_VERSION)"};' >>$(BUILD_INFO_CPP)
$(CPP_COM) $(BUILD_INFO_CPP) -o $(BUILD_INFO_OBJ)
$(LD_COM) $(LD_EXTRA)
$(GEN_PART_COM)
$(OBJCOPY)
$(SIZE_COM) | perl $(__TOOLS_DIR)/mem_use.pl "$(MEM_FLASH)" "$(MEM_RAM)"
ifneq ($(LWIP_INFO),)
@printf "LwIPVariant: $(LWIP_INFO)\n"
endif
ifneq ($(FLASH_INFO),)
@printf "Flash size: $(FLASH_INFO)\n\n"
endif
@perl -e 'print "Build complete. Elapsed time: ", time()-$(START_TIME), " seconds\n\n"'
# Flashing operations
CHECK_PORT := $(if $(UPLOAD_PORT),\
@echo === Using upload port: $(UPLOAD_PORT) @ $(UPLOAD_SPEED),\
@echo "*** Upload port not found or defined" && exit 1)
upload flash: all
$(CHECK_PORT)
$(UPLOAD_COM)
ota: all
ifeq ($(OTA_ADDR),)
@echo == Error: Address of device must be specified via OTA_ADDR
exit 1
endif
$(OTA_PRE_COM)
$(OTA_TOOL) $(OTA_ARGS) --file="$(MAIN_EXE)"
http: all
ifeq ($(HTTP_ADDR),)
@echo == Error: Address of device must be specified via HTTP_ADDR
exit 1
endif
$(HTTP_TOOL) $(HTTP_OPT) -F image=@$(MAIN_EXE) --user $(HTTP_USR):$(HTTP_PWD) http://$(HTTP_ADDR)$(HTTP_URI)
@echo "\n"
$(FS_IMAGE): $(ARDUINO_MK) $(shell find $(FS_DIR)/ 2>/dev/null)
ifeq ($(SPIFFS_SIZE),)
@echo == Error: No file system specified in FLASH_DEF
exit 1
endif
@echo Generating file system image: $(FS_IMAGE)
$(MK_FS_COM)
fs: $(FS_IMAGE)
upload_fs flash_fs: $(FS_IMAGE)
$(CHECK_PORT)
$(FS_UPLOAD_COM)
ota_fs: $(FS_IMAGE)
ifeq ($(OTA_ADDR),)
@echo == Error: Address of device must be specified via OTA_ADDR
exit 1
endif
$(OTA_TOOL) $(OTA_ARGS) --spiffs --file="$(FS_IMAGE)"
run: flash
$(MONITOR_COM)
monitor:
ifeq ($(MONITOR_PORT),)
@echo "*** Monitor port not found or defined" && exit 1
endif
$(MONITOR_COM)
FLASH_FILE ?= $(BUILD_DIR)/esp_flash.bin
dump_flash:
$(CHECK_PORT)
@echo Dumping flash memory to file: $(FLASH_FILE)
$(ESPTOOL_COM) read_flash 0 $(shell perl -e 'shift =~ /(\d+)([MK])/ || die "Invalid memory size\n";$$mem_size=$$1*1024;$$mem_size*=1024 if $$2 eq "M";print $$mem_size;' $(FLASH_DEF)) $(FLASH_FILE)
dump_fs:
$(CHECK_PORT)
@echo Dumping flash file system to directory: $(FS_RESTORE_DIR)
-$(ESPTOOL_COM) read_flash $(SPIFFS_START) $(SPIFFS_SIZE) $(FS_IMAGE)
mkdir -p $(FS_RESTORE_DIR)
@echo
@echo == Files ==
$(RESTORE_FS_COM)
restore_flash:
$(CHECK_PORT)
@echo Restoring flash memory from file: $(FLASH_FILE)
$(ESPTOOL_COM) -a soft_reset write_flash 0 $(FLASH_FILE)
erase_flash:
$(CHECK_PORT)
$(ESPTOOL_COM) erase_flash
# Building library instead of executable
LIB_OUT_FILE ?= $(BUILD_DIR)/$(MAIN_NAME).a
.PHONY: lib
lib: $(LIB_OUT_FILE)
$(LIB_OUT_FILE): $(filter-out $(BUILD_DIR)/$(MAIN_NAME).cpp$(OBJ_EXT),$(USER_OBJ))
@echo Building library $(LIB_OUT_FILE)
rm -f $(LIB_OUT_FILE)
$(LIB_COM) $(LIB_OUT_FILE) $^
# Miscellaneous operations
clean:
@echo Removing all build files
rm -rf "$(BUILD_DIR)" $(FILES_TO_CLEAN)
list_boards:
$(BOARD_OP) $(BOARD) list_names
list_lib: $(SRC_LIST)
perl -e 'foreach (@ARGV) {print "$$_\n"}' "===== Include directories =====" $(USER_INC_DIRS) "===== Source files =====" $(USER_SRC)
list_flash_defs:
$(BOARD_OP) $(BOARD) list_flash
list_lwip:
$(BOARD_OP) $(BOARD) list_lwip
# Update the git version of the esp Arduino repo
set_git_version:
ifeq ($(REQ_GIT_VERSION),)
@echo == Error: Version tag must be specified via REQ_GIT_VERSION
exit 1
endif
@echo == Setting $(ESP_ROOT) to $(REQ_GIT_VERSION) ...
git -C $(ESP_ROOT) checkout -fq --recurse-submodules $(REQ_GIT_VERSION)
git -C $(ESP_ROOT) clean -fdxq -f
git -C $(ESP_ROOT) submodule update --init
git -C $(ESP_ROOT) submodule foreach -q --recursive git clean -xfd
cd $(ESP_ROOT)/tools; ./get.py -q
# Generate a Visual Studio Code configuration and launch
BIN_DIR = /usr/local/bin
_MAKE_COM = make -f $(__THIS_FILE) ESP_ROOT=$(ESP_ROOT)
ifeq ($(CHIP),esp32)
_MAKE_COM += CHIP=esp32
_SCRIPT = espmake32
else
_SCRIPT = espmake
endif
vscode: all
perl $(__TOOLS_DIR)/vscode.pl -n $(MAIN_NAME) -m "$(_MAKE_COM)" -w "$(VS_CODE_DIR)" -i "$(VSCODE_INC_EXTRA)" -p "$(VSCODE_PROJ_NAME)" $(CPP_COM)
# Create shortcut command for running this file
install:
@echo Creating command \"$(_SCRIPT)\" in $(BIN_DIR)
sudo sh -c 'echo $(_MAKE_COM) "\"\$$@\"" >$(BIN_DIR)/$(_SCRIPT)'
sudo chmod +x $(BIN_DIR)/$(_SCRIPT)
# Just return the path of the tools directory (intended to be used to find vscode.pl above from othe makefiles)
tools_dir:
@echo $(__TOOLS_DIR)
# Show ram memory usage per variable
ram_usage: $(MAIN_EXE)
$(shell find $(TOOLS_ROOT) | grep 'gcc-nm') -Clrtd --size-sort $(BUILD_DIR)/$(MAIN_NAME).elf | grep -i ' [b] '
# Show ram and flash usage per object files used in the build
OBJ_INFO_FORM ?= 0
OBJ_INFO_SORT ?= 1
obj_info: $(MAIN_EXE)
perl $(__TOOLS_DIR)/obj_info.pl "$(shell find $(TOOLS_ROOT) | grep 'elf-size$$')" "$(OBJ_INFO_FORM)" "$(OBJ_INFO_SORT)" $(BUILD_DIR)/*.o
# Analyze crash log
crash: $(MAIN_EXE)
perl $(__TOOLS_DIR)/crash_tool.pl $(ESP_ROOT) $(BUILD_DIR)/$(MAIN_NAME).elf
# Run compiler preprocessor to get full expanded source for a file
preproc:
ifeq ($(SRC_FILE),)
$(error SRC_FILE must be defined)
endif
$(CPP_COM) -E $(SRC_FILE)
# Main default rule, build the executable
.PHONY: all
all: $(BUILD_DIR) $(ARDUINO_MK) prebuild $(MAIN_EXE)
# Prebuild is currently only mandatory for esp32
USE_PREBUILD ?= $(if $(IS_ESP32),1,)
prebuild:
ifneq ($(USE_PREBUILD),)
$(PREBUILD)
endif
help: $(ARDUINO_MK)
@echo
@echo "Generic makefile for building Arduino esp8266 and esp32 projects"
@echo "This file can either be used directly or included from another makefile"
@echo ""
@echo "The following targets are available:"
@echo " all (default) Build the project application"
@echo " clean Remove all intermediate build files"
@echo " lib Build a library with all involved object files"
@echo " flash Build and and flash the project application"
@echo " flash_fs Build and and flash file system (when applicable)"
@echo " ota Build and and flash via OTA"
@echo " Params: OTA_ADDR, OTA_PORT and OTA_PWD"
@echo " ota_fs Build and and flash file system via OTA"
@echo " http Build and and flash via http (curl)"
@echo " Params: HTTP_ADDR, HTTP_URI, HTTP_PWD and HTTP_USR"
@echo " dump_flash Dump the whole board flash memory to a file"
@echo " restore_flash Restore flash memory from a previously dumped file"
@echo " dump_fs Extract all files from the flash file system"
@echo " Params: FS_DUMP_DIR"
@echo " erase_flash Erase the whole flash (use with care!)"
@echo " list_lib Show a list of used solurce files and include directories"
@echo " set_git_version Setup ESP Arduino git repo to a the tag version"
@echo " specified via REQ_GIT_VERSION"
@echo " install Create the commands \"espmake\" and \"espmake32\""
@echo " vscode Create config file for Visual Studio Code and launch"
@echo " ram_usage Show global variables RAM usage"
@echo " obj_info Show memory usage per object file"
@echo " monitor Start serial monitor on the upload port"
@echo " run Build flash and start serial monitor"
@echo " crash Analyze stack trace from a crash"
@echo " preproc Run compiler preprocessor on source file"
@echo " specified via SRC_FILE"
@echo " list_boards Show list of boards from the Arduino core"
@echo " info Show location and version of used esp Arduino"
@echo "Configurable parameters:"
@echo " SKETCH Main source file"
@echo " If not specified the first sketch in current"
@echo " directory will be used."
@echo " LIBS Use this variable to declare additional directories"
@echo " and/or files which should be included in the build"
@echo " CHIP Set to esp8266 or esp32. Default: '$(CHIP)'"
@echo " BOARD Name of the target board. Default: '$(BOARD)'"
@echo " Use 'list_boards' to get list of available ones"
@echo " FLASH_DEF Flash partitioning info. Default '$(FLASH_DEF)'"
@echo " Use 'list_flash_defs' to get list of available ones"
@echo " BUILD_DIR Directory for intermediate build files."
@echo " Default '$(BUILD_DIR)'"
@echo " BUILD_EXTRA_FLAGS Additional parameters for the compilation commands"
@echo " COMP_WARNINGS Compilation warning options. Default: $(COMP_WARNINGS)"
@echo " FS_TYPE File system type. Default: $(FS_TYPE)"
@echo " FS_DIR File system root directory"
@echo " UPLOAD_PORT Serial flashing port name. Default: '$(UPLOAD_PORT)'"
@echo " UPLOAD_SPEED Serial flashing baud rate. Default: '$(UPLOAD_SPEED)'"
@echo " MONITOR_SPEED Baud rate for the monitor. Default: '$(MONITOR_SPEED)'"
@echo " FLASH_FILE File name for dump and restore flash operations"
@echo " Default: '$(FLASH_FILE)'"
@echo " LWIP_VARIANT Use specified variant of the lwip library when applicable"
@echo " Use 'list_lwip' to get list of available ones"
@echo " Default: $(LWIP_VARIANT) ($(LWIP_INFO))"
@echo " VERBOSE Set to 1 to get full printout of the build"
@echo " BUILD_THREADS Number of parallel build threads"
@echo " Default: Maximum possible, based on number of CPUs"
@echo " USE_CCACHE Set to 0 to disable ccache when it is available"
@echo " NO_USER_OBJ_LIB Set to 1 to disable putting all object files into an archive"
@echo
# Show installation information
info:
echo == Build info
echo " CHIP: $(CHIP)"
echo " ESP_ROOT: $(ESP_ROOT)"
echo " Version: $(ESP_ARDUINO_VERSION)"
echo " Threads: $(BUILD_THREADS)"
echo " Upload port: $(UPLOAD_PORT)"
# Include all available dependencies from the previous compilation
-include $(wildcard $(BUILD_DIR)/*$(DEP_EXT))
DEFAULT_GOAL ?= all
.DEFAULT_GOAL := $(DEFAULT_GOAL)

13
script/bootstrap.sh Executable file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env bash
SCRIPTS_DIR=$(dirname -- $(readlink -f -- "$0"))
CWD=$(readlink -f "$SCRIPTS_DIR/..")
SRC="$CWD/src"
TEST="$CWD/test"
ARDMK="$CWD/Arduino-Makefile"
AVR_GCC="/usr/share/avr-gcc"
ARDUINO="/usr/share/arduino"
source "$SCRIPTS_DIR/install.sh"

24
script/install.sh Executable file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env bash
echo "Installing dependencies needed to build the sources and tests..."
ARDUINO_BASENAME="arduino-1.8.9"
ARDUINO_FILE="$ARDUINO_BASENAME-linux64.tar.xz"
ARDUINO_URL="https://downloads.arduino.cc/$ARDUINO_FILE"
echo "Downloading $ARDUINO_BASENAME from $ARDUINO_URL"
wget "$ARDUINO_URL" -O "$ARDUINO_FILE"
echo "Unzipping $ARDUINO_BASENAME"
tar xf "$ARDUINO_FILE"
echo "Installing avr-gcc to $AVR_GCC"
sudo mv "$ARDUINO_BASENAME/hardware/tools/avr" "$AVR_GCC"
echo "Install Arduino to $ARDUINO"
sudo mv "$ARDUINO_BASENAME/" "$ARDUINO"
echo "Installation of dependencies is complete, we are now going to run some tests..."
source "$SCRIPTS_DIR/runtests.sh"

77
script/runtests.sh Executable file
View File

@ -0,0 +1,77 @@
#!/usr/bin/env bash
failures=()
successes=()
cd "$SRC"
for dir in *; do
if [ -d "${dir}" ]; then
echo "Compiling $dir..."
echo $CWD
cd $dir
cp $CWD/Makefile-CI.mk Makefile
make PROJECT_DIR=$CWD ARDUINO_DIR=$ARDUINO AVR_TOOLS_DIR=$AVR_GCC
if [[ $? -ne 0 ]]; then
failures+=("$dir")
echo "Source $dir failed"
else
successes+=("$dir")
echo "Source $dir succeeded"
fi
cd ..
fi
done
cd "$TEST"
for dir in *; do
if [ -d "${dir}" ]; then
echo "Compiling $dir..."
cd $dir
cp $CWD/Makefile-CI.mk Makefile
make PROJECT_DIR=$CWD ARDUINO_DIR=$ARDUINO AVR_TOOLS_DIR=$AVR_GCC
if [[ $? -ne 0 ]]; then
failures+=("$dir")
echo "Test $dir failed"
else
successes+=("$dir")
echo "Source $dir succeeded"
fi
cd ..
fi
done
if [[ ${#failures[@]} -ne 0 ]]; then
echo "The following builds succeeded:"
for success in "${successes[@]}"; do
echo "- Building $success succeeded"
done
echo "The following builds failed:"
for failure in "${failures[@]}"; do
echo "- Building $failure failed"
done
fi
if [[ ${#failures[@]} -eq 0 ]]; then
echo "All tests passed."
else
exit 1
fi

View File

@ -0,0 +1,144 @@
//Built-in
#include <Arduino.h>
#include <SPI.h>
#include <Ethernet.h>
// Additoinal Libraries
#include <ArduinoJson.h>
#include <MQTT.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidMenu.h> // LiquidMenu_config.h needs to be modified to use I2C.
#include <MD_REncoder.h>
#include <Adafruit_MAX31865.h>
// My Includes
#include "config.h"
#include <Button.h>
#include <SlowPWM.h>
// Pin definitions
#define encoderCLK 2
#define encoderDT 3
#define encoderBTN 4
#define kettlePWM 5
// Global variables.
byte KettleDuty = 0;
bool KettleOn = false;
// User I/O objects.
Button Enter;
slowPWM boilPWM;
MD_REncoder rotary = MD_REncoder(encoderDT, encoderCLK);
LiquidCrystal_I2C lcd(0x27,20,4);
EthernetClient net;
MQTTClient mqtt_client;
unsigned long lastRun = 0;
// Return a character array to represent the
// On/Off state of the kettle.
char* KettleState() {
if (KettleOn) {
return (char*)"On";
} else {
return (char*)"Off";
}
}
// Interrupt function to run when encoder is turned.
//
// Increases/decreases the kettle output to a max
// of 100% and minimum of 0%.
void doEncoder()
{
uint8_t result = rotary.read();
uint8_t inc;
if (result) {
uint8_t speed = rotary.speed();
speed >= 10 ? inc = 5 : inc = 1;
}
if (result == DIR_CW && KettleDuty < 100) {
KettleDuty = (KettleDuty / inc) * inc + inc;
} else if (result == DIR_CCW && KettleDuty > 0) {
KettleDuty = (KettleDuty / inc) * inc - inc;
}
}
// LCD menu setup.
LiquidLine KettleState_line(0, 0, "Boil Kettle ", KettleState);
LiquidLine kettle_power_line(0, 1, "Kettle Power % ", KettleDuty);
LiquidScreen home_screen(KettleState_line, kettle_power_line);
LiquidMenu menu(lcd);
void setup() {
unsigned long lastRun = millis() - UpdateInterval;
Serial.begin(9600);
rotary.begin();
Ethernet.begin(mac, ip);
Serial.println("Setting up...");
attachInterrupt(digitalPinToInterrupt(encoderCLK), doEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderDT), doEncoder, CHANGE);
pinMode(encoderCLK, INPUT_PULLUP);
pinMode(encoderDT, INPUT_PULLUP);
Enter.begin(encoderBTN);
boilPWM.begin(kettlePWM, PeriodPWM);
// if you get a connection, report back via serial:
if (Ethernet.linkStatus() == LinkON) {
SetupMQTT(MQTT_BROKER);
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
lcd.init();
lcd.backlight();
menu.init();
menu.add_screen(home_screen);
menu.update();
};
void UpdateBoilKettle(){
static byte last_KettleDuty = 0;
if (Enter.pressed()) {
KettleOn = !KettleOn;
menu.update();
}
if (last_KettleDuty != KettleDuty) {
last_KettleDuty = KettleDuty;
menu.update();
}
if (KettleOn) {
boilPWM.compute(KettleDuty);
} else {
boilPWM.compute(0);
}
}
void loop() {
UpdateBoilKettle();
unsigned long elapsedTime = (millis() - lastRun);
if (Ethernet.linkStatus() == LinkON && elapsedTime >= UpdateInterval) {
mqtt_client.loop();
//if (!mqtt_client.connected()) ConnectMQTT();
SendSensorData();
lastRun = millis();
}
}

View File

@ -0,0 +1,4 @@
#ifndef config_h
#define config_h
const char version[] = "1.0.0"
#endif

159
src/BrewController/main.cpp Executable file
View File

@ -0,0 +1,159 @@
//Built-in
#include <Arduino.h>
#include <SPI.h>
#include <Ethernet.h>
// Additoinal Libraries
#include <ArduinoJson.h>
#include <MQTT.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidMenu.h> // LiquidMenu_config.h needs to be modified to use I2C.
#include <MD_REncoder.h>
#include <Adafruit_MAX31865.h>
// My Includes
#include "config.h"
#include "main.h"
#include "button.h"
#include "slowPWM.h"
// User I/O objects.
Button Enter;
slowPWM boilPWM;
MD_REncoder rotary = MD_REncoder(encoderDT, encoderCLK);
LiquidCrystal_I2C lcd(0x27,20,4);
unsigned long lastRun = 0;
// Return a character array to represent the
// On/Off state of the kettle.
char* KettleState() {
if (KettleOn) {
return (char*)"On";
} else {
return (char*)"Off";
}
}
// Interrupt function to run when encoder is turned.
//
// Increases/decreases the kettle output to a max
// of 100% and minimum of 0%.
void doEncoder()
{
uint8_t result = rotary.read();
uint8_t inc;
if (result) {
uint8_t speed = rotary.speed();
speed >= 10 ? inc = 5 : inc = 1;
}
if (result == DIR_CW && KettleDuty < 100) {
KettleDuty = (KettleDuty / inc) * inc + inc;
} else if (result == DIR_CCW && KettleDuty > 0) {
KettleDuty = (KettleDuty / inc) * inc - inc;
}
}
void setupHASS() {
auto chipid = String(ESP.getChipId(), HEX);
DynamicJsonDocument device(200);
JsonObject dev = device.createNestedObject("dev");
dev["name"] = "Brewhouse";
dev["mdl"] = "Brewhouse v2";
dev["sw"] = FIRMWAREVERSION;
dev["mf"] = "Damn Yankee Brewing";
dev["ids"] = "[\"" + chipid + "\"]";
publishTemperature(device, "Boil");
// Temp sensors
mqtt_discovery(
topic + "mash_temperature/config",
"{\"uniq_id\": \"" + chipid + "_mash_temp\"," +
"\"dev_cla\": \"temperature\"," +
"\"name\": \"Mash Temperature\"," +
"\"unit_of_meas\": \"°" + TEMP_UNIT + "\"," +
"\"val_tpl\": \"{{ value_json }}\"," +
"\"stat_t\": \"damn_yankee/brewhouse/mash_temperature\"," +
device + "}"
);
}
// LCD menu setup.
LiquidLine KettleState_line(0, 0, "Boil Kettle ", KettleState);
LiquidLine kettle_power_line(0, 1, "Kettle Power % ", KettleDuty);
LiquidScreen home_screen(KettleState_line, kettle_power_line);
LiquidMenu menu(lcd);
void setup() {
unsigned long lastRun = millis() - UpdateInterval;
Serial.begin(9600);
rotary.begin();
Ethernet.begin(mac, ip);
Serial.println("Setting up...");
attachInterrupt(digitalPinToInterrupt(encoderCLK), doEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderDT), doEncoder, CHANGE);
pinMode(encoderCLK, INPUT_PULLUP);
pinMode(encoderDT, INPUT_PULLUP);
Enter.begin(encoderBTN);
boilPWM.begin(kettlePWM, PeriodPWM);
// if you get a connection, report back via serial:
if (Ethernet.linkStatus() == LinkON) {
SetupMQTT(MQTT_BROKER);
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
lcd.init();
lcd.backlight();
menu.init();
menu.add_screen(home_screen);
menu.update();
};
void UpdateBoilKettle(){
static byte last_KettleDuty = 0;
if (Enter.pressed()) {
KettleOn = !KettleOn;
menu.update();
}
if (last_KettleDuty != KettleDuty) {
last_KettleDuty = KettleDuty;
menu.update();
}
if (KettleOn) {
digitalWrite(kettlePWM, boilPWM.compute(KettleDuty));
} else {
digitalWrite(kettlePWM, boilPWM.compute(0));
}
}
void loop() {
UpdateBoilKettle();
unsigned long elapsedTime = (millis() - lastRun);
if (Ethernet.linkStatus() == LinkON && elapsedTime >= UpdateInterval) {
mqtt_client.loop();
//if (!mqtt_client.connected()) ConnectMQTT();
SendSensorData();
lastRun = millis();
}
}

9
src/BrewController/main.h Executable file
View File

@ -0,0 +1,9 @@
#define FIRMWAREVERSION "0.0.1"
#define TEMP_UNIT "F"
// Global variables.
byte KettleDuty = 0;
bool KettleOn = false;

72
src/BrewController/mqtt.ino Executable file
View File

@ -0,0 +1,72 @@
void ConnectMQTT() {
static const char *password = MQTT_PASSWORD;
static const char *user = MQTT_USER;
Serial.println("connecting MQTT...");
while (!mqtt_client.connect("brewhouse", user, password)) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
mqtt_client.subscribe("brewery/setpoint/bk");
}
void MessageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
/** JSON Parser Setup */
StaticJsonDocument<200> doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, payload);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
char buf[30];
strcpy(buf,TOPIC_PREFIX);
strcat(buf,BOIL_SETPOINT_TOPIC);
if (topic == buf) {
// Update PWM setpoint.
String name = doc["entity"];
String setting = doc["setpoint"];
KettleDuty = setting.toInt();
String unit = doc["units"];
Serial.println("Updating setpoint for " + name + " to " + setting + " " + unit);
}
}
void SetupMQTT(const char *broker) {
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
// by Arduino. You need to set the IP address directly.
Serial.println("Setup MQTT client.");
mqtt_client.begin(broker, net);
mqtt_client.onMessage(MessageReceived);
ConnectMQTT();
}
static void SendSensorData() {
Serial.println("Sending data...");
// NOTE: max message length is 250 bytes.
StaticJsonDocument<200> doc;
doc["entity"] = "boil_kettle";
doc["setpoint"] = KettleDuty;
doc["units"] = "%";
String jstr;
serializeJson(doc, jstr);
String topic = TOPIC_PREFIX;
topic += "sensor/boil_kettle";
mqtt_client.publish(topic, jstr);
}

View File

@ -0,0 +1,127 @@
//#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <IPAddress.h>
// My Libraries
#include <secrets.h>
#include <global.h>
#include <communicator.h>
String chiller_state = "idle";
int tank_setpoint = 28;
WiFiClient net;
void mqttCallback(char *topic, byte *payload, unsigned int length) {
Serial.print("incoming: ");
Serial.println(topic);
for (unsigned int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println("");
}
Communicator hass_comm = Communicator(net, &mqttCallback);
String slugify(String input) {
input.toLowerCase();
input.replace(" ", "_");
return input;
}
void merge(JsonObject dest, JsonObjectConst src) {
for (auto kvp : src) {
dest[kvp.key()] = kvp.value();
}
}
/* Setup MQTT discovery for chiller tank control.
*
* Using climate device.
*/
void climateDevice(String name, boolean multimode=false) {
auto chipid = String(ESP.getChipId(), HEX);
String name_slug = slugify(name);
String config_topic = "homeassistant/climate/" + name_slug + "_" + chipid + "/config";
String topic_root = "brewhouse/" + name_slug + "/";
StaticJsonDocument<1536> entity;
entity["uniq_id"] = chipid + "_" + name_slug;
entity["name"] = name;
entity["temp_unit"] = "F";
// Mode setup
entity["mode_cmd_t"] = topic_root + "mode/set";
entity["mode_cmd_tpl"] = "{{ value }}";
entity["mode_stat_t"] = topic_root + "mode/state";
entity["mode_stat_tpl"] = "{{ value_json }}";
JsonArray modes = entity.createNestedArray("modes");
modes.add("off");
modes.add("cool");
entity["temp_cmd_t"] = topic_root + "temp/set";
entity["temp_cmd_tpl"] = "{{ value }}";
entity["temp_stat_t"] = topic_root + "temp/state";
entity["temp_stat_tpl"] = "{{ value_json }}";
entity["curr_temp_t"] = topic_root + "temp/current";
entity["curr_temp_tpl"] = "{{ value }}";
if (multimode == true) {
entity["temp_hi_cmd_t"] = topic_root + "temp_hi/set";
entity["temp_hi_cmd_tpl"] = "{{ value }}";
entity["temp_hi_stat_t"] = topic_root + "temp_hi/state";
entity["temp_hi_stat_tpl"] = "{{ value_json }}";
entity["temp_lo_cmd_t"] = topic_root + "temp_lo/set";
entity["temp_lo_cmd_tpl"] = "{{ value }}";
entity["temp_lo_stat_t"] = topic_root + "temp_lo/state";
entity["temp_lo_stat_tpl"] = "{{ value_json }}";
modes.add("heat");
modes.add("auto");
}
JsonObject dev = entity.createNestedObject("dev");
dev["name"] = DEVICE_NAME;
dev["mdl"] = DEVICE_MDL;
dev["sw"] = DEVICE_SW;
dev["mf"] = DEVICE_MF;
JsonArray ids = dev.createNestedArray("ids");
ids.add(chipid);
//dev["ids"] = "[\"" + chipid +"\"]";
hass_comm.mqtt_discovery(config_topic, entity);
}
void setup() {
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
Serial.begin(115200);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed!");
return;
}
Serial.println();
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
climateDevice("Coolant Tank");
String f_name = "Fermenter ";
for (int i=0;i<FERMENTER_COUNT;i++) {
int f_num = i+1;
Serial.println(f_name + f_num);
climateDevice(f_name + f_num, true);
}
}
void loop() {
delay(5000);
hass_comm.publish_data("brewhouse/coolant_tank/temp/current", String(tank_setpoint));
hass_comm.publish_data("brewhouse/coolant_tank/mode/state", chiller_state);
hass_comm.loop();
}

View File

@ -0,0 +1,50 @@
/* This file is all the stuff I want to be
able to change without having to update the
source repository.
*/
#ifndef CONFIG_H
#define CONFIG_H
#ifdef CONFIG_ROOT
#undef CONFIG_ROOT
#define CONFIG_ROOT "homeassistant/climate/"
#endif
#ifdef DEVICE_NAME
#undef DEVICE_NAME
#define DEVICE_NAME "Glycol Chiller"
#endif
#ifdef DEVICE_MDL
#undef DEVICE_MDL
#define DEVICE_MDL "Chillenator v0.1"
#endif
#ifdef DEVICE_MF
#undef DEVICE_MF
#define DEVICE_MF "Damn Yankee Brewing"
#endif
// MQTT Topic Definitions
#ifdef ROOT
#undef ROOT
#define ROOT "brewery/"
#endif
static const byte mac[] = { 0xA6, 0x61, 0x0A, 0xAE, 0x89, 0xDE }; //physical mac address
static const IPAddress ip(192,168,1,177);
// Addresses of 3 DS18B20s
uint8_t glycol[8] = { 0x28, 0xEE, 0xD5, 0x64, 0x1A, 0x16, 0x02, 0xEC };
uint8_t fermA[8] = { 0x28, 0x61, 0x64, 0x12, 0x3C, 0x7C, 0x2F, 0x27 };
uint8_t fermB[8] = { 0x28, 0x61, 0x64, 0x12, 0x3F, 0xFD, 0x80, 0xC6 };
#define ONE_WIRE_BUS D2
#define COOL_A_PIN D3
#define COOL_B_PIN D4
#define HEAT_A_PIN D5
#define HEAT_B_PIN D6
#define GLYCOL_PIN D7
#endif

View File

@ -0,0 +1,79 @@
#include <ArduinoJson.h>
class Device {
public:
String device_topic;
Communicator hass_comm;
Device(Communicator& comm, String topic) {
this->hass_comm = comm;
this->device_topic = topic;
}
void addParameter(String id, String value){
_params[_paramcount] = Parameter(id, value);
_paramcount++
}
void attachEntity(Entity entity){
_entities[_entitycount] = entity;
_entitycount++;
}
void registerDevice(){
for (int i=0;i<_entitycount;i++){
StaticJsonDocument<512> doc;
for (int j=0;j<_entities[i].paramcount;j++) {
doc[_entities[i].params[j].getID()] = _entities[i].params[j].getValue();
}
}
JsonObject dev = doc.createNestedObject("dev");
for (int i=0; i<_paramcount;i++) {
dev[_params[j].getID()] = _params[j].getValue();
}
//Register
hass_comm.mqtt_discovery(device_topic, doc);
}
private:
Parameter _params[];
int _paramcount = 0;
Entity _entities[];
int _entitycount = 0;
}
class Entity {
public:
Parameter params[];
int paramcount = 0;
void addParameter(String id, String value){
params[paramcount] = Parameter(id, value);
paramcount++
}
}
class Parameter {
public:
Parameter(String id, String value){
this->_id = id;
this->_value = value;
}
String getID(){
return this->_id;
}
String getValue(){
return this->_value;
}
private:
String _id;
String _value;
}

View File

@ -0,0 +1,11 @@
#include <Arduino.h>
void setup() {
Serial.begin(115200);
}
void loop() {
}

11
src/KegController/main.cpp Executable file
View File

@ -0,0 +1,11 @@
#include <Arduino.h>
void setup() {
Serial.begin(115200);
}
void loop() {
}