Tired of the sun being in your eyes?
Paying for air conditioning when you are not home?
Sweating profusely when you get into your hot car?
Hiding valuables in your car because you are afraid of theft?
HAVE NO FEAR! SmartWindow is here!
For this project, we will be using the MSP432 Launchpad and a CC3100 Wi-Fi BoosterPack from Texas Instruments. In addition, we will be using the light sensor from the Grove Sensor starter kit in order to measure the amount of light incident upon a window, and a 4" x 2" piece of electrochromic film that we received as a free engineering sample. For the circuit itself, we used an N-Channel MOSFET and a 4.7K resistor to control how much power is delivered to the film and adjust the tint. And finally, the product will feature an iPhone app as a user interface to control various settings.
DISCLAIMER:
In order to make the effect of the tint as visible as possible in the video, we only showed the two extreme cases: completely tinted and completely clear. The actual system is NOT binary: the level of the tint is adjusted between 256 possible values, based on how much light is incident on the light sensor. This is best seen in a real-life demonstration.
Also, we used a film that was given as a free sample. Electrochromic film is available in (much) larger sizes, as well as different colors. There are much higher quality films (at higher prices) that possess greater tint : clear ratios. Although our SmartWindow system will work with any type of electrochromic film, you may have to replace the resistor in the circuit with another one; the resistor value is dependent upon the resistance of the film.
THE HARDWARE
MSP432 Launchpad
The MSP432 LaunchPad is a low-cost, easy-to-use development kit featuring the MSP432 micro-controller. This device is known for ultra-low-power consumption, which enables a very long battery life. The board features an on-board programmer, access to all I/O pins of the MSP device, an on-board RGB LED, and push buttons.
This will be the application processor for our SmartWindow project.
Electrochromic Film
Electrochromic film is a new technology that allows individuals to control the opacity of glass with a voltage drop across the film. We will be using a 4" x 2" piece of electrochromic film that we received as an engineering sample for this project. To learn more about electrochromic film and how it works, please click follow the link below.
https://en.wikipedia.org/wiki/Electrochromism
N-Channel MOSFET
In order to control power delivered to the film, an N-Channel MOSFET with a drain-to-source voltage of at least 36 volts, and gate-to-source voltage of at least 5 volts, is needed. By turning the FET on and off via Post Wave Modulation (PWM), voltage to the film is controlled by setting the duty cycle of the PWM.
CC3100 Wi-Fi BoosterPack
The CC3100 BoosterPack will be used by this project in order to connect this project to the Internet of Things. Information about the CC3100 BoosterPack can found below
Grove Ambient Light Sensor
The light sensor for this projects allows our window sensor to detect the amount of light that is incident upon it. Information about the Grove Ambient Light Sensor can be found below.
http://www.seeedstudio.com/wiki/Grove_-_Light_Sensor
THE SOFTWARE
Energia MT!
We will be using Energia 16 for this project. Energia 16 supports multi-threading across different tabs in the programs, and uses an updated I2C Wire library that has better support for the MSP432 Launchpad.
IBM Blue-Mix
A free cloud service with a Node-Red server interface will allow our MSP432 Launchpad to connect to the Internet of Things.
Xcode 6
Xcode 6 was used for the development of an iOS application that provides a simple user interface for our SmartWindow system. We will utilize an MQTT open-source library for iOS called Moscapsule, which will allow us to bridge the gap between iOS and the Node-Red server. A link to the Moscapsule MQTT library can be found below.
https://github.com/flightonary/Moscapsule
Freeboard
Freeboard is a fantastic front end interface for simple Internet of Things projects. It provides many useful tools to visualize your sensor data, and takes advantage of JSON events and a REST API in order to makes the communication from the Node-Red server to Freeboard seamless! We will be using Freeboard as a visual aid for server functionality in this project.
LET'S GET DOWN TO BUSINESS
Alright, it's time you learned how to build your very own SmartWindow! The first step is going to be following adrianF's tutorial for Creating a Multi-Tasking IoT Wi-Fi sensor. We will only be using the ambient light sensor from this guide, so the steps related to reading values from other Grove sensors can be ignored. Please come back when you are done!
Did you complete adrianF's guide? Awesome! You should now have an ambient light sensor that is connected to your own Wi-Fi sensor and the Freeboard front end interface!
Alright, there are a few changes that we need to make to the code that you already have written in order to make it work with our SmartWindow system. First, include a new variable definition called outputVal in your main file with all your global declarations. If you followed adrianF's guide, then this file should be titled myWifiSensor.ino. Now outputVal will be updated when we receive packets from the Node-Red server.
aJsonStream serial_stream(&Serial);
char* jsonPayload;
// sensor globals
int light = 0;
int outputVal = 100; // updated by MQTT subscribe call
Now that we have declared our update value, it is time to use it! Head over to your MQTT.ino value and look for the callback() function at the top of the file. This function is called every time you successfully subscribe to the MQTT broker. By subscribing to the MQTT server, you now have a new message to process! This message will contain the value that you need to output to the MOSFET. However, the server will pass the value back to us in String format, so we must convert the number to an integer using the ascii-to-int function from the standard library.
void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0'; // append null terminator to msg
outputVal = atoi((char *) payload); // convert to int
}
Now you should be able to receive a message from the Node-Red server and store its value. You must be wondering how we are going to use this new value that we received from the server? Let me show you! Go to the upper right hand corner of your energia development environment and click on the "Add Tab" button. We are going to create a new file called "PWM.ino". This file will be responsible for taking the contents of outputVal and sending it to the gate of the MOSFET transistor using Pulse Width Modulation. If you would like to learn more about PWM and how it works, this link will help. Here are the contents of our PWM.ino file, make sure to include this in your project.
/*
* Setup for the pwm function for output to the electrochromic film
*/
int PWM_PIN = 38;
void setup() {
pinMode(PWM_PIN, OUTPUT);
}
/*
* use PWM to control the MOSFET transistor for the electrochromic film
* analogWrite generates PWM with dutycyle = outputVal and f ~ 500Hz
*/
void loop() {
analogWrite(PWM_PIN, outputVal);
delay(100);
}
Lastly, make sure you change the topic of your publish call in the MQTT.ino file to "mspSensorVal" and the topic of your subscribe call to "outputFuzzyVal".
Congratulations! That is all the code that needs to be changed on the MSP432 side of things. The rest of the processing is handled in the cloud (ooooohh aaaaaahhh)! It is time to learn how to take advantage of our Node-Red server and all of the functionality that it has to offer.
THE CLOUD
I know what you are thinking...why do we even need the cloud? All of the functionality for this project already exists without involving the cloud! In fact, I could probably have a better window response time if I did not use the server because I would not need to wait for a response from the server.
Our reasoning for using a server to process all of the light sensor information was quite simple; a server scales well as more SmartWindow's are created. In addition, iOS interfacing with the Node-Red server is much simpler than interfacing with the MSP432 Launchpad because we can take advantage of common communications protocols, such as MQTT, that have already been implemented on the server!
Alright, now that you have picked up your two slices of bread and put them on the table, its time to make the sandwich. The Node-Red server is the glue that holds this project together, and there is a lot of content here that is necessary in order to increase the performance and scalability of our Smart Window system. Before I push you out of the nest and tell you to fly, we are going to walk through the data flow of this entire project before concentrating on the internals of the server.
As you can see, our cloud server must serve as the main interface between each and every platform in our system. A few of the services that our cloud service must provide are:
1 ) Retrieve light sensor data from the MSP432 Launchpad
2) Process user input from an iOS Device
3) Perform Fuzzy Automated Control Logic on the light sensor data to generate an output value for the window circuit
4) Send metadata about the system to the Freeboard GUI using a REST API
5) Send the window circuit output value back to the MSP432 Launchpad
Are you still with me? Good. There is still a lot more to cover! Now we are going to dive into the details of how the node-red server will perform all of the functions listed above. [SPOILER] This next picture will blow your mind.
Holy smokes Batman! That is a complicated data flow! We are going to have to tackle this one module at a time.
mspSensorVal
The first module that we are going to look at is the mspSensorVal module. The mspSensorValue MQTT broker is responsible for receiving packets of information from the MSP432 Launchpad that contain light sensor readings. These messages are sent from the MSP432 as a JSON String, and therefore must be parsed back into a JSON Object by the connecting JSON block. To learn more about JSON and how it works, or why engineers love 4 letter acronyms, click HERE. Below is an image of the MQTT configuration options that we are using for the mspSensorVal block.
Fuzzy Logic
The fuzzy logic function block is where the most cloud processing takes place. Fuzzy logic is a complicated automated control algorithm that uses geometric shapes that vary in size and range and spreading them across the range of valid input values. These geometric shapes are known as membership sets. When an input value is received it's membership grade, or the amount that input value lies within a membership set, is calculated for each of the membership sets. Because each membership grade is guaranteed to be a number in between 0 and 1, we can relate the membership grades using traditional logic statements, such as AND, OR, and NOT. The results of our logic equation are saved applied to a weighted average to calculate our crisp output. The crisp output is the value that server must send back to the MSP432 and the window circuit! Fuzzy logic is not a trivial concept, and my explanation may not seem perfectly clear. If you are still a little confused about how fuzzy logic works, I recommend doing further research on it. Of course, fuzzy logic is not an absolutely necessary component of this project, but it improves performance by calculating very precise and accurate output values for a given input based on parameters you may easily modify.
There are distinct advantages to using a fuzzy logic control system. It is very easy to expand the control system to include more states. In addition, the code is very easy to follow, which is unusual for automatic control systems; there are often some pretty ugly equations involved. Having a system that can adapt easily to new environments and is easy to understand/maintain is a fantastic feature that we incorporated into the SmartWindow system. The fuzzylogic.js file also handles the processing of input from our iOS device. In order to view the full contents of this file, click here.
Stay with me, you have just made it past the most difficult part of the project! It is all smooth sailing from here.
saveFuzzyMessage
Now that we understand how the control loop for the server works, the rest of the flow graph is quite simple. The saveFuzzyMessage group just saves the current message on the server so that other function blocks can access it. By saving the message after all of the fuzzy logic processing, the Freeboard GUI will have access to all of the membership grade set values, and we should be able to see the crisp values that we are sending back to the MSP432! Here is what the saveFuzzyMessage function block should look like.
FormatOutput
The FormatOutput function block is responsible for trimming all of the metadata from our fuzzylogic message so that we only send the window circuit output value back to the MSP432. We took advantage of a template parsing library called Mustache in order to help us parse the JSON Object coming from the fuzzylogic function block.
outputFuzzyVal
This function block is one of the last pieces in the puzzle! It servers as an MQTT broker that the MSP432 can subscribe to in order to pull the latest crisp output from the server! The configuration options are the same as mspSensorVal with a topic of "outputFuzzyVal".
iOSInput
MQTT broker that handles incoming messages from our iOS application. The configuration options are the same as mspSensorVal with a topic of iOSInput".
savePhoneMsgContents
Because the iPhone application is sending JSON Strings we must first parse the input and turn it into a JSON Object. Afterwards, we set global server variables equal to the contents of the current iPhone message. The contents of the savePhoneMsgContents function block have been displayed below.
Lastly, the REST API functionality to interface with Freeboard remains unchanged from adrainF's project.
Congratulations! You know have a fully functioning SmartWindow server! Now it is time to look at the user interface.
iOS SIDE OF THINGS.
The iPhone application for our project provides a user-friendly interface to control the tint. Too tired to get out of bed, but want that sunshine out of your face? Not a problem! Just reach over for your phone and adjust the tint wirelessly. In addition to manually controlling the brightness, the application allows you to choose between a 'manual mode' and an 'automatic mode'. In manual mode, you essentially short-circuit the fuzzy logic calculations, and directly tell the window to tint to your desired level. In automatic mode, fuzzy logic calculations and optical feedback adjust the tint automatically to the ideal tint level. And finally, the app allows you to choose between a 'winter mode' and a 'summer mode'. In summer mode, the behavior of the system is exactly as has been previously described: the more light there is, the more tint will be present. Winter mode changes things up. When you are in automatic mode and winter mode simultaneously, the app will be clearer when there is light present, and tinted when there is no light. This is because, during the cold winter months, you want to allow light in when it is present to warm up the interior. When there is no light present, you want to tint the window to prevent heat from escaping.
Here is the application source code:
https://github.com/RohanTanna/SmartWindowApplication
THE CIRCUIT
Now this is where the real magic happens! When designing our circuit, we kept one important engineering principle in mind: Beauty in Simplicity. As a result, we were able to design an effective circuit with just one N-Channel MOSFET and a resistor. In the following circuit diagram, the SIN wave represents a pin on the MSP432 board that supports PWM (any one of the purple pins in the pin mapping diagram). This pin sends a PWM signal at 500Hz to the gate of the transistor, turning it off and on. Interestingly, the logic is counter-intuitive: when the PWM signal is high, the film does NOT receive a voltage, and vice versa. Here's how this circuit works:
When the PWM signal is low, the FET acts as an infinite impedance. Therefore, all of the current flows through the 4.7K resistor and the film. The resistance of the film is significantly higher than 4.7K (it is in the order of megaohms), and so voltage division tells us that essentially all of the 30 Volts drops across the film.
When the PWM signal is high, the FET acts as a short. Since the film is in parallel with the FET, it is shorted and receives no voltage drop. Thus, all of the voltage drops across the resistor.
Our window can be adjusted between 256 levels of tint. However, what is really cool is that at any given time, the window is actually either completely tinted, or completely clear! This is a result of using PWM. Lets say, for example, we set the tint value to 128 (half of 255). You will see a window that is constantly half-tinted. However, this is your eye playing a trick on you: the window is toggling between being completely tinted and being completely clear at a very fast rate. Our eyes cannot discern toggling at a rate of 50Hz, and the result is a window that is half-tinted all the time! Pretty cool, right?
CONCLUSION
As you can see, SmartWindow is a revolutionary product that has many useful applications. Hopefully, you will stop by our booth on August 4th at the TI DIY Intern Expo and play with our awesome dynamically tinting window!
Keep it classy,
Team TINTerns
Comments