Advertisement
rozman50

Arduino reddit

Jun 16th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. 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.
  2.  
  3. 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)
  4.  
  5. First of all, components.
  6. 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.
  7. This is what website looks like: https://i.imgur.com/uIWp0x3.png
  8.  
  9. 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.
  10.  
  11. So how does this work?
  12. 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).
  13. 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.
  14.  
  15. It's important to know, how data is stored in database, here is a pic from table items: https://i.imgur.com/iUJ0GSu.png
  16. Now let's decrypt what you see:
  17. 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)
  18. state - either 1 or 0 (on or off)
  19. name - name of the element, which is shown on website (Coffee machine)
  20. slika (SLO, picture in ENG) - is name of the picture, stored in /img directory
  21. id_prostor (SLO, id_room in ENG) is id of room in which element is located
  22.  
  23. Now let's look at prostor (room) table in DB: https://i.imgur.com/LVLpQFB.png
  24. id_prostor - primary key, is a foreign key in table items
  25. name - name of the room, which is shown on website
  26. id_arduino - 5 digit unique ID for identifying Arduino (NRF24L01, we will get to this)
  27.  
  28. 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.
  29.  
  30. 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)
  31.  
  32. 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.
  33.  
  34.  
  35. Now how do you add/remove room or add/remove element?
  36. In top left corner, there is blue settings button, where you can do all that.
  37. If you want to **add element**, you choose Add element and this menu slides on: https://i.imgur.com/FoMCVbO.png
  38. 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.
  39.  
  40. To **remove element** you chose remove element you choose Remove element: https://i.imgur.com/uCgVylD.png
  41. 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.
  42. This was the basic stuff, that this website does.
  43.  
  44. To **add or remove room** you select Room settings and this slides on: https://i.imgur.com/H6uRGX9.png
  45. To add a room, you choose its name and its 5 digit ID, which will represent Arduino, that's in it.
  46. To remove it, you simply choose it from dropdown menu and click Odstrani prostor (SLO, remove room ENG).
  47.  
  48. 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.
  49.  
  50. 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 ---.
  51.  
  52. 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.
  53.  
  54. Now to the problems.
  55. 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.
  56. 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.
  57.  
  58.  
  59. PROBLEMS:
  60. NRF24L01 not quite working
  61. DHT11 read and UDP.send does not work together
  62. when removing room, elements state is not set to 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement