Machine to Machine Communication Vehicle Sensor
This lab is available as part of my online portfolio at: https://lehi.dev/labs/lab3
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.
- 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
- Jump wires
- Soldering iron
- GPIO pinout guide
The following resources were used in this lab:
- https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/ – Great tutorial on how to use Ultrasonic Sensor HC-SR04
- https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/ – Another great resource explaining how Ultrasonic Sensor HC-SR04 works.
- https://techtutorialsx.com/2016/07/17/esp8266-http-get-requests/ – Good resouce to understand how to send a GET request protocol.
- https://www.arduino.cc/reference/en/language/structure/control-structure/for/ – Arduino documentation about “For” loop implementation.
- https://www.arduino.cc/reference/en/language/variables/data-types/array/ – Arduino documentation about Array Data structure implementation.
- https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/ – Arduino documentation about Switch case implementation.
- http://www.cplusplus.com/forum/general/34133/ – passing by reference good explanation.
- https://www.arduino.cc/en/Tutorial/StringAdditionOperator – Arduino documentation about String Concatenation.
- https://www.tutorialspoint.com/arduino/arduino_passing_arrays_to_functions.htm – Passing arrays to functions good explanantion.
- https://learningsuite.byu.edu/plugins/Upload/fileDownload.php?fileId=70f7d913-ioSF-xMVW-I06U-m67a1cc00e96 – Lab write up example.
- https://github.com/todddb/example-lab/blob/master/example-lab/example-lab.ino – Greate example on how to get started with Arduino and get a server up and running.
- http://www.esp8266learning.com/wemos-webserver-example.php – great arduino webserver example.
- http://www.instructables.com/id/Programming-the-WeMos-Using-Arduino-SoftwareIDE/ – programming wemos D1 mini
- https://www.w3schools.com/howto/howto_css_switch.asp – Good css slider
- https://www.arduino.cc/en/Main/Software – Arduino IDE software download
- http://fritzing.org/download/ – Fritzing is a great tool to visualize electronics prototype.
- Solder the headers on 2 Wemos D1 mini in order to utilize the breadboard and jump wires and LEDs and Ultrasonic Sensor HC-SR04.
- Download and install Arduino IDE from https://www.arduino.cc/en/Main/Software
- 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
- Add Wemos D1 library to the Arduino IDE:
- 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)
- Tools > Board > Boards Manager > (search for “esp8266”) then click install.
- Tools > Board > “LOLIN(WEMOS) D1 R2 & mini”
- Then selected the connected arduino by going to:
- Tools > Port > /dev/cu.wchusbserial14510
- Used the Arduino IDE to develop the code for the stoplight and Ultrasonic Sensor HC-SR04.
- Used the Arduino GPIO pinout to decide which pins to use for this project from https://wiki.wemos.cc/products:d1:d1_mini#documentation.
- 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.
- 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.
- I Chose the following pins to be on:
- Pin D6: Green color
- Pin D8: Yellow color
- Pin D1: Red color
- Pin G: Ground
- I Chose the following pins to be on:
- I had one Ground pin from GPIO to breadboard negative column, then connected 220 Ω resistors to LEDs negative end point.
- 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.
- The logic set up was the following in pseudo code:
- if distance > farAway then turn on green led light
- if distance > caution then turn on yellow led light
- if distance > perfectDistance then turn on red led light
- if distance > tooFar then blink the turning on yellow led light
- 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.
- Turn on the led that should be on
- 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.
- System Flow: See Appendix 1.
- 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.
- Program the code in the Arduino IDE
- Code the Arduino GPIO pins interaction with breadboards, resistors, LEDs, Ultrasonic Sensor HC-SR04 accordingly to match the previous component/schematic diagram.
- 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 .
- 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.
- 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.
- 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 1: System Flowchart
Appendix 2: Stoplight Server Monitor Output
Appendix 3: Ultrasonic Sensor HC-SR04 Monitor Output
Appendix 4: Arduino Code
(Available at https://github.com/ylehilds/arduinoStoplight)