During this project we have had some problems with the code. Some where from incorporating new things received from Ali others were more fundamental. However it is hard to say what is the reason for all of these issues as it often is with programming, which is why we have also spent a lot of time trouble shooting. One thing we did seem to, find a reason for, and potentially this could have been the reason for other issues, was that when having the serial monitor open on the screen while running the code everything worked smoothly. As soon as the serial monitor was closed, or the NodeMCU was plugged directly into a power charge, the switch (to detect whether the locker door was closed or not) would suddenly stop working. After hours of searching for an answer, we found out that the pin we connected the button to, pin D0 had more than just one function and when switching this to pin D3, which only has this one function of detecting the button state, the mechanism started working without the serial monitor being open. Something to perhaps remember for future projects.
Explanation of our state system:
To start out the RFID scans the serial number of the studentcard and send this through the NodeMCU to NodeRED. From here NodeRed send a request to the servers database (mothership) to check if the serial number(SN) is in the database.
If SN is not registered in the database: A string with a URL to register at is sent from NodeRED to cloudMQTT which gives a message to the OLED to show this on the screen for 10 seconds.
If SN is registered in the database: The SN, name, email etc. are stored in NodeRED. From here the next state is to ask the mothership whether the locker is locked or not.
If the locker is open: Sends a string by MQTT to the locker to lock the door. Now the locker checks if the door is open (by checking the button state). If the door is closed the locker will lock. If not there will be displayed a message on the OLED to close the door before locking is possible and the LED will also blink to give a signal. When the locker is locked the LED will turn off to symbolize that the locker is occupied. Furthermore the locker sends a message to NodeRED through MQTT which is then passed on to the mothership about the locker state now being locked.
If the locker is locked: The scanned serial number is send from the locker to NodeRED through MQTT which then asks the mothership if the scanned serial number is the one occupying the locker.
If SN is the one that already uses the locker: The locker gets the message to open up from the mothership. When it is open the locker sends a message back that the locker state is open.
If SN is not the one using the locker: From NodeRED through MQTT a message is sent to the locker to print that the locker is occupied on the OLED. The LED also blinks to say that it has received the SN.