B4R Question Write relay status to Eeprom

Cesar_Morisco

Active Member
Hey guys
All good.
I'm stuck on this question
Record the status of the relay and switch it off
On my example it writes and reads the error relay status and the program restarts and exits MQTT
Does anyone have an idea how to solve this
I don't have much knowledge
Here is the code I'm using Thank you
B4R:
Dim STAR As Int

STAR = 1
eeprom.WriteBytes(bc.IntsToBytes(Array As Int(STAR)),2)

STAR = 0
eeprom.WriteBytes(bc.IntsToBytes(Array As Int(STAR)),2)


Dim b() As Byte = eeprom.ReadBytes(0, 2)
    b = eeprom.ReadBytes(2, 2)
    STAR = bc.IntsFromBytes(b)(0)
    If STAR = 1 Then
        Log("NO RELAY 1")
         PinGPIO05.DigitalWrite(TRUE)'Relay 1
    Else
        Log("OFF RELAY 1")
         PinGPIO05.DigitalWrite(FALSE)'Relay 1
    End If
 

RJB

Active Member
Licensed User
Longtime User
Hi,
Not really sure what you are doing there but:
you are writing 1 and 0 to the same location (2, lines 4 and 7)
you are reading from a location that you have not set (0, line 10)
then overwriting that in line 11
you are putting an array into an int (line 12)
 
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
Here is the code I'm using
I don't know if the project is the same as the other posts previously,
where from the code we see, you make use of the eeprom to store the ssid and password.
Be careful to store on eeprom the relays state values on eeprom addresses you are already using
 
Upvote 0

Cesar_Morisco

Active Member
Hi, [CODE lang="b4x" title="B4R"]Out of bounds error. Array length = 0, Index = 1[/CODE]RJB
Not really sure what you are doing there but:
you are writing 1 and 0 to the same location (2, lines 4 and 7)
you are reading from a location that you have not set (0, line 10)
then overwriting that in line 11
you are putting an array into an int (line 12)
Hi,
Not really sure what you are doing there but:
you are writing 1 and 0 to the same location (2, lines 4 and 7)
you are reading from a location that you have not set (0, line 10)
then overwriting that in line 11
you are putting an array into an int (line 12)
Hello
Thanks for the answer
I made the modifications
As you said
Error continues
here.
 
Upvote 0

Cesar_Morisco

Active Member
I don't know if the project is the same as the other posts previously,
where from the code we see, you make use of the eeprom to store the ssid and password.
Be careful to store on eeprom the relays state values on eeprom addresses you are already using
hello again my friend
Your question yes and the same program here in my electronic repair shop I have a lot of power outage I need my relays to go back to where they were.
Thanks for your response
Any ideas A hug
 
Upvote 0

Cesar_Morisco

Active Member
VocĂŞ pode nos mostrar seu cĂłdigo atualizado?

Hello RJB
My code is the same as this post here
If you can help me I will be very grateful
A hug
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
Hi,
I'm afraid I had a lot of problems using B4X MQTT so no longer use it.
What I really meant was could you post here the code from post #1, modified a you said in post #4 that is still giving errors.
Alternatively if you could described what you are trying to achieve with it then we might be able to help.
What I have guessed at is something like the following. Obviously not tested as I don't have the relay setup.
Call TestRelay. The relay should operate then release.
B4X:
Sub Process_Globals
   private const RelayInfoStart as int = 10    'i.e first location for relay info storage
End Sub

'OnOff = 0 for off, 1 for on
Sub SaveRelay(RelayNo As Int, OnOff As Byte)
    EEProm.WriteBytes(Array As Byte(OnOff), RelayInfoStart + RelayNo)
End Sub

Sub SetRelay(RelayNo As Int)
    Dim State() As Byte = EEProm.ReadBytes(RelayInfoStart + RelayNo, 1)
    Select RelayNo
        Case 0
            If State(0) = 1 Then PinGPIO05.DigitalWrite(True) Else PinGPIO05.DigitalWrite(False)'Relay 1
        Case 1
            'If State(0) = 1 Then PinGPIO0X.DigitalWrite(True) Else PinGPIO0X.DigitalWrite(False)'Relay 1
        'case etc.
    End Select

End Sub

Sub TestRelay
   
    SaveRelay(0, 0)

    SetRelay(0)    'relay 0, released. NO contacts open. Off
    Delay(1000)

    SaveRelay(0, 1)

    SetRelay(0)    'relay 0, operated. NO contacts closed. On
    Delay(1000)

    SaveRelay(0, 0)

    SetRelay(0)    'off again

End Sub
[/code]
 
Upvote 0

Cesar_Morisco

Active Member
Hi,
I'm afraid I had a lot of problems using B4X MQTT so no longer use it.
What I really meant was could you post here the code from post #1, modified a you said in post #4 that is still giving errors.
Alternatively if you could described what you are trying to achieve with it then we might be able to help.
What I have guessed at is something like the following. Obviously not tested as I don't have the relay setup.
Call TestRelay. The relay should operate then release.
B4X:
Sub Process_Globals
   private const RelayInfoStart as int = 10    'i.e first location for relay info storage
End Sub

'OnOff = 0 for off, 1 for on
Sub SaveRelay(RelayNo As Int, OnOff As Byte)
    EEProm.WriteBytes(Array As Byte(OnOff), RelayInfoStart + RelayNo)
End Sub

Sub SetRelay(RelayNo As Int)
    Dim State() As Byte = EEProm.ReadBytes(RelayInfoStart + RelayNo, 1)
    Select RelayNo
        Case 0
            If State(0) = 1 Then PinGPIO05.DigitalWrite(True) Else PinGPIO05.DigitalWrite(False)'Relay 1
        Case 1
            'If State(0) = 1 Then PinGPIO0X.DigitalWrite(True) Else PinGPIO0X.DigitalWrite(False)'Relay 1
        'case etc.
    End Select

End Sub

Sub TestRelay
  
    SaveRelay(0, 0)

    SetRelay(0)    'relay 0, released. NO contacts open. Off
    Delay(1000)

    SaveRelay(0, 1)

    SetRelay(0)    'relay 0, operated. NO contacts closed. On
    Delay(1000)

    SaveRelay(0, 0)

    SetRelay(0)    'off again

End Sub
[/code]
Hello
All good
About post number 1 I've already been resolved
I'm new to MQTT Starting now I have some doubts
My program is already working very well, I just need to save the status of the relay so that it returns when the power fails
 
Upvote 0

Cesar_Morisco

Active Member
Hello again
I made your example it records you in the eeprom of the error in reading the wifi it goes back to AP mode
It seems to me that it is recording in the place that it is using for wifi password
I thank
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
That's possible as I don't know what you are already using the EEPROM for.
I put the line 'private const RelayInfoStart as int = 10 ' in Process Global so that you can move the location of the relay information in the EEPROM. The = 10 is the start position in the EPPROM, change it to something after the locations you are using for the password etc.
If you don't know what's already in use then try 20, 40, 60 etc. in place of the 10 and see what happens!
 
Upvote 0

Cesar_Morisco

Active Member
That's possible as I don't know what you are already using the EEPROM for.
I put the line 'private const RelayInfoStart as int = 10 ' in Process Global so that you can move the location of the relay information in the EEPROM. The = 10 is the start position in the EPPROM, change it to something after the locations you are using for the password etc.
If you don't know what's already in use then try 20, 40, 60 etc. in place of the 10 and see what happens!
Yes, Eeeprom is also used for wifi password and Mqtt information with url, name, password, id, etc.
B4R:
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)
    ConnectToNetwork
End Sub

Public Sub ConnectToNetwork
    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
            ConnectToMqtt
       Else
            Log("WIFI Falha ao Se Conectar - Iniciar WifiServer ")
            WiFiServer.Start
      End If
    Else
        Log("Dados de Rede nĂŁo Encontrados .")
        WiFiServer.Start
    End If
End Sub


Private Sub ConnectToMqtt
    mqtt.Initialize2(mqttSocket.Stream, bc.StringFromBytes(GStore1.slot2),bc.StringFromBytes(GStore1.slot3), bc.StringFromBytes(GStore1.slot4), "mqtt_MessageArrived", "mqtt_Disconnected") 'ignore
    MQTTOpt.Initialize(bc.StringFromBytes(GStore1.Slot5),bc.StringFromBytes(GStore1.slot6))
    
    If mqtt.Connect2(MQTTOpt) = False Then
 
Upvote 0

Cesar_Morisco

Active Member
hello RJB
All good
I did with you said I changed the eeprom address value it's working fine
thank you heart
and everyone
I'll leave the example of the code here
B4R:
Private const RelayInfoStart As Int = 250 '''Process_Globals
SetRelay(0)'AppStart

Public Sub SetRelay(RelayNo As Int)
    Dim State1() As Byte = eeprom.ReadBytes(RelayInfoStart + RelayNo,1)
    Log("LER RELE: ",State1," Rele: ",RelayNo)
    Select RelayNo
        Case 0
            If State1(0) = 1 Then PinGPIO05.DigitalWrite(True) 'Relay 1
        Case 1
            If State1(0) = 0 Then PinGPIO05.DigitalWrite(True) 'Relay 1
            'case etc.
    End Select
End Sub

Select Case Topic
        Case "Rele_A"
            PinGPIO05.DigitalWrite(Not(True))'Relay 1
            SaveRelay(0,1)
       Case "Rele_B"
             PinGPIO05.DigitalWrite(Not(False))'Relay 1
            SaveRelay(0,1)
    End Select
    
    Public Sub SaveRelay(RelayNo As Int, OnOff As Byte)
    eeprom.WriteBytes(Array As Byte(OnOff), RelayInfoStart + RelayNo)
End Sub
 
Upvote 0
Top