Remote Control Light Switch via Facebook and PLC

This application use m.facebook.com for remote control light and PLC as a replacement light switch. The following demo video about this application.

A. Architecture of Remote Control Light Switch via Facebook
For this application, I use architecture as shown below.
Architecture of Remote Control Light Switch via Facebook and PLC

B. Preparations to make this application:
1. Basic Communication between PLC and Computer
2. Two Facebook accounts: one account for the server and one for client account
3. Internet connection
4. Download and Install Visual Basic 2010 express:
http://download.microsoft.com/download/1/E/5/1E5F1C0A-0D5B-426A-A603-1798B951DDAE/VS2010Express1.iso
5. Download project file: Control-Light-via-m-Facebook. Zip

C. Wiring PLC output, Light, Switch and AC Source
Wiring PLC output Light Switch and AC Source

D. Facebook Account Setting
1. Facebook Account Setting in Visual Basic
facebook account setting in visual basic

Remarks for the picture above:
1. Fill email for login to facebook account in this application (Facebook Server).
2. Fill password for login to facebook account in this application (Facebook Server).
3. Fill facebook id number for chat with this visual basic program, example Facebook id 40xxxxxxxxxxx43 and username my-light (Facebook Client)
Note: Facebook server name and Facebook client name has been friends.
This video about how to get facebook id number


This video about fill email, password, and facebook id number into visual basic program


2. Change Language to English (US)
Facebook Account for in visual basic program only support English (US) Language. How to change language facebook account to English (US):
1. Go to facebook.com in browser such as Google Chrome, Firefox, Internet Explorer, etc.
2. Login with email and password with Facebook account that used in visual basic
3. See picture to the next step
How to change language facebook account to English US


E. Application Test
1. Open Browser
2. Go to facebook.com and Login with Facebook client, example Facebook id 40xxxxxxxxxxx43 and username my-light that set in Facebook server (visual basic). 3. Go to Messages or Chat
4. Select Facebook Server Name or Facebook Account Name in visual basic
Note: Facebook server name and Facebook client name has been friends.
5. Type messages and click replay
F. Visual Basic Layout and Code for Remote Control Light Switch via Facebook and PLC
1. Visual basic layout
Visual basic layout of Remote Control Light Switch via Facebook and PLC

Visual basic layout running of Remote Control Light Switch via Facebook and PLC


2. Visual basic code
Imports System.Threading
Public Class Form1
'YOU’RE SETTING HERE
'Login to Facebook Server or Login to this facebook Visual Basic Program
Dim email_for_facebook_server As String = "fill your facebook email"
Dim password_for_facebook_server As String = "fill your facebook password"

'Facebook account ID for Chat with Facebook Server
Dim id_facebook_client As String = "fill number facebook account id"

'Serial Port Name for Communication PLC and Computer
Dim port_name As String = "COM3" 'change port name

'keyword for light on
Dim light_on As String = "my light on"

'keyword for light off
Dim light_off As String = "my light off"

'response for light on
Dim response_on As String = "Light Condition is Lit"

'response for light off
Dim response_off As String = "Light Condition is Not Lit"

'Time for messages read scan and response
Dim interval_messages_read As Integer = 5000 ' 5 second for messages scan


Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
WebBrowser1.ScriptErrorsSuppressed = True
Timer2.Interval = interval_messages_read

'Serial Communication Setting
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = IO.Ports.Parity.Even
SerialPort1.PortName = port_name
SerialPort1.StopBits = IO.Ports.StopBits.One
Try
If SerialPort1.IsOpen = False Then SerialPort1.Open()
readQ_comd()
Catch ex As Exception
End Try
End Sub

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles
Timer1.Tick
Timer1.Enabled = False
internet_checker:
Timer2.Enabled = False

WebBrowser1.Navigate("http://m.facebook.com")
While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While

'Change Language to English (US)
Dim page As HtmlElementCollection = WebBrowser1.Document.All
For Each elem As HtmlElement In page
If elem.GetAttribute("value") = "Log In" Then
Exit For
End If
If InStr(elem.GetAttribute("href"), "en_US") Then
elem.InvokeMember("click")
While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
Exit For
End If
Next

'Login Checker in m.facebook
Try
WebBrowser1.Document.GetElementById("email").SetAttribute("value", email_for_facebook_server)
WebBrowser1.Document.GetElementById("pass").SetAttribute("value", password_for_facebook_server)
Catch ex As Exception
GoTo messages_read
End Try

Dim loginpage As HtmlElementCollection = WebBrowser1.Document.All
For Each elem As HtmlElement In loginpage
If elem.GetAttribute("value") = "Log In" Then
elem.InvokeMember("click")
While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
Exit For
End If
Next

messages_read:
Timer2.Enabled = True
End Sub

Private Sub WebBrowser1_Navigated(sender As System.Object, e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
ToolStripStatusLabel2.Text = WebBrowser1.Url.ToString
End Sub

Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs) Handles Timer2.Tick
Try
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
'get facebook messages
WebBrowser1.Navigate("http://m.facebook.com/messages/read/?tid=id." & id_facebook_client)
While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
'scroll to down
WebBrowser1.Document.Window.ScrollTo(0, WebBrowser1.Document.Window.Size.Height)
'check facebook messages
Dim msg As String = WebBrowser1.Document.GetElementById("messageGroup").InnerText
Dim line() As String = msg.Split(vbNewLine)
Dim index As Integer = line.Length - 2
Dim latest_messages As String = line(index)

If InStr(latest_messages, light_off) > 0 Then
bitQ(0)
Thread.Sleep(200)
readQ_comd()
End If
If InStr(latest_messages, light_on) > 0 Then
bitQ(1)
Thread.Sleep(200)
readQ_comd()
End If

End If
Catch ex As Exception
Timer2.Enabled = False
Timer1.Enabled = True
End Try
End Sub

Sub bitQ(ByVal Qv As Byte)
Dim Qx As Byte
Dim Qy As Byte

Qx = 0
Qy = 0

Dim str_write(0 To 37) As Byte
Dim Temp_FCS As VariantType
Dim i As Long

str_write(0) = &H68
str_write(1) = &H20
str_write(2) = &H20
str_write(3) = &H68
str_write(4) = &H2
str_write(5) = &H0
str_write(6) = &H7C
str_write(7) = &H32
str_write(8) = &H1
str_write(9) = &H0
str_write(10) = &H0
str_write(11) = &H43
str_write(12) = &H1
str_write(13) = &H0
str_write(14) = &HE
str_write(15) = &H0
str_write(16) = &H5
str_write(17) = &H5
str_write(18) = &H1
str_write(19) = &H12
str_write(20) = &HA
str_write(21) = &H10
str_write(22) = &H1 'len = 1
str_write(23) = &H0
str_write(24) = &H1
str_write(25) = &H0
str_write(26) = &H0 ' type q
str_write(27) = &H82 'type q
str_write(28) = &H0
str_write(29) = &H0
str_write(30) = Qx * 8 + Qy
str_write(31) = &H0
str_write(32) = &H3
str_write(33) = &H0
str_write(34) = &H1
str_write(35) = Qv

For i = 4 To 35
Temp_FCS = Temp_FCS + str_write(i)
Next
str_write(36) = Temp_FCS Mod 256
str_write(37) = &H16
If (SerialPort1.IsOpen = True) Then
'Clear in/out buffers:
SerialPort1.DiscardOutBuffer()
SerialPort1.DiscardInBuffer()

' Send the binary data out the port
SerialPort1.Write(str_write, 0, str_write.Length)

Thread.Sleep(100) 'wait 100ms

Call End_comm() 'end Communication

End If
End Sub
Sub End_comm()
'End Communication
Dim str_val(0 To 5) As Byte
str_val(0) = &H10
str_val(1) = &H2
str_val(2) = &H0
str_val(3) = &H5C
str_val(4) = &H5E
str_val(5) = &H16
If (SerialPort1.IsOpen = True) Then
SerialPort1.Write(str_val, 0, str_val.Length)
End If
End Sub

Sub readQ_comd()
Dim str_read(0 To 32) As Byte
Dim i As Integer
Dim Temp_FCS As VariantType
Dim Rx As String

Rx = 0

str_read(0) = &H68
str_read(1) = &H1B
str_read(2) = &H1B
str_read(3) = &H68
str_read(4) = &H2
str_read(5) = &H0
str_read(6) = &H6C
str_read(7) = &H32
str_read(8) = &H1
str_read(9) = &H0
str_read(10) = &H0
str_read(11) = &H0
str_read(12) = &H0
str_read(13) = &H0
str_read(14) = &HE
str_read(15) = &H0
str_read(16) = &H0
str_read(17) = &H4
str_read(18) = &H1
str_read(19) = &H12
str_read(20) = &HA
str_read(21) = &H10
str_read(22) = &H8 'read len 1=bit, 8=byte
str_read(23) = &H0
str_read(24) = &H1
str_read(25) = &H0

str_read(26) = &H0 ' type Ixy
str_read(27) = &H82 'type Ixy=&H81, Qxy=&H82
str_read(28) = &H0
str_read(29) = (Str(Rx) * 8) \ 256
str_read(30) = (Str(Rx) * 8) Mod 256

For i = 4 To 30
Temp_FCS = Temp_FCS + str_read(i)
Next i
str_read(31) = Temp_FCS Mod 256
str_read(32) = &H16
If (SerialPort1.IsOpen = True) Then
'Clear in/out buffers:
SerialPort1.DiscardOutBuffer()
SerialPort1.DiscardInBuffer()

' Send the binary data out the port
SerialPort1.Write(str_read, 0, str_read.Length)
Thread.Sleep(100) 'wait 100ms
Call End_comm() 'end communication
End If
End Sub

Public Delegate Sub DisplayData(ByVal Buffer() As Byte)
Private Sub Display(ByVal Buffer() As Byte)
Dim xyval As Byte
xyval = Buffer(26)
If (xyval And 1) = 1 Then
Try
WebBrowser1.Document.GetElementById("composerInput").SetAttribute("value", response_on)
Dim Replypage As HtmlElementCollection = WebBrowser1.Document.All
For Each elem As HtmlElement In Replypage
If elem.GetAttribute("value") = "Reply" Then
elem.InvokeMember("click")
Exit For
End If
Next
Catch ex As Exception
End Try
ToolStripStatusLabel1.Text = "ON"
End If

If (xyval And 1) = 0 Then
Try
WebBrowser1.Document.GetElementById("composerInput").SetAttribute("value", response_off)
Dim Replypage As HtmlElementCollection = WebBrowser1.Document.All
For Each elem As HtmlElement In Replypage
If elem.GetAttribute("value") = "Reply" Then
elem.InvokeMember("click")
Exit For
End If
Next
Catch ex As Exception
End Try
ToolStripStatusLabel1.Text = "OFF"
End If
End Sub

Private DisplayDelegate As New DisplayData(AddressOf Display)
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
If (SerialPort1.IsOpen) Then
Try
Dim BytesAvailable As Integer = SerialPort1.BytesToRead
If BytesAvailable >= 29 Then
Dim Buffer(0 To BytesAvailable - 1) As Byte
SerialPort1.Read(Buffer, 0, BytesAvailable)
Me.BeginInvoke(DisplayDelegate, Buffer)
End If
Catch ex As TimeoutException
End Try
End If
End Sub

Private Sub Form1_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
Try
If SerialPort1.IsOpen Then SerialPort1.Close()
Catch ex As Exception
End Try
End Sub
End Class

Hopefully this article can add your insights.
Please follow me:

ShareTo
Share on Facebook Tweet this Share to Google Plus Stumble this article
CLOSE

Enter Your Email Address




All Labels

  1. ABB PLC
  2. Allen Bradley PLC
  3. Allen-Bradley MicroLogix
  4. Allen-Bradley PLC
  5. Analog Input
  6. Android
  7. Android and PLC
  8. Android Bluetooth
  9. Arduino
  10. Arduino Microcontroller
  11. Audio and PLC
  12. Automatic Gates using PLC
  13. Baldor PLC
  14. Battery Level Indicator using PLC Analog
  15. BMS
  16. Building Management System
  17. Car Ticketing Machine using PLC
  18. CIMON PLC
  19. Click Series PLC
  20. Computer Vision
  21. Computer Vision and PLC
  22. Control Seven Segment Display with PLC
  23. Conveyor Control Using PLC
  24. CPU PLC
  25. CV and PLC
  26. CX-One
  27. DCS
  28. DDC
  29. DELMIA V5
  30. DeltaV
  31. Digital Input
  32. DirectLogic PLC
  33. DirectSOFT PLC
  34. Eaton PLC
  35. Elevator with PLC Program
  36. EPICS PLC
  37. Fatek PLC
  38. Festo PLC
  39. Fieldbus
  40. Forex Trading and PLC
  41. FPGA PLC
  42. GE Fanuc PLC
  43. GP proface
  44. GX Developer
  45. Heater Controller with PLC
  46. Hitachi PLC
  47. HMI
  48. Home Automation
  49. Honeywell PLC
  50. IDEC PLC
  51. Internet of Things
  52. IoT
  53. Keyence PLC
  54. Koyo PLC
  55. LED Display
  56. LG PLC
  57. Lock Unlock Door Using PLC
  58. Logixpro PLC
  59. Maple OIT
  60. Matsushita PLC
  61. MELSEC A
  62. MELSEC Q
  63. Memory PLC
  64. Micro PLC
  65. Microcontroller
  66. MicroLogix 1000
  67. MicroSmart PLC
  68. Mitsubishi PLC
  69. Mobile Application
  70. Modbus
  71. ModBus Communication
  72. ModBus RTU
  73. ModBus RTU Communication
  74. Modicon PLC
  75. Momentum PLC
  76. Motion Control
  77. National Instruments PLC
  78. Omron PLC
  79. OPC
  80. PAC
  81. Panasonic PLC
  82. Parking Information using PLC
  83. Password Using PLC
  84. PLC
  85. PLC Analog Input Module
  86. PLC and Android
  87. PLC and Arduino
  88. PLC and CNC
  89. PLC and Computer
  90. PLC and Facebook
  91. PLC and PC
  92. PLC and Servo
  93. PLC and Visual Basic
  94. PLC Application Project
  95. PLC Battery
  96. PLC Beginner Guide
  97. PLC Book
  98. PLC Cable
  99. PLC Codesys
  100. PLC Companies List
  101. PLC Computers
  102. PLC Design
  103. PLC for 3 Movement
  104. PLC for Assembly Line
  105. PLC for Automatic Welding
  106. PLC for Binding Machine
  107. PLC for Cutting Machine
  108. PLC for Filling Machine
  109. PLC for Fountain Application
  110. PLC for Injection Molding
  111. PLC for Marking
  112. PLC for Packing
  113. PLC for Rotary Bottle Washing
  114. PLC for Snack Vending Machine
  115. PLC for Sorter Machine
  116. PLC for Water Treatment
  117. PLC Hardware
  118. PLC Hybrid
  119. PLC Information
  120. PLC Knowledge
  121. PLC Language
  122. PLC Link Model
  123. PLC Mitsubishi Download
  124. PLC Model
  125. PLC Modules
  126. PLC Real Time Clock
  127. PLC Simulator
  128. PLC Software
  129. PLC Supplier
  130. PLC Types
  131. PLC Wiring
  132. PLC WorkShop
  133. PLC-ANALYZER
  134. PLCSIM
  135. Power Supply for PLC
  136. Profibus
  137. ProSafe PLC
  138. Push ON Push OFF with PLC
  139. Quickpanel Operator Interfaces
  140. Raspberry Pi
  141. Raspberry Pi and PLC
  142. Read Data
  143. RTU
  144. RTU and PLC
  145. SAS
  146. SCADA
  147. Schneider PLC
  148. SHARP PLC
  149. Siemens PLC
  150. Simple ModBus
  151. SINUMERIK CNC
  152. Small Modular PLC
  153. Standard Sequence Programming for PLC
  154. Substation Automation System
  155. Teco PLC
  156. Telemetry
  157. TEP PLC
  158. Texas Instruments PLC
  159. Timer Countdown with PLC
  160. Toshiba PLC
  161. Touch Screen PLC
  162. Traffic Light with PLC
  163. TWIDO PLC
  164. Types of PLC
  165. Unitary PLC
  166. Unitronics PLC
  167. USB Keyboard and PLC
  168. VIPA PLC
  169. Vision PLC
  170. Weighing Machine Using PLC
  171. Write Data
  172. Yokogawa PLC
  173. Zelio PLC

Search This Blog

Loading...
Copyright © 2009- All Rights Reserved