Raspberry Pi ZeroW WiFi Power Management

Every Raspberry Pi Zero W I’ve had has had intermittent connection problems on Wi-Fi. I’ve been able to fix the problems by disabling power management on the Wi-Fi interface each time. This page gave me my preferred solution for taking care of the problem on each machine. I’m duplicating the information here to contribute/prevent webrot.

See the current state of power management:

sudo iw wlan0 get power_save

set power management off:

sudo iw wlan0 set power_save off

Create a systemd unit file to set Wi-Fi power management:

sudo systemctl --full --force edit wifi_powersave@.service

With this as the contents of the unit file:

[Unit]
Description=Set WiFi power save %i
After=sys-subsystem-net-devices-wlan0.device

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/sbin/iw dev wlan0 set power_save %i

[Install]
WantedBy=sys-subsystem-net-devices-wlan0.device

Then enable the unit file, setting power management to off whenever wlan0 is activated.

sudo systemctl disable wifi_powersave@on.service
sudo systemctl enable wifi_powersave@off.service

IPv6 DDWRT and Astound Broadband

My apartment has three options for high speed internet. Centurylink, Xfinity, and Astound Broadband. When I moved in I chose Wave Broadband because I’d had good experience in the past with Condointernet and it later became Wave Broadband. Now they seem to be going with the moniker Astound Broadband powered by Wave. One of the features that I’ve used in the past and am using now is their support of IPv6.

I’ve had occasional hiccups in my networking, most often recently the IPv4 networking stops working while the IPv6 network is still working. It has some interesting effects, such as my browser connected to gmail.com working fine but hitting a link on a web page in an email that goes to an IPv4 only address not working. Usually the problem can be fixed by connecting to my router and having it release its external IPv4 DHCP address and requesting a new lease. Occasionally there are problems on the ISP side of things that make things more problematic. Last week when I was having problems, I managed to mess up my IPv6 configuration and then spent a long time figuring out what the problem was. I’m writing this up mainly as a suggestion for a reliable working configuration.

I’m running DDWRT on a Netgear R7000 router. It’s somewhat strange, as it seems there’s no real release, only betas. It explicitly says go look at the directory structure and “DO NOT USE THE ROUTER DATABASE.” I’ve run it in various incarnations for well over a decade.

DD-WRT Basic Setup

The Basic IPv4 page is very standard, naming my local network and router and picking the IPv4 address range I want to use locally. I’ve generally been using the same address block since they were defined in RFC1918, though that’s been superseded by newer RFC’s. Wikipedia has a good description of private address space.

DD-WRT IPv6 Setup

I’m enabling IPv6 and seelcting DHCPv6 with Prefix Delegation. I’ve left all of the other items on this page either blank or at their defaults.

The way IPv6 hosts generally allocate global addresses in stateless mode is that they recognize router advertisements arriving on the network port and configure their own address based on information received. They then broadcast locally to make sure there are no address collisions. This may be a significant oversimplification, but it’s a useful one.

Initially I had the Router Advertisement Daemon enabled on this page but have since changed to use DNSMASQ for router advertisements since it’s already doing so much of the work on the router. Part of the reason I stopped using radvd was also based on the fact that I was trying to add two static DNS entries for IPv6 in the fields on this page. I was adding the google IPv6 DNS entries of 2001:4860:4860::8888 and 2001:4860:4860::8844. The problem I was running into was that DDWRT was creating a radvd.conf file that included the two static DNS servers I listed while also including the DNS servers inherited from the advertisement it received from my ISP router. Radvd was seeing more than three DNS servers and exiting with an error. This meant that no router advertisements were being broadcast on my local network. I’d been looking at the configuration of the radvd deamon with the command cat /tmp/radvd.conf in my terminal window. I asked questions about this over on the DDWRT forums as well as opening an issue with the radvd software itself. The slightly unhelpful answer to my question told me to use dnsmasq for router advertisements. I’d tried that by adding the enable-ra option but never succeeded in having my machines get global IPv6 addresses.

DD-WRT Services DNSMasq Additional Options

I finally came across a set of options that I needed to get dnsmasq to broadcast router advertisements that are recognized by hosts on my local network to have them configure IPv6 global addresses.

dhcp-range=::1000,::FFFF,constructor:br0,ra-stateless,5m
ra-param=br0,10,300
enable-ra

In the terminal window of DDWRT I can dump the contents of the dnsmasq configuration file with the command cat /tmp/dnsmasq.conf. In doing so, I see what DDWRT writes to the configuration followed by my own customizations. When I do that I see multiple dhcp-range= lines, which initially confused me, but seems to work properly. The first is the IPv4 range and lease time, while the second is the IPv6 information from my “Additional Options” entry.

The tool I’ve used to diagnose my router advertisment issues has been radvdump. It seems to be available on most linux distributions as well as being on ddwrt. When I was running it on my router and the router was not sending out its own advertisments, I’d get messages from my ISP that looked like this:

# radvd configuration generated by radvdump 2.19
# based on Router Advertisement from fe80::22c:c8ff:fe42:24bf
# received by interface vlan2
#

interface vlan2
{
        AdvSendAdvert on;
        # Note: {Min,Max}RtrAdvInterval cannot be obtained with radvdump
        AdvManagedFlag off;
        AdvOtherConfigFlag on;
        AdvReachableTime 0;
        AdvRetransTimer 0;
        AdvCurHopLimit 64;
        AdvDefaultLifetime 1800;
        AdvHomeAgentFlag off;
        AdvDefaultPreference high;
        AdvSourceLLAddress on;
        AdvLinkMTU 1500;

        prefix 2604:4080:1304::/64
        {
                AdvValidLifetime 2592000;
                AdvPreferredLifetime 604800;
                AdvOnLink on;
                AdvAutonomous on;
                AdvRouterAddr off;
        }; # End of prefix definition

}; # End of interface definition

When I was successfully running radvd on ddwrt, I never saw its own advertisements but could see them on other machines on my local network. Now that I’m using dnsmasq for my advertisements, I see messages on the router itself.

# radvd configuration generated by radvdump 2.19
# based on Router Advertisement from fe80::b27f:b9ff:fe83:6591
# received by interface br0
#

interface br0
{
        AdvSendAdvert on;
        # Note: {Min,Max}RtrAdvInterval cannot be obtained with radvdump
        AdvManagedFlag off;
        AdvOtherConfigFlag on;
        AdvReachableTime 0;
        AdvRetransTimer 0;
        AdvCurHopLimit 64;
        AdvDefaultLifetime 300;
        AdvHomeAgentFlag off;
        AdvDefaultPreference medium;
        AdvLinkMTU 1500;
        AdvSourceLLAddress on;

        prefix 2604:4080:1304:8010::/64
        {
                AdvValidLifetime 300;
                AdvPreferredLifetime 300;
                AdvOnLink on;
                AdvAutonomous on;
                AdvRouterAddr off;
        }; # End of prefix definition

        DNSSL wimsworld.com wimsworld.local local
        {
                AdvDNSSLLifetime 300;
        }; # End of DNSSL definition

        RDNSS fe80::b27f:b9ff:fe83:6591
        {
                AdvRDNSSLifetime 300;
        }; # End of RDNSS definition

}; # End of interface definition

I looked at the radvdump output from a friend’s location where he’s on XFinity and IPv6 just seems to be configured and works and this is what I saw. This is from a client machine since he has no access to the Xfinity supplied router itself.

# radvd configuration generated by radvdump 2.17
# based on Router Advertisement from fe80::16c0:3eff:fe4e:400c
# received by interface wlan0
#

interface wlan0
{
        AdvSendAdvert on;
        # Note: {Min,Max}RtrAdvInterval cannot be obtained with radvdump
        AdvManagedFlag on;
        AdvOtherConfigFlag on;
        AdvReachableTime 0;
        AdvRetransTimer 0;
        AdvCurHopLimit 64;
        AdvDefaultLifetime 180;
        AdvHomeAgentFlag off;
        AdvDefaultPreference medium;
        AdvSourceLLAddress on;

        RDNSS 2001:558:feed::1 2001:558:feed::2
        {
                AdvRDNSSLifetime 180;
        }; # End of RDNSS definition


        prefix 2601:603:4c7f:41e0::/64
        {
                AdvValidLifetime 300;
                AdvPreferredLifetime 300;
                AdvOnLink on;
                AdvAutonomous on;
                AdvRouterAddr off;
        }; # End of prefix definition


        route ::/0
        {
                AdvRoutePreference medium;
                AdvRouteLifetime 180;
        }; # End of route definition

}; # End of interface definition

When IPv6 is working properly on my router I have a globally scoped IPv6 address on my vlan2 interface and one on my br0 interface. I can recognize that my vlan2 interface is the outside world because the IPv4 address associated with it is a routable IPv4 address while the IPv4 address associated with br0 is 192.168.0.1.

I learned a lot about how IPv6 allocates addresses during this process, A bunch of the resources I used came from this set of locations.

Sunrise and Sunset in C++

I wanted to calculate sunrise and sunset at my location for a recent project I was working on where I wouldn’t necessarily have access to internet connectivity. I did some quick searching and found a Wikipedia page with equations and thought it would be straightforward to put those equations into code. Somehow, I was not able to get the routines working correctly.

I came across a NOAA page that used equations in a different form, and also had links to an excel spreadsheet I could download and play with. I was able to reverse engineer the spreadsheet into C++ code to get a working routine that calculates the sunrise and sunset to an accuracy that is good enough for my needs.

I’m sharing my code here. If you see anything I’ve done wrong, please let me know. If you find it useful, please let me know about that as well.

/////////////////////////////////////////////////////////////////////////////
// From NOAA Spreadsheet https://gml.noaa.gov/grad/solcalc/calcdetails.html
bool getSunriseSunset(time_t& Sunrise, time_t& Sunset, const time_t& TheTime, const double Latitude, double Longitude)
{
	bool rval = false;
	struct tm LocalTime;
	if (0 != localtime_r(&TheTime, &LocalTime))
	{
		// if we don't have a valid latitude or longitude, declare sunrise to be midnight, and sunset one second before midnight
		if ((Latitude == 0) || (Longitude == 0))
		{
			LocalTime.tm_hour = 0;
			LocalTime.tm_min = 0;
			LocalTime.tm_sec = 0;
			Sunrise = mktime(&LocalTime);
			Sunset = Sunrise + 24*60*60 - 1;
		}
		else
		{
			double JulianDay = Time2JulianDate(TheTime); // F
			double JulianCentury = (JulianDay - 2451545) / 36525;	// G
			double GeomMeanLongSun = fmod(280.46646 + JulianCentury * (36000.76983 + JulianCentury * 0.0003032), 360);	// I
			double GeomMeanAnomSun = 357.52911 + JulianCentury * (35999.05029 - 0.0001537 * JulianCentury);	// J
			double EccentEarthOrbit = 0.016708634 - JulianCentury * (0.000042037 + 0.0000001267 * JulianCentury);	// K
			double SunEqOfCtr = sin(radians(GeomMeanAnomSun)) * (1.914602 - JulianCentury * (0.004817 + 0.000014 * JulianCentury)) + sin(radians(2 * GeomMeanAnomSun)) * (0.019993 - 0.000101 * JulianCentury) + sin(radians(3 * GeomMeanAnomSun)) * 0.000289; // L
			double SunTrueLong = GeomMeanLongSun + SunEqOfCtr;	// M
			double SunAppLong = SunTrueLong - 0.00569 - 0.00478 * sin(radians(125.04 - 1934.136 * JulianCentury));	// P
			double MeanObliqEcliptic = 23 + (26 + ((21.448 - JulianCentury * (46.815 + JulianCentury * (0.00059 - JulianCentury * 0.001813)))) / 60) / 60;	// Q
			double ObliqCorr = MeanObliqEcliptic + 0.00256 * cos(radians(125.04 - 1934.136 * JulianCentury));	// R
			double SunDeclin = degrees(asin(sin(radians(ObliqCorr)) * sin(radians(SunAppLong))));	// T
			double var_y = tan(radians(ObliqCorr / 2)) * tan(radians(ObliqCorr / 2));	// U
			double EquationOfTime = 4 * degrees(var_y * sin(2 * radians(GeomMeanLongSun)) - 2 * EccentEarthOrbit * sin(radians(GeomMeanAnomSun)) + 4 * EccentEarthOrbit * var_y * sin(radians(GeomMeanAnomSun)) * sin(2 * radians(GeomMeanLongSun)) - 0.5 * var_y * var_y * sin(4 * radians(GeomMeanLongSun)) - 1.25 * EccentEarthOrbit * EccentEarthOrbit * sin(2 * radians(GeomMeanAnomSun))); // V
			double HASunriseDeg = degrees(acos(cos(radians(90.833)) / (cos(radians(Latitude)) * cos(radians(SunDeclin))) - tan(radians(Latitude)) * tan(radians(SunDeclin)))); // W
			double SolarNoon = (720 - 4 * Longitude - EquationOfTime + LocalTime.tm_gmtoff / 60) / 1440; // X
			double SunriseTime = SolarNoon - HASunriseDeg * 4 / 1440;	// Y
			double SunsetTime = SolarNoon + HASunriseDeg * 4 / 1440;	// Z
			LocalTime.tm_hour = 0;
			LocalTime.tm_min = 0;
			LocalTime.tm_sec = 0;
			time_t Midnight = mktime(&LocalTime);
			Sunrise = Midnight + SunriseTime * 86400;
			Sunset = Midnight + SunsetTime * 86400;
		}
		rval = true;
	}
	return(rval);
}

I’ve included the individual routines I created when trying to duplicate the Wikipedia equations. If anyone can point out why they didn’t work for me, I’d appreciate that as well.

/////////////////////////////////////////////////////////////////////////////
double radians(const double degrees)
{
	return((degrees * M_PI) / 180.0);
}
double degrees(const double radians)
{
	return((radians * 180.0) / M_PI);
}
double Time2JulianDate(const time_t& TheTime)
{
	double JulianDay = 0;
	struct tm UTC;
	if (0 != gmtime_r(&TheTime, &UTC))
	{
		// https://en.wikipedia.org/wiki/Julian_day
		// JDN = (1461 × (Y + 4800 + (M ? 14)/12))/4 +(367 × (M ? 2 ? 12 × ((M ? 14)/12)))/12 ? (3 × ((Y + 4900 + (M - 14)/12)/100))/4 + D ? 32075
		JulianDay = (1461 * ((UTC.tm_year + 1900) + 4800 + ((UTC.tm_mon + 1) - 14) / 12)) / 4
			+ (367 * ((UTC.tm_mon + 1) - 2 - 12 * (((UTC.tm_mon + 1) - 14) / 12))) / 12
			- (3 * (((UTC.tm_year + 1900) + 4900 + ((UTC.tm_mon + 1) - 14) / 12) / 100)) / 4
			+ (UTC.tm_mday)
			- 32075;
		// JD = JDN + (hour-12)/24 + minute/1440 + second/86400
		double partialday = (static_cast<double>((UTC.tm_hour - 12)) / 24) + (static_cast<double>(UTC.tm_min) / 1440.0) + (static_cast<double>(UTC.tm_sec) / 86400.0);
		JulianDay += partialday;
	}
	return(JulianDay);
}
time_t JulianDate2Time(const double JulianDate)
{
	time_t TheTime = (JulianDate - 2440587.5) * 86400;
	return(TheTime);
}
double JulianDate2JulianDay(const double JulianDate)
{
	double n = JulianDate - 2451545.0 + 0.0008;
	return(n);
}
/////////////////////////////////////////////////////////////////////////////
// These equations all come from https://en.wikipedia.org/wiki/Sunrise_equation
double getMeanSolarTime(const double JulianDay, const double longitude)
{
	// an approximation of mean solar time expressed as a Julian day with the day fraction.
	double MeanSolarTime = JulianDay - (longitude / 360);
	return (MeanSolarTime);
}
double getSolarMeanAnomaly(const double MeanSolarTime)
{
	double SolarMeanAnomaly = fmod(357.5291 + 0.98560028 * MeanSolarTime, 360);
	return(SolarMeanAnomaly);
}
double getEquationOfTheCenter(const double SolarMeanAnomaly)
{
	double EquationOfTheCenter = 1.9148 * sin(radians(SolarMeanAnomaly)) + 0.0200 * sin(radians(2 * SolarMeanAnomaly)) + 0.0003 * sin(radians(3 * SolarMeanAnomaly));
	return(EquationOfTheCenter);
}
double getEclipticLongitude(const double SolarMeanAnomaly, const double EquationOfTheCenter)
{
	double EclipticLongitude = fmod(SolarMeanAnomaly + EquationOfTheCenter + 180 + 102.9372, 360);
	return(EclipticLongitude);
}
double getSolarTransit(const double MeanSolarTime, const double SolarMeanAnomaly, const double EclipticLongitude)
{
	// the Julian date for the local true solar transit (or solar noon).
	double SolarTransit = 2451545.0 + MeanSolarTime + 0.0053 * sin(radians(SolarMeanAnomaly)) - 0.0069 * sin(radians(2 * EclipticLongitude));
	return(SolarTransit);
}
double getDeclinationOfTheSun(const double EclipticLongitude)
{
	double DeclinationOfTheSun = sin(radians(EclipticLongitude)) * sin(radians(23.44));
	return(DeclinationOfTheSun);
}
double getHourAngle(const double Latitude, const double DeclinationOfTheSun)
{
	double HourAngle = (sin(radians(-0.83)) - sin(radians(Latitude)) * sin(radians(DeclinationOfTheSun))) / (cos(radians(Latitude)) * cos(radians(DeclinationOfTheSun)));
	return(HourAngle);
}
double getSunrise(const double SolarTransit, const double HourAngle)
{
	double Sunrise = SolarTransit - (HourAngle / 360);
	return(Sunrise);
}
double getSunset(const double SolarTransit, const double HourAngle)
{
	double Sunset = SolarTransit + (HourAngle / 360);
	return(Sunset);
}

Here’s the code snippet I was using when trying to get the Wikipedia equations working.

double JulianDay = JulianDate2JulianDay(Time2JulianDate(LoopStartTime));
double MeanSolarTime = getMeanSolarTime(JulianDay, Longitude);
double SolarMeanAnomaly = getSolarMeanAnomaly(MeanSolarTime);
double EquationOfTheCenter = getEquationOfTheCenter(SolarMeanAnomaly);
double EclipticLongitude = getEclipticLongitude(SolarMeanAnomaly, EquationOfTheCenter);
double SolarTransit = getSolarTransit(MeanSolarTime, SolarMeanAnomaly, EclipticLongitude);
double DeclinationOfTheSun = getDeclinationOfTheSun(EclipticLongitude);
double HourAngle = getHourAngle(Latitude, DeclinationOfTheSun);
double Sunrise = getSunrise(SolarTransit, HourAngle);
double Sunset = getSunset(SolarTransit, HourAngle);
std::cout.precision(std::numeric_limits<double>::max_digits10);
std::cout << "         Julian Date: " << Time2JulianDate(LoopStartTime) << std::endl;
std::cout << "           Unix Time: " << LoopStartTime << std::endl;
std::cout << "         Julian Date: " << timeToExcelLocal(JulianDate2Time(Time2JulianDate(LoopStartTime))) << std::endl;
std::cout << "            Latitude: " << Latitude << std::endl;
std::cout << "           Longitude: " << Longitude << std::endl;
std::cout << "          Julian Day: " << JulianDay << std::endl;
std::cout << "       MeanSolarTime: " << MeanSolarTime << std::endl;
std::cout << "    SolarMeanAnomaly: " << SolarMeanAnomaly << std::endl;
std::cout << " EquationOfTheCenter: " << EquationOfTheCenter << std::endl;
std::cout << "   EclipticLongitude: " << EclipticLongitude << std::endl;
std::cout << "        SolarTransit: " << timeToExcelLocal(JulianDate2Time(SolarTransit)) << std::endl;
std::cout << " DeclinationOfTheSun: " << DeclinationOfTheSun << std::endl;
std::cout << "           HourAngle: " << HourAngle << std::endl;
std::cout << "             Sunrise: " << timeToExcelLocal(JulianDate2Time(Sunrise)) << std::endl;
std::cout << "              Sunset: " << timeToExcelLocal(JulianDate2Time(Sunset)) << std::endl;