B4R Question Could anyone tell me what's wrong that Alexa can't find the devices?

Cesar_Morisco

Active Member
Hey guys
I want to use my Alexa to control my fan, I did the following example, Alexa can't find my device, here is the excerpt from my start program, thank you in advance
B4R:
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    timer1.Initialize("timer1_Tick",2000)
    Timer2.Initialize("Stats",1000)
 
    onewire.Initialize(ESPin.D4)'Pino D4 DS18B20
    
    PinGPIO05.Initialize(d1.D1,PinGPIO05.MODE_OUTPUT)'Pino D1 Triac1
    PinGPIO04.Initialize(d1.D2,PinGPIO04.MODE_OUTPUT)'Pino D2 Triac2
    PinGPIO13.Initialize(d1.D7,PinGPIO13.MODE_OUTPUT)'Pino D7 Buzzer
    PinGPIO14.Initialize(d1.D5,PinGPIO14.MODE_OUTPUT)'Pino D5 Triac1
    PinGPIO16.Initialize(d1.D0,PinGPIO16.MODE_OUTPUT)'Pino D16 Led
    
    'Set Desligar Pinos ESP
    PinGPIO05.DigitalWrite(False)
    PinGPIO04.DigitalWrite(False)
    PinGPIO14.DigitalWrite(False)
    PinGPIO16.DigitalWrite(False)
    
    PinGPIO13.DigitalWrite(True)
    Delay(200)
    PinGPIO13.DigitalWrite(False)
    
    alexa.Initialize("DeviceChanged")
    Timer3.Initialize("LoopRun",1)
    Timer3.Enabled = True
    
    alexa.addDevice("ventilador auto",0)
    alexa.addDevice("ventilador normal",1)
    alexa.addDevice("ventilador baixo",2)
    alexa.addDevice("Temporizador 1 hora",3)
    
    SetRelay(0)
    SetRelay(1)
    SetRelay(2)
'    SetRelay(3)
'    SetRelay(4)
'    SetRelay(5)
'    SetRelay(6)
'    SetRelay(7)
    WiFiServer.Start
    ConnectToNetwork(0)
    
End Sub

Sub LoopRun
    alexa.RunLoop
    time = time + 1
    If time = 90000 Then
        time = 0
        Log("loop running")
        Log("memory=", AvailableRAM,"stack :",StackBufferUsage)
    End If
End Sub

Sub  DeviceChanged(index As Byte)
    'unique callback for all device, callback is done with device number
    Log("deviceindex=",index,"deviceName=",alexa.getName(index))
    Select Case index
        Case 0
            If alexa.getValue(0) = 0 Then
                PinGPIO05.DigitalWrite(True)
                PinGPIO13.DigitalWrite(True)
                Delay(200)
                PinGPIO13.DigitalWrite(False)
                RS1 = (True)
                If State = False Then
                    SaveRelay(0,1)
                Else
                    SaveRelay(0,0)
                End If
            Else
                PinGPIO05.DigitalWrite(False)
                PinGPIO13.DigitalWrite(True)
                Delay(200)
                PinGPIO13.DigitalWrite(False)
                RS1 = (False)
                If State = False Then
                    SaveRelay(0,1)
                Else
                    SaveRelay(0,0)
                End If
            End If
        Case 1
            If alexa.getValue(1) = 0 Then
                PinGPIO04.DigitalWrite(True)
                PinGPIO13.DigitalWrite(True)
                Delay(200)
                PinGPIO13.DigitalWrite(False)
                RS2 = (True)
                If State = False Then
                    SaveRelay(1,1)
                Else
                    SaveRelay(1,0)
                End If
            Else
                PinGPIO04.DigitalWrite(False)
                PinGPIO13.DigitalWrite(True)
                Delay(200)
                PinGPIO13.DigitalWrite(False)
                RS2 = (False)
                If State = False Then
                    SaveRelay(1,1)
                Else
                    SaveRelay(1,0)
                End If
            End If
        Case 2
            If alexa.getValue(2) = 0 Then
                PinGPIO14.DigitalWrite(True)
                PinGPIO13.DigitalWrite(True)
                Delay(200)
                PinGPIO13.DigitalWrite(False)
                RS3 = (True)
                If State = False Then
                    SaveRelay(2,1)
                Else
                    SaveRelay(2,0)
                End If
            Else
                PinGPIO14.DigitalWrite(False)
                PinGPIO13.DigitalWrite(True)
                Delay(200)
                PinGPIO13.DigitalWrite(False)
                RS3 = (False)
                If State = False Then
                    SaveRelay(2,1)
                Else
                    SaveRelay(2,0)
                End If
            End If
    End Select
End Sub
 

Cesar_Morisco

Active Member
Hey guys
About the example
Which was attached along with the Espalexa library, it works and Alexa can discover the normal device

In my example, Alexa cannot find the devices
Could anyone tell me what I'm doing wrong, I really appreciate it
B4R:
Sub Process_Globals
    Public Serial1 As Serial
    Public wifi As ESP8266WiFi
    Public eeprom As EEPROM
    Private const MAGIC_EEPROM As Byte = 213
    Private sr As B4RSerializator
    Private bc As ByteConverter
    Public esp As ESP8266
    Public alexa As Espalexa
    Public device As Device
    Public Timer1 As Timer
    Public time = 0 As UInt
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    WiFiServer.Start
    ConnectToNetwork(0)
    
    alexa.Initialize("DeviceChanged")
    Timer1.Initialize("LoopRun",1000)
    Timer1.Enabled = True
    
    alexa.addDevice("Ventilador auto",0)
    alexa.addDevice("Ventilador norma",1)
    alexa.addDevice("Ventilador baixo",2)
End Sub

Sub LoopRun
    alexa.RunLoop
    time = time + 1
    If time = 90000 Then
        time = 0
        Log(time)
    End If
End Sub

Sub DeviceChanged(index As Byte)
    Log("deviceindex=",index,"deviceName=",alexa.getName(index))
    Select Case index
        Case 0
            If alexa.getValue(0) = 0 Then
                Log("CMDA ",(1))
            Else
                Log("CMDA ",(0))
            End If
        Case 1
            If alexa.getValue(1) = 0 Then
                Log("CMDB ",(1))
            Else
                Log("CMDB ",(0))
            End If
        Case 2
            If alexa.getValue(2) = 0 Then
                Log("CMDC ",(1))
            Else
                Log("CMDC ",(0))
            End If
        Case 3
            If alexa.getValue(3) = 0 Then
                Log("CMDD ",(1))
            Else
                Log("CMDD ",(0))
            End If
    End Select
End Sub
Public Sub SaveNetworkDetails(Data() As Byte)'Gravar Eeprom
    Log("Salvando dados de Rede")
    Log("Comprimento de Dados : ", Data.Length)
    Log("Memoria=", AvailableRAM)
    Log("Tamanho :",StackBufferUsage)
    eeprom.WriteBytes(Array As Byte(MAGIC_EEPROM,Data.Length), 0)
    eeprom.WriteBytes(Data, 2)
End Sub

Public Sub ConnectToNetwork(tag As Byte)
    wifi.Disconnect
    If GetStoredData = 1 Then
        'read settings from EEPROM
        Dim SSID As String = bc.StringFromBytes(GStore1.slot0) 'ignore
        Dim Password As String = bc.StringFromBytes(GStore1.slot1) 'ignore" '
        Log("Tentando se Conectar a: ", SSID, " Senha: ", Password)
        If wifi.Connect2(SSID,Password) Then
            Log("Conectado com Sucesso : ", GStore1.slot0)
            Log("IP Local: ", wifi.LocalIp)
            StopAP
        Else
            Log("WIFI Falha Ao Se Conectar - Iniciar WifiServer ")
            Log("Automatico Reset em 1 m")
            CallSubPlus("ConnectToNetwork",60*1000,0) 'tente conectar novamente após 1 minuto
        End If
    Else
        Log("Dados Da Rede Não Encontrados .")
        WiFiServer.Start
    End If
End Sub

Public Sub GetStoredDataLength As Byte'Ler Eemprom
    Dim header() As Byte = eeprom.ReadBytes(0, 2)
    If header(0) = MAGIC_EEPROM  Then
        Return header(1)
    End If
    Return 0
End Sub

Sub    GetStoredData As Byte'Ler Objetos Eeeprom
    Dim length As Byte = GetStoredDataLength
    Log("Parâmetros do Wifi Salvo na Eeprom  =",length)
    If length > 0 Then
        Dim ObjectsBuffer(8) As Object
        Dim Data() As Byte = eeprom.ReadBytes(2, length)
        Dim Objects() As Object = sr.ConvertBytesToArray(Data, ObjectsBuffer)
        If Objects.Length <> 0 Then
            For i = 0 To 7
                GStore1.Put(i,Objects(i))
            Next
            Return 1
        Else
            Return 0
        End If
    Else
        Return 0
    End If
End Sub

Public Sub ClearStoredDataLength'Apagar Eeeprom
    Dim header() As Byte = eeprom.ReadBytes(0, 2)
    If header(0) = MAGIC_EEPROM Then
        header(1) = 0
        eeprom.WriteBytes(header,0)
        Log("EEprom Apagada")
    End If
    GStore1.Slot0(" ")
    GStore1.Slot1(" ")
    Delay(100)
    esp.restart
End Sub

public Sub StopAP
    Log("Stop AP")
    RunNative("stopAP", Null)
End Sub

#if C
  #include <ESP8266WiFi.h>
  void stopAP (B4R::Object* u) {
  WiFi.softAPdisconnect(1);
  }
#end if
 
Upvote 0

candide

Active Member
Licensed User
you should try to replace :
B4X:
    alexa.addDevice("Ventilador auto",0)
    alexa.addDevice("Ventilador norma",1)
    alexa.addDevice("Ventilador baixo",2)
by
B4X:
    alexa.addDeviceOnOff("Ventilador auto",127)
    alexa.addDeviceOnOff("Ventilador norma",127)
    alexa.addDeviceOnOff("Ventilador baixo",127)
 
Upvote 0

Cesar_Morisco

Active Member
Hello how are you?
I made the changes you said and Alexa can't find it
And when Alexa she searches, this appears in the logo
B4R:
Astream NewData startB=GET /description.xml HTTP/1.1
Host: 10.0.0.13
Accept: */*
Content-Type: application/json
Astream NewData startB=GET /description.xml HTTP/1.1
Host: 10.0.0.13
Accept: */*
Content-Type: application/json
 
Upvote 0

Cesar_Morisco

Active Member
Hi Candide How are you?
I did other tests and it doesn't work with my program. Alexa doesn't find anything even when recalibrating it.
In your example, Alexa finds the devices normally and quickly. Anyone else use Alexa to help me, what am I doing wrong, I have to do it, thanks again
B4X:
WiFiServer.Start
    ConnectToNetwork(0)
   
    alexa.Initialize("DeviceChanged")
    Timer3.Initialize("LoopRun",1)
    Timer3.Enabled = True
   
    alexa.addDeviceOnOff("test0",127)
    Log("device test0 created")
    alexa.addDeviceDimming("test1",126)
    Log("device test1 created")
    alexa.addDeviceWhiteSpectrum("test2",250)
    Log("device test2 created")
   
    Delay(1000)
    For i = 0 To 9
      Log("device name: ",alexa.getName(i))
      Log("value=",alexa.getValue(i))
    Next
    Log("mac address: ",alexa.getEscapedMac)
   
   
   
 
Meu IP AP: 192.168.4.1
Parâmetros do Wifi Salvo na Eeprom  =89
Tentando se Conectar a: Help-Info Central Tec 2.4G Senha: Pp1c2s3m4
Conectado com Sucesso : Help-Info Central Tec 2.4G
IP Local: 10.0.0.13
Stop AP
device test0 created
device test1 created
device test2 created
device test3 created
device test4 created
device name: test0
value=127
device name: test1
value=126
device name: test2
value=250
device name: test3
value=127
device name: test4
value=255
device name:
value=0
device name:
value=0
device name:
value=0
device name:
value=0
device name:
value=0
mac address: e8db84986f41
 
Upvote 0
Top