2017-02-26 17:28:58 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
"""
|
|
|
|
Author: JP Meijers
|
|
|
|
Date: 2017-02-26
|
|
|
|
Based on: https://github.com/rayozzie/ttn-resin-gateway-rpi/blob/master/run.sh
|
|
|
|
"""
|
2017-02-26 17:35:46 +00:00
|
|
|
import os
|
2017-02-26 17:28:58 +00:00
|
|
|
import os.path
|
|
|
|
import sys
|
|
|
|
import urllib2
|
|
|
|
import time
|
|
|
|
import uuid
|
2017-02-26 17:58:09 +00:00
|
|
|
import json
|
2017-02-26 17:28:58 +00:00
|
|
|
import subprocess
|
2017-04-06 16:47:50 +00:00
|
|
|
try:
|
|
|
|
import urlparse
|
|
|
|
except:
|
|
|
|
import urllib.parse as urlparse
|
2017-02-26 17:28:58 +00:00
|
|
|
try:
|
2017-03-09 00:21:43 +00:00
|
|
|
import RPi.GPIO as GPIO
|
2017-02-26 17:28:58 +00:00
|
|
|
except RuntimeError:
|
2017-03-09 00:21:43 +00:00
|
|
|
print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")
|
2017-02-26 17:28:58 +00:00
|
|
|
|
|
|
|
GWID_PREFIX="FFFE"
|
|
|
|
|
2017-02-26 17:45:18 +00:00
|
|
|
if not os.path.exists("mp_pkt_fwd"):
|
2017-02-26 17:28:58 +00:00
|
|
|
print ("ERROR: gateway executable not found. Is it built yet?")
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if os.environ.get('HALT') != None:
|
|
|
|
print ("*** HALT asserted - exiting ***")
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
# Show info about the machine we're running on
|
|
|
|
print ("*** Resin Machine Info:")
|
2017-03-09 00:21:43 +00:00
|
|
|
print ("*** Type: "+str(os.environ.get('RESIN_MACHINE_NAME')))
|
|
|
|
print ("*** Arch: "+str(os.environ.get('RESIN_ARCH')))
|
2017-02-26 17:28:58 +00:00
|
|
|
|
|
|
|
if os.environ.get("RESIN_HOST_CONFIG_core_freq")!=None:
|
2017-03-09 00:21:43 +00:00
|
|
|
print ("*** Core freq: "+str(os.environ.get('RESIN_HOST_CONFIG_core_freq')))
|
2017-02-26 17:28:58 +00:00
|
|
|
|
|
|
|
if os.environ.get("RESIN_HOST_CONFIG_dtoverlay")!=None:
|
2017-03-09 00:21:43 +00:00
|
|
|
print ("*** UART mode: "+str(os.environ.get('RESIN_HOST_CONFIG_dtoverlay')))
|
2017-02-26 17:28:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Check if the correct environment variables are set
|
|
|
|
|
|
|
|
print ("*******************")
|
|
|
|
print ("*** Configuration:")
|
|
|
|
print ("*******************")
|
|
|
|
|
|
|
|
if os.environ.get("GW_ID")==None:
|
|
|
|
print ("ERROR: GW_ID required")
|
|
|
|
print ("See https://www.thethingsnetwork.org/docs/gateways/registration.html#via-gateway-connector")
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if os.environ.get("GW_KEY")==None:
|
|
|
|
print ("ERROR: GW_KEY required")
|
|
|
|
print ("See https://www.thethingsnetwork.org/docs/gateways/registration.html#via-gateway-connector")
|
|
|
|
sys.exit(0)
|
|
|
|
|
2017-02-28 00:16:26 +00:00
|
|
|
# The FFFE should be inserted in the middle (so xxxxxxFFFExxxxxx)
|
|
|
|
my_eui = format(uuid.getnode(), '012x')
|
|
|
|
my_eui = my_eui[:6]+GWID_PREFIX+my_eui[6:]
|
2017-02-26 18:41:43 +00:00
|
|
|
my_eui = my_eui.upper()
|
2017-02-26 17:28:58 +00:00
|
|
|
|
|
|
|
print ("Gateway ID:\t"+os.environ.get("GW_ID"))
|
|
|
|
print ("Gateway EUI:\t"+my_eui)
|
|
|
|
print ("Gateway Key:\t"+os.environ.get("GW_KEY"))
|
2017-02-26 17:58:09 +00:00
|
|
|
print ("Has hardware GPS:\t"+str(os.getenv('GW_GPS', False)))
|
|
|
|
print ("Hardware GPS port:\t"+os.getenv('GW_GPS_PORT', "/dev/ttyAMA0"))
|
2017-02-26 17:28:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
print ("*******************")
|
|
|
|
print ("*** Fetching config from TTN account server")
|
|
|
|
print ("*******************")
|
|
|
|
|
|
|
|
# Define default configs
|
2017-06-13 15:58:30 +00:00
|
|
|
description = os.getenv('GW_DESCRIPTION', "")
|
2017-02-26 17:28:58 +00:00
|
|
|
placement = ""
|
|
|
|
latitude = os.getenv('GW_REF_LATITUDE', 0)
|
|
|
|
longitude = os.getenv('GW_REF_LONGITUDE', 0)
|
|
|
|
altitude = os.getenv('GW_REF_ALTITUDE', 0)
|
|
|
|
frequency_plan_url = "https://account.thethingsnetwork.org/api/v2/frequency-plans/EU_863_870"
|
|
|
|
|
|
|
|
# Fetch config from TTN if TTN is enabled
|
|
|
|
if(os.getenv('SERVER_TTN', True)):
|
|
|
|
# Fetch the URL, if it fails try 30 seconds later again.
|
|
|
|
config_response = ""
|
|
|
|
try:
|
|
|
|
req = urllib2.Request('https://account.thethingsnetwork.org/gateways/'+os.environ.get("GW_ID"))
|
|
|
|
req.add_header('Authorization', 'Key '+os.environ.get("GW_KEY"))
|
|
|
|
response = urllib2.urlopen(req, timeout=30)
|
|
|
|
config_response = response.read()
|
|
|
|
except urllib2.URLError as err:
|
2017-03-09 00:33:25 +00:00
|
|
|
print ("Unable to fetch configuration from TTN. Are your GW_ID and GW_KEY correct?")
|
2017-02-26 17:28:58 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
# Parse config
|
|
|
|
ttn_config = {}
|
|
|
|
try:
|
|
|
|
ttn_config = json.loads(config_response)
|
|
|
|
except:
|
|
|
|
print ("Unable to parse configuration from TTN")
|
|
|
|
sys.exit(0)
|
|
|
|
|
2017-03-09 00:25:17 +00:00
|
|
|
frequency_plan = ttn_config.get('frequency_plan', "EU_863_870")
|
|
|
|
frequency_plan_url = ttn_config.get('frequency_plan_url', "https://account.thethingsnetwork.org/api/v2/frequency-plans/EU_863_870")
|
|
|
|
|
|
|
|
if "router" in ttn_config:
|
2017-04-06 16:47:50 +00:00
|
|
|
router = ttn_config['router'].get('mqtt_address', "mqtt://router.dev.thethings.network:1883")
|
|
|
|
router = urlparse.urlparse(router)
|
|
|
|
router = router.hostname # mp_pkt_fwd only wants the hostname, not the protocol and port
|
2017-03-09 00:25:17 +00:00
|
|
|
else:
|
|
|
|
router = "router.dev.thethings.network"
|
2017-02-26 17:28:58 +00:00
|
|
|
|
|
|
|
if "attributes" in ttn_config:
|
|
|
|
description = ttn_config['attributes'].get('description', "")
|
|
|
|
placement = ttn_config['attributes'].get('placement', "unknown")
|
|
|
|
|
2017-03-09 16:21:25 +00:00
|
|
|
if "antenna_location" in ttn_config:
|
|
|
|
latitude = ttn_config['antenna_location'].get('latitude', 0)
|
|
|
|
longitude = ttn_config['antenna_location'].get('longitude', 0)
|
|
|
|
altitude = ttn_config['antenna_location'].get('altitude', 0)
|
2017-02-26 17:28:58 +00:00
|
|
|
|
2017-03-09 16:21:25 +00:00
|
|
|
fallback_routers = []
|
|
|
|
if "fallback_routers" in ttn_config:
|
|
|
|
for fb_router in ttn_config["fallback_routers"]:
|
|
|
|
if "mqtt_address" in fb_router:
|
|
|
|
fallback_routers.append(fb_router["mqtt_address"])
|
|
|
|
|
|
|
|
|
|
|
|
print ("Router:\t\t\t"+router)
|
|
|
|
print ("Frequency plan:\t\t"+frequency_plan)
|
2017-02-26 17:28:58 +00:00
|
|
|
print ("Frequency plan url:\t"+frequency_plan_url)
|
|
|
|
print ("Gateway description:\t"+description)
|
|
|
|
print ("Gateway placement:\t"+placement)
|
2017-03-09 16:21:25 +00:00
|
|
|
print ("Latitude:\t\t"+str(latitude))
|
|
|
|
print ("Longitude:\t\t"+str(longitude))
|
|
|
|
print ("Altitude:\t\t"+str(altitude))
|
|
|
|
print ("")
|
|
|
|
print ("Fallback routers:")
|
|
|
|
for fb_router in fallback_routers:
|
|
|
|
print ("\t"+fb_router)
|
2017-02-26 17:28:58 +00:00
|
|
|
# Done fetching config from TTN
|
|
|
|
|
|
|
|
|
|
|
|
# Retrieve global_conf
|
2017-06-13 16:45:24 +00:00
|
|
|
sx1301_conf = {}
|
2017-02-26 17:28:58 +00:00
|
|
|
try:
|
|
|
|
response = urllib2.urlopen(frequency_plan_url, timeout=30)
|
|
|
|
global_conf = response.read()
|
2017-06-13 16:45:24 +00:00
|
|
|
global_conf_object = json.loads(global_conf)
|
|
|
|
if('SX1301_conf' in global_conf_object):
|
|
|
|
sx1301_conf = global_conf_object['SX1301_conf']
|
2017-02-26 17:28:58 +00:00
|
|
|
except urllib2.URLError as err:
|
|
|
|
print ("Unable to fetch global conf from Github")
|
|
|
|
sys.exit(0)
|
|
|
|
|
2017-06-13 16:45:24 +00:00
|
|
|
sx1301_conf['antenna_gain'] = float(os.getenv('GW_ANTENNA_GAIN', 0))
|
2017-02-26 17:28:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Build local_conf
|
|
|
|
gateway_conf = {}
|
|
|
|
gateway_conf['gateway_ID'] = my_eui
|
|
|
|
gateway_conf['contact_email'] = os.getenv('GW_CONTACT_EMAIL', "")
|
|
|
|
gateway_conf['description'] = description
|
2017-06-13 15:58:30 +00:00
|
|
|
|
|
|
|
if(os.getenv('GW_FWD_CRC_ERR', False)=="true"):
|
|
|
|
#default is False
|
|
|
|
gateway_conf['forward_crc_error'] = True
|
|
|
|
|
|
|
|
if(os.getenv('GW_FWD_CRC_VAL', True)=="false"):
|
|
|
|
#default is True
|
|
|
|
gateway_conf['forward_crc_valid'] = False
|
|
|
|
|
|
|
|
# Parse GW_GPS env var. It is a string, we need a boolean.
|
|
|
|
gw_gps = os.getenv('GW_GPS', False)
|
|
|
|
if(gw_gps=="true"):
|
|
|
|
gw_gps = True
|
|
|
|
else:
|
|
|
|
gw_gps = False
|
2017-02-26 17:28:58 +00:00
|
|
|
|
|
|
|
# Use hardware GPS
|
2017-06-13 15:58:30 +00:00
|
|
|
if(gw_gps):
|
2017-02-26 18:41:43 +00:00
|
|
|
print ("Using real GPS")
|
2017-02-26 17:28:58 +00:00
|
|
|
gateway_conf['gps'] = True
|
|
|
|
gateway_conf['fake_gps'] = False
|
|
|
|
gateway_conf['gps_tty_path'] = os.getenv('GW_GPS_PORT', "/dev/ttyAMA0")
|
|
|
|
# Use fake GPS with coordinates from TTN
|
2017-06-13 15:58:30 +00:00
|
|
|
elif(gw_gps==False and latitude!=0 and longitude!=0):
|
2017-02-26 18:41:43 +00:00
|
|
|
print ("Using fake GPS")
|
2017-02-26 17:28:58 +00:00
|
|
|
gateway_conf['gps'] = True
|
|
|
|
gateway_conf['fake_gps'] = True
|
2017-06-13 15:58:30 +00:00
|
|
|
gateway_conf['ref_latitude'] = float(latitude)
|
|
|
|
gateway_conf['ref_longitude'] = float(longitude)
|
|
|
|
gateway_conf['ref_altitude'] = float(altitude)
|
2017-02-26 17:28:58 +00:00
|
|
|
# No GPS coordinates
|
|
|
|
else:
|
2017-02-26 18:41:43 +00:00
|
|
|
print ("Not sending coordinates")
|
2017-02-26 17:28:58 +00:00
|
|
|
gateway_conf['gps'] = False
|
|
|
|
gateway_conf['fake_gps'] = False
|
|
|
|
|
|
|
|
|
|
|
|
# Add server configuration
|
|
|
|
gateway_conf['servers'] = []
|
|
|
|
|
|
|
|
# Add TTN server
|
2017-06-13 15:58:30 +00:00
|
|
|
if(os.getenv('SERVER_TTN', True)!="false"):
|
2017-02-26 17:28:58 +00:00
|
|
|
server = {}
|
|
|
|
server['serv_type'] = "ttn"
|
|
|
|
server['server_address'] = router
|
2017-03-09 16:21:25 +00:00
|
|
|
server['server_fallbacks'] = fallback_routers
|
2017-02-26 17:28:58 +00:00
|
|
|
server['serv_gw_id'] = os.environ.get("GW_ID")
|
|
|
|
server['serv_gw_key'] = os.environ.get("GW_KEY")
|
|
|
|
server['serv_enabled'] = True
|
|
|
|
gateway_conf['servers'].append(server)
|
|
|
|
|
|
|
|
# Add up to 3 additional servers
|
2017-06-13 15:58:30 +00:00
|
|
|
if(os.getenv('SERVER_1_ENABLED', "false")=="true"):
|
2017-02-26 17:28:58 +00:00
|
|
|
server = {}
|
|
|
|
if(os.getenv('SERVER_1_TYPE', "semtech")=="ttn"):
|
|
|
|
server['serv_type'] = "ttn"
|
2017-05-29 13:14:02 +00:00
|
|
|
server['serv_gw_id'] = os.environ.get("SERVER_1_GWID")
|
|
|
|
server['serv_gw_key'] = os.environ.get("SERVER_1_GWKEY")
|
2017-02-26 17:28:58 +00:00
|
|
|
server['server_address'] = os.environ.get("SERVER_1_ADDRESS")
|
2017-06-13 17:14:47 +00:00
|
|
|
server['serv_port_up'] = int(os.getenv("SERVER_1_PORTUP", 1700))
|
|
|
|
server['serv_port_down'] = int(os.getenv("SERVER_1_PORTDOWN", 1700))
|
2017-06-13 15:58:30 +00:00
|
|
|
server['serv_enabled'] = True
|
2017-02-26 19:28:09 +00:00
|
|
|
if(os.getenv('SERVER_1_DOWNLINK', "false")=="true"):
|
|
|
|
server['serv_down_enabled'] = True
|
|
|
|
else:
|
|
|
|
server['serv_down_enabled'] = False
|
2017-02-26 17:28:58 +00:00
|
|
|
gateway_conf['servers'].append(server)
|
|
|
|
|
2017-02-26 18:41:43 +00:00
|
|
|
if(os.getenv('SERVER_2_ENABLED', False)):
|
2017-02-26 17:28:58 +00:00
|
|
|
server = {}
|
2017-02-26 19:28:09 +00:00
|
|
|
if(os.getenv('SERVER_2_TYPE', "semtech")=="ttn"):
|
2017-02-26 17:28:58 +00:00
|
|
|
server['serv_type'] = "ttn"
|
2017-06-13 15:58:30 +00:00
|
|
|
server['serv_gw_id'] = os.environ.get("SERVER_2_GWID")
|
|
|
|
server['serv_gw_key'] = os.environ.get("SERVER_2_GWKEY")
|
2017-02-26 19:28:09 +00:00
|
|
|
server['server_address'] = os.environ.get("SERVER_2_ADDRESS")
|
2017-06-13 17:14:47 +00:00
|
|
|
server['serv_port_up'] = int(os.getenv("SERVER_2_PORTUP", 1700))
|
|
|
|
server['serv_port_down'] = int(os.getenv("SERVER_2_PORTDOWN", 1700))
|
2017-06-13 15:58:30 +00:00
|
|
|
server['serv_enabled'] = True
|
2017-02-26 19:28:09 +00:00
|
|
|
if(os.getenv('SERVER_2_DOWNLINK', "false")=="true"):
|
|
|
|
server['serv_down_enabled'] = True
|
|
|
|
else:
|
|
|
|
server['serv_down_enabled'] = False
|
2017-02-26 17:28:58 +00:00
|
|
|
gateway_conf['servers'].append(server)
|
|
|
|
|
2017-02-26 18:41:43 +00:00
|
|
|
if(os.getenv('SERVER_3_ENABLED', False)):
|
2017-02-26 17:28:58 +00:00
|
|
|
server = {}
|
2017-02-26 19:28:09 +00:00
|
|
|
if(os.getenv('SERVER_3_TYPE', "semtech")=="ttn"):
|
2017-02-26 17:28:58 +00:00
|
|
|
server['serv_type'] = "ttn"
|
2017-06-13 15:58:30 +00:00
|
|
|
server['serv_gw_id'] = os.environ.get("SERVER_3_GWID")
|
|
|
|
server['serv_gw_key'] = os.environ.get("SERVER_3_GWKEY")
|
2017-02-26 19:28:09 +00:00
|
|
|
server['server_address'] = os.environ.get("SERVER_3_ADDRESS")
|
2017-06-13 17:14:47 +00:00
|
|
|
server['serv_port_up'] = int(os.getenv("SERVER_3_PORTUP", 1700))
|
|
|
|
server['serv_port_down'] = int(os.getenv("SERVER_3_PORTDOWN", 1700))
|
2017-06-13 15:58:30 +00:00
|
|
|
server['serv_enabled'] = True
|
2017-02-26 19:28:09 +00:00
|
|
|
if(os.getenv('SERVER_3_DOWNLINK', "false")=="true"):
|
|
|
|
server['serv_down_enabled'] = True
|
|
|
|
else:
|
|
|
|
server['serv_down_enabled'] = False
|
2017-02-26 17:28:58 +00:00
|
|
|
gateway_conf['servers'].append(server)
|
|
|
|
|
|
|
|
|
2017-06-13 16:45:24 +00:00
|
|
|
# We merge the json objects from the global_conf and local_conf and save it to the global_conf.
|
|
|
|
# Therefore there will not be a local_conf.json file.
|
|
|
|
local_conf = {'SX1301_conf': sx1301_conf, 'gateway_conf': gateway_conf}
|
|
|
|
with open('global_conf.json', 'w') as the_file:
|
2017-02-26 18:41:43 +00:00
|
|
|
the_file.write(json.dumps(local_conf, indent=4))
|
2017-02-26 17:28:58 +00:00
|
|
|
|
2017-06-13 15:58:30 +00:00
|
|
|
|
|
|
|
# TODO: Cayenne monitoring script
|
|
|
|
|
|
|
|
|
2017-02-26 17:28:58 +00:00
|
|
|
# Endless loop to reset and restart packet forwarder
|
|
|
|
while True:
|
2017-03-04 23:17:50 +00:00
|
|
|
# Reset the gateway board - this only works for the Raspberry Pi.
|
|
|
|
GPIO.setmode(GPIO.BOARD) # hardware pin numbers, just like gpio -1
|
|
|
|
|
2017-03-05 00:04:25 +00:00
|
|
|
if (os.environ.get("GW_RESET_PIN")!=None):
|
2017-03-04 23:17:50 +00:00
|
|
|
try:
|
|
|
|
pin_number = int(os.environ.get("GW_RESET_PIN"))
|
2017-03-09 16:58:16 +00:00
|
|
|
print ("[TTN Gateway]: Resetting concentrator on pin "+str(os.environ.get("GW_RESET_PIN")))
|
2017-03-04 23:17:50 +00:00
|
|
|
GPIO.setup(pin_number, GPIO.OUT, initial=GPIO.LOW)
|
|
|
|
GPIO.output(pin_number, 0)
|
|
|
|
time.sleep(0.1)
|
|
|
|
GPIO.output(pin_number, 1)
|
|
|
|
time.sleep(0.1)
|
|
|
|
GPIO.output(pin_number, 0)
|
|
|
|
time.sleep(0.1)
|
|
|
|
GPIO.input(pin_number)
|
|
|
|
GPIO.cleanup(pin_number)
|
|
|
|
except ValueError:
|
|
|
|
print ("Can't interpret "+os.environ.get("GW_RESET_PIN")+" as a valid pin number.")
|
2017-02-26 17:28:58 +00:00
|
|
|
|
2017-03-04 23:17:50 +00:00
|
|
|
else:
|
|
|
|
print ("[TTN Gateway]: Resetting concentrator on default pin 22.")
|
2017-02-26 17:28:58 +00:00
|
|
|
GPIO.setup(22, GPIO.OUT, initial=GPIO.LOW)
|
|
|
|
GPIO.output(22, 0)
|
|
|
|
time.sleep(0.1)
|
|
|
|
GPIO.output(22, 1)
|
|
|
|
time.sleep(0.1)
|
|
|
|
GPIO.output(22, 0)
|
|
|
|
time.sleep(0.1)
|
2017-03-04 23:17:50 +00:00
|
|
|
GPIO.input(22)
|
2017-02-26 18:41:43 +00:00
|
|
|
GPIO.cleanup(22)
|
2017-02-26 17:28:58 +00:00
|
|
|
|
|
|
|
# Start forwarder
|
|
|
|
subprocess.call(["./mp_pkt_fwd"])
|
2017-02-28 00:16:26 +00:00
|
|
|
time.sleep(15)
|