## 4 SENSED-SG Design and Protocols
### Sensed-Sg MAC Layer
Information about the duration of the ON/OFF periods. This information is added in order to enable adaptive duty-cycle functionalities, which have been already proved in previous studies [54] to be of great value for improving the performance of duty-cycling mechanisms. It is worth noting that if no modification of the duty-cycle settings (ON/OFF periods) has been issued, there is no need to include this second type of extra information in a message. It is important to understand that communications in Smart Grid environments are of a very \"bursty\" nature. Nodes can remain in normal operation for long period of times only sending their sensed physical data. However, when a malfunction/non-ideal condition is detected in a power asset, the activity in the WSN greatly increases in both ways--nodes would send as much information about the incident as possible, while the central node would take decisions and return orders to nodes. Under such circumstances, to achieve maximum network performance, the MAC layer must allow nodes to transmit data messages in a bursty way. To this end, when a sender has more than one message to be dispatched in a very short period of time (as it occurs, for instance, if an incident arises), the sender will transmit one after another as they are being acknowledged by the receiver (this node remains in awake period until it does not receive any more data messages). As a contribution, it is not necessary for the sender to wait for the receiver to enter in as many ON phases as messages the sender has to dispatch, thus finding a solution to the incident more quickly. Furthermore, a scheduling algorithm has been designed and implemented to aid nodes to find the most suitable instant to transmit their messages and thus exploiting as much as possible the physical medium. Under this premise, messages from a sender are not dispatched to different nodes following a FIFO (First in, First out) approach; but instead, messages are sent in such a way as to minimize the end-to-end delay. This mechanism seeks reducing the senders' waiting times and leads to smaller power consumption and a boost in the global network performance. In order to implement these two contributions (packet bursts and non-FIFO approach), exhaustive work has been accomplished to modify diverse TinyOS components and primitives as required. The Figure 4 shows (i) the phase-lock mechanism in which both nodes get mutually synchronized; (ii) the process of adjustment/adaptation of the duty-cycle of A to, for instance, route a higher amount of messages; and a (iii) burst of data from node B. For further interested readers, the following flowchart (Figure 5) presents a representation of the MAC logic implemented in nodes. Two procedures are shown: the left one is in charge of handling the dispatching of messages and the right one deals with the reception of messages. Figure 4: Figure of our proposed MAC layer. Figure 5: Flowchart of our proposed MAC layer. Left diagram presents the procedure for dispatching messages and right diagram the procedure for reception messages. When designing an algorithm that strongly relies on a temporal accuracy--like the phase-lock synchronization mechanism--it is of great importance to take into account the hardware limitations of WSN nodes and, in particular, their clock drift. To this end, in our implementation of the phase-lock mechanism, sender nodes compensate the potential effects of clock drift in synchronized nodes by anticipating the transmission of their preamble. This \"compensation time\" increases as more time a sender/receiver pair spends without exchanging any message. However, in our solution, because of the acceptable clock-drift of the WSN nodes--about 30-50 ppm [58]--and the not-too-long periods without communication (maximum of 15 min as described in following sections), the clock deviation to be adjusted is, in the worst case, in the order of 50 milliseconds.