Project tutorial
 Open Data Home Automation - Arduino 101 & App Inventor

Open Data Home Automation - Arduino 101 & App Inventor © GPL3+

Get temperature data from a webpage and compare with Arduino 101's LM35 sensor. And turn a fan on/off according to the comparison result.

  • 2,517 views
  • 1 comment
  • 11 respects

Components and supplies

Ardgen 101
Arduino 101 & Genuino 101
×1
DFRobot Gravity: IO Expansion Shield for Arduino V7.1
If you use components not from DFRobot, this item is not required.
×1
DFRobot Gravity: Digital 5A Relay Module
Or use any compatible relay module.
×1
DFRobot Gravity: Analog LM35 Temperature Sensor For Arduino
You can use LM35 component or DHT11 (code modification required).
×1

Apps and online services

About this project

Introduction

Open data is becoming a popular data source in many respects, and the most common application is the weather station. This project will get temperature open data from a webpage and compare it with the temperature with LM35 sensor on your Arduino 101.

If the sensor data is higher than the web data, this means it's hotter inside. In this case, we will ask Arduino 101 to turn on a small fan (using a relay module). Otherwise the fan will be shut down.

Video

demo video

Hardware

Prepare your hardware as below:

  • Attach Gravity expansion board on your Arduino 101.
  • Attach Gravity LM35 sensor's data pin to Arduino 101's A4 analog pin, and connect 5V/GND to Arduino 101's 5V/GND separately.
  • Attach Gravity relay module's data pin to Arduino 101's D13 digital pin, and connect 5V/GND to Arduino 101's 5V/GND separately.

Finish as below:

Software

This project use Arduino IDE to finish logics on Arduino, and use App Inventor to design a small Android app. The two are communicating through BLE, you can use other BLE-supported board instead.

Arduino

Please check the complete Arduino sketch in this project's attachments. Here we only go through several key commands:

  • #5: declare a new LM35 object (name: temp) and set data pin to Arduino 101's A4 pin.
  • #6~11: set BLE UUID, must be identical with which in your App Inventor. switchCharacteristic is for reading char from App Inventor and LM35Data is for sending LM35 sensor data to App Inventor
  • #12: set relay module attach to Arduino's D13 pin.
  • #17~36: start BLE service (peripheral).
  • #53~70: check which char is sent from App Inventor. Switch relay on when receive 'O' and switch relay off when receive 'C'.
  • #70~78: send LM35 sensor data to App Inventor.

App Inventor - online graphical Android IDE

App Inventor is an online graphical Android IDE hosted by MIT. You can design apps by drag-n-drop components you like, and finish coding like playing puzzles. App Inventor’s app can not only run on emulators and also installed on real Android devices.

For more tutorials please visit: http://www.appinventor.tw

Please log in MIT App Inventor website with your Gmail account. Create a new project, you will first see the Designer page, we will decide which components will be used in this app.

Since AI2’s BLE components is still under testing, therefore it must be import to your project as an extension (.aix).

Please find the “Import extension” option on the left hand side of the Designer page, and click URL and paste this link: http://iot.appinventor.mit.edu/assets/edu.mit.appinventor.ble.aix

App Inventor’s Designer page:

Please add components as figure below:

  • ListPicker x 1: To view parsed JSON result.
  • Button x 2: one is for connect/disconnect from Arduino 101, the other on is for turning on/off Arduino's relay module according to the comparison result.
  • label x 5: show messages.
  • Web x1: get webpage data and parsed JSON format into list.
  • BLE x 1: send/receive data by BLE.
  • Clock x2: Clock1 is for receiving LM35 sensor data from Arduino (Timer Interval: 1000 ms). And Clock2 is for getting JSON data from webpage and parse the result into list(Timer Interval: 1000 ms).

Your Designer should be very similar like figure below, but doesn’t have to be exactly the same.

App Inventor’s Blocks page:

Next please click “Blocks” on the top-right corner of the webpage, it will switch to Blocks, we are going to decide the behavior of this app.

STEP1: Set BLE address and connect

You can find Arduino 101's BLE address on its back side (like 98:4F:EE:0F:42), please modify the addr variable to yours, otherwise your phone will be unable to connect to Arduino 101.

Your phone will scan for available BLE devices nearby when started (Screen1.Initialized event). Please click Button_connect to connect to your Arduino 101, your phone will show different message when BLE device is connected (BluetoothLE.Connected event) or disconnected (BluetoothLE.Disconnected event).

STEP 2: Get LM35 temperature data from Arduino

STEP 3: Get temperature data from JSON webpage

When connected, Arduino 101 will send LM35 sensor data to App Inventor every one second.

App Inventor will get weather content(https://works.ioa.tw/weather/api/weathers/1.json) every 10 second. Actuallt this webpage will update every 20 minutes, we set 10 second here is just for getting result quicker.

You can see how to use [select list item] to get the temperature of Taipei City.

STEP 4: Show JSON result in ListPicker

Here we use JsonTextDecode command of the Web component to parse JSON data into list ,then set the result as ListPicker's element. This is more friendly for us to view and select the data.

ListPicker is for viewing JSON result in list, as below:

STEP 5: Compare data and control Arduino 101

App Inventor will send char 'O' if taipei_data is smaller than temp_data, which means it's hotter inside. Arduino 101 will switch on relay module to turn on a small fan. Otherwise, App Inventor will send char 'C' if taipei_data is larger than temp_data and turn off the fan accordingly.

Play

Please make sure your Arduino 101 is running correctly.

Click Connect button to connect to Arduino; LM35 sensor data should be shown on screen when connected. And Taipei City's temperature should be seen on screen right away, as figure below:

You can click the [show the json format] listpicker to view the JSON data in list.

References

Code

App Inventor .aia Scratch
No preview (download only).
Arduino 101 code - get opendata parse result from Android phone(App Inventor)Arduino
#include <CurieBLE.h>

#include <LM35.h>

LM35 temp(A4);
BLEPeripheral blePeripheral;  // BLE Peripheral Device (the board you're programming)
BLEService RelayService("19B10010-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedCharCharacteristic switchCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEUnsignedIntCharacteristic LM35Data( "19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
const int  RelayPin = 13; // pin to use for the Relay
char control = '0';
int old_data ;

void setup() {
  Serial.begin(9600);

  // set Light pin to output mode
  pinMode( RelayPin, OUTPUT);

  // set advertised local name and service UUID:
  blePeripheral.setLocalName("LM35");
  blePeripheral.setAdvertisedServiceUuid( RelayService.uuid());

  // add service and characteristic:
  blePeripheral.addAttribute(RelayService);
  blePeripheral.addAttribute(switchCharacteristic);
  blePeripheral.addAttribute(LM35Data);
  // set the initial value for the characeristic:
  switchCharacteristic.setValue(0);

  // begin advertising BLE Relay service:
  blePeripheral.begin();

  Serial.println("BLE Relay service.");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLECentral central = blePeripheral.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the Light:
      if (switchCharacteristic.written()) {

        control = switchCharacteristic.value();
        Serial.print("value : ");
        Serial.println(control);
        
        if (control == 'O') {      
          Serial.println("Relay on");
          Serial.println(switchCharacteristic.value());
          digitalWrite(13, HIGH);                           // Open relay
        } else if(control == 'C'){ 
          
          Serial.println(F("Relay off"));
          Serial.println(switchCharacteristic.value());
          digitalWrite(13, LOW);                            // Close Relay
        }
        
      }
      int new_data = temp.cel();
      if(old_data != new_data)
      { 
        LM35Data.setValue(new_data);
        Serial.println(new_data);
        old_data = new_data;
        delay(1000);
      }
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

Schematics

circuit of this project - relay / LM35
001 qwi1gfheoc
Arduino101_relay_lm35
arduino101iot_v6cqNgZGUT.fzz

Comments

Similar projects you might like

Control RGB LED by Dragging – Arduino 101 & App Inventor

Project tutorial by DFRobot and CAVEDU Education

  • 4,446 views
  • 0 comments
  • 13 respects

Arduino 101 BLE App

Project in progress by Alexis Santiago Allende

  • 10,794 views
  • 26 comments
  • 51 respects

Home Automation With Arduino 101 Using Bluetooth Low Energy

Project tutorial by naveen manwani

  • 1,770 views
  • 0 comments
  • 4 respects

Arduino 101 Home BLE System

Project tutorial by Alexis Santiago Allende

  • 2,625 views
  • 0 comments
  • 12 respects

Smart Garbage Monitoring System Using Arduino 101

Project tutorial by Technovation

  • 22,750 views
  • 7 comments
  • 37 respects
Add projectSign up / Login