﻿Build1=Default,B4RDev
Group=Default Group
Library1=rcore
Library2=resp8266
Library3=resp8266wifi
Library4=rrandomaccessfile
Library5=radafruit_st7735
Library6=radafruitgfx
Module1=Display
NumberOfFiles=0
NumberOfLibraries=6
NumberOfModules=1
Version=3.9
@EndOfDesignText@
'Tiffany's Light-Radio-Fan
#Region Project Attributes
	#AutoFlushLogs: True
	#CheckArrayBounds: True
	#StackBufferSize: 300
#End Region

Sub Process_Globals
	Public Serial1 As Serial
	Private Server As WiFiServerSocket
	Private nmcuPins As D1Pins
	Private espWifi As ESP8266WiFi
	Private astream As AsyncStreams
	Private switch0 As Pin
	Private switch1 As Pin
	Private switch2 As Pin
	Private switch0State As Boolean
	Private switch1State As Boolean
	Private switch2State As Boolean
	Private Ser As B4RSerializator
	
	Dim MacArray(6) As Byte
	Private bc As ByteConverter
	
End Sub

Private Sub AppStart
	
	RunNative( "SetIP" , Null )
	espWifi.Disconnect()

#if C
  void SetIP(B4R::Object* o) {

  IPAddress ip(192, 168, 1, 108);  // (Replace xxx with desired IP) ,95
  IPAddress gateway(192, 168, 1, 1);
  IPAddress subnet(255, 255, 255, 0);
  WiFi.config(ip, gateway, subnet);

  }
  #end if
	Serial1.Initialize(115200)
	Log("AppStart")
	Display.page_Connecting
	If espWifi.Connect2("********","********") Then 'your network SSID, your network key
	
		Server.Initialize(51042, "server_NewConnection")
		Server.Listen
		Log("Waiting for connection.")
		Display.page_Connected
		Log("My ip: ", espWifi.LocalIp)' 192.168.1.95
		Log(bc.HexFromBytes(MacAddress))
		
		Else
			Log("Connection Failed.")
			astream_Error
	End If
	switch0.Initialize(nmcuPins.D1, switch0.MODE_OUTPUT)' Light
	switch1.Initialize(nmcuPins.D2, switch0.MODE_OUTPUT)' Radio
	switch2.Initialize(nmcuPins.D6, switch0.MODE_OUTPUT)' Fan
	switch0.DigitalWrite(False)
	switch1.DigitalWrite(False)
	switch2.DigitalWrite(False)
	switch0State = False
	switch1State = False
	switch2State = False
	'choose_Display
End Sub

Sub Server_NewConnection (NewSocket As WiFiSocket)
	astream.Initialize(NewSocket.Stream,"astream_NewData", "astream_Error")
	Log("server new connection")
	'Display.page_Connected
	choose_Display
End Sub

Sub astream_NewData (Buffer() As Byte)
	Dim be(10) As Object 'used as a storage buffer.
	Dim objects() As Object = Ser.ConvertBytesToArray(Buffer, be)
	Log("astream newdata")
		For Each o As Object In objects
			Select o
				Case "S0" ' Light
					If switch0State = False Then
						switch0.DigitalWrite(True)
						switch0State = True
					Else
						switch0.DigitalWrite(False)
						switch0State = False
					End If
					astream.Write(Ser.ConvertArrayToBytes(Array("S0",switch0State)))
				Case "S1" ' Radio
					If switch1State = False Then
						switch1.DigitalWrite(True)
						switch1State = True
					Else
						switch1.DigitalWrite(False)
						switch1State = False
					End If
					astream.Write(Ser.ConvertArrayToBytes(Array("S1",switch1State)))
				Case "S2" ' Fan
					If switch2State = False Then
						switch2.DigitalWrite(True)
						switch2State = True
					Else
						switch2.DigitalWrite(False)
						switch2State = False
					End If
					astream.Write(Ser.ConvertArrayToBytes(Array("S2",switch2State)))					
			End Select
		Next
		choose_Display
End Sub

Sub astream_Error
	Log ("Astream Error")
	'Display.page_ASError
	Delay (4000)
	Server.Listen
End Sub

Sub choose_Display
	If switch0State == False And switch1State == False And switch2State == False Then
		Display.page_OOO
	End If
	If switch0State == False And switch1State == False And switch2State == True Then
		Display.page_OOI
	End If
	If switch0State == False And switch1State == True And switch2State == False Then
		Display.page_OIO
	End If
	If switch0State == False And switch1State == True And switch2State == True Then
		Display.page_OII
	End If
	If switch0State == True And switch1State == False And switch2State == False Then
		Display.page_IOO
	End If
	If switch0State == True And switch1State == False And switch2State == True Then
		Display.page_IOI
	End If
	If switch0State == True And switch1State == True And switch2State == False Then
		Display.page_IIO
	End If
	If switch0State == True And switch1State == True And switch2State == True Then
		Display.page_III
	End If
End Sub

Sub MacAddress() As Byte()
	RunNative("getMac", Null)
	Return MacArray
End Sub
#if C
  #include <ESP8266WiFi.h>
  void getMac(B4R::Object* u) {
  WiFi.macAddress((Byte*)b4r_main::_macarray->data);
  }
#end if
 
