1 Introduction

Wireless sensor networks (WSN) are composed of wireless embedded devices, called sensor nodes, capable to monitor in real-time different environmental phenomena through transducers and to forward the measured data to a special node called gateway or sink, where data are collected for further processing.

A sensor node is typically powered by limited-capacity batteries. The batteries feed the sensor node circuitry by providing the current draw required to sustain the operation of its components. A sensor node integrates several physical components (e.g. radio, transducers, microcontroller, and flash memory), each of which may operate in different states of energy. The sensor node energy consumption is then computed as the sum of the individual consumptions of each component at each state; hence, the sensor node lifetime is the time taken to discharge its battery below the minimum charge level that is required to sustain the sensor nodes operation.

To provide potentially infinite lifetime, recent studies have investigated the use of energy harvesting to power on the sensor nodes [19]. Unfortunately, in realistic scenarios, energy harvesting is generally intermittent and insufficient to constantly fulfill the needs of the sensor nodes. For this reason, the batteries are still necessary: they are recharged when the energy-harvesting system produces an excess of energy and they sustain the sensor node when the energy production of the energy harvesting is not sufficient. However, it could even happen that the battery level is too low and the energy-harvesting system is not able to produce enough energy to recharge the battery, which results in (temporary) periods of sensor node unavailability. To reduce these periods (and possibly to avoid them), the energy harvesting and battery recharge cycles need to be managed appropriately.

In this paper, we consider WSN for outdoor monitoring applications, where the sensor nodes are exposed to different sources of energy found naturally in the environment, such as the sun, vibration, or acoustic noise. Under this assumption, this work brings together the research areas of green computing and quality of service (QoS) of WSN applications. To achieve this, we define an energy-harvesting system that exploits the energy obtained from the solar light to sustain the sensor node operation and charge its batteries. The main contributions of this paper are as follows: (1) a model for the energy harvesting by means of solar cells; (2) a model that allows to express the QoS of the WSN applications from the user perspective; and (3) an efficient algorithm that optimizes the QoS of the WSN applications accordingly to the estimated energy production from the energy-harvesting system. The algorithm has a low complexity so that it can be executed on low-power sensor nodes. Its goal is to maintain the level of service required by the user as long as possible while the energy production allows it. On the other hand, when the estimated energy production is insufficient to maintain the sensor node operations at the current level, the system progressively degrades the sensor node operation level up to the idle state, that is to say, no operation.

We stress that our view of QoS in WSNs is rather different than the usual view of QoS in the WSN literature. As the primary goal of a sensor node is to continuously monitor the environment, we observe that this activity can be achieved in different ways corresponding to different quality levels. For example, the sensor node may use different sampling rates where, generally speaking, a higher sampling rate implies higher energy cost and better quality of the monitoring activity, it may monitor the environment by using different sets of transducers with different energy costs and different resolutions, and/or it may use different communication patterns (for example, it may transmit each sample in real time or it may group samples to reduce the communications but affecting the delay with which the user receives the samples). We model the different ways of achieving the monitoring goal as scheduling plans of tasks, where each scheduling plan corresponds to a QoS and to an energy cost, and we propose a model in which a sensor node can select, at discrete times, the scheduling plans to be executed during its lifetime, depending on the residual battery power and the expected energy production.

The rest of this paper is organized as follows: after reviewing the related work in Sect. 2, we present in Sect. 3 the design of our energy-harvesting system based on solar cells. Section 4 provides the energy production model from the solar cell and presents a realistic battery discharge and charge model. Section 5 describes the tasks execution model and battery charge. In Sect. 6, we describe our approach to efficiently adapt the QoS level of the WSN application on the basis of the solar energy to be produced by the solar cell, and in Sect. 7, we provide the simulation results. Finally, Sect. 8 discusses the conclusions and further research.

2 Related work

Green computing is a novel general term referring to the capabilities of the Information Technologies (IT) to minimize their negative impact on the environment, which is generally achieved by reducing the CO 2 emissions in the manufacturing processes as well as their energy consumption [10, 18]. In contrast, since in the WSN field, energy is one of the most limited resources, it must be efficiently managed in each architectural layer involved in the process of WSN application building, from the hardware to the application itself.

Dynamic Power Management (DPM) [3] is a technique aimed at energy saving, which consists in switching off the components of a sensor node when not necessary and in waking them up on demand. The probabilistic energy model proposed in [24] allows to take decisions on when to turn on and off the devices on the basis of event statistics. Sensor node components have different energy states, and the events with a consumption above a certain threshold allow to transit from one state to another. While the model takes into account the energy overhead due to the components’ state switching, it does not rely on a realistic battery discharge model as proposed in [22] and further extended in [21]. In particular, in [22], the authors use the Rakhmatov–Vrudhula battery model instead of a linear discharge model (which is the default) and prolong the battery lifetime by scheduling the transitions to a sleep state after packet transmissions and by taking advantage of the recovery battery effect. To the best of our knowledge, neither these works nor other DPM methods focus on the improvement of the QoS of a WSN application while preserving energy or use a realistic charge and discharge model which is derived from an energy-harvesting system.

There exist many works in the literature that deal with QoS in WSN [5, 26]. Most of these works focuses on QoS at network or at MAC layers: they aim at defining routing and MAC protocols able to meet the end-to-end QoS requirements while taking into account energy efficiency. A QoS-aware routing protocol not only has to find a route between a source and a destination but also must find the better route according to a QoS metric. Among these metrics, latency is one of the most used [1, 9, 11]. SPEED [11] and its evolution MMSPEED [9] provide soft real-time delivery guarantee by re-routing packets toward less loaded links. In [1], the problem of finding the optimal path is addressed for WSN real-time applications that use imaging and video sensors. This work aims at finding the lesser delay-constrained path and provides best effort services for throughput. The guarantee of end-to-end bandwidth in wireless ad-hoc networks is the focus of other existing works. In [28], the authors provide an algorithm to distributively compute QoS routes with reserved bandwidth in TDMA networks. Similarly, in [15], the problem is addressed for wireless multimedia ad-hoc networks. At the MAC layer, a few protocols address the QoS by providing services differentiation based on static or dynamic criteria and by providing a specific-application QoS support. A survey of these protocols is presented in [27].

At the application level, a different approach consists in defining an application-specific QoS metric such as coverage, exposure, deployment, or reliability. In [12], QoS is defined as the optimum number of sensor nodes that should send data. In this sense, the sampling rate may also be considered a QoS metric. There exist practical scenarios in which the power consumption due to the sensing activity could be greater than those due to the communication activity. The survey in [2] discusses the strategies that adapt the sampling rates to the real dynamics of the process in order to make an efficient power management in WSN with energy-hungry sensors, and it defines a taxonomy of adaptive sensing strategies: hierarchical sensing, model-based active sensing, and adaptive sampling. The idea behind the hierarchical sensing is that within a node may coexist several transducers able to monitor the same event, where each one has a different accuracy and, in turn, a different energy consumption. Each strategy must dynamically select the transducer that better performs by trading off energy saving and accuracy. Model-based active sensing consists in predicting the next sample of data given a forecasting model of the sensed phenomenon, which leads to a reduction in the sampling rate. Obviously, the effectiveness of this approach depends on the accuracy of the model.

Adaptive sampling techniques adjust the sensor node sampling rate based on the spatial/temporal correlation of the data and, additionally, on estimations of the energy to be harvested from the environment. Among the forms of energy harvested from the environment, the power produced from the sun can be predicted with reasonable accuracy [4]. This is the case of [13], where a mathematical model is defined to express the energy-neutrality condition, which means that the energy consumed by the sensor node in a period of time is always less or equal than the energy harvested from the environment in the same period of time. Based on this concept, the authors propose an algorithm to find the maximum sampling rate of the sensor nodes that keeps the constraint of energy neutrality. The algorithm proceeds by assigning to each time slot a sampling rate. Another example is [17] that considers alternative tasks with discrete service levels and proposes a dynamic programming algorithm that selects the task to be executed for each time slot in a given time horizon.

Our proposal is framed within adaptive sampling techniques that use an energy-harvesting system to recharge the batteries of the sensor node. In our work, we formalize the QoS as a measure of quality level of the tasks executed by the sensor nodes, and we propose an algorithm aimed at optimizing the QoS while maintaining the system energy-neutral in each considered period of time. For this purpose, we dynamically schedule the tasks that will be executed in the time, on the basis of the estimated energy production of the solar cell and of the tasks’ energy costs and quality level. Differently than the above works (except [13] and [17]), we discretize the time axis into slots and, at the end of each one, we take the decision of re-optimizing the scheduling for all slots in the considered period of time. With respect to [13], we do not only consider the adaptation of the duty cycle, but also consider different scheduling plans that may use different duty cycles, transducers, and/or communication patterns. Differently than [17], we consider the constraint of energy neutrality and we focus on a low complexity algorithm that can be easily embedded in low-power sensor nodes. In a preliminary work [8], we addressed a similar problem which only considers the optimization of the sampling rate.

3 Energy-harvesting system

We have designed an energy-harvesting system that produces energy from the sunlight. The system is designed to be robust, and it includes ultracapacitors and rechargeable batteries that power the sensor node in conditions of darkness or deficient sunlight. We establish three priorities of use: by default, the sensor node is powered directly from the solar cell, second from the ultracapacitors connected in parallel to the cell, and third from the two rechargeable batteries. Based on this design, we present the requirements of the solar cell and the sensor node.

Solar cells are composed of wafers, in the order of micrometers thick, made of some semiconductor material at crystalline state, typically silicon, with the property of converting the energy of the photons in electricity by the photovoltaic effect. The efficiency of a solar cell (η) is a measure of the energy conversion efficiency of the incident light absorbed by a solar cell into electric power, computed as \(\eta = \frac{P_{\rm max}}{D \times S}\), where P max is the maximum output power provided by the cell (in Watts), D is the irradiance, defined as the density of incident power on a surface under standard assumptions (in W/m2), and S is the surface area of the solar cell (in m2). For this work, we selected the MSX-005F solar cell [6] manufactured by BP-SOLAR, a polycrystalline cell that provides a maximum current of 150 mA. Table 1 summarizes the specifications of this cell.

Table 1 Specifications of MSX-005F solar cell

The solar cell efficiency depends on the amount of light that it is able to absorb. In turn, this depends on intrinsic factors of the region where it is located: its geographic coordinates, the solar time, its inclination with respect to the sun rays (which should be close to 90 degrees to obtain the maximum efficiency), etc. Consider, for example, the sun irradiance in Spain, where the system was deployed for some tests. For each Spanish province, given its latitude and longitude, we obtained the average irradiance in each month of the year from RetScreen NASA program [16]. To ensure that the system works in any location, we took the lowest irradiance, which corresponds to the Lugo province (in the North West) during December. Hence, in this case, the efficiency of the solar cell is η = 11.38 %.

To validate our system, we configure our simulation by considering the energy consumption parameters of MicaZ [7], a well-known platform for sensor networks applications. Note, however, that any other platform with similar energy requirements can be used instead. MicaZ is composed of a 7 Mhz μ controller, a Zigbee-compliant CC2420 radio, 3 LEDs, and an external flash memory. To provide capacity of sensing, it may be integrated with different sensor boards. MicaZ is powered by 2-AA batteries, which should provide a voltage between 2.7 and 3.3 for the correct working of the sensor node. According to the documentation, the energy requirements of MicaZ are shown in Table 2. The power demand of MicaZ depends on the components that need to be on in the application and in which mode. In the worst case, if we take into account the maximum current draw of each component (whose sum is 64.7 mA according to Table 2), the power required by MicaZ is 64.7 × 3.3 = 213.5 mW. However, it is typically sufficient to guarantee 100 mW to power the device.

Table 2 MicaZ energy requirements

Knowing the energy requirements of all components and the I ld provided by the cell, we estimate that nearly 100 mA are needed to charge the batteries of the sensor node. With this current and assuming an initial capacity of the batteries of 2,500 mAh, the system takes about 25 h to charge the batteries. The discharge time in the worst case is \(\frac{2,500 \times 1.5 \times 2}{64.7 \times 3.3}=35.12\) h.

4 Energy production model

We present in this section a realistic battery charge and discharge model which is derived from the energy-harvesting system presented in Sect. 3. For simplicity, we assume that the solar cell is used to directly charge the batteries of the sensor node. Thus, we need to formulate first the energy production function from the solar cell and second the charge and discharge model based on the sensor node energy requirements.

To compute the energy production of the solar cell, we take into account the relevant parameters of the solar cell (ηS) and the average monthly solar irradiance (D) in some location. Thus, the output power is computed as P out = η × D × S and the average energy production E p of the solar cell is P out/V ld. Figure 1 shows the energy production by taking the values of average monthly irradiance in Madrid.

Fig. 1
figure 1

Average monthly output power and energy production taking the average monthly solar irradiance in Madrid

However, to obtain the instantaneous irradiance of the solar cell at time t, we should apply a correction factor depending on the current day and solar time. For example, in hours with no solar light, the energy production is close to zero. Thus, at time t, the instantaneous irradiance can be expressed as \(D(t)=\frac{(t-h)^2}{4p}+D_{\rm month}\), where t is any hour between 0:00 and 23:00 h is the hour of maximum solar light (12:00 am), D month is equal to the average irradiance in a month and p = STDHOURS month/D month, where STDHOURS month is the number of standard hours of solar light in a month. As shown, the irradiance at time t is a quadratic function which draws a parabola with vertex at 12:00 in the noon. By taking different values of irradiance, we can obtain the instantaneous irradiance in any hour of the day, as shown in Fig. 2. Then, the estimated energy production at time t is \(E_p(t)=\frac{D(t) \times \eta \times S}{V_{ld}}\). In Fig. 2, the point (around 6:00) in which energy production begins corresponds to the sunrise and the point (around 18:00) in which energy production ends corresponds to the sunset.

Fig. 2
figure 2

Irradiance per hour for different D month

4.1 Battery discharge and recharge model

The battery discharge model depends mainly on the power requirements of the node, in our case MicaZ. As explained, MicaZ is powered by two batteries of 1.5 V with capacity ranging between 1,500 and 3,000 mAh. To illustrate how these batteries are drained, we consider three different sample applications corresponding to three different operation levels in the sensor node, which have average current draws of 27.7, 50, and 80 mA, respectively. Figure 3 on the left illustrates the discharge curves for every current draw and a battery capacity of 2,500 mAh.

Fig. 3
figure 3

Battery discharge (on the left side) and charge curves (on the right side)

In the system proposed, where the batteries are charged from the solar cell, they experiment an increase in the stored energy according to the energy production. Figure 3 on the right shows the time of charge (in hours) required to completely charge a battery with capacities of 1,500 and 2,500 mAh assuming the energy production obtained from the average monthly irradiance in Madrid (see Fig. 1).

5 System model

We consider a WSN for outdoor monitoring applications, where the sensor nodes are powered by batteries that are recharged by solar cells. Nodes drain the energy from their batteries accordingly to the application they are executing. The application defines an energy consumption pattern, which is characterized by the time of usage of its hardware components in the different operational states and by the energy consumption (or current draw) that each component consumes in each state.

5.1 Task execution model

A sensor node is modeled as an automation that executes predefined tasks. In a given period of time, the sensor node may be assigned to execute a subset of the available tasks \(\Uptheta=\{T_0,{\ldots},T_n\}\). A task T i with i > 0 is represented by a triple \(T_i=\langle t_i,p_i, w_i\rangle\), where t i is the time of execution of the task, p i is the period of execution of the task (note that must be p i  > t i , otherwise the period of execution of the task cannot be met), and w i is the energy consumption of the node due to the execution of task T i . The special task T 0 (also called Idle task) is executed when there is no other task to execute. For this reason, T 0 does not have a period and a predefined execution time, and its energy consumption w 0 is defined as the energy consumed per unit of time. In many cases during the execution of the idle task, the processor and most hardware components are switched in a low consumption state in order to save energy. Hereafter, we denote the execution of T 0 for a duration t as T t0 . The tasks are defined during the design of the sensor node application, in such a way that they are schedulable, that is, there exists a scheduling plan for all these tasks such that they can be executed according to their period and with a bounded jitter. Note that, in order to save the battery capacity and to better exploit the harvesting of energy, it is typical in sensor networks that all the tasks in a scheduling plan of a sensor node are executed in a small fraction of the time, in order to let the sensor spend most of the time executing the idle task to save the battery charge and possibly to recharge it by means of the harvested energy. In the rest of this paper, we will disregard all aspects related to the scheduling of the tasks and we refer to [14, 20, 23, 25] the readers interested in this topic.

A scheduling plan P that executes all the tasks in \(\Uptheta\) can be represented as a finite sequence of tasks. Note that since the tasks are periodic, the scheduling plan P is cyclic and the total execution time of the cycle of P equals to the Least Common Multiple (LCM) of the period of the tasks, i.e. \(t_P = LCM (T_0,{\ldots},T_n)\). For example, in a simple case in which \(\Uptheta=\{T_0,T_1\}\) and \(T_1=\langle 1,3,w_1\rangle\), a possible scheduling plan for these tasks is \(P=\langle T_1,T_0^2\rangle\). The cost of execution of the scheduling plan P per unit of time is then given by the following:

$$ c=\frac{1}{t_P} \left(\sum\limits_{\substack{T_i \in \Uptheta \\ T_i \neq T_0}} \left(w_i \times \frac{t_P}{p_i}\right)\right) + \frac{1}{t_P} \left(t_P - \sum\limits_{\substack{T_i \in \Uptheta \\ T_i \neq T_0}} \left(t_i \times \frac{t_P}{p_i}\right) \times w_0 \right) .$$

where the first sum accounts for the cost of the tasks that are not idle, each of which may be executed more than once in P, and the second term accounts for the cost of the idle task, that is executed in the time remaining after the execution of the other tasks. With simple algebra, Eq. 1 can be rewritten as follows:

$$ c = \sum\limits_{\substack{T_i \in \Uptheta \\ T_i \neq T_0}} \frac{w_i}{p_i} + w_0 \left(1-\sum_{\substack{T_i \in \Uptheta \\ T_i \neq T_0}} \frac{t_i}{p_i}\right) .$$
(1)

5.2 Scheduling plan example

Consider three sets of tasks \(\Uptheta_1, \Uptheta_2\), and \(\Uptheta_3\) composed as follows: \(\Uptheta_1=\{T_{1,0},T_{1,1},T_{1,2}\}, \Uptheta_2=\{T_{2,0},T_{2,1},T_{2,2}\}\), and \(\Uptheta_3=\{T_{3,0},T_{3,1},T_{3,2}\}\). In the definition of the tasks, the first subindex corresponds to the set of tasks and the second subindex corresponds to the task. The Idle task is denoted as T 1,0T 2,0, and T 3,0 in \(\Uptheta_1, \Uptheta_2\), and \(\Uptheta_3\) , respectively, and, as explained, they do not have a predefined execution time and period. The rest of the tasks are defined as follows: \(T_{1,1} = \langle 2, 10, 5\rangle, T_{1,2} = \langle 2, 10, 8\rangle; T_{2,1}= \langle 3,15,3\rangle\) and \(T_{2,2} = \langle 8, 15, 6\rangle\); and \(T_{3,1} = \langle 2, 20, 3\rangle\) and \(T_{3,2} = \langle 3, 20, 4\rangle\).

The description of the tasks in \(\Uptheta_1\) is as follows: T 1,1 samples an energy-hungry transducer of temperature and T 1,2 turns the radio on and, after receiving the messages directed to itself, sends all the messages (including the data read from the transducer) to the next hop and turns the radio off; in \(\Uptheta_2\), the task T 2,1 samples a temperature transducer and T 2,2 turns the radio on, receives the messages from its neighbors, computes the average of the temperature data, sends a single message including the result and, finally, turns the radio off; And finally, in \(\Uptheta_3\), the task T 3,1 samples a temperature transducer and T 3,2 turns the radio on to send a single message with the temperature data and turns the radio off.

Let us consider a scheduling plan for each set of tasks: \(P_1=\langle T_{1,1},T_{1,2},T_{1,0}^6\rangle, P_2=\langle T_{2,1},T_{2,2},T_{2,0}^{4}\rangle\), and \(P_3=\langle T_{3,1},T_{3,2},T_{3,0}^{15}\rangle\). When a plan is selected to be executed, all of its tasks are cyclically executed, such as Fig. 4 shows. Knowing the cost of each task, we can compute the cost of the scheduling plans as indicated in Eq. 1.

Fig. 4
figure 4

An example of execution of the scheduling plans P 1, P 2, and P 3

An initial assignment of scheduling plans P 1, P 2, and P 3 to the k slots should be done in such a way that the assignment maximizes the overall QoS while the system is kept energy-neutral. Figure 5 above shows an example of initial assignment of the scheduling plans to the k slots. The assignment matches the most consuming plans to the slots of time that predictably coincide with the hours of more solar light and the lowest consuming plans to the slots in the night and hours of less solar light. However, it is possible that the energy harvested from the system is higher (or lower) than the predicted for each slot, and then, a new assignment should be computed to adapt the solar conditions. We call re-optimization to this process. This fact is shown in Fig. 5 below, where at approximately noon, the solar conditions change with regard to the expected and a re-assignment of scheduling plans to the following slots (marked in red) must be performed.

Fig. 5
figure 5

An example of initial assignment (above) and re-optimization (below) of the assignment

5.3 Energy model

Let B max be the maximum battery capacity and B min be the minimum battery capacity necessary to maintain the sensor node working.

Consider a sensor node connected to our solar energy-harvesting system as explained in Sect. 3. The solar cell obtains the energy from the sun to charge the node batteries according to the light conditions at each moment. Thus, the energy available in the batteries may increase (or decrease) depending on the solar irradiance received in a place and instant of time. Here, the system could be compared to a producer-consumer system, where the producer is the solar cell and the consumer is the sensor node.

Let e p (t) be the energy produced by the solar cell at time t and e c (t) be the energy consumed by the sensor node at time t, and B t be the battery level at time t. Let also \(\phi \in [0,1)\) be the charging efficiency of the battery and the rectifier function [x]+ be defined as follows:

$$ [x]^+ = \left \{\begin{array}{cc} x & x \ge 0 \\ 0 & x < 0 \end{array} \right. $$
(2)

According to [13], and disregarding the power leakage of the battery, energy production and consumption at time t are subject to the constraint:

$$ B_0 + \phi \int\limits_0^t [e_p(t) - e_c(t)]^+ {\rm d}t - \int\limits_0^t [e_c(t) - e_p(t)]^+ {\rm d}t \geq 0 \forall t \in [0,\infty) $$
(3)

where the second term accounts for the energy which is produced and not consumed (which recharges the battery), and the last term accounts for the energy which is drained from the battery. This equation considers the fact that the battery is not an ideal energy buffer, and it assumes that the excess energy that is neither used nor accumulated in the battery is dissipated by the system. Letting B t be the battery level at time t, we thus have that \(0 \leq B_t \leq B_{\rm max} \forall t \in [0,\infty)\).

As observed in [13], an energy-harvesting sensor node can operate uninterruptedly if its energy management is energy-neutral. Considering a reference period of time \([t^{\prime},t^{\prime \prime}]\) , the energy management of a sensor node is energy-neutral if \(B_{t^{\prime}} = B_{t^{\prime \prime}}\). In our case, considering that the energy-harvesting system is based on solar cells, the energy production has a cycle of 24 h, for this reason, we aimed at devising an energy-management strategy that is energy-neutral in the 24 h.

6 Energy-management optimization

As shown in Sect. 4, the energy production given by a solar cell is predictable, although it is uncontrollable. We exploit the predictability of the energy production in order to devise energy-neutral strategy for the energy management of the nodes. We assume that alternative scheduling plans are available and that these scheduling plans are all energy-neutral, i.e. sustainable, at least under some environmental conditions. For example, a scheduling plan may not be sustainable for winter cloudy days, but it may be sustainable in a summer sunny day. Two scheduling plans may implement the same functionalities with different duty cycles, or it may implement different functionalities, as defined by the user.

Although, in principle, knowing all the parameters of the energy production, it could be possible to select the best scheduling plan that is always sustainable, we observe that these parameters may not be known in advance and they may also change with the time. Thus, the energy-management strategy should be adaptable to different environmental conditions, and hence, it should switch among the scheduling plans depending on such conditions.

Let us assume there are n possible sets of tasks to be executed \(\Uptheta_1,{\ldots},\Uptheta_n\). For each subset \(\Uptheta_j\) , a scheduling plan P j is defined with an energy cost per unit of time c j and a user defined quality level q j . The main objective of our energy-management algorithm is to select the scheduling plans in order to maximize the overall quality level. To this purpose, we consider a time frame of 24 h, and we discretize the time axis into k slots of duration \(\Updelta T=\frac{3,600 \times 24}{k}\) seconds. For each slot i the energy-management algorithm selects the scheduling plan S[i] (thus S[i] = P j for some j), corresponding to quality level q(i) = q j and energy consumption c(i) = c j . Let also e p (i) be the energy production during slot i and e e (i) be the expected energy production in slot i, which can be estimated by the energy production model discussed in Sect. 4.

We denote with B(i) the battery level at the end of slot i; if the duration of the slots is small enough, we can assume that if the charge tends to overflow the battery above B max, then the battery charge will be B max also at the end of the slot. Similarly, if the charge tends to underflow the battery below 0, then the battery charge will be 0 also at the end of the slot. This assumption is motivated by the fact that the slots are short, the energy production will keep the same trend while the energy consumption will remain almost the same. Under this assumption, we can express the estimated battery level B(i) for all \(i \in [1,k]\) as follows:

$$ B(i) = {\rm max}\{B_{\rm max}, B(i-1)+\phi [e_e(i) - c(i)]^+ - [c(i) - e_e(i)]^+\} $$
(4)

where B(0) is the initial battery level at the beginning of slot 1.

The quality of service (QoS) of the assignment is defined as the sum of the quality levels in all the slots, and the problem of assignment can be formalized as an optimization problem as follows:

$$ \begin{aligned} {\rm max} \sum_{i=1}^{k} q(i) &\\ B(0) \leq B(k) & \\ B_{\rm min} \leq B(i) \forall i \in [1,k] & \\ \end{aligned} $$
(5)

The first constraint of the above optimization problem states that the assignment of the scheduling plans made by the algorithm is energy-neutral. The second constraint states that the battery level can never be below B min because otherwise the sensor node would have to stop its operations. Note also that the expression of B(i) is not linear, thus leading to a nonlinear optimization problem.

To simplify the problem, we can consider the case of an ideal battery, in which the initial charge is high enough and the maximum charge is infinite (this assumption can be considered realistic also if the battery has a very high capacity). In this case, we can disregard the second constraint, and we can rewrite the optimization problem as follows:

$$ \begin{aligned} &{\rm max}\,\sum_{i=1}^{k} q(i) \\ & \sum_{i=1}^k e_e(i) \geq \sum_{i=1}^k c(i) \\ \end{aligned} $$
(6)

where the constraint states that the energy production during the 24 h should exceed the energy consumption.

We observe that this problem differs from the Unbounded Knapsack Problem (UKP) since in Eq. 6, there is a constraint on the number of items (scheduling plans) that can be taken (there are exactly k slots), while in UKP, there is not such a limit. However, UKP can be reduced to 6 as follows. Let us consider an instance of UKP:

$$ {\rm max}\,\sum_{i=1}^{n} x_i \times q_i,\quad{\rm where}\,\sum_{i=1}^n x_i \times c_i \leq W $$
(7)

where x i is a positive integer and represents the number of times an item P i with cost c i and value q i is selected. W.l.g. let us assume that \(c_1 \leq c_2 \leq {\cdots} \leq c_n\). It is immediate that, for each admissible solution of the problem, must be \(\sum\nolimits_{i=1}^n x_i \leq W/c_1\), which means that the number of items that can be selected in an admissible solution is at most m = W/c 1. We can then transform this problem in a set of m instances of problem 6, where k varies between 1 and m. In particular, for each \(i \in [1,n]\), let \(q(i)=q_i, c(i)=c_i, \sum\nolimits_{i=1}^k e_e(i)=W\), and consider all the resulting instances of problem 6:

$$ \forall k \in [1,m] {\rm max}\,Q_k=\sum_{i=1}^{k} q(i),\quad{\rm where}\,\sum_{i=1}^k c(i) \leq W $$

By definition of problem 6, the result of instance k maximizes the value of the selected items in the case where exactly k items can be selected. Hence, the solution to the instance \(k^{\prime}\) with maximum \(Q_{k^{\prime}}\) is also optimal for problem 7. Consequently, solving problem 6 is at least as difficult as solving UKP, which is known to be NP-Complete. For this reason, we present in the next section a greedy heuristic aimed to solve the problem.

6.1 Initial assignment algorithm

We assume that the scheduling plans \(P_1,{\ldots},P_n\) are preliminarily sorted according to \(\frac{q_i}{c_i}\) i.e. \(\frac{q_i}{c_i} \geq \frac{q_j}{c_j}\) iff i < j and q i  > q j if \(\frac{q_i}{c_i}=\frac{q_j}{c_j}\). It should be noted that a plan P j for which q j  < q i and c j  ≥ c i for some i is inefficient, and it can be excluded a priori.

Let us consider the array P[] that contains the scheduling plans (where, initially, P[i] = P i ) and let q P (i) and c P (i) be the quality level and the energy cost of the scheduling plan contained in P[i], respectively. Let also assume that a time frame of 24 h is divided into k slots, where r 1 is the slot immediately after the sunrise, and r 2 is the slot immediately after the sunset. Let e p (s) be the energy production in the slot s and e e (s) be the estimated energy production in the same slot. Let S[] be the vector containing the assignment of the scheduling plans to the slots. Then, for a given assignment S[], c(s) is the energy consumption in slot s, and the expected energy budget of S[] is given by B(k) − B(0).

We denote with P[]| q the vector obtained from P[] by removing all elements that have quality level smaller or equal to q (i.e. q P (i) ≤ q). Similarly, we denote with P[]|c the vector obtained from P[] by removing all elements that have a cost larger or equal to c (i.e. c P (i) ≥ c). The operations P[]| q and P[]|c return an empty vector, denoted with ∅, if no elements in P[] satisfy the requested property. Note that, if P[] is sorted, the vectors P[]| q and P[]|c are also sorted according to the same order, and both operations can be performed in a linear time with the size of the vector.

The pseudo-code for the initial assignment is presented in Algorithm 1. This algorithm progressively achieves a (sub)optimal solution in several iterations. It starts initializing the most efficient plan P[1] to all of the slots. After assigning a plan, the algorithm evaluates the energy-neutral condition by comparing the battery level at slot 0 and at slot k. An optimal solution is achieved if the battery level is equal at these two slots and the battery level is at least B min in all slots. Otherwise, two cases may occur: (1) If the battery level at slot k is larger than the battery level at slot 0 (B(k) − B(0) > 0) and B(i) ≥ B mini), this means that the estimated energy production is higher than the consumed energy, and therefore, there is a choice of finding a better assignment; then, the algorithm selects the plan in P[] with the next higher quality (and higher consumption), which replaces the plan assigned in all of the slots in S[] (this replacement continue until the solution remains energy-neutral, and it is performed by the function Upgrade); (2) If the battery level at slot k is lower than the battery level at slot 0 (B(k) − B(0) < 0) or there exists a slot i for which B(i) < B min, this means that the current assignment is not sustainable, and then, the algorithm must select the plan in P[] with the next lower cost (and lower consumption), which replaces the current plan in all of the slots (this replacement stops as soon as the solution becomes energy-neutral, and it is performed by the function Downgrade). The algorithm iterates until there exists no plan in P[] that improves the achieved solution.

figure a

The functions Upgrade and Downgrade are shown in algorithms 2 and 3, respectively. They take a scheduling plan assignment S[] and replace the elements of S[] with a new scheduling plan P i , starting from an initial slot. Function Upgrade replaces elements from S[] with scheduling plans that have a better quality level (and, typically, a higher energy costs). For this reason, it starts the replacement from the slots after the sunrise (i.e. at slot r 1), since in these slots, the energy production will compensate for the additional energy costs. The Function Upgrade replaces elements as long as the resulting assignment is energy-neutral, and the battery level is at least B min for all slots. On the other hand, Function Downgrade replaces elements from S[] with scheduling plans that have a lower cost (and, typically, a lower quality level). For this reason, it starts the replacement from the slots after the sunset (i.e. at slot r 2) in the slots where there is no energy production. The Function Downgrade stops replacing elements as soon as the assignment becomes energy-neutral and the battery level is at least B min for all slots.

It can be observed that the complexity of Algorithm 1 is \(O(n \cdot(k+n))\). In fact, in each iteration of the loop, a scheduling plan P i is selected and assigned to each one of the k slots by the Function Upgrade (or Downgrade). These functions take complexity O(k). When a plan P i is selected, it is also removed from P[] (along with all the scheduling plans with a lower quality level or higher cost, depending on the case), thus reducing the number of scheduling plans in P[] of at least 1 element. As a consequence, the main loop is repeated at most n times. Note that the removal of elements from P[] can be done in O(n). Consequently, the overall complexity is \(O(n \cdot (k+n))\). Considering that, typically k ≫ n holds, the complexity is \(O(n \cdot k)\).

figure b
figure c

6.2 Re-optimization algorithm

Algorithm 1 is used to compute an initial assignment vector that should hold for all the slots in a time frame, based on the estimation of the energy production and the cost of the scheduling plans. Typically, the time frame is 24 h and the Algorithm 1 is thus executed once per day. However, at execution time, the solar conditions may change leading to a production higher or lower than the expected one, making inefficient or non-energy-neutral the initial assignment, which should be adapted to the new conditions. To this end, we propose a re-optimization algorithm, whose pseudo-code is presented in Algorithm 4. This algorithm re-optimizes the assignment starting from the slot x to slot k (assuming that at slot x − 1 a difference between the real energy production and the expected one is detected). Two cases may occur: (1) If the expected battery charge at the end of time frame B k exceeds the initial battery level B 0 for more than a parameter \(\epsilon\), the current assignment can be improved to maximize the overall QoS. To this purpose, the algorithm keeps improving the plans in the slots [xk]. This improvement continues until there exists no plan that improves the achieved solution or the assignment becomes non-energy-neutral; (2) Otherwise, if the expected battery charge at the end of time frame B k is below the initial battery level B 0 for more than a parameter \(\epsilon\) or B(i) < B min for some slot i, the algorithm looks for an assignment with a lower cost. To this purpose, the algorithm keeps replacing the plans in slots [xk] with plans with smaller energy cost. This improvement continues until there exists no plan that improves the achieved solution or the assignment becomes again energy-neutral and the battery level is at least B min for all slots. It is immediate that the computational cost of this algorithm is \(O(k\cdot n)\).

figure d

7 Evaluation

We have evaluated the algorithms proposed by using a simulator implemented in C. We consider a time frame of 24 h composed of 300 slots (k = 300) with a duration of 288 seconds each one. To estimate the energy production, we take the average monthly solar irradiance in Madrid (data provided by RetScreen [16]): 84.58, 123.33, 178.75, 212.92, 247.92, 295.42, 300.0, 264.1, 202.9, 130.42, 88.75, 70.83 W/m2, corresponding to the months of the year from January to December. We compute the instantaneous energy production at hour t (between 0:00 and 23:00) by using the energy production model described in Sect. 4 and we distribute the energy production E p (t) equally among each one of the slots in an hour (\(\frac{k}{24}\)). Additionally, to do more realistic simulations, we varied the first and last light hour accordingly to each month, i.e. the sunrise slot r 1 and sunset slot r 2. The maximum capacity of the battery B max was configured to be 2,500 mAh and the minimum B min to be 1,975 mAh. We fix the battery level at slot 0 to be 2,000 mAh and a tolerance parameter \(\epsilon=10^{-4}\). Thus, to validate the initial assignment algorithm, we need to check B(300) ≥ B(0), which means that the system is sustainable and energy-neutral in a day.

We define six sets of tasks (\(\Uptheta_1 {\ldots} \Uptheta_6\)) and their corresponding scheduling plans (\(P_1 {\ldots} P_6\)), whose description is detailed in Table 3. For each plan, the columns from 2 to 5 present the definition of the tasks 1–4, respectively (the notation "–" means that the plan has not that task); the column 6 indicates the execution time for the Idle task; and, in subsequent columns, we specify its cost per unit of time c (as defined by Eq. 1), its quality level q (in percentage) and its efficiency, which is computed as \(\frac{c}{q}\).

Table 3 Parameters of the scheduling plans employed in the simulation

We execute the initial assignment algorithm for the first day of each month of the year, starting at 0:00 am that corresponds to the slot 0. Figure 6 presents the results of its execution. Since at slot 300 the battery level is always larger or equal to 2,000 mAh, the algorithm finds an assignment of scheduling plans to slots such that the system is energy-neutral each month of the year. As shown, there is a clear relationship between the energy production and the battery level: the bigger is the energy production, the larger is the battery level achieved at the last light hour (approximately at slot r 2) and the lower is the battery level before r 1, due to the fact that the most consuming plans can be assigned when the production is larger. In the figure, the biggest energy production happens in July, where a maximum battery level of approximately 2,015 mAh is achieved at slot r 2. Correspondingly, the lowest battery level of approximately 1,980 mAh is achieved also in July, since the most consuming plans are assigned in that month.

Fig. 6
figure 6

Battery level during the 1st day of each month of the year

Figure 7 shows the result of the initial assignment of scheduling plans to slots and the overall QoS (in percentage). To avoid excessive cluttering, the assignment is only shown for the months of January, March, July, and September. The scheduling plans are assigned according to the energy consumption and production at each slot. Thus, it is expected that the most consuming plans be assigned in the months of summer and the lowest consuming plans in the months of winter. Once the assignment is done, we compute the overall QoS after the first day of each month as the average of the quality levels corresponding to the scheduling plans assigned. As shown, the maximum QoS (89.3 %) is achieved in July when the production of energy is the biggest and P 2 is assigned to most of the slots (note that P 1 cannot be assigned because it would become non-energy-neutral the system). Only in some slots previous to r 1 (coinciding with the time where there is no energy production), our strategy has to degrade the QoS and select the next lower energy plan P 3 to be assigned. Analogously, the minimum QoS (71.18 %) corresponds to January, where the lowest consuming plan P 6 is assigned to most of the slots except some slots after r 1 (coinciding with the beginning of the energy production). Here, our strategy may upgrade the QoS and select the next higher energy plan P 5 to be assigned.

Fig. 7
figure 7

Initial assignment of scheduling plans to slots for the months of January, March, July, and September

As the energy consumption is expected to be constant, the battery level draws a pattern that is repeated each day. To show this effect, Fig. 8 on the left side presents the battery level during the first week of January (the worst case). On the right side, we show the initial assignment during that week and the overall QoS.

Fig. 8
figure 8

On the left side, the battery level during the first week of January (k = 2,100). On the right side, the assignment of scheduling plans to the slots of the same week

We also evaluate the re-optimization algorithm. Under an excess (or deficit) of energy production with regard to the estimated production, the re-optimization algorithm is able to adjust the initial assignment of scheduling plans to slots, in such a way that the overall QoS is maximized while the system remains energy-neutral. We consider an overproduction and an underproduction of solar energy of 1, 5, 10, and 20 % with regard to the expected energy production. This overproduction (or underproduction) is applied to all the slots between [x,r 2] (where x is randomly selected, x ≥ r 1) due to the fact that if we consider that a day is sunnier (or cloudier) than the expected, in general it will be sunny (or cloudy) during most part of the day and not only at a specific slot. We compute the effect of re-optimization on the QoS for all months of the year and present the results in Table 4. For each month, the column on the left presents the QoS when an excess of energy production exists, and the column on the right, the QoS when there is a deficit. A deficit of energy with regard to the estimated energy may make non-energy-neutral the system. This happens in the months where the energy production is low (from October to March), and there exists no a more efficient plan that can be assigned (note that, if in a real application this is an issue, the developer should design the energy-harvesting system and the battery capacity in an appropriate way). In the months with higher production, between April and September, the underproduction results in a new assignment that keeps energy-neutral the system as well as reducing the overall QoS. As observed, the larger is the deficit in the energy production, the larger is the reduction in the resulting QoS, and vice versa. Excess of production of 1 and 5 % do not have significant impact on the overall QoS achieved, which is similar to the QoS achieved by the initial assignment algorithm. An excess of 10 and 20 % increases meaningfully the QoS, especially in the months of summer (from June to September). As an example, in July, we achieve a QoS of 90.7 and 94.5 %, respectively. Figures 9, 10, 11, 12 present the new assignment of scheduling plans to slots after re-optimization in January, April, July, and September as well as the QoS achieved. On the left side of the figures, we present the results when there exists underproduction and, on the right side, when there exists overproduction. A deficit of solar energy implies to assign lower consuming plans which, in turn, reduce the resulting QoS. The worst case happens when the re-optimization algorithm cannot find an energy-neutral assignment (for example, in January). This fact is shown in Fig. 9 on the left, where only the initial assignment is shown. However, as depicted in Figs. 10, 11, 12 on the left, for the other months, the algorithm produces a new valid assignment. Similarly, an excess of solar energy implies to assign higher consuming plans and, in turn, to increase the QoS. On the right, we present the new assignment and its QoS, which improves the QoS achieved by the initial assignment.

Table 4 Summary of the QoS obtained for each month of the year after re-optimization due to an overproduction/underproduction of solar energy of 1,5,10, and 20 %
Fig. 9
figure 9

Re-optimization in January due to a deficit (on the left side) and an excess (on the right side) of solar production (Ep) of 10 and 20 % with regard to the expected production (Ee)

Fig. 10
figure 10

Re-optimization in April due to a deficit (on the left side) and an excess (on the right side) of solar production (Ep) of 10 and 20 % with regard to the expected production (Ee)

Fig. 11
figure 11

Re-optimization in July due to a deficit (on the left side) and an excess (on the right side) of solar production (Ep) of 10 and 20 % with regard to the expected production (Ee)

Fig. 12
figure 12

Re-optimization in September due to a deficit (on the left side) and an excess (on the right side) of solar production (Ep) of 10 and 20 % with regard to the expected production (Ee)

8 Conclusions and future work

Energy-harvesting systems (e.g. solar cells) allow to achieve potentially infinite sensor node’s lifetime. However, in practice, the production of energy is intermittent and, in some cases, it could be insufficient to constantly satisfy the power demand of WSN applications. In this paper, we model the energy production of the solar cell and the energy consumption of a typical MicaZ sensor node. Assuming that there exist different scheduling plans with different energy cost and QoS and that we discretize the time axis into k slots, we propose an algorithm to find an assignment of scheduling plans to the slots that aims at maximizing the QoS while keeping the system energy-neutral. Since the energy produced could vary from the expected one, we also propose a re-optimization algorithm that corrects the initial assignment to meet the new energy conditions. We have evaluated the algorithms by simulation and shown how a valid assignment that maintains energy-neutral the system is found for all months of the year. We applied our re-optimization algorithm to show how the assignment is adapted to the conditions of underproduction and overproduction, which is expressed in terms of a lower and higher QoS, respectively. As future work, we plan to dynamically adapt the sampling frequency of the applications according to the estimated production and computing the solar cells requirements based on the power demand of sensor nodes.