MQTT broker connect without pre-set IP address - raspberry-pi

I am building a application to use raspberry as MQTT broker & client and receive the message from ESP32 which is another MQTT client. I need to have ESP32 to get the broker connected without pre-setting IP address manually. Does anyone know how to let raspberry share the IP address wirelessly and safely? Or could the raspberry pi get a static IP address no matter what network configuration user changed? Thank you for any help or discuss.

These two instructions gave some help: to build mDNS lookups in raspberry and mDNS client to receive string input. More logic needed to define the principle of "blind search" but considering the security reason, we preferred to transfer string host name by BLE from app side.
mDNS client in ESP32 to receive string input
How is the ESP32 (DOIT DevKit) finding another host in the same LAN via mDNS?
to build mDNS lookups in raspberry:
http://root42.blogspot.com/2015/02/how-to-setup-mdns-lookups-on-raspberry.html

Related

UDP broadcast for IoT discovery using Raspberry PI and public wifi

A couple questions.
TLDR is sending UDP broadcast packets on a Wifi network to allow for discovery of a IoT device (Raspberry PI) a conventional practice?
Long version: I'm working on an IoT project for a class. Based on other IoT devices I've used (a Christmas carol lighting system and music player), we decided to utilize an already working application (https://github.com/balena-os/wifi-connect). The way it works is it runs on the Raspberry PI and hosts a wifi endpoint through the RPI's network card that the user can connect to. Once a user connects to this Wifi endpoint on his/her Iphone, a Wifi selection and password entry page appears on his/her Iphone. The user enters the Wifi/password that the IoT device (Raspberry PI) should connect to. The entered Wifi will then be connected to by the Raspberry PI. The user can then connect to the entered Wifi as well and now both the user's Iphone and Raspberry PI will be connected to the same Wifi network.
We decided to send UDP packets to the broadcast address of the Wifi network from the IoT device, so that users connected to the Wifi (via their Iphones) can "discover" the IoT devices IP address by listening for UDP packets. Once the user discovers the IoT device's IP address, they can send HTTP RESTful API calls to the IoT device. I was wondering if the described process is conventionally used.
# script we are using to send UDP broadcast packets
import socket
import time
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
server.settimeout(0.2)
message = b"message"
while True:
server.sendto(message, ('<broadcast>', 16123))
time.sleep(1)
We tried this setup at a public library, using their free public wifi. However, the UDP broadcast packet discovery process failed. UDP broadcast packets were sent out by the IoT device, but were not received by the Iphone. We're wondering if this has something to do with a firewall. We could successfully send UDP broadcast packets from a Macbook (via a python script) to an IOS emulator residing on the same Macbook, such that the source IP address of the packet was the same as the recipient. Is there a firewall rule such that a broadcast packet sent from IP address [A] can be delivered back to IP address [A] but not to IP address [B].
Also, I didn't mess around with the port number, which might have helped if certain ports were disabled by a firewall. I'm not sure. Help is appreciated. We are both relatively new to this.
DNS-SD (RFC6763) is the a fairly common and standardized way to do network discovery. It actually supports both TCP & UDP services. It was originally invented by Apple under the brand name "Bonjour" for printer discovery.
So yes, it is common to use UDP broadcast packets (via DNS-SD) for IoT device discovery on a Wifi network.

Periodically the Internet disappears in the router Mikrotik

Good afternoon need help. There is a router Mikrotik. The Internet and wi-fi started to disappear, but the connection is periodically resumed. The Mac address is specified by my router in an error.
Error in logs : bridge port received packet with own address as source address (ee:ee:8c:0c:ee:fd), probably loop.
You need share a better environment description.
However, assuming you're using wireless interface, you can try proxy ARP on Wireless interface for avoid loops under WiFi bridge.
/interface wireless set arp=proxy-arp
More info here.

Create a web server using gsm module

I am using raspberry pi
I am using signalR to send data to my client browser.
If my raspberry is on the same network via Ethernet or WiFi then I can get it to work.
If I now disconnect Ethernet and WiFi and switch on my gsm module I get assigned a dhcp address like 10.126.88.4 etc
If I now go to a client browser and type that ip address in the browser it will eventually time-out and upon investigation I find the raspberry pi has been assigned a different IP address.
This happens every time I refresh the browser.
After googling I find that it is the network providers doing this to stop the gsm being used as a server gateway.
One of the solutions is to use a proxy server but I could potentially have a lot of data passing through and
my initial idea was to have client browser talking directly to the raspberry pi device using gsm and thus not going through my own server.
Is there a solution to my predicament?

TCP/IP using Socket Test 3.0 software

I have 'Socket test v-3.0' software installed on two different PCs.
Using it can create a TCP server on 1st PC and a TCP client on 2nd PC.
When I connect both the PCs on LAN I can establish a connection & can communicate between the Server & Client.
But, When I connect both the PCs to individual Internet connections (Using dongles having different ISPs), I couldn't establish the connection between the Server & Client.
How can I do that?
Please help me out..
The private address 192.168.x.x is address that can be used for direct connection only in your private network. If you want to connect to your machine from public internet you have two options:
Get a public IP address
Configure a port forwarding
Public IP address could be get from your internet provider but it is usually requires some extra payment dependent on your service provider policy.
Port forwarding can be configured at the device at the border between your private network and internet. The device does the NAT (network address translation) between your private network and intenet. Use google if you are not sure about NAT.
Such a device can be your own device like an ADSL modem or a set top box. But such a device could be in the internet provider network. If you own the device then you can configure the port forwarding yourself. Modems usually have a web interface where you can easily configure port forwarding. See the documentation to your modem or whatever you have.
If the NAT device is in internet provider network then you have to ask it to configure port forwarding for you. Before you ask please read something about port forwarding so you are sure what you want to configure.

Communicating between networks using sockets

I have a question about network connections among computers.
I've made some applications where messages pass through the Internet (via sockets) to make a connection between two devices. However, a strong condition is that two devices must be connected to the same network.
Can anyone give me a trick how to create a communication using sockets between two computers even if they are connected to different netwkorks?
Thank you in advance.
Here is a great tutorial on how to use sockets and general networking
(in java) http://www.thenewboston.org/watch.php?cat=25&number=38
In order to communicate between two diffrent networks over the internet, you will need to do something called port forwarding. What that does is that when your public IP of your network receives a packet with a spesific port number. The router knows where to send that packet to which local IP.
If you dont port forward and receive some data. The router doesent know where to send the packet. Therefore it discards it, which means others wont be able to connect to you.
You will only need to port forward the network with your server (using the example i linked). How you do that is by logging in to your router, and say that a port which the server uses gets forwarded to the IP of the PC hosting the server.
On the other network (client) you will need to change the IP address of which the client shall connect to. That IP address needs to be your public IP of your server's network. You can find that by connecting to the server's network and go to: http://www.whatsmyip.org/ . Keep in mind that public IP addresses may change over time.
Hope this helped!
-Kad