80 Commits

Author SHA1 Message Date
W1CDN acdee84d3e Update requirements. 2023-08-26 15:26:44 -05:00
W1CDN fcd776174c Log with timestamp. 2023-08-25 21:38:17 -05:00
W1CDN ef539e2aa9 Revert relative paths, which don't work on production. 2023-07-12 14:54:43 -05:00
W1CDN 9fb3d28cdc API calls should use relative paths. 2023-07-12 14:48:26 -05:00
W1CDN f447a807b2 Gotta update the datestamp too. 2023-07-12 13:36:27 -05:00
W1CDN 875546040f Logging encoding. 2023-07-12 12:47:29 -05:00
W1CDN 1a5df46eca Add basic logging. 2023-07-12 12:43:24 -05:00
W1CDN ebd237d9d3 Get UTC time from stations table. 2023-07-12 12:34:50 -05:00
mattbk 9c11d8d494 Try something else 2023-07-12 09:57:54 -05:00
mattbk e3cb68551b Try to fix race condition in frames and stations tables. 2023-07-12 09:34:39 -05:00
W1CDN d2cdaa820a Undo relative URL for now. 2023-07-09 22:17:50 -05:00
W1CDN 93156311e2 Fix bug. 2023-07-09 22:15:03 -05:00
W1CDN d382a2b8f7 Better station list. 2023-07-09 22:06:57 -05:00
W1CDN 40513cc488 Use stations table on index.html. 2023-07-09 11:22:23 -05:00
W1CDN c0ff61063f Fix query for updating the stations table.
I am still not good at knowing when to use quotes for values.
2023-07-09 10:15:54 -05:00
W1CDN b0f0a4f8dc Try to combine all the processes to there aren't orphans. 2023-07-09 09:37:03 -05:00
W1CDN 8d94794c90 Snapshot after adding station table update code, but hasn't been tested on real frames yet. 2023-07-09 09:14:09 -05:00
W1CDN ee75cccc68 Add stations table definiton. 2023-07-08 21:56:15 -05:00
W1CDN 1c057a5555 Stub out db upsert code for stations table in test_db.py.
See table definition at 
W1CDN/aprs_tool#30 (comment).
2023-07-08 21:54:37 -05:00
W1CDN c25a10ae77 Fix path. 2023-07-07 18:25:20 -05:00
W1CDN e1cd7ccaae Remove config.ini. 2023-07-07 18:17:06 -05:00
W1CDN 00ede8860f Add default config as example, gitignore real config. 2023-07-07 18:16:00 -05:00
W1CDN 8f3b2ae707 Don't autodelete yet. 2023-06-27 21:44:12 -05:00
W1CDN e05a3790d6 Add links to aprs.fi. 2023-06-27 21:43:14 -05:00
W1CDN fb4d89cd9b Remove old dev file. 2023-06-27 21:37:05 -05:00
W1CDN 8972c8d447 Snapshot. Get closer to new stations table. 2023-06-24 21:30:05 -05:00
W1CDN 1362558deb Stub out station query. 2023-06-24 19:06:37 -05:00
W1CDN f6a71e4851 Add raw column. 2023-06-24 18:44:18 -05:00
W1CDN 1d8699df94 Remove extra argument. 2023-06-24 18:44:08 -05:00
W1CDN 1ad8c848c4 Fix bug on production. 2023-06-24 11:43:06 -05:00
W1CDN 9deb160989 Add help link. 2023-06-24 11:20:58 -05:00
W1CDN e2c3adf5c0 Stub out index/status page. 2023-06-24 11:17:53 -05:00
W1CDN 4203af5b81 Merge pull request 'Sort, filter, and limit results at /packets' (#23) from sort-filter into main
Reviewed-on: W1CDN/aprs_tool#23
2023-05-14 10:18:08 -05:00
W1CDN 494f53bd62 Add created_unix field to frames table. 2023-05-13 20:59:38 -05:00
W1CDN e19a8c777c Fix bad comment. 2023-05-13 20:59:17 -05:00
W1CDN cb9af0f5b8 Move tuple of frame table fields to a separate files in case we need it more places. 2023-05-13 17:26:38 -05:00
W1CDN 863efdd84c Use LIKE instead of =. 2023-05-13 11:14:21 -05:00
W1CDN a99de3a859 Allow query on any field in the packets table. 2023-05-13 11:09:41 -05:00
W1CDN efe61ae4c5 Add msgNo field. 2023-04-27 19:29:22 -05:00
W1CDN cc89ab1a4c Don't mess with frame if it can't be parsed. 2023-04-27 19:19:12 -05:00
W1CDN f396fe87af Order by created date in db call. 2023-04-25 14:19:29 -05:00
W1CDN ab850a76a3 Don't get hung up on parsing errors. 2023-04-23 21:13:27 -05:00
W1CDN 2121119365 Update docs. 2023-04-23 21:13:06 -05:00
W1CDN 6957219468 Add basic API call to select by "from" station. 2023-04-16 21:04:26 -05:00
W1CDN cd5d24b641 Add comments. 2023-04-16 19:29:09 -05:00
W1CDN 1cffde2903 At /packets, return 10 records by default and add n=x to return x packets. 2023-04-16 18:50:39 -05:00
W1CDN 5793e57aa9 Sort /packets descending by created. 2023-04-16 16:59:09 -05:00
W1CDN 19b3a54d98 Update readme. 2023-04-16 16:49:25 -05:00
W1CDN 467ec11522 Merge pull request 'Add background KISS connection, log frames to database' (#20) from add-kiss into main
Reviewed-on: W1CDN/aprs_tool#20
2023-04-16 16:45:53 -05:00
W1CDN 2ca627f973 Add kiss stuff to config. 2023-04-16 16:45:22 -05:00
W1CDN ef5765e87f Add missing fields and try to catch sqlite errors. 2023-04-16 10:04:48 -05:00
W1CDN 7453d50598 Update waitress IP to make it work on production. 2023-04-15 16:37:31 -05:00
W1CDN 14effc9746 Try to get working on production. 2023-04-15 14:20:07 -05:00
W1CDN 6e6693bfbc Read from database for API. 2023-04-15 13:27:00 -05:00
W1CDN 63963c0ade Store frames in database, remove frames older than chosen age. 2023-04-14 22:06:48 -05:00
W1CDN b06b87fe4e Barely parse a frame. 2023-04-13 21:38:51 -05:00
W1CDN 4f83f76bf5 Switch to aprs3 package. 2023-04-13 21:11:01 -05:00
W1CDN b4bc632ded Snapshot. 2023-04-13 20:46:28 -05:00
W1CDN 5e86b40f38 Stub out a KISS connection.
It seems to simultaneously run the API and keep the KISS connection open 
in the background.
2023-04-13 17:16:16 -05:00
W1CDN fb027194e8 Update data on API call
Oops!
2023-04-12 09:24:31 -05:00
W1CDN 795d2ef403 Remove old tutorial files. 2023-04-11 21:30:46 -05:00
W1CDN 0f1edd34af Update readme. 2023-04-11 20:55:58 -05:00
W1CDN 23d52a80da Set up to run waitress in screen if needed. 2023-04-11 20:52:59 -05:00
W1CDN f9547c289e Add Waitress but needs work. 2023-04-08 20:05:35 -05:00
W1CDN a06bedb1c9 Test commit. 2023-04-08 17:08:44 -05:00
W1CDN 37d57db772 Rename first application. 2023-04-08 17:07:09 -05:00
W1CDN 939e96ad7e Add "Contributing" to readme, move TODOs to Gitea issues. 2023-04-08 16:54:45 -05:00
W1CDN 796c666f4d Test commit. 2023-04-08 15:36:54 -05:00
mattbk 270a63bc1f Test. 2023-04-08 15:35:04 -05:00
mattbk 008396ed1b Add .gitignore. 2023-04-06 20:46:10 -05:00
mattbk 9ad72c4d7a Lots of changes. 2023-04-06 20:45:31 -05:00
mattbk 6451e13ac4 Drop numpy and pandas. 2023-04-06 14:34:59 -05:00
mattbk 133690efc4 Add readme and stub out requirements.txt. 2023-04-05 22:43:35 -05:00
mattbk 392c2afb6c Get a config file set up, reload it every API call. 2023-04-05 20:36:19 -05:00
mattbk dcff8433ed Orient JSON by records. 2023-04-05 19:05:57 -05:00
mattbk b16fb4a630 Fix NaN JSON error. 2023-04-05 18:27:14 -05:00
mattbk d2479746eb Start work reading from direwolf log files. 2023-04-05 18:21:37 -05:00
mattbk 9451b6ea22 Throw some things at the wall for KISS/AGW. 2023-04-05 17:36:16 -05:00
mattbk b6631ccaef Very basic API example done. 2023-04-05 16:58:35 -05:00
mattbk 398bd8160f Init. 2023-04-05 16:28:39 -05:00
22 changed files with 654 additions and 1353 deletions
+4 -7
View File
@@ -1,7 +1,4 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
.vscode/*
*/config.h
/logs/*
config.ini
*.db
*.log
+79 -4
View File
@@ -1,5 +1,80 @@
# Vulpes
# README
## Access Point
When using as a wireless access point, the network SSID is "vulpes"
with no password. Navigate to http://192.168.0.1 to access webform.
I got tired of APRS-IS websites not showing me all the paths that packets
take, only the shortest path to IS. So this is a Python 3 tool that turns
direwolf logs into a REST API in JSON format.
## Setup
1. Run direwolf with logging to CSV on by using `-l`. (`-L` not yet implemented).
1. Install requirements using `pip install -r requirements.txt`.
1. Set up database file with `python init_db.py`.
2. Run `app.py` with either a Python call or a real WSGI server.
You can use screen to detach the session.
- Default URL is http://127.0.0.1:5001
- Example `waitress` and `screen` scripts are included, see
- `api_waitress.py` and
- `start-aprs_api.sh`
3. Access the API from whatever other system you want.
## Endpoints:
-`/packets` - gives the most recent packets, sorted descending by time received.
- argument `n` will return a specific number of packets, default 10. E.g.,
`https://digi.w1cdn.net/aprs_api/packets?n=1` returns one packet.
- argument `from` will return packets from the named station-SSID (no wildcards).
E.g., `https://digi.w1cdn.net/aprs_api/packets?n=1&from=W1CDN-1` returns
one packet from W1CDN-1.
Example of an object packet sent by W1CDN-1 and digipeated by K0UND-2:
```
{
"id": 1,
"addresse": null,
"alive": null,
"altitude": null,
"comment": "Leave a message to say hi!",
"course": null,
"created": "2023-04-16 15:04:03",
"format": "uncompressed",
"frame": null,
"from": "W1CDN-2",
"gpsfixstatus": null,
"latitude": 47.94133333333333,
"longitude": -97.02683333333333,
"mbits": null,
"messagecapable": 1,
"message_text": null,
"mtype": null,
"object_format": null,
"object_name": null,
"path": "['K0UND-2', 'WIDE2-2']",
"phg": null,
"phg_dir": null,
"phg_gain": null,
"phg_height": null,
"phg_power": null,
"phg_range": null,
"posambiguity": 0,
"raw": "W1CDN-2>APQTH1,K0UND-2,WIDE2-2:@150321h4756.48N/09701.61W-Leave a message to say hi!",
"raw_timestamp": "150321h",
"speed": null,
"station_call": "W1CDN-1",
"station_lat": 47.9415,
"station_lon": -97.027,
"status": null,
"symbol": "-",
"symbol_table": "/",
"telemetry": null,
"timestamp": 1681657401,
"to": "APQTH1",
"tEQNS": null,
"tPARM": null,
"tUNIT": null,
"via": "",
"weather": null,
"wx_raw_timestamp": null
}
```
# Contributing
If you want to contribute, please get in touch with me on Mastodon at
https://mastodon.radio/@W1CDN.
+187
View File
@@ -0,0 +1,187 @@
from flask import Flask, request, render_template
from flask_restful import Resource, Api, reqparse, url_for
from datetime import date, timedelta
import configparser
import csv
import datetime
import ast
import glob
import json, operator
import requests
import sqlite3
api_app = Flask(__name__)
api = Api(api_app)
# TODO this is duplicated from kiss_and_db.py, can I avoid that?
import constants
def read_config():
config = configparser.ConfigParser()
config.read('config.ini')
return config
def read_logs(log_folder):
# Read some log files
# UTC time, so let's look at tomorrow, today, and yesterday.
today = date.today()
yesterday = today - timedelta(days = 1)
tomorrow = today + timedelta(days = 1)
file_list = glob.glob(log_folder+str(yesterday)+"*") + \
glob.glob(log_folder+str(today)+"*") + \
glob.glob(log_folder+str(tomorrow)+"*")
# https://stackoverflow.com/a/66071962
json_array = []
for file in file_list:
with open(file, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
for row in csvReader:
#add this python dict to json array
json_array.append(row)
# Add the call and location of this station to the packet info
config = read_config()
for item in json_array:
item['station_name'] = config['Settings']['station_call']
item['station_lat'] = config['Settings']['station_lat']
item['station_lon'] = config['Settings']['station_lon']
return(json_array)
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def get_db_connection():
conn = sqlite3.connect('database.db')
conn.row_factory = dict_factory
return conn
def select_all_frames(conn):
"""
Query all rows in the frames table
:param conn: the Connection object
:return:
"""
cur = conn.cursor()
cur.execute("SELECT * FROM frames")
rows = cur.fetchall()
return rows
def select_all_stations(conn):
"""
Query all rows in the stations table
:param conn: the Connection object
:return:
"""
cur = conn.cursor()
cur.execute("SELECT * FROM stations ORDER BY last_heard_unix DESC")
rows = cur.fetchall()
return rows
def unique_stations(conn):
"""
Query all rows in the frames table
:param conn: the Connection object
:return:
"""
cur = conn.cursor()
cur.execute('SELECT *, MAX(id), COUNT(id) FROM frames GROUP BY "from" ORDER BY MAX(id) DESC')
rows = cur.fetchall()
return rows
def select_frames(conn, n, url_params):
# Should pass this a dict of fields and values (request.args)
# TODO clean data before sending to DB
# Filter out any keys that don't match db fields
# From https://stackoverflow.com/a/20256491
dictfilt = lambda x, y: dict([ (i,x[i]) for i in x if i in set(y) ])
field_where = dictfilt(url_params, constants.db_frames_fields)
# Then loop through fields to create query parts
# From https://stackoverflow.com/a/73512269/2152245
field_where_str = ' AND '.join([f'"{k}" LIKE \'{v}\'' for k,v in field_where.items()])
cur = conn.cursor()
# Workaround to deal with missing value in WHERE
field_where_query = "" if field_where_str == "" else "WHERE "+field_where_str
sql = 'SELECT * FROM frames {field_where_query} ORDER BY created DESC LIMIT {n}'.format(field_where_query=field_where_query, n=n)
print(sql)
cur.execute(sql)
rows = cur.fetchall()
return rows
@api_app.route('/')
def index():
# Get list of recent packets using API
# TODO use relative path
response = json.loads(requests.get("https://digi.w1cdn.net/aprs_api/packets").text)['data']
# Play with function to create station list
#stations = select_all_stations(get_db_connection())
#print(url_for("static", filename="test.txt", _external=True))
# this should work: stations = json.loads(requests.get(url_for("stations", _external=True)).text)['data']
stations = json.loads(requests.get("https://digi.w1cdn.net/aprs_api/stations").text)['data']
# Convert unix time to datetime on the fly because I'm lazy right now
for station in stations:
if station['last_heard_unix'] != None:
station['last_heard'] = datetime.datetime.utcfromtimestamp(station['last_heard_unix'])
return render_template('index.html',
station_call = config['Settings']['station_call'],
station_lat = config['Settings']['station_lat'],
station_lon = config['Settings']['station_lon'],
frames = response,
stations = stations)
class Packets(Resource):
def get(self):
# Handle arguments that may or may not exist
try:
n = int(request.args.get('n'))
except:
n = 10
conn = get_db_connection()
# Limit to number of records requested
data = select_frames(conn, n = n, url_params = request.args.to_dict())
# Sort by created date, descending (https://stackoverflow.com/a/45266808)
#data.sort(key=operator.itemgetter('created'), reverse=True)
return {'data':data}, 200 # return data and 200 OK code
class Stations(Resource):
def get(self):
# Handle arguments that may or may not exist
try:
n = int(request.args.get('n'))
except:
n = 10
conn = get_db_connection()
# Limit to number of records requested
data = select_all_stations(conn)
# Sort by created date, descending (https://stackoverflow.com/a/45266808)
#data.sort(key=operator.itemgetter('created'), reverse=True)
return {'data':data}, 200 # return data and 200 OK code
# Read config
config = read_config()
log_folder = config['Settings']['log_folder']
# Start subprocess to watch KISS connection
import subprocess
#proc = subprocess.Popen(["python3","kiss_and_db.py"])
# Combine under one process https://stackoverflow.com/a/13143013/2152245
proc = subprocess.Popen("exec " + "python3 kiss_and_db.py", stdout=subprocess.PIPE, shell=True)
print("kiss_and_db.py as subprocess pid "+str(proc.pid))
# The packets endpoint
api.add_resource(Packets, '/packets')
# The stations endpoint
api.add_resource(Stations, '/stations')
if __name__ == '__main__':
api_app.run(debug=True, host='0.0.0.0', port=5001) # run our Flask app
+12
View File
@@ -0,0 +1,12 @@
# run.py from https://www.devdungeon.com/content/run-python-wsgi-web-app-waitress
import os
from waitress import serve
from api_app import api_app # Import your app
# Run from the same directory as this script
this_files_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(this_files_dir)
# `url_prefix` is optional, but useful if you are serving app on a sub-dir
# behind a reverse-proxy.
serve(api_app, host='0.0.0.0', port=5001)
+20
View File
@@ -0,0 +1,20 @@
[Settings]
# Path to direwolf log folder, include trailing slash
log_folder = logs/
#log_folder = /home/pi/logs/direwolf/
# Name and location of this station, for inclusion in the API
station_call = W1CDN-1
station_lat = 47.941500
station_lon = -97.027000
# How long to keep packets (frames) e.g., "2 days", "5 minutes"
keep_time = "2 days"
# KISS settings
kiss_host = 192.168.0.30
kiss_port = 8001
# Development settings (not operational yet)
mycall = W1CDN-15
log_path = aprs_api.log
+49
View File
@@ -0,0 +1,49 @@
# Tuple of frames table fields
db_frames_fields = ("id",
"addresse",
"alive",
"altitude",
"comment",
"course",
"created",
"created_unix",
"format",
"frame",
"from",
"gpsfixstatus",
"latitude",
"longitude",
"mbits",
"messagecapable",
"message_text",
"msgNo",
"mtype",
"object_format",
"object_name",
"path",
"phg",
"phg_dir",
"phg_gain",
"phg_height",
"phg_power",
"phg_range",
"posambiguity",
"raw",
"raw_timestamp",
"speed",
"station_call",
"station_lat",
"station_lon",
"status",
"subpacket",
"symbol",
"symbol_table",
"telemetry",
"timestamp",
"to",
"tEQNS",
"tPARM",
"tUNIT",
"via",
"weather",
"wx_raw_timestamp")
-39
View File
@@ -1,39 +0,0 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
+19
View File
@@ -0,0 +1,19 @@
import sqlite3
connection = sqlite3.connect('database.db')
with open('schema.sql') as f:
connection.executescript(f.read())
cur = connection.cursor()
# cur.execute("INSERT INTO posts (title, content) VALUES (?, ?)",
# ('First Post', 'Content for the first post')
# )
#
# cur.execute("INSERT INTO posts (title, content) VALUES (?, ?)",
# ('Second Post', 'Content for the second post')
# )
connection.commit()
connection.close()
+116
View File
@@ -0,0 +1,116 @@
#!/usr/bin/env python3
import os
import sqlite3
import aprs
import json
import aprslib
import configparser
import time
import logging
def read_config():
config = configparser.ConfigParser()
config.read('config.ini')
return config
def get_db_connection():
conn = sqlite3.connect('database.db')
conn.row_factory = sqlite3.Row
return conn
def main():
# Add the call and location of this station to the packet info
config = read_config()
# MYCALL = os.environ.get("MYCALL", "W1CDN")
# KISS_HOST = os.environ.get("KISS_HOST", "192.168.0.30")
# KISS_PORT = os.environ.get("KISS_PORT", "8001")
logging.basicConfig(filename=config['Settings']['log_path'], level=logging.DEBUG, \
format='%(asctime)s - %(message)s')
logging.debug('kiss_and_db.py running')
ki = aprs.TCPKISS(host=config['Settings']['kiss_host'], port=int(config['Settings']['kiss_port']))
ki.start()
# Make a simple frame and send it
frame = aprs.APRSFrame.ui(
destination="APZ001",
source=config['Settings']['mycall'],
path=["WIDE1-1"],
info=b">Hello World!",
)
#ki.write(frame)
# Watch for new packets to come in
while True:
conn = get_db_connection()
for frame in ki.read(min_frames=1):
try:
a = aprslib.parse(str(frame))
a['station_call'] = config['Settings']['station_call']
a['station_lat'] = config['Settings']['station_lat']
a['station_lon'] = config['Settings']['station_lon']
a['created_unix'] = int(time.time())
print(a)
# Make this a string and deal with it later (probably a mistake)
a['path'] = str(a['path'])
# Store true/false as 1/0
if 'alive' in a:
if a['alive'] == True:
a['alive'] = 1
else:
a['alive'] = 0
# Build an INSERT statement based on the fields we have from the frame
attrib_names = ', '.join('"%s"' % w for w in a.keys())
attrib_values = ", ".join("?" * len(a.keys()))
try:
# Insert data
sql = "INSERT INTO frames ("+attrib_names+") VALUES ("+attrib_values+")"
logging.debug(sql)
conn.execute(sql, list(a.values()))
# TODO update stations table here
# Original intent was to include the id from the frames table,
# but that would mean making another query.
# It's not immediately needed, so I'm skipping it.
# Build query
# "from" is wrappedin [] because it is a reserved word and using '' doesn't work.
# https://www.sqlite.org/lang_keywords.html
#try:
station_update = "'"+a['from'] +"', '"+ str(a['created_unix']) +"', '1'"
query3 = "INSERT INTO stations ([from], last_heard_unix, count) \
VALUES("+station_update+") \
ON CONFLICT([from]) \
DO UPDATE SET count = count + 1,\
last_heard_unix = excluded.last_heard_unix;"
#print(query3)
logging.debug(query3)
# Insert/update data
conn.execute(query3)
conn.commit()
#except:
# print("Stations table couldn't be updated.")
# TODO remove packets that are older ('created') than a limit set in config.ini
# "5 minutes" also works
#conn.execute("DELETE FROM frames WHERE created < DATETIME('now', '"+config['Settings']['keep_time']+"')")
#conn.commit()
except:
#print("Error with SQLite!")
logging.error("Error with SQLite!")
except:
#print("Frame could not be parsed.")
logging.error("Frame could not be parsed.")
conn.close()
if __name__ == "__main__":
main()
-46
View File
@@ -1,46 +0,0 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
-26
View File
@@ -1,26 +0,0 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
upload_speed = 921600
monitor_speed = 115200
monitor_filters = esp32_exception_decoder
build_type = debug
lib_deps =
me-no-dev/AsyncTCP@^1.1.1
me-no-dev/ESP Async WebServer@^1.2.3
contrem/arduino-timer@^3.0.1
jandelgado/JLed@^4.13.1
https://github.com/adafruit/RTClib.git
adafruit/Adafruit BusIO@^1.14.3
erropix/ESP32 AnalogWrite@^0.2
+8
View File
@@ -0,0 +1,8 @@
flask
flask_restful
aprs3
kiss3
kiss
aprslib
sqlite3
json
+61
View File
@@ -0,0 +1,61 @@
DROP TABLE IF EXISTS frames;
CREATE TABLE frames (
id INTEGER PRIMARY KEY AUTOINCREMENT,
addresse TEXT,
alive INT,
altitude REAL,
comment TEXT,
course REAL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
created_unix INT,
format TEXT,
frame TEXT,
"from" TEXT,
gpsfixstatus TEXT,
latitude REAL,
longitude REAL,
mbits INT,
messagecapable INT,
message_text TEXT,
msgNo INT,
mtype TEXT,
object_format TEXT,
object_name TEXT,
path TEXT,
phg REAL,
phg_dir TEXT,
phg_gain REAL,
phg_height REAL,
phg_power REAL,
phg_range REAL,
posambiguity INT,
raw TEXT,
raw_timestamp TEXT,
speed REAL,
station_call TEXT,
station_lat REAL,
station_lon REAL,
status TEXT,
subpacket TEXT,
symbol TEXT,
symbol_table TEXT,
telemetry TEXT,
timestamp INT,
"to" TEXT,
tEQNS TEXT,
tPARM TEXT,
tUNIT TEXT,
via TEXT,
weather TEXT,
wx_raw_timestamp TIMESTAMP
);
CREATE TABLE "stations" (
"id" INTEGER NOT NULL UNIQUE,
"from" TEXT UNIQUE,
"frames_id" INTEGER,
"last_heard_unix" INTEGER,
"count" INTEGER,
PRIMARY KEY("id" AUTOINCREMENT)
);
-2
View File
@@ -1,2 +0,0 @@
#define WIFI_SSID "wifi_name"
#define WIFI_PASSWORD "wifi_pass"
-702
View File
@@ -1,702 +0,0 @@
/*********
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-input-data-html-form/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*********/
// include wifi password
#include "config.h"
#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <SPIFFS.h>
#include <Preferences.h>
#include "morse.h"
#include <Adafruit_BusIO_Register.h> // for DS3231
#include <RTClib.h> // for DS3231
#include <string>
// download zip from https://github.com/me-no-dev/ESPAsyncWebServer and install.
#include <ESPAsyncWebServer.h>
AsyncWebServer server(80);
// Assign output variables to GPIO pins
//LED_BUILTIN for ESP32 onboard LED, 32 for transmitter keyer
const int keyer = 32;
const int blinker = LED_BUILTIN;
// Battery voltage monitor
const int voltage_pin = 33;
int voltage_pin_value = 0;
// RTC connections
RTC_DS3231 rtc; // set up RTC
const int alarmPin = 4; // pin to monitor for RTC alarms
// Network options: "0" for existing netowrk, "1" to be an access point
const int network = 1;
// Connect to existing network
// Read from config.h
//const char* ssid = WIFI_SSID;
//const char* password = WIFI_PASSWORD;
// Create a new access point
// Replace with your desired network credentials
const char* ssid_ap = "vulpes";
const char* password_ap = NULL; //"123456789"; //NULL is empty
IPAddress local_ip(192,168,0,1);
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);
const char* PARAM_SEND = "inputSend";
const char* PARAM_WPM = "inputWPM";
const char* PARAM_MSG = "inputMsg";
const char* PARAM_CMSG = "inputCustomMsg";
const char* PARAM_FLOAT = "inputFloat";
const char* PARAM_TIME = "inputTimeUnix";
const char* PARAM_START = "inputStartTimeUnix";
const char* PARAM_RUNNING = "programRunning";
const char* PARAM_STEPLENGTH = "inputStepLength";
const char* PARAM_CYCLEID = "inputCycleID";
const char* PARAM_NTRANS = "inputNtransmitters";
const char* PARAM_NETWORK = "inputNetwork";
const char* PARAM_SSID = "inputSSID";
const char* PARAM_PASSWORD = "inputPassword";
// Global variables
int yourInputSend;
int yourInputWPM;
int yourInputMsg;
int yourInputMsg_old; // to save previous state and check changes
String yourInputCustomMsg;
float yourInputFloat;
uint32_t yourInputTime; //to keep time
uint32_t yourInputStartTimeUnix;
bool startProgram;
bool programRunning;
int yourInputStepLength;
int yourInputCycleID;
int yourInputNtransmitters;
int yourInputNetwork;
String yourInputSSID;
String yourInputPassword;
long start_millis = 0;
long stop_millis = 0;
long pause_until_millis = 0;
// HTML web page to handle input fields
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
<link rel="icon" href="data:,">
<title>ESP Input Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript">
// Utility from https://webreflection.medium.com/using-the-input-datetime-local-9503e7efdce
Date.prototype.toDatetimeLocal = function toDatetimeLocal() {
var
date = this,
ten = function (i) {
return (i < 10 ? '0' : '') + i;
},
YYYY = date.getFullYear(),
MM = ten(date.getMonth() + 1),
DD = ten(date.getDate()),
HH = ten(date.getHours()),
II = ten(date.getMinutes()),
SS = ten(date.getSeconds())
;
return YYYY + '-' + MM + '-' + DD + 'T' +
HH + ':' + II + ':' + SS;
}
// Submit timestamps as unix seconds when form is submitted
var putDate = function(form) {
form.inputTimeUnix.value = Math.floor(Date.now() / 1000);// - new Date().getTimezoneOffset()*60;
form.inputStartTimeUnix.value = ((Date.parse(js_start_time_unix_entry.value))/1000);
//document.getElementById("js_start_time_unix").value = ((Date.parse(js_start_time_unix_entry.value))/1000);
}
// Fill in page values
window.onload = function() {
s = %inputStartTimeUnix%;
current_start = new Date(s * 1000);
document.getElementById('current-start').innerHTML = current_start.toLocaleString();
// Show the local time as a string
local_time_unix = new Date().toLocaleString();//toUTCString();
document.getElementById('local-time-unix').innerHTML = local_time_unix.toString();
// Fill in the start time field as local time
document.getElementById('js_start_time_unix_entry').value = current_start.toDatetimeLocal();
// Fill in the other form fields
document.getElementById("send-program").value = %inputSend%;
document.getElementById("message").value = %inputMsg%;
document.getElementById("network").value = %inputNetwork%;
}
</script></head><body>
<h1>Vulpes Radio Orienteering Controller</h1>
<p>Local time: <b><span id=local-time-unix></span></b>. If this is incorrect, your browser is not providing the correct time
(<a href="https://support.mozilla.org/en-US/questions/1297208">Firefox example</a>).</p>
<form action="/get" onsubmit="putDate(this);" accept-charset=utf-8>
<h2>General Settings</h2>
<p>Sending program:
<select name="inputSend" id="send-program">
<option value="0" >0 - Off</option>
<option value="1">1 - Continuous</option>
<option value="2">2 - Cycle</option>
</select><br>
Message:
<select name="inputMsg" id="message">
<option value="0">0 - Custom Message</option>
<option value="1">1 - MOE</option>
<option value="2">2 - MOI</option>
<option value="3">3 - MOS</option>
<option value="4">4 - MOH</option>
<option value="5">5 - MO5</option>
</select><br>
Custom message: <input type="text" name="inputCustomMsg" value = "%inputCustomMsg%"><br>
Speed: <input type="number" name="inputWPM" value = %inputWPM%> WPM
</p>
<h2>Cycle Settings</h2>
<p>Only applies when <em>Sending Program</em> is set to "2 - Cycle". You cannot set a cycle start date more than a month in advance.</p>
<p>Cycle start time <input type="datetime-local" id="js_start_time_unix_entry" /><br>
Current value: <b><span id=current-start></span></b>
<!-- JS converts the entered start time to a unix timestamp, and copies that value
to this hidden field so the user doesn't have to see it. -->
<input type="hidden" name="inputStartTimeUnix" id="js_start_time_unix" /></p>
<p>
Step length: <input type="number" name="inputStepLength" min=1000 step=1000 value = %inputStepLength%> milliseconds <br>
Cycle ID: <input type="number" name="inputCycleID" min=1 value = %inputCycleID%><br>
Number of transmitters: <input type="number" name="inputNtransmitters" min=1 value = %inputNtransmitters%><br>
</p>
<!-- This field is hidden so people don't change the submit time (it will be wrong).
The value is automatically filled in with JS. -->
<input type="hidden" name="inputTimeUnix" id="js_time_unix">
<!-- Extra fields just in case I need them -->
<input type="hidden" name="inputFloat" value = %inputFloat%>
<input type="submit" value="Submit"">
</form>
<iframe style="display:none" name="hidden-form" id="hidden-form"></iframe>
<br><hr>
<h2>Network Settings</h2>
<form onsubmit="return confirm('Are you sure you want to change the network and reboot?');" action="/get2" accept-charset=utf-8>
<p>Network Access:
<select name="inputNetwork" id="network">
<option value="0">Access Point</option>
<option value="1">Existing Wireless Network (advanced)</option>
</select><br>
Existing Wireless Network SSID: <input type="text" name="inputSSID" value = "%inputSSID%"><br>
Existing Wireless Network Password: <input type="password" name="inputPassword" value = "%inputPassword%"><br>
</p><p>
Access Point: Connect to wireless network "vulpes" and point your browser to URL <a href="http://192.168.0.1">http://192.168.0.1</a> (http, not http<b>s</b>)<br>
Existing Network (advanced): Connect to the same existing network and use the proper IP address (useful if you have access to the router or a serial connection).<br>
If an existing network can't be connected to, an access point will be set up.
</p>
<input type="submit" value="Submit and Reboot">
</form>
<iframe style="display:none" name="hidden-form02" id="hidden-form02"></iframe>
<script type="text/javascript">
</script>
</body></html>)rawliteral";
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
String readFile(fs::FS &fs, const char * path){
//Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path, "r");
if(!file || file.isDirectory()){
Serial.println("- empty file or failed to open file");
return String();
}
//Serial.println("- read from file:");
String fileContent;
while(file.available()){
fileContent+=String((char)file.read());
}
file.close();
//Serial.println(fileContent);
return fileContent;
}
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\r\n", path);
File file = fs.open(path, "w");
if(!file){
Serial.println("- failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("- file written");
} else {
Serial.println("- write failed");
}
file.close();
}
// Replaces placeholder in web UI with stored values
String processor(const String& var){
//Serial.println(var);
if(var == "inputCustomMsg"){
return readFile(SPIFFS, "/inputCustomMsg.txt");
}
else if(var == "inputSend"){
return readFile(SPIFFS, "/inputSend.txt");
}
else if(var == "inputWPM"){
return readFile(SPIFFS, "/inputWPM.txt");
}
else if(var == "inputMsg"){
return readFile(SPIFFS, "/inputMsg.txt");
}
else if(var == "inputStepLength"){
return readFile(SPIFFS, "/inputStepLength.txt");
}
else if(var == "inputCycleID"){
return readFile(SPIFFS, "/inputCycleID.txt");
}
else if(var == "inputNtransmitters"){
return readFile(SPIFFS, "/inputNtransmitters.txt");
}
else if(var == "inputNetwork"){
return readFile(SPIFFS, "/inputNetwork.txt");
}
else if(var == "inputSSID"){
return readFile(SPIFFS, "/inputSSID.txt");
}
else if(var == "inputPassword"){
return readFile(SPIFFS, "/inputPassword.txt");
}
else if(var == "inputFloat"){
return readFile(SPIFFS, "/inputFloat.txt");
} else if(var == "inputStartTimeUnix"){
// Webform breaks if this value is empty.
String temp = readFile(SPIFFS, "/inputStartTimeUnix.txt");
if(temp == ""){
temp = "0";
}
return temp;
}
return String();
}
// https://www.thegeekpub.com/276838/how-to-reset-an-arduino-using-code/
void(* resetFunc) (void) = 0; // create a standard reset function
// Set up arduinomorse pin and default WPM
LEDMorseSender sender_blink(blinker, 10.0f); //f makes it a float
LEDMorseSender sender_key(keyer, 10.0f);
//================================================================================
// setup(): stuff that only gets done once, after power up (KB1OIQ's description)
//================================================================================
void setup() {
Serial.begin(115200);
// Get arduinomorse ready to go
sender_blink.setup();
sender_key.setup();
pinMode(alarmPin, INPUT_PULLUP); // Set alarm pin as pullup
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2023, 9, 2, 17, 32, 0));
}
// Report the RTC time after waiting two seconds
// https://amiok.net/gitea/W1CDN/vulpes/issues/50#issuecomment-1376
Serial.println("Wait 2s for RTC");
delay(2000);
Serial.println("RTC time on startup: ");
Serial.println(rtc.now().unixtime());
Serial.println(rtc.now().timestamp());
// Are there any RTC alarms set?
DateTime alarm_one = rtc.getAlarm1(); // Get the current time
char buff[] = "Alarm 1 set for at hh:mm:ss DDD, DD MMM YYYY";
Serial.print(alarm_one.toString(buff));
Serial.println(" (only HH:MM:SS day-of-month are accurate)");
// Initialize the output variables as outputs
pinMode(keyer, OUTPUT);
pinMode(blinker, OUTPUT);
// Set outputs to LOW
digitalWrite(keyer, LOW);
digitalWrite(blinker, LOW);
// Initialize SPIFFS
SPIFFS.begin(true);
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
// Read in existing data
yourInputCustomMsg = readFile(SPIFFS, "/inputCustomMsg.txt");
yourInputSend = readFile(SPIFFS, "/inputSend.txt").toInt();
yourInputWPM = readFile(SPIFFS, "/inputWPM.txt").toFloat();
yourInputMsg = readFile(SPIFFS, "/inputMsg.txt").toInt();
yourInputFloat = readFile(SPIFFS, "/inputFloat.txt").toFloat();
yourInputStartTimeUnix = readFile(SPIFFS, "/inputStartTimeUnix.txt").toInt();
yourInputStepLength = readFile(SPIFFS, "/inputStepLength.txt").toInt();
yourInputCycleID = readFile(SPIFFS, "/inputCycleID.txt").toInt();
yourInputNtransmitters = readFile(SPIFFS, "/inputNtransmitters.txt").toInt();
yourInputNetwork = readFile(SPIFFS, "/inputNetwork.txt").toInt();
yourInputSSID = readFile(SPIFFS, "/inputSSID.txt");
yourInputPassword = readFile(SPIFFS, "/inputPassword.txt");
// Set WPM from saved value
sender_blink.setWPM(yourInputWPM);
sender_key.setWPM(yourInputWPM);
// On restart, keep doing what you were doing before
yourInputMsg_old = yourInputMsg;
if(yourInputMsg == 0){
sender_blink.setMessage(yourInputCustomMsg);
sender_key.setMessage(yourInputCustomMsg);
} else if(yourInputMsg == 1){
sender_blink.setMessage(String("moe "));
sender_key.setMessage(String("moe "));
} else if(yourInputMsg == 2){
sender_blink.setMessage(String("moi "));
sender_key.setMessage(String("moi "));
} else if(yourInputMsg == 3){
sender_blink.setMessage(String("mos "));
sender_key.setMessage(String("mos "));
} else if(yourInputMsg == 4){
sender_blink.setMessage(String("moh "));
sender_key.setMessage(String("moh "));
} else if(yourInputMsg == 5){
sender_blink.setMessage(String("mo5 "));
sender_key.setMessage(String("mo5 "));
}
WiFi.setHostname("vulpes");
if (yourInputNetwork == 1){
// Attach to existing wifi
WiFi.mode(WIFI_STA);
const char* ssid_char = yourInputSSID.c_str();
const char* password_char = yourInputPassword.c_str();
WiFi.begin(ssid_char, password_char);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed! Setting up access point 'vulpes'...");
// If you fail to connect, act as new access point
WiFi.disconnect(true);
WiFi.softAPConfig(local_ip, gateway, subnet);
WiFi.softAP(ssid_ap, password_ap);
// update the file so the webform is right
writeFile(SPIFFS, "/inputNetwork.txt", "0");
//return;
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
} else if (yourInputNetwork == 0){
// Act as new access point
WiFi.softAPConfig(local_ip, gateway, subnet);
WiFi.softAP(ssid_ap, password_ap);
}
// Send web page with input fields to client
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
// Form 1
// Send a GET request to <ESP_IP>/get?inputCustomMsg=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
// GET inputCustomMsg value on <ESP_IP>/get?inputCustomMsg=<inputMessage>
if (request->hasParam(PARAM_CMSG)) {
inputMessage = request->getParam(PARAM_CMSG)->value();
// arduinomorse needs lowercase characters
std::transform(inputMessage.begin(), inputMessage.end(), inputMessage.begin(), ::tolower);
writeFile(SPIFFS, "/inputCustomMsg.txt", inputMessage.c_str());
yourInputCustomMsg = inputMessage;
}
// GET inputSend value on <ESP_IP>/get?inputSend=<inputMessage>
if (request->hasParam(PARAM_SEND)) {
inputMessage = request->getParam(PARAM_SEND)->value();
writeFile(SPIFFS, "/inputSend.txt", inputMessage.c_str());
yourInputSend = inputMessage.toInt();
// if not running a program, set the program running off
//if(yourInputSend != 2){
// Cease all programs on new input
startProgram = false;
programRunning = false;
//}
}
// GET inputWPM value on <ESP_IP>/get?inputWPM=<inputMessage>
if (request->hasParam(PARAM_WPM)) {
inputMessage = request->getParam(PARAM_WPM)->value();
writeFile(SPIFFS, "/inputWPM.txt", inputMessage.c_str());
yourInputWPM = inputMessage.toFloat();
sender_blink.setWPM(yourInputWPM);
sender_key.setWPM(yourInputWPM);
}
// GET inputMsg value on <ESP_IP>/get?inputMsg=<inputMessage>
if (request->hasParam(PARAM_MSG)) {
inputMessage = request->getParam(PARAM_MSG)->value();
writeFile(SPIFFS, "/inputMsg.txt", inputMessage.c_str());
// save previous state
yourInputMsg_old = yourInputMsg;
yourInputMsg = inputMessage.toInt();
// Check the message every time the form is submitted.
if(yourInputMsg == 0){
sender_blink.setMessage(yourInputCustomMsg);
sender_key.setMessage(yourInputCustomMsg);
} else if(yourInputMsg == 1){
sender_blink.setMessage(String("moe "));
sender_key.setMessage(String("moe "));
} else if(yourInputMsg == 2){
sender_blink.setMessage(String("moi "));
sender_key.setMessage(String("moi "));
} else if(yourInputMsg == 3){
sender_blink.setMessage(String("mos "));
sender_key.setMessage(String("mos "));
} else if(yourInputMsg == 4){
sender_blink.setMessage(String("moh "));
sender_key.setMessage(String("moh "));
} else if(yourInputMsg == 5){
sender_blink.setMessage(String("mo5 "));
sender_key.setMessage(String("mo5 "));
}
}
// GET inputStepLength value on <ESP_IP>/get?inputStepLength=<inputMessage>
if (request->hasParam(PARAM_STEPLENGTH)) {
inputMessage = request->getParam(PARAM_STEPLENGTH)->value();
writeFile(SPIFFS, "/inputStepLength.txt", inputMessage.c_str());
yourInputStepLength = inputMessage.toInt();
}
// GET inputCycleID value on <ESP_IP>/get?inputCycleID=<inputMessage>
if (request->hasParam(PARAM_CYCLEID)) {
inputMessage = request->getParam(PARAM_CYCLEID)->value();
writeFile(SPIFFS, "/inputCycleID.txt", inputMessage.c_str());
yourInputCycleID = inputMessage.toInt();
}
// GET inputNtransmitters value on <ESP_IP>/get?inputNtransmitters=<inputMessage>
if (request->hasParam(PARAM_NTRANS)) {
inputMessage = request->getParam(PARAM_NTRANS)->value();
writeFile(SPIFFS, "/inputNtransmitters.txt", inputMessage.c_str());
yourInputNtransmitters = inputMessage.toInt();
}
// GET inputTimeUnix value on <ESP_IP>/get?inputTimeUnix=<inputMessage>
if (request->hasParam(PARAM_TIME)) {
inputMessage = request->getParam(PARAM_TIME)->value();
//https://stackoverflow.com/a/22733127/2152245
yourInputTime = atol(inputMessage.c_str());
Serial.print("yourInputTime: ");
Serial.println(yourInputTime);
// update the RTC time
rtc.adjust(DateTime(yourInputTime));
DateTime now = rtc.now();
// Might work to fix random errors? If date is far in the future,
// try to update again.
// replace if with while if you want it to try a bunch...
if(now.year() > 2040){
Serial.print("Year is ");
Serial.println(now.year());
Serial.println("RTC can't set time. Trying again.");
rtc.adjust(DateTime(yourInputTime));
}
Serial.print("UTC time from browser: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(now.dayOfTheWeek());
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print("rtc.now().unixtime(): ");
Serial.println(rtc.now().unixtime());
}
// GET inputFloat value on <ESP_IP>/get?inputFloat=<inputMessage>
if (request->hasParam(PARAM_FLOAT)) {
inputMessage = request->getParam(PARAM_FLOAT)->value();
writeFile(SPIFFS, "/inputFloat.txt", inputMessage.c_str());
yourInputFloat = inputMessage.toFloat();
}
// GET inputStartTimeUnix value on <ESP_IP>/get?inputStartTimeUnix=<inputMessage>
if (request->hasParam(PARAM_START)) {
inputMessage = request->getParam(PARAM_START)->value();
Serial.println(inputMessage);
// if a start time isn't entered, don't overwrite the old one
//if(!(inputMessage != NULL && inputMessage[0] == '\0')){
writeFile(SPIFFS, "/inputStartTimeUnix.txt", inputMessage.c_str());
yourInputStartTimeUnix = atol(inputMessage.c_str());
//}
Serial.println(yourInputStartTimeUnix);
// Use alarm built into RTC
rtc.setAlarm1(DateTime(yourInputStartTimeUnix), DS3231_A1_Date);
//rtc.setAlarm1(DateTime(2020, 6, 25, 15, 34, 0), DS3231_A2_Date);
DateTime alarm_one = rtc.getAlarm1(); // Get the current alarm time
char buff[] = "Alarm 1 set for at hh:mm:ss DDD, DD MMM YYYY";
Serial.print(alarm_one.toString(buff));
Serial.println(" (only HH:MM:SS day-of-month are accurate)");
}
// https://techtutorialsx.com/2018/01/14/esp32-arduino-http-server-external-and-internal-redirects/
request->redirect("/");
});
// Form 2
server.on("/get2", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
/// GET inputNetwork value on <ESP_IP>/get2?inputNetwork=<inputMessage>
if (request->hasParam(PARAM_NETWORK)) {
inputMessage = request->getParam(PARAM_NETWORK)->value();
writeFile(SPIFFS, "/inputNetwork.txt", inputMessage.c_str());
yourInputNetwork = inputMessage.toInt();
Serial.println(yourInputNetwork);
}
/// GET inputSSID value on <ESP_IP>/get2?inputSSID=<inputMessage>
if (request->hasParam(PARAM_SSID)) {
inputMessage = request->getParam(PARAM_SSID)->value();
writeFile(SPIFFS, "/inputSSID.txt", inputMessage.c_str());
yourInputSSID = inputMessage;
Serial.println(yourInputSSID);
}
/// GET inputNetwork value on <ESP_IP>/get2?inputNetwork=<inputMessage>
if (request->hasParam(PARAM_PASSWORD)) {
inputMessage = request->getParam(PARAM_PASSWORD)->value();
writeFile(SPIFFS, "/inputPassword.txt", inputMessage.c_str());
yourInputPassword = inputMessage;
Serial.println(yourInputPassword);
}
// Shouldn't need to do this if using this form.
request->redirect("/");
resetFunc(); // reset the Arduino via software function
});
server.onNotFound(notFound);
server.begin();
}
void loop() {
// For testing, print the value returned from the voltage circuit.
// This is not voltage, and needs to be scaled to the battery. See KB1OIQ's solution.
voltage_pin_value = analogRead(voltage_pin);
Serial.println(voltage_pin_value);
// This statement from https://github.com/garrysblog/DS3231-Alarm-With-Adafruit-RTClib-Library/blob/master/DS3231-RTClib-Adafruit-Alarm-Poll-alarmFired/DS3231-RTClib-Adafruit-Alarm-Poll-alarmFired.ino
// Check if alarm by polling SQW alarm pin
if((yourInputSend == 2) & (digitalRead(alarmPin) == LOW)) {
// Print current time and date
DateTime now = rtc.now(); // Get the current time
char buff[] = "Alarm triggered at hh:mm:ss DDD, DD MMM YYYY";
Serial.println(now.toString(buff));
startProgram = true;
// Disable and clear alarm
rtc.clearAlarm(1);
rtc.clearAlarm(2); // clear the other one just in case
}
// Once alarm has started the program, set things up to run
if(startProgram == true){
//Serial.println("Start sending");
start_millis = millis() + ((yourInputCycleID - 1) * yourInputStepLength);
stop_millis = start_millis + yourInputStepLength;
if(yourInputCycleID == 1){
pause_until_millis = stop_millis + (yourInputStepLength * (yourInputNtransmitters - 1));
} else {
// Subtract 2 rather than 1 here to account for start_millis duration at beginning of repeat.
pause_until_millis = stop_millis + (yourInputStepLength * (yourInputNtransmitters - 2));
}
programRunning = true;
startProgram = false;
}
// if you want to send continuous code, and it's not sending, then start it up
if((yourInputSend == 1)){
// If not sending, start sending. Yes, these need to be separate statements.
if (!sender_blink.continueSending()){
sender_blink.startSending();
}
if (!sender_key.continueSending()){
sender_key.startSending();
}
// if you want to send cycle code and it's not sending, then start it up
} else if((yourInputSend == 2) & (programRunning == true)){
if((millis() < start_millis)){
// Shut the pin off manually
digitalWrite(blinker, LOW);
digitalWrite(keyer, LOW);
} else if((millis() >= start_millis) & (millis() <= stop_millis)){
// If not sending, start sending. Yes, these need to be separate statements
// for the blinker and keyer.
if (!sender_blink.continueSending()){
sender_blink.startSending();
}
if (!sender_key.continueSending()){
sender_key.startSending();
}
} else if((millis() >= stop_millis) & (millis() <= pause_until_millis)){
// do nothing in this case -- in between cycles
// Shut the pin off manually
digitalWrite(blinker, LOW);
digitalWrite(keyer, LOW);
} else if((millis() >= pause_until_millis)){
startProgram = true;
}
// if the cycle program is not running
} else if((yourInputSend == 2) & (programRunning == false)){
// do we need something here?
// if you don't want to send code
} else if(yourInputSend == 0){
// Shut the pin off manually
digitalWrite(blinker, LOW);
digitalWrite(keyer, LOW);
}
}
-232
View File
@@ -1,232 +0,0 @@
// Morse Code sending library
#include <morse.h>
// MorseSender
int MorseSender::copyTimings(
morseTiming_t *rawOut,
morseBitmask_t definition)
{
int t = 0;
boolean foundSentinel = false;
for(morseBitmask_t mask = MORSE_BITMASK_HIGH_BIT;
mask > 0; mask = mask >> 1)
{
boolean isDah = (mask & definition) > 0;
if(!foundSentinel)
{
if (isDah) { foundSentinel = true; }
continue;
}
rawOut[2*t] = isDah ? DAH : DIT;
rawOut[2*t + 1] = DIT;
t++;
}
return t;
}
unsigned int MorseSender::fillTimings(char c)
{
int t = 0;
unsigned int start = 0;
if (c >= 'a' && c <= 'z')
{
t = copyTimings(timingBuffer, MORSE_LETTERS[c-'a']);
}
else if (c >= '0' && c <= '9')
{
int n = c - '0';
boolean ditsFirst = (n <= 5);
if (!ditsFirst)
{
n -= 5;
}
while(t < 5)
{
timingBuffer[2*t] = ((t < n) == ditsFirst) ? DIT : DAH;
timingBuffer[2*t + 1] = DIT;
t++;
}
}
else
{
int s = 0;
while(MORSE_PUNCT_ETC[s].c != END)
{
if(MORSE_PUNCT_ETC[s].c == c)
{
t = copyTimings(timingBuffer,
MORSE_PUNCT_ETC[s].timing);
break;
}
s++;
}
if (MORSE_PUNCT_ETC[s].c == END)
{
start = t = 1; // start on a space
}
}
timingBuffer[2*t - 1] = DAH;
timingBuffer[2*t] = END;
/*
Serial.print("Refilled timing buffer for '");
Serial.print(c);
Serial.print("': ");
int i = start;
while(timingBuffer[i] != END)
{
Serial.print((int)timingBuffer[i]);
Serial.print(", ");
i++;
}
Serial.println("END");
*/
return start;
}
// see note in header about pure-virtual-ness
void MorseSender::setOn() {};
void MorseSender::setOff() {};
// noop defaults
void MorseSender::setReady() {};
void MorseSender::setComplete() {};
MorseSender::MorseSender(unsigned int outputPin, float wpm) :
pin(outputPin)
{
setWPM(wpm);
}
void MorseSender::setup() { pinMode(pin, OUTPUT); }
void MorseSender::setWPM(float wpm)
{
setSpeed((morseTiming_t)(1000.0*60.0/(max(1.0f, wpm)*DITS_PER_WORD)));
}
void MorseSender::setSpeed(morseTiming_t duration)
{
DIT = max(duration, (morseTiming_t) 1);
DAH = 3*DIT;
}
void MorseSender::setMessage(const String newMessage)
{
message = newMessage;
// Force startSending() before continueSending().
messageIndex = message.length();
// If a different message was in progress, make sure it stops cleanly.
if (timingIndex % 2 == 0) {
setOff();
}
}
void MorseSender::sendBlocking()
{
//Serial.println("Sending blocking: ");
//Serial.println(message);
startSending();
while(continueSending());
}
void MorseSender::startSending()
{
messageIndex = 0;
if (message.length() == 0) { return; }
timingIndex = fillTimings(message[0]);
setReady();
if (timingIndex % 2 == 0) {
setOn();
//Serial.print("Starting with on, duration=");
} else {
//Serial.print("Starting with off, duration=");
}
lastChangedMillis = millis();
//Serial.println((int)timingBuffer[timingIndex]);
}
boolean MorseSender::continueSending()
{
if(messageIndex >= message.length()) { return false; }
unsigned long elapsedMillis = millis() - lastChangedMillis;
if (elapsedMillis < timingBuffer[timingIndex]) { return true; }
timingIndex++;
if (timingBuffer[timingIndex] == END)
{
messageIndex++;
if(messageIndex >= message.length()) {
setOff();
setComplete();
return false;
}
timingIndex = fillTimings(message[messageIndex]);
}
lastChangedMillis += elapsedMillis;
//Serial.print("Next is ");
if (timingIndex % 2 == 0) {
//Serial.print("(on) ");
setOn();
} else {
//Serial.print("(off) ");
setOff();
}
//Serial.println((int)timingBuffer[timingIndex]);
return true;
}
void *MorseSender::operator new(size_t size) { return malloc(size); }
void MorseSender::operator delete(void* ptr) { if (ptr) free(ptr); }
// SpeakerMorseSender
// void SpeakerMorseSender::setOn() { tone(pin, frequency); }
// void SpeakerMorseSender::setOff() {
// if (carrFrequency == CARRIER_FREQUENCY_NONE) {
// noTone(pin);
// } else {
// tone(pin, carrFrequency);
// }
// }
// void SpeakerMorseSender::setReady() { setOff(); }
// void SpeakerMorseSender::setComplete() { noTone(pin); }
// SpeakerMorseSender::SpeakerMorseSender(
// int outputPin,
// unsigned int toneFrequency,
// unsigned int carrierFrequency,
// float wpm)
// : MorseSender(outputPin, wpm),
// frequency(toneFrequency),
// carrFrequency(carrierFrequency) {};
// LEDMorseSender
void LEDMorseSender::setOn() { digitalWrite(pin, activeLow ? LOW : HIGH); }
void LEDMorseSender::setOff() { digitalWrite(pin, activeLow ? HIGH : LOW); }
LEDMorseSender::LEDMorseSender(int outputPin, bool activeLow, float wpm)
: MorseSender(outputPin, wpm), activeLow(activeLow) {};
LEDMorseSender::LEDMorseSender(int outputPin, float wpm)
: MorseSender(outputPin, wpm), activeLow(false) {};
// PWMMorseSender
void PWMMorseSender::setOn() { analogWrite(pin, brightness); }
void PWMMorseSender::setOff() { analogWrite(pin, 0); }
void PWMMorseSender::setBrightness(byte bright) {
brightness = bright;
}
PWMMorseSender::PWMMorseSender(
int outputPin,
float wpm,
byte bright)
: MorseSender(outputPin, wpm), brightness(bright) {};
-276
View File
@@ -1,276 +0,0 @@
#pragma once
/**
* Generate and send Morse Code on an LED or a speaker. Allow sending
* in a non-blocking manner (by calling a 'continue sending' method
* every so often to turn an LED on/off, or to call tone/noTone appropriately).
*
* All input should be lowercase. Prosigns (SK, KN, etc) have special
* character values #defined.
*
* See also:
* Morse decoder (using binary tree):
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1289074596/15
* Generator (on playground):
* http://www.arduino.cc/playground/Code/Morse
*/
// for malloc and free, for the new/delete operators
#include <stdlib.h>
#include <analogWrite.h>
// Arduino language types
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define WPM_DEFAULT 12.0
// PARIS WPM measurement: 50; CODEX WPM measurement: 60 (Wikipedia:Morse_code)
#define DITS_PER_WORD 50
// Pass to SpeakerMorseSender as carrierFrequency to suppress the carrier.
#define CARRIER_FREQUENCY_NONE 0
// Bitmasks are 1 for dah and 0 for dit, in left-to-right order;
// the sequence proper begins after the first 1 (a sentinel).
// Credit for this scheme to Mark VandeWettering K6HX ( brainwagon.org ).
typedef unsigned int morseTiming_t;
typedef unsigned char morseBitmask_t; // see also MAX_TIMINGS
#define MORSE_BITMASK_HIGH_BIT B10000000
// sentinel
#define END 0
// the most timing numbers any unit will need; ex: k = on,off,on,off,on,end = 5
#define MAX_TIMINGS 15
// Punctuation and Prosigns
#define PROSIGN_SK 'S'
#define PROSIGN_KN 'K'
#define PROSIGN_BT 'B'
typedef struct {
char c;
morseBitmask_t timing;
} specialTiming;
const specialTiming MORSE_PUNCT_ETC[] = {
{'.', B1010101},
{'?', B1001100},
{'/', B110010},
{PROSIGN_SK, B1000101},
{PROSIGN_KN, B110110},
{PROSIGN_BT, B110001},
{END, B1},
};
// Morse Code (explicit declaration of letter timings)
const morseBitmask_t MORSE_LETTERS[26] = {
/* a */ B101,
/* b */ B11000,
/* c */ B11010,
/* d */ B1100,
/* e */ B10,
/* f */ B10010,
/* g */ B1110,
/* h */ B10000,
/* i */ B100,
/* j */ B10111,
/* k */ B1101,
/* l */ B10100,
/* m */ B111,
/* n */ B110,
/* o */ B1111,
/* p */ B10110,
/* q */ B11101,
/* r */ B1010,
/* s */ B1000,
/* t */ B11,
/* u */ B1001,
/* v */ B10001,
/* w */ B1011,
/* x */ B11001,
/* y */ B11011,
/* z */ B11100,
};
/**
* Define the logic of converting characters to on/off timing,
* and encapsulate the state of one sending-in-progress Morse message.
*
* Subclasses define setOn and setOff for (for example) LED and speaker output.
*/
class MorseSender {
protected:
const unsigned int pin;
// The setOn and setOff methods would be pure virtual,
// but that has compiler issues.
// See: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1167672075 .
/**
* Called to set put the output in 'on' state, during a dit or dah.
*/
virtual void setOn();
virtual void setOff();
/**
* Called before sending a message. Used for example to enable a
* carrier. (Noop in the base class.)
*/
virtual void setReady();
virtual void setComplete();
private:
morseTiming_t DIT, DAH;
String message;
// on,off,...,wait,0 list, millis
morseTiming_t timingBuffer[MAX_TIMINGS+1];
// index of the character currently being sent
unsigned int messageIndex;
// timing unit currently being sent
unsigned int timingIndex;
// when this timing unit was started
unsigned long lastChangedMillis;
/**
* Copy definition timings (on only) to raw timings (on/off).
* @return the number of 'on' timings copied
*/
int copyTimings(morseTiming_t *rawOut,
morseBitmask_t definition);
/**
* Fill a buffer with on,off,..,END timings (millis)
* @return the index at which to start within the new timing sequence
*/
unsigned int fillTimings(char c);
public:
/**
* Create a sender which will output to the given pin.
*/
MorseSender(unsigned int outputPin, float wpm=WPM_DEFAULT);
/**
* To be called during the Arduino setup(); set the pin as OUTPUT.
*/
void setup();
/**
* Set the words per minute (based on PARIS timing).
*/
void setWPM(float wpm);
/**
* Set the duration, in milliseconds, of a DIT.
*/
void setSpeed(morseTiming_t duration);
/**
* Set the message to be sent.
* This halts any sending in progress.
*/
void setMessage(const String newMessage);
/**
* Send the entirety of the current message before returning. See the "simple"
* example, which uses sendBlocking to send one message.
*/
void sendBlocking();
/**
* Prepare to send and begin sending the current message. After calling this,
* call continueSending repeatedly until it returns false to finish sending
* the message. See the "speeds" example, which calls startSending and
* continueSending on two different senders.
*/
void startSending();
/**
* Switch outputs on and off (and refill the internal timing buffer)
* as necessary to continue with the sending of the current message.
* This should be called every few milliseconds (at a significantly
* smaller interval than a DIT) to produce a legible fist.
*
* @see startSending, which must be called first
* @return false if sending is complete, otherwise true (keep sending)
*/
boolean continueSending();
void *operator new(size_t size);
void operator delete(void* ptr);
};
/**
* Adapt Morse sending to use the Arduino language tone() and noTone()
* functions, for use with a speaker.
*
* If a carrierFrequency is given, instead of calling noTone, call tone
* with a low frequency. This is useful ex. for maintaining radio links.
*/
class SpeakerMorseSender: public MorseSender {
private:
unsigned int frequency;
unsigned int carrFrequency;
protected:
virtual void setOn();
virtual void setOff();
virtual void setReady();
virtual void setComplete();
public:
// concert A = 440
// middle C = 261.626; higher octaves = 523.251, 1046.502
SpeakerMorseSender(
int outputPin,
unsigned int toneFrequency=1046,
unsigned int carrierFrequency=CARRIER_FREQUENCY_NONE,
float wpm=WPM_DEFAULT);
};
/**
* Sends Morse on a digital output pin.
*/
class LEDMorseSender: public MorseSender {
private:
bool activeLow;
protected:
virtual void setOn();
virtual void setOff();
public:
/**
* Creates a LED Morse code sender with the given GPIO pin. The optional
* boolean activeLow indicates LED is ON with digital LOW value.
* @param outputPin GPIO pin number
* @param activeLow set to true to indicate the LED ON with digital LOW value. default: false
* @param wpm words per minute, default: WPM_DEFAULT
*/
LEDMorseSender(int outputPin, bool activeLow = false, float wpm=WPM_DEFAULT);
/**
* Creates a LED Morse code sender with the given GPIO pin. This constructor is for backward compability.
* @param outputPin GPIO pin number
* @param wpm words per minute
*/
LEDMorseSender(int outputPin, float wpm);
};
/**
* Sends Morse on an analog output pin (using PWM). The brightness value is
* between 0 and 255 and is passed directly to analogWrite.
*/
class PWMMorseSender: public MorseSender {
private:
byte brightness;
protected:
virtual void setOn();
virtual void setOff();
public:
PWMMorseSender(int outputPin, float wpm=WPM_DEFAULT, byte brightness=255);
void setBrightness(byte brightness);
};
+4
View File
@@ -0,0 +1,4 @@
#!/bin/bash
# Run `chmod +x start-aprs_api.sh` so this can be run
screen -dmS aprs_api python3 /home/pi/aprs_tool/api_waitress.py
+59
View File
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{station_call}} Status</title>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<h1>{{station_call}} Status</h1>
{{station_lat}}, {{station_lon}}
<h2> Recent RF Packets </h2>
<table>
<tr>
<th> from </th>
<th> object_name </th>
<th> raw </th>
<th> created (utc) </th>
<th> more </th>
</tr>
{% for i in frames %}
<tr>
<td> <a href="https://digi.w1cdn.net/aprs_api/packets?from={{ i['from'] }}">{{ i['from'] }}</a> </td>
<td> {{ i['object_name'] }} </td>
<td> {{ i['raw'] }} </td>
<td> {{ i['created'] }} </td>
<td> <a href="https://digi.w1cdn.net/aprs_api/packets?id={{ i['id'] }}">query</a>,
<a href="https://aprs.fi/#!mt=roadmap&z=12&call=a%2F{{ i['from'] }}">aprs.fi</a></td>
</tr>
{% endfor %}
</table>
<h2> Recent Stations </h2>
<table>
<tr>
<th> from </th>
<th> last heard (utc) </th>
<th> count </th>
<th> more </th>
</tr>
{% for i in stations %}
<tr>
<td> <a href="https://digi.w1cdn.net/aprs_api/packets?from={{ i['from'] }}">{{ i['from'] }}</a> </td>
<td> {{ i['last_heard'] }} </td>
<td> {{ i['count']}} </td>
<td> <a href="https://aprs.fi/#!mt=roadmap&z=12&call=a%2F{{ i['from'] }}">aprs.fi</a></td>
</tr>
{% endfor %}
</table>
<h2> Help </h2>
This is a work in progress. See <a href="https://amiok.net/gitea/W1CDN/aprs_tool">https://amiok.net/gitea/W1CDN/aprs_tool</a> for usage.
</body>
</html>
-11
View File
@@ -1,11 +0,0 @@
This directory is intended for PlatformIO Test Runner and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html
+36
View File
@@ -0,0 +1,36 @@
# Learn how to update database
import sqlite3
def get_db_connection():
conn = sqlite3.connect('database.db')
conn.row_factory = sqlite3.Row
return conn
conn = get_db_connection()
# Grab a random row from frames table and pretend it is new
cur = conn.cursor()
cur.execute("SELECT [from], id, created_unix FROM frames ORDER BY RANDOM() LIMIT 1;")
rows = cur.fetchall()
results = dict(rows[0])
values = ', '.join('"%s"' % w for w in results.values())
# Build query
# "from" is wrappedin [] because it is a reserved word and using '' doesn't work.
query3 = "INSERT INTO stations ([from], frames_id, last_heard_unix, count) \
VALUES("+values+", 1) \
ON CONFLICT([from]) \
DO UPDATE SET count = count + 1;"
# example https://stackoverflow.com/a/50718957/2152245
# query2 = "INSERT INTO stations ([from], frames_id, last_heard_unix, count) \
# VALUES('KC9TZN-8', 4068, 1687623864, 1) \
# ON CONFLICT([from]) \
# DO UPDATE SET count = count + 1;"
conn.execute(query3)
conn.commit()
conn.close()
-8
View File
@@ -1,8 +0,0 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}