Close It System
This manual is made for my study Communication & Multi-Media Design at the University of Amsterdam.
3rd year student
Internet of Things
Teacher: Harm van Vugt.
Supplies
- Download and Install Arduino: https://www.arduino.cc/en/Main/Software
- Make sure you have a 'NodeMCU' (version B) Development Kit.
- Download the software for the NodeMCU kit: https://github.com/MPParsley/ch340g-ch34g-ch34x-m...
- Adafruit account: https://io.adafruit.com
- Servo Motor
- Wires (see picture)
When you have all the supplies, you are ready to start!
Install the Arduino IO Libraries
Start with opening your Arduino.
We use Adafruit IO platform (MQTT protocol) because we want to make the sensor data online visible in a feed. So that it is possible to operate the system via your desktop or mobile.
If you want to know more about Adafruit: https://learn.adafruit.com/series/adafruit-io-basi...
To communicate with Adafruit IO you have to install a few extra libraries.
So in Arduino, we go to Sketch > Include Library > Manage Libraries. (Image 1)
When you get to the Library Manager you want to typ in the search bar 'Adafruit IO Arduino' and install the latest version. (Image 2)
You also need to download 'Adafruit MQTT', 'ArduinoHttpClient' and 'Adafruit Neopixel' the same way.
If you have done all the things above, you restart your Arduino and go to the next step.
Rainradar.ino
The rain radar code retrieves the rain data and indicates whether it will rain in the next 25 minutes.
First, you want to add the Adafruit Neopixel and WiFi of your NodeMCU at the beginning of your code to make a connection.
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> #include "Adafruit_NeoPixel.h"
Then we want to connect with the wifi server to the location where you are at. So you might wanna change the server to your own wifi network at home or a local one.
const char* ssid = "IOT"; const char* password = "!HVAIOT!";
You want the server to get the data from de buienradar api.
String server = "http://gpsgadget.buienradar.nl/data/raintext/";
The coordinates in this code are for Amsterdam. But you might live somewhere else where you want this system to work. If so change this code into the coordinates of your own area.
//Amsterdam String lat = "?lat=52.36"; //NB String lon = "&lon=4.91"; //OL
And last but not least add this line of rain data.
int rainData[] = { 100, 200, 000, 000, 000, 000, 000, 000 };
You are now ready to go to the next step!
Get the Rain Data
The code I will explain next is the code which makes you get the rain data from the 'buienradar ' API and use it.
You start by calling the functions. With 'getRainData' you bring in the rain data of the 'buienradar' website. The 'evaluateRain' looks into the data for when it is going to rain or not.
The delay (250000) lets the code above run each 250 seconds.
void loop() { getRainData(); evaluateRain() delay(250000);
The function down here is made to get the content of the 'buienradar' page
void getRainData() { String url = server + lat + lon; Serial.println("Loading: " + url); HTTPClient http; http.begin(url); int httpCode = http.GET(); if (httpCode > 0) { if (httpCode == HTTP_CODE_OK) { Serial.println("HTTP OK"); String payload = http.getString(); extractDataFromAPI(payload); delay(1000); return; } } else { Serial.print("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); http.end(); delay(1000); return; }
The code of this function sets the HTML page into usable data.
void extractDataFromAPI(String data) { int iterarions = 8; for (int i = 0; i < iterarions; i++ ) { int subStringStart = i * 11; int subStringEnd = i * 11 + 3; rainData[i] = data.substring(subStringStart, subStringEnd).toInt(); Serial.println(rainData[i]) } }
We want to understand what is happening so we make a function who looks at the data and make it more understandable by giving it a name. We use if, else if and else for this code to make it work after 25 minutes, etc.
void evaluateRain() { if (rainData[0] + rainData[1] + rainData[2] <= 15) { Serial.println("No Rain"); } else if (rainData[0] + rainData[1] + rainData[2] < 150) { Serial.println("Too bad, it is Raining"); } else { Serial.println("It is working!"); } }
Serial Monitor
In the serial monitor, you want to read the data of the sensors and the outcome.
To do this you start with a void setup to open de serial monitor with your computer.
void setup(){ Serial.begin(115200;) delay(10); WiFi.begin(ssid, password); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); }
Make sure you check your serial monitor to view if the connecting is right and it shows you what is happening! View the image above to see how the outcome should look like.
Servo Motor
We want some motion in this product and for that, you can use a servo motor.
Start with connection the servo to the NodeMCU.
You need 3 wires for this.
- Connect the brown wire to GND on your NodeMCU
- Connect the red wire to 3v on your NodeMCU
- Connect the yellow wire to D5 on your NodeMCU
Refer to the servo library (make sure you download this)
#include <Servo.h>
Start after this with making an object like:
Servo servo;
In the void setup you wanto to refer to the pin you use on your NodeMCU.
servo.attach(D8); servo.write(0);
After this you make a new loop to run this code
void myServo(){ servo.write(90); delay(1000); servo.write(0); delay(1000); }
Now upload the code and see the servo motor in action.
Concept Comes to Life
We want the buienradar API to move the servo motor.
To make this happen we want to make a new loop with an if else.
I describe a fiction version here:
void makeItHappen(){ if (rainData[0] + rainData[1] + rainData[2] <= 15){ makeMove (right) } Serial.printIn("Right way"); } else if (rainData[0] + rainData[1] + rainData[2] <= 150){ makeMove (left) } Serial.printIn("left way"); } else { makeMove (close) } Serial.printIn("Close It!"); }
In this way, you use the information of the serial monitor to make a connection with the servo motor.
When it is raining the servo motor will close the window.
THE END.