﻿Build1=Default,B4RDev
Group=Default Group
Library1=rcore
Library2=rirremoteesp8266
Library3=rrandomaccessfile
Library4=resp8266wifi
NumberOfFiles=0
NumberOfLibraries=4
NumberOfModules=0
Version=3.9
@EndOfDesignText@
'This project uses ESP8266 (LEK12 project box)
#Region Project Attributes
	#AutoFlushLogs: True
	#CheckArrayBounds: True
	#StackBufferSize: 300
#End Region

Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'Public variables can be accessed from all modules.
	Public Serial1 As Serial
	Private Server As WiFiServerSocket
	Private espWifi As ESP8266WiFi
	Private astream As AsyncStreams
	Private Ser As B4RSerializator
	Private irsend As IrSend
	Private grnLED As Pin
	Private redLED As Pin
	
	
	'sendNEC
	Dim intTvPwr As ULong = 16726215 'result: 1, 16726215 SSTV pwr
	Dim intVup As ULong = 16752735 'result: 1, 16752735 SSTV vol +
	Dim intVdn As ULong = 16744575 'result: 1, 16744575 SSTV vol -
	Dim intMute As ULong = 16742535 'result: 1, 16742535 SSTV mute
	
	'sendRaw
	Dim intCablePwr(47) As ULong = Array As ULong(3330,3370, 830,2570, 830,2570, 830,870, 830,2570, 830,2570, 830,2570, 830,2570, 830,2570, 830,820, 830,870, 830,820, 830,870, 830,820, 830,2570, 830,870, 830,820, 830,870, 780,870, 830,870, 780,2620, 780,2620, 780,2620, 830)
	Dim intChUp(143) As ULong = Array As ULong(3344, 3320,  834, 2532,  836, 2530,  830, 826,  830, 2534,  834, 2534,  834, 2530,  838, 820,  836, 2530,  830, 2536,  832, 2534,  834, 822,  834, 822,  834, 824,  830, 2536,  832, 824,  832, 824,  832, 826,  830, 2536,  832, 824,  832, 824,  832, 826,  830, 2536,  854, 38136,  3348, 3314,  832, 2534,  834, 2532,  836, 820,  834, 2532,  830, 2536,  832, 2534,  834, 822,  834, 2532,  836, 2530,  830, 2536,  832, 824,  832, 826,  830, 828,  836, 2530,  832, 826,  830, 828,  836, 820,  836, 2532,  830, 826,  836, 820,  836, 820,  834, 2532,  850, 38140,  3344, 3318,  836, 2532,  830, 2536,  832, 824,  830, 2536,  832, 2534,  836, 2530,  830, 826,  836, 2530,  834, 2534,  832, 2534,  834, 822,  834, 822,  832, 824,  832, 2534,  834, 824,  834, 822,  832, 824,  830, 2536,  832, 824,  832, 824,  830, 826,  830, 2536,  854)
	Dim intChDn(143) As ULong = Array As ULong(3344, 3320,  834, 2532,  836, 2530,  832, 826,  830, 2536,  832, 2534,  834, 822,  832, 2534,  834, 2532,  830, 2536,  832, 2534,  834, 822,  832, 824,  830, 826,  830, 2536,  832, 824,  830, 826,  830, 2536,  832, 824,  832, 824,  832, 826,  830, 826,  836, 2530,  852, 38138,  3346, 3316,  830, 2536,  832, 2534,  834, 824,  832, 2532,  836, 2532,  830, 826,  836, 2530,  830, 2536,  832, 2534,  834, 2532,  830, 826,  836, 820,  834, 822,  834, 2532,  836, 820,  834, 822,  834, 2532,  836, 820,  834, 822,  834, 824,  832, 824,  832, 2534,  848, 38144,  3350, 3314,  832, 2534,  834, 2532,  836, 822,  834, 2532,  830, 2538,  830, 826,  830, 2536,  832, 2534,  834, 2532,  836, 2530,  830, 826,  836, 820,  836, 820,  834, 2532,  830, 826,  836, 822,  834, 2532,  830, 826,  836, 822,  834, 822,  832, 822,  834, 2532,  848)
	Dim intNum0(143) As ULong = Array As ULong(3344, 3318,  836, 2530,  830, 2536,  834, 822,  834, 2532,  834, 2530,  832, 2534,  834, 822,  832, 824,  832, 2532,  836, 2530,  830, 826,  838, 818,  838, 818,  838, 2528,  832, 824,  832, 826,  830, 826,  830, 2536,  832, 2532,  836, 820,  836, 820,  836, 2530,  852, 38134,  3348, 3314,  832, 2534,  834, 2530,  832, 826,  836, 2528,  832, 2534,  834, 2530,  832, 824,  838, 820,  836, 2530,  832, 2534,  834, 822,  834, 822,  834, 822,  832, 2534,  836, 820,  834, 822,  834, 822,  834, 2532,  836, 2530,  832, 824,  830, 826,  838, 2528,  854, 38132,  3344, 3318,  836, 2530,  830, 2536,  834, 822,  832, 2532,  836, 2532,  830, 2536,  834, 822,  832, 824,  832, 2534,  834, 2532,  830, 826,  838, 818,  838, 820,  836, 2530,  832, 826,  830, 824,  832, 826,  836, 2530,  832, 2534,  836, 822,  834, 822,  834, 2532,  850)
	Dim intNum1(143) As ULong = Array As ULong(3342, 3320,  834, 2532,  836, 2530,  832, 824,  832, 2534,  834, 2532,  838, 820,  836, 820,  836, 820,  834, 822,  834, 2530,  830, 826,  836, 820,  836, 786,  934, 2466,  832, 824,  838, 818,  830, 2536,  832, 2534,  834, 2530,  832, 2534,  834, 822,  834, 2532,  850, 38136,  3346, 3316,  832, 2534,  834, 2532,  836, 820,  836, 2530,  832, 2534,  834, 822,  832, 822,  834, 822,  832, 824,  832, 2534,  834, 822,  834, 822,  834, 788,  868, 2532,  836, 820,  836, 820,  834, 2532,  830, 2534,  834, 2532,  836, 2530,  832, 824,  832, 2534,  846, 38138,  3344, 3318,  830, 2536,  832, 2534,  834, 822,  834, 2530,  832, 2536,  832, 822,  832, 824,  832, 824,  832, 824,  832, 2534,  834, 822,  834, 822,  834, 822,  834, 2532,  836, 820,  834, 822,  834, 2532,  830, 2536,  832, 2534,  834, 2532,  830, 826,  836, 2528,  854)
	Dim intNum2(143) As ULong = Array As ULong(3348, 3314,  836, 2530,  830, 2534,  834, 840,  816, 2532,  836, 2530,  832, 2534,  834, 838,  818, 840,  816, 840,  816, 2532,  830, 844,  820, 838,  818, 838,  818, 2530,  830, 844,  812, 842,  812, 844,  812, 2536,  832, 2532,  836, 2530,  830, 842,  814, 2536,  848, 38136,  3348, 3316,  832, 2534,  834, 2530,  832, 842,  812, 2536,  832, 2532,  836, 2530,  832, 842,  814, 844,  812, 844,  812, 2536,  832, 840,  816, 842,  814, 842,  814, 2534,  834, 840,  816, 840,  816, 840,  816, 2532,  836, 2530,  832, 2534,  836, 838,  818, 2532,  850, 38134,  3344, 3318,  836, 2530,  832, 2534,  834, 838,  816, 2532,  830, 2536,  834, 2532,  836, 838,  818, 838,  818, 838,  818, 2530,  830, 844,  812, 844,  812, 844,  820, 2528,  832, 842,  814, 842,  814, 842,  812, 2536,  834, 2532,  836, 2530,  832, 842,  814, 2534,  848)
	Dim intNum3(143) As ULong = Array As ULong(3346, 3316,  832, 2534,  834, 2532,  830, 844,  818, 2530,  832, 2532,  836, 838,  818, 2530,  832, 842,  812, 844,  820, 2528,  832, 842,  814, 842,  814, 842,  814, 2534,  834, 840,  816, 840,  816, 2532,  836, 838,  818, 2530,  832, 2534,  834, 840,  816, 2532,  850, 38132,  3344, 3318,  836, 2530,  832, 2534,  834, 840,  816, 2532,  836, 2530,  832, 842,  814, 2534,  834, 838,  816, 840,  816, 2532,  830, 844,  818, 838,  818, 838,  818, 2530,  830, 842,  812, 844,  812, 2536,  832, 842,  814, 2534,  834, 2530,  832, 842,  812, 2536,  848, 38136,  3346, 3316,  832, 2534,  834, 2532,  830, 844,  818, 2530,  832, 2532,  836, 838,  818, 2530,  832, 842,  814, 842,  812, 2536,  832, 842,  814, 842,  814, 842,  814, 2534,  834, 840,  816, 840,  816, 2532,  838, 838,  818, 2528,  832, 2534,  834, 840,  816, 2532,  850)
	Dim intNum4(143) As ULong = Array As ULong(3350, 3312,  834, 2530,  838, 2528,  834, 822,  832, 2534,  834, 2530,  832, 2534,  834, 2532,  830, 826,  836, 820,  836, 2530,  832, 824,  832, 824,  832, 824,  832, 2534,  834, 822,  834, 822,  834, 822,  834, 822,  834, 2532,  836, 2530,  832, 824,  832, 2534,  848, 38136,  3348, 3316,  832, 2532,  836, 2530,  832, 824,  832, 2534,  834, 2532,  836, 2530,  832, 2532,  836, 820,  836, 750,  904, 2532,  830, 826,  836, 820,  836, 820,  836, 2528,  832, 824,  832, 824,  838, 818,  838, 818,  836, 2528,  832, 2534,  836, 820,  834, 2530,  852, 38132,  3344, 3318,  836, 2530,  832, 2534,  834, 822,  834, 2530,  838, 2528,  832, 2532,  836, 2530,  832, 824,  832, 824,  832, 2534,  834, 822,  834, 822,  832, 824,  832, 2532,  836, 820,  836, 820,  836, 820,  836, 822,  834, 2530,  838, 2528,  832, 824,  832, 2534,  848)
	Dim intNum5(143) As ULong = Array As ULong(3344, 3318,  830, 2534,  834, 2532,  838, 818,  836, 2530,  832, 2534,  836, 820,  836, 838,  818, 2530,  830, 826,  830, 2536,  834, 824,  832, 840,  814, 842,  814, 2534,  834, 840,  816, 840,  816, 2532,  830, 2536,  834, 840,  814, 2534,  836, 838,  818, 2530,  852, 38132,  3346, 3316,  832, 2534,  834, 2532,  832, 826,  836, 2528,  832, 2532,  836, 822,  834, 822,  834, 2530,  830, 826,  838, 2528,  832, 824,  834, 822,  832, 824,  832, 2534,  834, 822,  834, 840,  814, 2532,  836, 2530,  832, 824,  832, 2534,  834, 822,  834, 2532,  850, 38134,  3344, 3318,  836, 2530,  832, 2534,  834, 822,  834, 2532,  830, 2536,  834, 822,  834, 824,  832, 2534,  836, 822,  834, 2530,  832, 826,  838, 818,  836, 820,  836, 2528,  832, 824,  832, 824,  832, 2534,  834, 2532,  836, 818,  838, 2530,  832, 824,  832, 2534,  848)
	Dim intNum6(143) As ULong = Array As ULong(3344, 3318,  836, 2530,  832, 2534,  834, 838,  816, 2532,  830, 2534,  834, 2532,  830, 844,  818, 2530,  832, 842,  814, 2534,  834, 840,  816, 840,  816, 840,  816, 2532,  836, 838,  818, 838,  818, 838,  818, 2530,  830, 844,  812, 2536,  832, 840,  816, 2534,  848, 38134,  3348, 3314,  832, 2532,  836, 2530,  832, 842,  814, 2534,  834, 2532,  830, 2536,  832, 840,  814, 2534,  834, 838,  818, 2530,  830, 842,  812, 844,  812, 844,  818, 2528,  832, 842,  814, 842,  814, 842,  814, 2534,  834, 840,  816, 2532,  828, 844,  818, 2530,  852, 38132,  3344, 3318,  828, 2536,  832, 2532,  836, 838,  820, 2530,  830, 2534,  834, 2532,  830, 842,  812, 2536,  832, 840,  816, 2534,  834, 838,  818, 838,  818, 840,  816, 2532,  830, 844,  812, 844,  812, 844,  818, 2530,  832, 842,  814, 2534,  834, 840,  816, 2532,  852)
	Dim intNum7(143) As ULong = Array As ULong(3342, 3320,  836, 2530,  830, 2534,  834, 822,  834, 2532,  836, 2528,  832, 824,  832, 2534,  834, 2530,  832, 824,  832, 2534,  834, 822,  834, 822,  832, 824,  832, 2532,  836, 820,  836, 822,  834, 2530,  832, 824,  830, 826,  838, 2528,  834, 822,  834, 2532,  850, 38134,  3342, 3320,  834, 2530,  830, 2534,  834, 822,  832, 2532,  836, 2530,  832, 824,  832, 2534,  834, 2530,  838, 818,  838, 2528,  832, 824,  832, 824,  832, 824,  832, 2534,  834, 822,  834, 822,  832, 2532,  836, 820,  836, 820,  836, 2530,  832, 824,  832, 2534,  848, 38136,  3348, 3314,  834, 2532,  836, 2528,  834, 822,  832, 2534,  836, 2530,  832, 824,  832, 2534,  834, 2532,  836, 820,  836, 2530,  832, 824,  832, 826,  830, 824,  832, 2534,  834, 822,  832, 824,  832, 2532,  836, 820,  834, 822,  834, 2530,  830, 826,  838, 2528,  854)
	Dim intNum8(143) As ULong = Array As ULong(3348, 3314,  834, 2532,  830, 2536,  834, 822,  834, 2532,  836, 2530,  832, 2534,  836, 2530,  830, 2536,  834, 822,  832, 2532,  836, 820,  836, 820,  834, 822,  834, 2532,  832, 824,  836, 802,  854, 820,  838, 818,  836, 820,  836, 2530,  832, 824,  832, 2534,  848, 38136,  3348, 3314,  834, 2532,  836, 2528,  832, 824,  832, 2534,  834, 2530,  830, 2536,  832, 2532,  836, 2528,  832, 824,  832, 2534,  834, 840,  816, 840,  816, 840,  816, 2532,  836, 820,  836, 820,  836, 820,  836, 820,  836, 838,  818, 2530,  830, 826,  836, 2528,  854, 38130,  3346, 3316,  832, 2534,  834, 2530,  830, 826,  830, 2534,  834, 2532,  836, 2528,  832, 2534,  834, 2530,  832, 824,  832, 2534,  834, 822,  834, 822,  832, 824,  832, 2532,  836, 820,  834, 822,  834, 822,  834, 822,  834, 840,  816, 2532,  836, 820,  836, 2530,  852)
	Dim intNum9(143) As ULong = Array As ULong(3346, 3316,  830, 2534,  834, 2532,  830, 844,  812, 2536,  832, 2532,  836, 838,  816, 840,  816, 840,  816, 2532,  830, 2536,  832, 842,  814, 842,  814, 842,  814, 2536,  832, 840,  816, 840,  816, 2534,  834, 2530,  832, 2534,  834, 840,  816, 840,  816, 2532,  850, 38134,  3348, 3314,  834, 2532,  830, 2536,  832, 840,  816, 2534,  836, 2530,  832, 842,  814, 844,  812, 844,  812, 2536,  832, 2532,  836, 838,  818, 838,  818, 838,  816, 2532,  830, 844,  812, 844,  818, 2530,  832, 2534,  836, 2530,  832, 842,  812, 844,  812, 2536,  846, 38138,  3346, 3316,  832, 2536,  832, 2532,  830, 844,  812, 2536,  832, 2534,  836, 838,  816, 840,  816, 840,  816, 2532,  830, 2536,  832, 842,  814, 842,  814, 842,  814, 2534,  834, 840,  816, 840,  816, 2534,  836, 2530,  832, 2534,  834, 840,  816, 840,  816, 2532,  850)
 
	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, 94);  // (Replace xxx with desired IP) ,94
  IPAddress gateway(192, 168, 1, 1);
  IPAddress subnet(255, 255, 255, 0);
  WiFi.config(ip, gateway, subnet);

  }
  #end if	
	
	Serial1.Initialize(115200)
	Log("AppStart")
	
	
	If espWifi.Connect2("L*******4","1******h") Then
		'If espWifi.Connect2("G********7","a*******4") Then
		'If espWifi.Connect2("L********3","a********4") Then
		Server.Initialize(51042, "server_NewConnection")
		Server.Listen
		Log("Waiting for connection.")
		
		Log("My ip: ", espWifi.LocalIp)' 192.168.1.95
		Log(bc.HexFromBytes(MacAddress))
		
	Else
		Log("Connection Failed.")
		astream_Error
	End If
	
	irsend.Enable(4,38) 'GPIO-4 D2, 38HZ
	
	grnLED.Initialize(13, grnLED.MODE_OUTPUT)' green LED GPIO-13 D7
	redLED.Initialize(15, redLED.MODE_OUTPUT)' red LED GPIO-15 D8
	grnLED.DigitalWrite(False)
	redLED.DigitalWrite(False)
	
End Sub

Sub Server_NewConnection (NewSocket As WiFiSocket)
	astream.Initialize(NewSocket.Stream,"astream_NewData", "astream_Error")
	Log("server new connection")
	grnLED.DigitalWrite(True)
	
	
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 "tvpwr" ' SSTV Power On/Off
				Log("sending: tv ", intTvPwr)
				irsend.SendNec(intTvPwr, 32)'ULong data,  Int nbits
				astream.Write(Ser.ConvertArrayToBytes(Array("tvpwr")))
				
			Case "vup" ' SSTV Volume Up
				Log("sending: vol+ ", intVup)
				irsend.SendNec(intVup, 32)'ULong data,  Int nbits
				astream.Write(Ser.ConvertArrayToBytes(Array("vup")))
				
			Case "vdn" ' SSTV Volume Down
				Log("sending: vol- ", intVdn)
				irsend.SendNec(intVdn, 32)'ULong data,  Int nbits
				astream.Write(Ser.ConvertArrayToBytes(Array("vdn")))
				
			Case "mute" ' SSTV Mute
				Log("sending: mute ", intMute)
				irsend.SendNec(intMute, 32)'ULong data,  Int nbits
				astream.Write(Ser.ConvertArrayToBytes(Array("mute")))
				
			Case "cblpwr" ' Cable Power On/Off
				Log("sending: cable ", intCablePwr.Length)
				irsend.SendRaw(intCablePwr, 38)'array,khz
				astream.Write(Ser.ConvertArrayToBytes(Array("cblpwr")))
				
			Case "chup" ' Channel Up
				Log("sending: chan up ", intChUp.Length)
				irsend.SendRaw(intChUp, 38)'array,khz
				astream.Write(Ser.ConvertArrayToBytes(Array("chup")))
				
			Case "chdn" ' Channel Down
				Log("sending: chan down ", intChDn.Length)
				irsend.SendRaw(intChDn, 38)'array,khz
				astream.Write(Ser.ConvertArrayToBytes(Array("chdn")))
				
			Case "num0" ' Number 0
				Log("sending: number 0 ", intNum0.Length)
				irsend.SendRaw(intNum0, 38)'array,khz
				astream.Write(Ser.ConvertArrayToBytes(Array("num0")))
				
			Case "num1" ' Number 1
				Log("sending: number 1 ", intNum1.Length)
				irsend.SendRaw(intNum1, 38)'array,khz
				astream.Write(Ser.ConvertArrayToBytes(Array("num1")))
				
			Case "num2" ' Number 2
				Log("sending: number 2 ", intNum2.Length)
				irsend.SendRaw(intNum2, 38)'array,khz
				astream.Write(Ser.ConvertArrayToBytes(Array("num2")))
				
			Case "num3" ' Number 3
				Log("sending: number 3 ", intNum3.Length)
				irsend.SendRaw(intNum3, 38)'array,khz
				astream.Write(Ser.ConvertArrayToBytes(Array("num3")))
				
			Case "num4" ' Number 4
				Log("sending: number 4 ", intNum4.Length)
				irsend.SendRaw(intNum4, 38)'array,khz
				astream.Write(Ser.ConvertArrayToBytes(Array("num4")))
				
			Case "num5" ' Number 5
				Log("sending: number 5 ", intNum5.Length)
				irsend.SendRaw(intNum5, 38)'array,khz
				astream.Write(Ser.ConvertArrayToBytes(Array("num5")))
				
			Case "num6" ' Number 6
				Log("sending: number 6 ", intNum6.Length)
				irsend.SendRaw(intNum6, 38)'array,khz
				astream.Write(Ser.ConvertArrayToBytes(Array("num6")))
				
			Case "num7" ' Number 7
				Log("sending: number 7 ", intNum7.Length)
				irsend.SendRaw(intNum7, 38)'array,khz
				astream.Write(Ser.ConvertArrayToBytes(Array("num7")))
				
			Case "num8" ' Number 8
				Log("sending: number 8 ", intNum8.Length)
				irsend.SendRaw(intNum8, 38)'array,khz
				astream.Write(Ser.ConvertArrayToBytes(Array("num8")))
				
			Case "num9" ' Number 9
				Log("sending: number 9 ", intNum9.Length)
				irsend.SendRaw(intNum9, 38)'array,khz
				astream.Write(Ser.ConvertArrayToBytes(Array("num9")))
				
		End Select
	Next
	
	redLED.DigitalWrite(True)
	Log("red led on")
	Delay(1500)
	redLED.DigitalWrite(False)
	Log("red led off")
	
End Sub

Sub astream_Error
	Log ("Astream Error")
	'Display.page_ASError
	Delay (4000)
	Server.Listen
	grnLED.DigitalWrite(False)
	
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