Professional Documents
Culture Documents
04 Pw4 Dec50132 Prosedure
04 Pw4 Dec50132 Prosedure
04 Pw4 Dec50132 Prosedure
P4 60%
Result 20%
LEARNING OUTCOME:
Upon completion of this practical work, students should be able to:
1. Use MIT App Inventors 2 (AI2) to build android apps to send data to the Thing Speak
platform.
2. Control LED and Buzzer on ESP32 DEVKIT NodeMCU-32S from MIT App
3. Use MIT App inventors 2 (AI2) to display humidity and temperature read from Thing
Speak platform Display data from sensor to web browser.
APPARATUS / EQUIPMENT:
INTRODUCTION / THEORY:
MIT App Inventor is an open-source web application for Android. Originally it was created
by Google, but it is maintained by the Massachusetts Institute of Technology (MIT). MIT app
inventor used to create software applications for Android. MIT app inventor uses graphical
interface, in which users can drag-and-drop visual objects to create an application that can run
on Android devices.
After designing app on MIT app inventor, it can be download it on Android phone using the
QR code, or APK file on PC before install it on smartphone. The App Inventor servers store
user work and help them to keep track of their projects as illustrate in Figure 1.
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
1
Figure 1: MIT App Inventor 2 Illustration
App Inventor consists of the Designer and the Blocks Editor. These are described in detail in
Figure 2 and Figure 3.
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
2
Figure 3: MIT App Inventor Block Editor Window
LED
Channe
LED
l
ESP32 Android
Phone with
MIT App
Figure 4: IoT Application with MIT App Inventor and ThingSpeak Platform
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
3
The user sends commands (ON or OFF) to LED using MIT App Inventor, by writing them
on ThingSpeak.com LED Channel
The ThingSpeak.com will collect and store data in the cloud.
The local unit (ESP32) will capture the data by reading the data from ThingSpeak.com LED
Channel and active the LED (ON or OFF)
An Android App will be also reading data from the ThingSpeak.com Status Channel and
displaying them for the user.
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
4
Figure 6: API view
1. Connect the LED to pin 23 which refers to GPIO 23 using jumper wire (Figure 7)
GPIO23 →LED1
GPIO22 → BUZZER
5V → + (DHT Sensor)
GND → - (DHT Sensor)
GPIO25 → out (DHT Sensor)
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
5
2. Once the circuit ready, open Arduino IDE software and rewrite the code shown in Appendix
1. Change:
● ssid
● password
● channel_ID
● readAPIKey
● writeAPIKey
3. Upload the sketch. Before clicking the upload button, go to Tools → Board, and select the
board you’re using. Make sure that Board Node MCU-32S and select ESP32’s COM port.
4. Press the upload button and wait for the “Done uploading.” message.
5. Open Serial Monitor, wait until WiFi connected.
6. Observe the message printing on Serial Monitor and ThingSpeak (Field 2 and Field 3).
Write your observation in Practical Work Assessment Form.
3. Fill in your project name as Figure 9 and it will saved into your project folder as Figure
10:
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
6
Figure 10: My Project Folder
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
7
Figure 12: User Interface Figure 13: Main Screen
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
8
Figure 16: Components in Designer Editor
7. Click on component Web1 and copy “write channel feed Url” from ThingSpeak to Web
url (Figure 17). Repeat the same step on Web2.
(1)
(3)
(2)
Figure 17: Copy url from (Write Channel) ThingSpeak to (Web1) MIT App
WEB 1 URL:
(3) → GET https://api.thingspeak.com/update?api_key=XXXXXXXXXX&field1=1
WEB 2 URL:
(3) → GET https://api.thingspeak.com/update?api_key=XXXXXXXXXX&field1=0
Note: Replace xxxxxxxxxx is your write API Key
8. Click Blocks Editor button and the window will appear as Figure 18
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
9
Figure 18: Components in Designer Editor
Note: These windows are linked: changes made in the Designer will be
immediately reflected in the Blocks Editor.
9. On the Blocks Screen1, click Button 1 and select the block as display in Figure 19,
Repeat the same procedure for Button 2 and you will get the display as Figure 20.
10. On the Blocks Screen1, click Web 1 and select the block as display in Figure 21,
Repeat the same procedure for Web 2 and you will get the display as Figure 22
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
10
Figure 21: Select Button 1 and Button 2 Figure 22: Viewer Window
11. Click on pull down menu Build > App (provide QR code for APK) to download QR code
OR Build> App (save.apk to my computer) to download apk file then copy and install
the apk file at android directory to run apps on android platform.
12. At your App, click button LED ON, then observe (1) the message printing on Serial
Monitor, (2) ThingSpeak (Field 1) and (3) LED1 on board DEVKIT-ESp32. Write your
observation in Practical Work Assessment Form.
13. Repeat Step 12 above while clicking the button LED OFF.
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
11
Figure 23: App Designer
Layout: VerticalArrangement1
User Interface: Label1, Label2, Label3
Layout: VerticalArrangement2
User interface: Label4, Label5
Connectivity: Web1, Web 2
Sensors: Clock1
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
12
Url
https://api.thingspeak.com/channels/xxxxxx/fields/2/last.txt?api_key=yyyyyyyy
Web 2 Properties:
Url
https://api.thingspeak.com/channels/xxxxxx/fields/3/last.txt?api_key=yyyyyyyyyy
Note: Replace xxxxxx with your Channel ID and replace yyyyyyyyyy with your read API
Key
Clock 1 Properties:
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
13
Figure 24: Blocks Code
5. Click on pull down menu Build > App (provide QR code for APK) to download QR code
OR Build> App (save.apk to my computer) to download apk file then copy and install the
apk file at android directory to run apps on android platform.
6. At your App, observe (1) the message printing on Serial Monitor and (2) ThingSpeak (Field
2 and Field 3). Write your observation in Practical Work Assessment Form.
TASK:
a. Modify your app to add another TWO (2) button and create function as below:
• If button (first) is click, then send data 2 to Field1 (ThingSpeak)
• If button (second) is click, then send data 3 to Field1 (ThingSpeak)
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
14
APPENDIX 1
#include "ThingSpeak.h" //ThingSpeak by MathWorks Version 1.5.0
#include "DHTesp.h" //DHTesp by beegee_tokyo Version 1.17.0
#include <WiFi.h>
//Mapping Library
WiFiClient client;
DHTesp dht;
//Global Variable
unsigned long task1Tick=0;
unsigned long task2Tick=0;
unsigned long ledTick=0;
float temperature=0.0,humidity=0.0;
const unsigned int TIME_INTERVAL_TASK1 = 15000; //15 Sec
const unsigned int TIME_INTERVAL_TASK2 = 10000; //10 sec
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//------------------------------------------------
void setup() {
Serial.begin(115200); //Initialize serial
pinMode(ledPin,OUTPUT);
pinMode(led1,OUTPUT);
setup_wifi();
dht.setup(dhtPin,DHTesp::DHT22);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
int statusCode = 0;
//Task 1
//=================================================================
if(millis()>task1Tick){
task1Tick = millis()+ TIME_INTERVAL_TASK1;
unsigned int dataTS = ThingSpeak.readIntField(channel_ID, fieldLedState, readAPIKey);
// Check the status of the read operation to see if it was successful
statusCode = ThingSpeak.getLastReadStatus();
if(statusCode == 200){
Serial.println("Data From MIT: " + String(dataTS));
}
else{
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
}
//Application on ESP32 board
if(dataTS == 0){
Serial.println("LED1 OFF");
digitalWrite(led1,LOW);
}
else if(dataTS == 1){
Serial.println("LED1 ON");
digitalWrite(led1,HIGH);
}
}
//END TASK 1
//Task 2
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
16
//=================================================================
if(millis()>task2Tick){
task2Tick = millis()+ TIME_INTERVAL_TASK2;
humidity = dht.getHumidity();
temperature = dht.getTemperature();
Serial.print(dht.getStatusString());
Serial.print("\t");
Serial.print(temperature, 1);
Serial.print("\t\t");
Serial.print(humidity, 1);
Serial.println();
ThingSpeak.setField(fieldTemperature, temperature);
ThingSpeak.setField(fieldHumidity, humidity);
Lab work 4: IoT Application with MIT App Inventor and ThingSpeak Platform
17