Light Controller

Organization

The light control module was heavily built using software. The primary load is the ESP8266 software. A pre-built library was used to interface between the ESP8266 and the PIC32. This library was also modified to fit the needs of the project.

ESP Setup

Whenever the system is rebooted, the ESP8266 needs to be reconfigured. This requires communicating AT commands to the ESP via UART. The commands are as follows:

  1. AT+RST: Reset the ESP module (remove connections, disconnect from wifi, default modes)
  2. AT+CWMODE=1 :Places ESP in station mode
  3. AT+CIPSTA=”IP”,”Gateway IP”,”Netmask ID”:Changes the IP address of the module. This is necessary for the two ESP modules to know which IP address to communicate with.
  4. AT+CWJAP=”ssid”,”password”:Connect to the wifi network
  5. AT+CIPMUX=1: Enables multiple connection mode
  6. AT+CIPSERVER=1,<port> : Places ESP into server mode

All commands were carried out using UART communication as implemented by the esp866.c library. The setup is performed by a protothread (utilizing the pt_cornell library), which is only run once.

Communication

A protocol was developed to send commands to the shade controller module but also to be able to receive data from the module. The protocol also needed to be able to include commands received via the web interface. Any communication data sent via the ESP8266 modules is prepended with the characters ‘IPD+’ as per built in functionality of the ESP. All communication received by the light controller ESP is appended with an ‘!’ character, which is used as a stop-signal for the data packet.

Manual VS Automatic

A design decision was made to give precedence to any manual adjusted shade control over the automated system. This meant that any time the shades were adjusted via the shade controller buttons (connected to the shade motors), the LCD screen manual increase/decrease/stop/light buttons, or the web interface manual control, the shades would no longer respond to changes in brightness levels within the room. The priority can be shifted back to automatic mode by increasing or decreasing the desired brightness levels using the touch interface.

Motor Control

To control the motor, three types of commands are sent: ‘I’, ‘D’,’S’. These are ASCII characters which are sent from the server ESP module to the client shade controller ESP module via AT+CIPSEND command implemented by the esp8266 library. ‘I’ commands the motor to move in a fashion to increase brightness. ‘D’ seeks to decrease brightness (closing the shades). ‘S’ commands the motors to stop moving. All manual control buttons on the LCD screen are single pulsed. Between a move command (I or D) and a stop command, the shade controller can respond back with the characters ‘S!’, which indicate that the motor cannot move anymore.

Data reception

For data reception, a protothread continuously waits for data to be available on the UART peripheral of the PIC32. Once data is available, the data is checked for the first four characters ‘+IPD’ in succession using a state machine. Once those characters are verified, the remaining data is placed into a buffer. The buffer is then scanned for specific identifiers to indicate if the data is from the web interface or the other ESP module. The character buffer is then parsed for the actual command data to respond to.

ASM diagram depicting the communication flow and state of the light control unit

Touch UI

The user interface is implemented using the Adafruit 2.4″ LCD touchscreen. An initial drawButtons() method is called to draw the buttons and their labels. After that, a regular update occurs (in a protothread) which refreshes the ambient light brightness level and the current brightness level (as percentages) status displays. All buttons except for the increase/decrease brightness buttons are single pulsed.

Ambient Light Detection

Ambient light detection is carried out using an ambient light sensor, whose value is read in using an ADC on the PIC32. The value is then converted to a percentage representing the max level (divide by 10 using floating point). The range of the ADC value is 0-1023 and thus the percentage range becomes ~0-100%.


 Process of Software Development

While working on the software related to the Light controller, the greatest challenges were posed with the implementation of the ESP8266 communication. The esp8266.c library had a base implementation upon which further expansion was necessary. Due to the interaction of the protothread library with the UART, methods within the esp library had to be reconstructed. This led to self-induced bugs in the system, especially when dealing with receiving data via the UART from the ESP module. When trying to receive all data via the serial connection with the ESP, methods were written to read until the stop data ‘!’ was seen. The rest of the data was to be read through and discarded. If the incoming data was too fast though, reading from the UART would hang and the system would enter an infinite loop waiting for more data. This implementation of receiving data was later discarded by simplifying the protocol for communication

Another issue faced while handing the ESP modules is their response to any AT commands. Some AT commands respond back with an ‘OK’ while others may have a response of ‘ERROR’ etc. This means that any command to the ESP must also wait for the response before attempting to read/write further to the serial port on the ESP. This was troublesome as the state of the UART buffer on the PIC was hard to determine consistently. To solve this, a deeper understanding of the AT commands was required. Also, by carrying out the commands in a way where response from the ESP would not disrupt communication, became necessary. This included things like adding delay after sending a command or actively waiting for the ESP to respond before moving forward.