Software apps and online services
Hand tools and fabrication machines
Today we will learn how to Control Appliances and display sensor data using Web app over the Internet.
You have heard the name of NodeMCU ESP8266 WiFi Development Board. A lot of guys wrote introductions and tons of tutorials are present here on the internet. So, I'm going to focus on
- NodeMCU Setup to upload Code using Arduino.ide
- Connecting to a WiFi network and getting IP Address
- control appliances and display sensor data using a web app.
Install ESP8266 library in your Arduino IDE and upload the sketch given into NodeMcu and make the connections as given in schematics and make sure that you receive data in the Serial Monitor.Step 2: Configure AWS Instance
Go to AWS, click on Create an AWS account. If you are already registered, sign in, else create a new account. A new account lets you try the free tier for 12 months. Choose Personal Account unless you have a business setup for which you will need the company details.
You will have to enter your card details for a small refundable charge to verify your identity. Once the registration is confirmed, you can go to the console.
Now that the account is setup, you are ready to launch an EC2 instance. Select EC2 from the Services dropdown (top-left), click on Launch Instance and select Free Tier Only.
Select Amazon Linux AMI (which is usually the first option). On the next screen, select t2.micro (will be highlighted as Free tier eligible).
Select Review and Launch (other settings can be configured later if needed). The next screen might show a warning about the security of the instance and changing security group settings. You can ignore this for now. The only open port is 22 by default which is used for SSH access to the instance. Go ahead and click Launch.
On the next screen, you will be asked to create a key pair which allows you to SSH into your instance. Follow the instructions to create a custom name key pair and store it somewhere on your PC where you can access it later and in the future too.
Select Launch Instances and wait for your instance to be launched. On the final screen, you can select View Instances to go to your EC2 dashboard where you will see the instance up and running in a few minutes.
Your EC2 instance will be running now and you will be automatically assigned a Public DNS and an IPv4 Public IP which you will use to SSH into the instance.
SSH into your instance
Depending on the OS you are using on your PC, there are different methods to SSH into your instance. You can see the applicable method by clicking on Connect in the above screen. Since I use Windows OS, I use a third party SSH client called PuTTY to connect to the instance. You can follow the official tutorialon AWS docs for using PuTTY as the SSH client.
The key pair that you saved while creating the EC2 instance will be used now to connect via SSH. Once you have established the connection you will see a terminal similar to this.
Get Elastic IP for the instance to make sure you get the public IP (DNS) to provide it to your ESP code in URL section.Step 3: Install Node js in AWS instance
- Add Node.js Yum RepositoryFirst of all, you need to enable node.js yum repository in your system provided by the Node.js official website. You also need development tools to build native add-ons to be installed on your system.
For Latest Release
yum install -y gcc-c++ make
curl -sL https://rpm.nodesource.com/setup_11.x | sudo -E bash -
yum install nodejs
Check Node.js and NPM Version
Step 4: Run code on AWS instance
Download the Project files and unzip it and make a Git repository and move the web app folder contents and Git clone them in AWS instance or You can also copy the folder from your laptop to AWS instance using SCP method.
And navigate to your web app contents folder in AWS instance and run the server.js using the command
Keep the app running
The app will run as long as you have the terminal open and you have not stopped it manually. But as soon as you close the terminal or press Ctrl-C or its equivalent, the app will stop as its process is not running in the background, it’s tied to the active terminal. You can write a script to run the process in the background but you will also have to take care of restarting the process in the background in case it crashes or if you perform a pull from the repository or if you update the app in some other way. But there’s an easier way to do all this by using a process manager for node called PM2. We will focus on the auto-restart and background process functionalities of PM2 here. More functionalities in a later article on continuous integration.
Kill all the running node processes (if any) —
killall -9 node. Install PM2 globally with the following command:
npm install pm2 -g
Move to the cloned folder and execute one of the following commands depending on how you had started the app before:
pm2 start server.js
Now even if you close the terminal and check the url in the browser, the app will be running. Sweet! For automatically running PM2 when the server restarts, issue the following command:Step 5: Voila!!!
Connect the kit to power and test it with web app and check for the display of sensor data and controlling the appliances or load.The video: