B4R Question How can I set fixed IP for my arduino Mega 2560?

ALEX BISSARO PITTA

Member
Licensed User
I'm trying to set a static IP for Arduino Mega 2560. How can I do that?
I find out the example below but it is for Wifi module it looks like.

RunNative( "SetIP" , Null )#if C
void SetIP(B4R::Object* o) {

IPAddress ip(192, 168, 1, xxx); // (Replace xxx with desired IP)
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
WiFi.config(ip, gateway, subnet);

}#end if
 

ALEX BISSARO PITTA

Member
Licensed User
Thank you, it worked.

1) How about setting a static ip for multiple devices? How is possible to make that? I have used in the code below the IP showed on B4A Bridge.

2) When sending data for multiple devices, the way below should be the correct way or there is another alternative?

"Sub Btn_StateChanged (State As Boolean)

For i=1 To 2

If i=1 Then
udp.BeginPacket(serverIp1, serverPort)
Else If i=2 Then
udp.BeginPacket(serverIp2, serverPort)
End If"



B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private eth As Ethernet
    Private udp As EthernetUDP
    Private serverIp1() As Byte = Array As Byte(192, 168, 0, 104) 'IP Cell Phone 1
    Private serverIp2() As Byte = Array As Byte(192, 168, 0, 106) 'IP Cell Phone 2
    Private MacAddress() As Byte = Array As Byte(0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED)
    Private const serverPort As UInt = 56646'51042
    Private leds(2) As Pin
    Private btn As Pin
End Sub



Private Sub AppStart

   
    Serial1.Initialize(115200)
    Log("AppStart")
    leds(0).Initialize(13, leds(0).MODE_OUTPUT)
   
    btn.Initialize(btn.A0, btn.MODE_INPUT_PULLUP)
    btn.AddListener("btn_StateChanged")

    eth.Initialize(MacAddress, Array As Byte(192, 168, 0, 200))
    Log("Connected to network. My Arduino ip address: ", eth.LocalIp)
    'End If
    udp.Initialize(5, "udp_PacketArrived")
   
End Sub


Sub udp_PacketArrived (Data() As Byte, Ip() As Byte, Port As UInt)
    Log("PacketArrived from:")
    PrintIp(Ip)
    Log("Porta:",Port)
    Log( "Packet:",Data(0),Data(1))
    Log(" ")

    If Data.Length < 2 Then Return
    'first byte is the led index
    'second byte is the state
    leds(Data(0)).DigitalWrite(Data(1) = 1)
   
End Sub

Sub PrintIp(ip() As Byte)
    Log("IP: ",ip(0),".", ip(1),".", ip(2),".", ip(3))
End Sub


Sub Btn_StateChanged (State As Boolean)
   
    For i=1 To 2
       
        If i=1 Then
            udp.BeginPacket(serverIp1, serverPort)
        Else If i=2 Then
            udp.BeginPacket(serverIp2, serverPort)
        End If
       
       
        '(It makes more sense to send a single byte instead of a string.)
        udp.Write("Button is ".GetBytes)
        If State = False Then
            udp.Write("down".GetBytes)
            Log("down")
        Else
            udp.Write("up".GetBytes)
            Log("up")
        End If
        udp.SendPacket
       
    Next
End Sub
 
Upvote 0
hi
you can use this code :

B4X:
Private Sub AppStart
    RunNative( "SetIP" , Null )
End Sub

#if C
  void SetIP(B4R::Object* o) {
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAD
};
IPAddress ip(192, 168, 0, 85);
IPAddress myDns(8, 8, 8, 8);
IPAddress gateway(192, 168, 0, 2);
IPAddress subnet(255, 255, 255, 0);
Ethernet.begin(mac, ip, myDns, gateway, subnet);
  }
  #end if
 
Upvote 0

ALEX BISSARO PITTA

Member
Licensed User
Thanks. It works.

And there is anyway to set a static IP for the cell Phone?
I have three acess points at my home and all the time it connects to a different AP it changes the IP and it is not possible to send or receive packets.
 
Upvote 0

ALEX BISSARO PITTA

Member
Licensed User
Having a known MAC, do you see any way that I can set a static IP for three different cell phones?
I need to have a know static IP for each of the three devices in order to my arduino send the packets.
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
DHCP use MAC to distribute IP's, if it's a first time connection they receive a new IP address, if it's old they get the previous one.
Store somewhere the IP's on the network along with a user's name(a DB server is OK) and you can connect any phone via user's name(a static info).
 
Upvote 0
Top