Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- For a school project, I made a so called smart house. I posted on this forum before with this exact project, but deleted that post and made new account.
- In pictures and in code you'll see words, that you won't understand, that's because some of them are Slovene (for easier understanding, go to google translate and choose Slovenia)
- First of all, components.
- Arduino Uno with Ethernet Shield (I used W5100), 5V relay, NRF24L01 WiFi module and for software, you'll need a server (I used XAMPP), MySQL and python, to run a script.
- This is what website looks like: https://i.imgur.com/uIWp0x3.png
- I'll use word element for those fans, xbox, lights, basically everything you can turn on or off, I should probably call it component, but oh well.
- So how does this work?
- Every element (Coffee machine, Fridge) has pin, type, name and state. Pin is for what pin is physically connected on Arduino, type is for what type the element is (RLY - relay, LCK - lock,...), name - name that is shown on website (Coffee machine) and state (either 1 or 0).
- When you press the button on website, to switch state of the element, AJAX call is made and that's change is written to database. Python script is non-stop checking for any change being made to database, and if the change is made, script then send UDP packet (via Ethernet) to Arduino with W5100 and then Arduino makes the change.
- It's important to know, how data is stored in database, here is a pic from table items: https://i.imgur.com/iUJ0GSu.png
- Now let's decrypt what you see:
- item - RLY01 - RLY is type of the element (could be LCK, for lock, TRM, for thermometer, or any other 3 digit acronym that you wish), 01 is for a physical pin, element is connected to Arduino (works for 01 to 99)
- state - either 1 or 0 (on or off)
- name - name of the element, which is shown on website (Coffee machine)
- slika (SLO, picture in ENG) - is name of the picture, stored in /img directory
- id_prostor (SLO, id_room in ENG) is id of room in which element is located
- Now let's look at prostor (room) table in DB: https://i.imgur.com/LVLpQFB.png
- id_prostor - primary key, is a foreign key in table items
- name - name of the room, which is shown on website
- id_arduino - 5 digit unique ID for identifying Arduino (NRF24L01, we will get to this)
- Arduino controllers are separated in two groups, one is master arduino, others are slaves (nodes). Master Arduino is the one, with Ethernet shield plugged into him and has set IP and port. He'll receive all commands and send it to other slaves (nodes) via Wifi (NRF24L01) module. All arduinos have names, Master Arduino is named Mastr, one in the kitchen is named ktchn.
- Now let's go full circle. You press the button on the website, to turn on Fridge (RLY02, type Relay (RLY) on pin 02) in the kitchen with id_arduino ktchn. AJAX call is made, state of element is changed from 0 to 1. Python scripts sees the change and sends it to Master Arduino via UDP (right now, Arduino W5100 is set on 192.168.2.100, port 5000, you'll have to change it to your network settings) in format RLY021ktchn. (RLY - relay, 02 - pin, 1 - state, ktchn - id_arduino). When Arduino Master get's that data, it first checks, who's supposed to get it (in this case, ktchn, Arduino in kitchen) and then it sends it to him via NRF24L01. **NRF24L01 requires fix** (more below)
- When ktchn receives data, it's simply a matter of changing state digitalWrite(pin, state), so this is how you turn on or off fan, stove, light, TV, switching its power.
- Now how do you add/remove room or add/remove element?
- In top left corner, there is blue settings button, where you can do all that.
- If you want to **add element**, you choose Add element and this menu slides on: https://i.imgur.com/FoMCVbO.png
- First you choose type of element (relay, thermometer, lock,...), pin (physical pin element is connected to) in format (2 or 02), name (Fridge, Coffee machine) and to what room it belongs, you can also select image, which will represent it. (images can be added/removed in img folder). You click on button doday (SLO, add in ENG), site refreshes and you are good to go. Element is added to database, script got it and you can turn it on or off.
- To **remove element** you chose remove element you choose Remove element: https://i.imgur.com/uCgVylD.png
- You select room in which element is present, then in Element dropdown menu you select element group (Relay, Thermometer, Lock) and then in Name dropdown, you select element you want to remove and press the button Odstrani (SLO, remove ENG). Element is then set to 0 and removed from database.
- This was the basic stuff, that this website does.
- To **add or remove room** you select Room settings and this slides on: https://i.imgur.com/H6uRGX9.png
- To add a room, you choose its name and its 5 digit ID, which will represent Arduino, that's in it.
- To remove it, you simply choose it from dropdown menu and click Odstrani prostor (SLO, remove room ENG).
- In top right corner, there is ---°C, this is a place for temperature for selected room. Slave Arduino reads temperature with DHT11 (change the code a bit and it will work with any sensor) and sends it back to master via NRF24L01 in format TMP=22.50ktchn. (TMP= - acronym for temperature (not sure why I included =), 22.50 temperature in celsius, ktchn, id_arduino). Master then sends this data back to python script and script changes temperature for that room. Every 5 seconds, AJAX call is made (gets temperature for selected room from database) and temperature is printed out, if there is no temperature noted in database, ---°C is printed.
- There is also thermometer element, which needs a lot of tuning. I'm not really sure what to do with it. AC or a radiator could be connected to relay, and when measured temperature in room hits temperature you've set with slider, it would turn on/off AC/radiator. Slider works, but set temperature will not be always shown in top right corner, it goes back to ---.
- Now to a python script. I briefly wrote what it does. It's always querying (checking database) for changes and when change is found, it is sent to master Arduino. It's also responsible for changing temperature in database, which is being sent by master Arduino. When new element is added, size of array (which stores all elements) is changed and can no longer check for changes. Workaround I found, and it works very well (at least on computer, not sure how it would work on RaspPi) is to restart the script and it goes from the beginning. So when element is added or removed, script restarts itself and it's working again just fine.
- Now to the problems.
- The biggest problem is NRF24L01 module. I've had a lot of problems by setting it up and getting it to work. I managed to get it to work several times (in February, March, May) and it worked as expected. Master sent command to Slave and vice versa and then something happened and it stopped working. Fixed it 3 times, it broke down 3 times and now I won't try to fix it again, I don't have the time nor nerves, but will provide last working code.
- Then Master Arduino CAN'T read temperature with DHT11/22 and send that data via UDP to python script. Arduino crashes and required restart. Everything works fine until DHT11 reads temperature and sends it back. Slave Arduino can measure temperature and send it to Master via radio, that works just fine. DHT and UDP together, does not.
- PROBLEMS:
- NRF24L01 not quite working
- DHT11 read and UDP.send does not work together
- when removing room, elements state is not set to 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement