Software apps and online services
In this project we're going to show you how to easily use the built-in magnetic (reed) switch on the mcModule120. With the mcModule case (which includes two neodymium magnets), you can easily setup a wireless, coin-cell operated, door sensor (or anything else that opens) and be alerted if the door opens, see real time status and even count the number of times the door was opened!
The project includes information and code for sending the information to Android using MQTT, IFTTT and an IoT cloud service, Losant!How the platform works:
The mcThings platform includes 2 main components: mcModules and mcGateways. A powered and connected mcGateway creates the mcAir network (up to 200m range under optimal conditions) and bridges the information between the mcModules (within range) to and from the Internet. Using the IDE (mcStudio) and mcScript (ultra-low power programming language - a subset of VB.NET) allows you to wirelessly connect, debug and program modules with your customized scripts.
Note: You also require an mcDongle to complete firmware updates on the modules/devices and gateways! We recommend looking at the mcModule120 Dev kit which includes everything you need to get going!
Using the mcModule enclosure, you can easily install the module to a door/window/etc and place the magnet clip so that it lines up with the module when the object is closed. You can also use your own magnets! We recommend a strong magnet like neodymium. If you are building a customized enclosure or have a unique object that you want to use the reed switch with, this is a good option.
Similar to one of our other projects, we're going to push the information to an MQTT client app on an Android phone to see the status in real-time!
Check out the below video and code for putting this together as you'll need to setup your gateway and your MQTT broker properly to push information to your phone:Code:
Class ProductionRoom/YOURPROJECTNAMEHERE Shared Event ReedSwitchChanged() Dim payload As ListOfByte = New ListOfByte Dim payString As String = "" If ReedSwitch = True Then LedRed = True payString = "Open" Else LedRed = False payString = "Closed" End If payload.Add(payString) Lplan.Publish("mcThings/ProductionRoomDoor/YOURTOPICNAMEHERE", payload) Thread.Sleep(10000) Thread.ClearHardwareEvent() Dim uptime As Integer = Device.Uptime() End Event Shared Event CheckVoltage() RaiseEvent Every 2 Days Dim BattVolt As Short = Device.BatteryVoltage If BattVolt < 2200 Then Lplan.IFTTT("YOURIFTTTMAKERKEYTHERE", "YOUREVENTNAMEHERE") Else End If End Event End Class
We also included a bit of programming to check the battery voltage every 2 days and send a message to the maker channel on IFTTT if below 2.2 volts (above is measured in millivolts).IFTTT - Code
Below is code to setup a message to be sent to IFTTT if the door is opened and closed. Again we added a battery voltage check. Using IFTTT allows you to send the information to a ton of different services! We usually use notifications via the IFTTT Android app but you could receive an email, log the information into a spreadsheet, etc!
Losant - Code and Video
Class IFTTTAndroidDoor Shared Event ReedSwitchChanged() If ReedSwitch = True Then LedRed = True 'Enter your maker channel event name such as 'doorOpen' Lplan.IFTTT("YOURIFTTTMAKERKEYHERE", "doorOpen") Else LedRed = False 'Optional to add the below closed function as well Lplan.IFTTT("YOURIFTTTMAKERKEYHERE", "doorClosed") End If Thread.Sleep(10000) Thread.ClearHardwareEvent() Dim uptime As Integer = Device.Uptime() End Event 'check the battery voltage every 2 days Shared Event CheckVoltage() RaiseEvent Every 2 Days Dim BattVolt As Short = Device.BatteryVoltage If BattVolt < 2200 Then Lplan.IFTTT("YOURIFTTTKETHERE", "ProductionRoomBattery/YOURTOPICHERE") Else End If End Event End Class
Similar to our other Losant project, we'll be sending door open/closed status and count to a dashboard as we as temperature and battery information (Check out the LIVE Dashboard!).
Class LosantDoor // Device ID of Peripheral in Losant Const LosantDeivceId As String = "YourLosantDeviceIDHERE" // MQTT topic in Losant Const LosantTopic As String = "losant/" + LosantDeivceId + "/state" Shared _doorState As String Shared _doorcount As Integer Shared Event Boot() _doorState = "Open" _doorcount = 0 End Event Shared Event ReedSwitchChanged() 'debounce interrupt Thread.Sleep(100000) Thread.ClearHardwareEvent() LedRed = True If ReedSwitch = True Then _doorState = "Open" _doorcount = _doorcount + 1 Else _doorState = "Closed" End If sendMQTTData() LedRed = False End Event Shared Event CheckSensors() RaiseEvent Every 60 Seconds sendMQTTData() End Event Private Sub sendMQTTData() Dim tempTMP102string As String = TempSensor.GetTemp().ToString() Dim battShort As Short = Device.BatteryVoltage() Dim battFloat As Float = battShort / 1000 Dim battString As String = battFloat.ToString() // Create data JSON object Dim dataJson As Json = New Json dataJson.Add("battery", battString) dataJson.Add("temperature", tempTMP102string) dataJson.Add("doorState", _doorState) dataJson.Add("doorCount", _doorcount) // Create Losant preferred JSON object Dim losantPayload As Json = New Json losantPayload.Add("data", dataJson) // Publish to Losant MQTT Lplan.Publish(LosantTopic, losantPayload.ToListOfByte) End Sub End Class
Below is a video to help you walk-through setting up the mcThings platform with Losant so you can start sending tons of information and display it in the awesome dashboard!Sending the information to other services
You can modify or write your own code to send the information to another service if you wish! Because you can send the information out via MQTT or via IFTTT, there are numerous applications that can display, store, visualize and analyse the information.
Thanks for reading! Please become a member on our hub so that you can be alerted when we post new projects (many more to come!)