We have heard of the internet of things(aka IOT) for many years. And we actually have used many products that are equipped with IOT features too. Like amazon echo and the Nest Thermostats.
So what exactly is the IOT and is it possible for anyone to build an IOT product.
From what I can understand, IOT simply means a device can be controlled over the internet from a wired or wireless technology.
That's to say the traditional TV remote control isn’t an IOT device as it simply uses infrared lights to communicate with TV, and no internet is involved. While your smart refrigerator is an IOT device as it uses WiFi to connect to the internet so that the refrigerator is able to report its status to your phone.
Here comes to the second question.Is it possible for anyone to build an IOT product. The answer is definitely yes. And it just becomes much easier too compared with when the IOT concept just emerged.
In this series I will try to explain how to build an IOT featured product from just a concept steps by steps. So that once you get an idea in your mind, you can easily build it in reality.
What to build:I have been thinking about what demo to build for a long time, as it needs to be easy to demo and also I hope it can also be useful instead of just a demo. Until recently I found out that the smart plug could be a good start point as it’s theoretically not complex. Which can meet our design goal here as it’s easy to design but enough todemo. The only concern is we are dealing with high voltage when debugging. So We just need to pay attention to it.
Product Requirements Docs first:Before we hands on the real work, we need to think about what features/functions this smart plug needs to have. Usually call it PRD, the product requirements document. I of course will not write a separate document in this chapter.
I will just list down a tiny PRD below
- Must use 2.4G or 5G WiFi to connect to the internet.
- Must have one high power relay to control the plug on and off.
- Must have a button to control the plug on and off manually.
- Must have the ability to monitor the voltage and current and power used by devices that are plugged in.
- Must be able to be controlled and show power data by
- The plug itself should be ultra energy efficient.
As I said, it’s a tiny PRD, and also pretty blurry in spec. Like I didn’t mention how it connects to the internet over WiFi. What is the maximum current this plug is rated for? And how accurate the power measurement is. Those are important factors when defining the real PRD.And we will cover as many items as possible later.
Function Diagram:We have defined the requirements for this plug. So what’s next? Well, we just need to draw the structure, so we willl have a more clear understanding about how to achieve those requirements
So figure 1 shows, the smart plug is basically composed by below items:
DPST relay: which will be used to turn on or off the power line for any product that is connected to this smart plug.
- DPST relay: which will be used to turn on or off the power line for any product that is connected to this smart plug.
- Power Monitor IC: a dedicated chip that will be used to monitor the voltage, current consumption and the total power consumption by product.
- AC to DC converter: This converter will deliver DC power source for the MCU and WiFi module and the power monitor IC as long as driving the DPST relay.
- MCU + WiFi module: The brain of this plug, it will be connected to the internet for users to control the plug and retrieve information from the power monitor IC.
- Finally, an enclosure that can pack all of them, so that it can be a product.
We are almost in the real design stage, But before that, let’s make the next hands on stage easier. So in this part, we are going to materialize the above figure 1 structure.
MCU comes first. There are several factors for selecting the right chip.
- Engineers always prefer to choose the MCU they are familiar with.
- The hardware resources for this MCU need to meet the demand, in our case, we only need some GPIOs and a SPI interface.
- Make sure there is no silicon shortage for it anymore.
- There are many MCU integrated with WiFi features, so we should try to find one which is already embedded with WiFi, that will simplify the design significantly.
- If that chip happens to have cloud connectivity. Then it will definitely speed up the timeline from design to market.
- Last but not least, the price. Make sure it’s cheap enough to get the work done.
With the above requirements, we are going to choose P2 from Particle as the MCU. It’s in fact a module with WiFi feature, not a chip anymore. Which can make the design even simpler.
Figure 2
The next part that matters in our design would be the power relay.
A generic SPST Relay which rated as 20A at 125VAC would be sufficient in our design. I chose SRA-12VDC-CL which should meet our requirements.
Now we are going to decide which power monitor IC to use. This IC plays an important role for the plug’s functionality. Like the accuracy of the current and voltage. Or if there are any other features.
After doing some search, I found out there are many chips from Prolific like PL7221 or PL7223. However, their datasheet is not open sourced and I need to contact the sales representative, which I don’t want to spend time on right now. So I decided to use MCP39F521 as the monitor IC. And after I dig into their document, I find out they have a demo board which also helped to address the AC to DC converter selection.
The next part that matters in our design would be the power relay.
A generic SPST Relay which rated as 20A at 125VAC would be sufficient in our design. I chose SRA-12VDC-CL which should meet our requirements.
Now we are going to decide which power monitor IC to use. This IC plays an important role for the plug’s functionality. Like the accuracy of the current and voltage. Or if there are any other features.
After doing some search, I found out there are many chips from Prolific like PL7221 or PL7223. However, their datasheet is not open sourced and I need to contact the sales representative, which I don’t want to spend time on right now. So I decided to use MCP39F521 as the monitor IC. And after I dig into their document, I find out they have a demo board which also helped to address the AC to DC converter selection.
AC to DC converter is supposed to be a headache for me as it needs quite a lot of analog experience to design the converter efficiently and safely, especially when I need to put a AC to DC converter in a pretty tight space. But after I read the datasheet for this Demo board. I just realized it can be pretty easy if I use the LNK30x series. The only concern is if it’s able to deliver enough power in our application. So I built this table to calculate the total power consumption that is needed from this chip.
So that’s literally 1.7484w needed for all those components. If we choose a 12V to 3.3V DC-DC regulator with at least 80% converter efficiency.then we can figure out the minimal power required for the AC-DC converter would be 2w in total. 0.6w+(1.0725w+0.0429w+0.003w)/0.8 = 1.998w
Lucky enough, the LNK306 is able to meet our demand as the maximum power it can deliver is 2.7w(12v/225mA). We still have around 0.7w power margin. Which should be pretty safe in our design.
Prototyping:Finally, we are in the hands on stage. In this part, our target is to build an MVP(aka most viable product) from both hardware and firmware’s perspective.
Hardware:In order to quickly prove the functionality of the hardware that was selected, we are going to use P2 HDK(aka hardware develop kit) and the MCP39F521 eval board along with a relay breakout board and a DC-DC regulator for prototyping. Our goal is to validate all the functionality for the smart plug based on the tiny PRD by using all the hardwares that we assembled together without any schematic design and PCB layout work.
So below is what we assembled for the prototype.
Firmware:
As a hardware engineer. I’m definitely not an expert on coding. Let alone how to send the voltage/current value from monitor IC to the internet. But as I mentioned before, nowadays, it’s just way easier to build an IOT product. Like the P2 module I selected above, It just addressed all the difficult parts for me like how to connect a controller to the internet and how to send data to the cloud etc.Basically I just need to invoke some function which will publish the data to the cloud and I can also subscribe to the event so that the mcu will know it’s time to turn on or off the relay.
What's more, during the prototype stage, I can easily verify all the functions by using the particle console
As for the coding complexity, that P2 module can be coded by a seasoned embedded fw engineer by using C/C++. And it can be an arduino coding style too. So anyone who can blink the led on an arduino dev board can easily blink the led from the cloud on the P2 hardware.
Below is the firmware architecture that I used to achieve all the functions defined by PRD.
Basically, there are two ways to control the relay for the plug, through the physical button or by a command transmitted by wireless.The physical button will always have the top priority and will simply toggle the plug’s current ON and OFF status once it’s pressed, while the command will simply put the plug into a fixed status. That's to say if the plug had been turned on by button, then a command to turn on the plug will not impact the current plug’s status as it’s already turned on.
The energy monitor value will be reported to GUI as scheduled interval.Including the voltage, current and power consumption from any devices that are plugged in.
Below is the sample arduino type code which runs on the P2 HDK board.
void setup() {
gpioInit();
Serial.begin(9600); //turn on serial communication
smartPlug.begin(); // Pass in the appropriate address. Defaults to 0x74
//register particle variables and functions call.
Particle.variable("lineVoltage_V",lineVoltage);
Particle.variable("lineCurrent_A",lineCurrent);
Particle.variable("lineFrequency_HZ",lineFrequency);
Particle.variable("activePower_W",activePower);
Particle.variable("plugStatus",plugStatus);
Particle.function("plugRemoteControl", plugRemoteControl);
}
unsigned long readInterval = 0;
void loop(){
uint8_t btnState = buttonStat();
if (btnState==1){
plugStatus =!plugStatus;
digitalWrite(RELAY,plugStatus?HIGH:LOW);
digitalWrite(LED,plugStatus?HIGH:LOW);
}else if(btnState==2){
//Serial.println("long press");
}
if(millis()-readInterval>=1000){
readInterval = millis();
UpbeatLabs_MCP39F521_Data data;
UpbeatLabs_MCP39F521_FormattedData fData;
int readMCPretval = smartPlug.read(&data, NULL);
if (readMCPretval == UpbeatLabs_MCP39F521::SUCCESS) {
smartPlug.convertRawData(&data, &fData);
lineVoltage = fData.voltageRMS;
lineCurrent = fData.currentRMS;
lineFrequency = fData.lineFrequency;
activePower = fData.activePower;
} else {
Serial.printf("Error returned!%d\r\n",readMCPretval);
}
}
}Figure 7
The Demo:In this demo, We had successfully controlled the light bulb on and off by the IOT ways and also the manual finger press. It also reports the voltage, current as long as the power. Which basically meet our goals.
Conclusion:As promised, in this chapter, I had discussed how to build a quick prototype from a rough concept in our mind. Currently, the above demo shows the components we selected can totally meet our requirements.
In the next chapter, we are going to do more design work from both the hardware and software side. And we are going to achieve below goals:
Design a smart plug which is able to meet the EVT standard
- Design a smart plug which is able to meet the EVT standard
Covers all the functions that already exist on the similar products in the market.
- Covers all the functions that already exist on the similar products in the market.
A GUI on the desktop to show all the above features.
- A GUI on the desktop to show all the above features.
Finally, it’s a real smart plug you can use like an off-the-shelf one.
- Finally, it’s a real smart plug you can use like an off-the-shelf one.
Stay tuned.-:)


Comments