Post topics, source code that relate to the Arduino Platform

User avatar
By Torin42
#47870
we don't know how to send data to a specific, multiple and all rifle/s without having to know the exact routing table. I mean, we would like to be able, for example, to send a "host game packet" from a rifle, to the net, and all the players connected to the net could join that game. After this, a "start game packet" is sent to all players at the same time.

What I would do in your case is to set up a Raspberry Pi (or for now your computer) with a fix ip (for example 192.168.1.2 if the router is 192.168.1.1), and then the rifles connect to it for information when they want to start a game or join to a game.
Code: Select allrifle1 wants to start a game
rifle1 sends a packet to Raspberry (he knows its IP, because it's hardcoded): "I want to start a game"
Now Raspberry knows rifle1's IP
Raspberry logs it: new game by rifle1

rifle4 wants to join the game
rifle4 sends a packet to Raspberry: "List me the games"
Now Raspberry knows rifle4's IP.
Raspberry to rifle 4: "There is only one game: rifle1's game"
rifle4 to Raspberry: "I want to join to rifle1's game"

Then the rifles always send packets to the Raspberry Pi, and the Raspberry sends them to the appropriate receiver. So the Raspberry will have a nice log of everything. For example:
Code: Select allrifle1 was hit by rifle4 (the number 4 is sent with the infrared signal)
rifle1 sends a packet to the Raspberry: "I was hit by rifle4"
Raspberry knows the IP of rifle1, so he knows the origin of the message. He also knows the IP of rifle4 (since first every rifle connected to him to join the game).
Raspberry sends a packet to rifle4: "You hit rifle1" or "You hit someone" (depends on your logic)
Raspberry logs the action: kills_of_rifle4++; deaths_of_rifle1++;

In the end of the game Raspberry can send the statistics to everyone. Either one-by-one (one packet to each rifle; after all there won't be too many riffles around), or using multicast or broadcast (although I don't know if ESP8266 handles them correctly).

Of course you can do it without a central module, but then the code on the rifles will be more complicated, since the rifle which hosts the game needs to take care of more stuff.

Or alternatively you can just simply send all packets to the broadcast IP (eg 192.168.1.255) and then everyone will receive it and handle the logic in the data part. For example data would be like: "rifle1 was hit by rifle4" then rifle4 will play some sound ("Good job!") and every other rifle will store this kill in the score table. This will make a lot of unnecessary communication and processing, but if you only send packet if someone is killed (which won't happen 100 times per second) I think this could work. (Also I haven't tested the broadcast with ESP, but it should work.)
User avatar
By NovaTag OFFICIAL
#48053 Hey Torin42!

Thank you very much for your detailed answer. I'm sorry, I haven't answered it in few days because I have had to try several configurations to make the WiFi modules work correctly on the net.

As our intent is to provide a really simple and versatile system, we particulary love the last kind of approach that you correctly suggested us. In fact, we were trying to gather information about this particular type of transmission. After 3 days of trying, we are able to share the results with you and all the Esp Community:

Chapter 1: THE QUESTION
We had the problem of understanding how to correctly send data packets to:

    A single rifle
    Multiple rifles
    All the rifles in game

Since the module itself is capable of handling a maximum of 5 connections at the same time, sending data to more than 5 rifles is actually impossible, unless we decide to use a different technique, as Torin42 suggested: the broadcast ip.

Chapter 2: WHAT ARE WE TALKING ABOUT?
A broadcast ip is a specific ip in which you can send data packets that will be sent to all the clients of the network. This generates a lot of unnecessary traffic (but in our case it's the only solution) but let us doing this kind of job in a decent way.

Chapter 3: SOUNDS GOOD BUT HOW CAN YOU DO THIS?
To send packets in broadcast, there are few rules you have to follow. First one is that this technique works only if you are sending UTP packets, using TCP it won't work at all. The substantial difference in sending UDP packets is that they all make use of a specific port (you can freely choose the one you like) to route all the packets to an application. Also UDP packets are faster and lighter than TCP ones because they are shared with a "best effort technique": you don't have a warranty that you will receive them and you will receive correct data, but they will try their best.

Chapter 4: CODING
In order to make your module receive and send UDP packets at the same time, you have to do some steps so that it will open itself to the port you previously choose and also send data to that port, here is how to do it:

The first thing you have to do is to create a UDP server, to let the module receive packets. Just write these commands on the Serial port of the module:

Code: Select all(AT+CIPMUX=0) compulsory
AT+CIPSTART="UDP","REMOTE_IP",REMOTE_PORT,UDP_LOCAL_PORT,UDP_MODE


If you feel, you can get additional information by taking a look at this brilliant manual at page 44, you will find a clear definition of this kind of command.

From this moment, your module will listen to UDP packets incoming from the port you gave him into the command. It will output packets immediately after the +IPD string on the Serial.
Additional tip: if you wish to make the module listen to all UDP packets (broadcast mode) you can write something like this: AT+CIPSTART="UDP","0",0,UDP_LOCAL_PORT,2

Next step is to let the module send data, without loosing the possibility to also receive, here is the code:

Code: Select allAT+CIPMUX=1 compulsory
AT+CIPSEND=0,12
Hi everyone!


By doing this, the module will send a UDP packet containing the string "Hi everyone!" to the ip referring to the link 0.

You can easily find more information about the command syntax at page 45 of the manual.

If you wish to send data to a specific ip, just add it to the command syntax, like this: AT+CIPSEND=0,20,"DEST_IP",PORT

If you wish to send data to all the clients of the network, listening to the port you've specified, you can do it this way:
AT+CIPSEND=0,20,"BROADCAST_IP",PORT

To check the active connections, in order to determine the link id, you can write AT+CIPSTATUS? the module will answer, showing you the list of all the active connections, ordered by ID.

Last tip: to run both the receive and send mode on the module, don't swap them, otherwise you won't be able to enter the server mode.


Hope this tutorial could help other people, apart from me :lol:

Again, I would like to thank Torin42 for the precious help. We will continue to work to fix the last problems we're experiencing and let you guys know how to fix them in the easiest way.

See you next time! ;)