lab3

Machine to Machine Communication Vehicle Sensor

Online Link

This lab is available as part of my online portfolio at: https://lehi.dev/labs/lab3

 

Objective

The purpose of this lab is to learn how to create a machine to machine communication, vehicle sensor, wifi-controlled stoplight using Arduino. These principles include:

  • Become more familiar with Arduino IDE in order to use in conjunction with Wemos D1 Mini microcontrollers and Ultrasonic Sensor HC-SR04.
  • Enumerate requirements from use case and user stories.
  • Develop a minimum viable product and refactor it to account for new requirements.
  • Become more familiar with representing the desired system by using flowcharts, state diagrams, etc.
  • Learn to plan what materials will be necessary to accomplish the system’s purpose
  • Build an Iot sensor by utilizing GPIO pins for inputs.
  • Establish a machine to machine communication protocol.
  • Learn how to design an IoT interaction between a sensor and an acuator.

Materials

  • Personal Computer
  • 2 x Wemos D1 Mini microcontrollers (ESP8266)
  • 2 x 2.5A USB Power Supply with micro USB Cable
  • 2 x breadboard
  • 1 x Ultrasonic Sensor HC-SR04
  • 1 x Red LED
  • 1 x Yellow LED
  • 1 x Green LED
  • 3 x 220 Ω Resistor (RRBlBlBr)
  • Arduino IDE
  • Fritzing
  • Jump wires
  • Soldering iron
  • Solder
  • GPIO pinout guide

References

The following resources were used in this lab:

 

Procedures

  1. Solder the headers on 2 Wemos D1 mini in order to utilize the breadboard and jump wires and LEDs and Ultrasonic Sensor HC-SR04.
  2. Download and install Arduino IDE from https://www.arduino.cc/en/Main/Software
  3. Install CH341 version 1.4 from the following website: https://kig.re/2014/12/31/how-to-use-arduino-nano-mini-pro-with-CH340G-on-mac-osx-yosemite.html
  4. Add Wemos D1 library to the Arduino IDE:
    1. Arduino > Preferences > (in addition Boards Manager URLs paste the following: https://kig.re/2014/12/31/how-to-use-arduino-nano-mini-pro-with-CH340G-on-mac-osx-yosemite.html)
    2. Tools > Board > Boards Manager > (search for “esp8266”) then click install.
    3. Tools > Board > “LOLIN(WEMOS) D1 R2 & mini”
    4. Then selected the connected arduino by going to:
      1. Tools > Port > /dev/cu.wchusbserial14510
  5. Used the Arduino IDE to develop the code for the stoplight and Ultrasonic Sensor HC-SR04.
  6. Used the Arduino GPIO pinout to decide which pins to use for this project from https://wiki.wemos.cc/products:d1:d1_mini#documentation.
  7. In version 1 I wanted to design a system that all it would do was to measure the distance in centimeters of an object and the Ultrasonic Sensor HC-SR04 and print out the distance using Serial Monitor.
  8. I then reused the existing lab 2 source code and breadboard with Wemos D1 mini, resistors and led lights and ran a web server to accept commands to turn on and off led lights.
    1. I Chose the following pins to be on:
      1. Pin D6: Green color
      2. Pin D8: Yellow color
      3. Pin D1: Red color
      4. Pin G: Ground

    1. I had one Ground pin from GPIO to breadboard negative column, then connected 220 Ω resistors to LEDs negative end point.
  1. In version 2, once I got the Ultrasonic Sensor HC-SR04 working and printing out the distance of an object in comparison to the Ultrasonic Sensor HC-SR04. I then started the process to sending a GET request protocol to the existing arduino stoplight.

  1. The logic set up was the following in pseudo code:
    1. if distance > farAway then turn on green led light    
    2. if distance > caution then turn on yellow led light
    3. if distance > perfectDistance then turn on red led light
    4. if distance > tooFar then blink the turning on yellow led light
    5. Keep track which led is currently on, it will then be used to turn off certain lights that should be off as the state changes.
    6. Turn on the led that should be on
  2. Models:
    1. Functionality: State machine as shown on the diagram below, this is a simplified form of the state machine as in reality I have created many endpoints to change the state machine for each led, but the basic concept is described below for each endpoint it reacts as the following diagram:

  • On Start up, the machine will be on LED Off state
  • Case “LED OFF”
    • If system receives /ledOn then system changes to “LED ON” state.
    • If system receives /ledOff then system remain in “LED OFF” state.
  • Case “LED ON”
    • If system receives /ledOn then system remains in “LED ON” state.
    • If system receives /ledOff then system changes to “LED OFF” state.
    1. System Flow: See Appendix 1.
    2. Component and Schematic Diagram: The stoplight system and the Ultrasonic Sensor HC-SR04 are represented below by the following diagrams (Component and Schematic Diagram respectively)

 

 

 

 

Diagrams were facilitated by open source programs such as: Google drive plugin draw.io Diagrams and Fritzing.

 

  1. Program the code in the Arduino IDE
    1. Code the Arduino GPIO pins interaction with breadboards, resistors, LEDs, Ultrasonic Sensor HC-SR04 accordingly to match the previous component/schematic diagram.
    2. Test/Debug constantly so the final product would be bug free. You can debug by first making sure the baud rate is set up in setup method. Then click on serial monitor icon  .
    3. Use commands such as Serial.println(<variableToBeEvaluated>) to print out debug messages, so you can make sure what you think the code is doing is actually happening.
    4. I had some trouble uploading code to arduino and opening up the Serial monitor for debugging purposes. I had to resort if wanting to debug, to unplug the echo  jump wire and then open up the Serial monitor for debugging only on the beginning of the code run.

Thought Questions

  • Think of the interaction between your devices. How well would this scale to multiple devices? Is it easy to add another sensor? Another actuator? It would be harder to keep all devices coordinated, it would be a lot of code to keep all devices synchronized. Perhaps one more it wouldn’t be so bad, but as devices get added to the system it would be very difficult to keep all devices in sync and have a reliable system.
  • What are strengths and weaknesses of the tennis-ball-on-a-string system that Don had originally? What are strengths and weaknesses of the IoT system that he developed? What enhancements would you suggest? The strengths is that it is simple to implement , but Don had too many issues in the past as his kids got older, it was being pulled down regularly, and becoming a nuisance how frequently he had to keep re-attaching it. It was becoming a baseball ball practice. The strengths of IoT would be it would be a more precise way to let the driver know when he/she should stop and let them know if they have gone too far and that is something the tennis ball did not tell the driver. It is more customizable meaning you can add many more features in order to help the driver. As you add more features it will be more complicated to keep all the features working well, meaning maintenance will increase as the system evolves so that is a weakness. An enhancement I would suggest would be to have a sensor at to check at night time whether the garage light is on, or the car lights are on and notify the driver, but this would be helpful at night time, this would save the driver some money and maybe save the car battery from depleting itself.
  • What was the biggest challenge you overcame in this lab? The biggest challenge I overcame in this lab was to make the Ultrasonic Sensor HC-SR04 and the Serial monitor prints to work well together, it was failing on me for some reason, which I overcame for debugging reason to disconnect the echo jump wire after code upload phase. Another challenge was to work with a few data structures in arduino language, since it is a new language to me I had to do a few research on it (for loops, array, switch cases, passing array to function).
  • Please estimate the total time you spent on this lab and report.
    • 3 hour building and refactoring the circuit
    • 8 hours coding the server and fixing bugs and getting acquainted with the Ultrasonic Sensor HC-SR04, debugging issue.
    • 4 hours working on the lab report using tools such as Google draw.io Diagrams  and fritzing.
    • 2 hours publishing a website with lab 1 instructions, mainly dealing with formats.

 

Certification of Work

I certify that the solution presented in this lab represents my own work. In the case where I have borrowed code or ideas from another person, I have provided a link to the author’s work in the references, and included a citation in the comments of my code.

 

— Lehi Alcantara

Appendix

Appendix 1: System Flowchart

Appendix 2: Stoplight Server Monitor Output

Appendix 3: Ultrasonic Sensor HC-SR04 Monitor Output

Appendix 4: Arduino Code

Sensor.ino:

(Available at https://github.com/ylehilds/iot-sensor/blob/master/sensor/sensor.ino)

arduinoStoplight.ino:

(Available at https://github.com/ylehilds/arduinoStoplight)