Two Arduino Messaging System - Software

Layer by Layer 

We'll be building up our messaging system layer by layer. Starting with the physical layer through the transport layer. We'll be using this same system for our Weather Station as well as a couple more projects in this kit.

Creating a Network and Receiver 

This is the first project with two Arduinos. There is one with a black base shield and one without. Make sure you know which is which. In order to test the Arduinos as we make code for them, you'll need to power both. It's easiest to have one connected to the computer and other connected to power my battery or wall charger.

A Network

Our first step is to create a WiFi network. The code below adds sections for the physical and data link layer. Starting the WiFi module itself enabled the WiFi radio and this the physical layer. The same chip that handles that also takes care of the data link layer. We are using the WiFi standard so a lot of work in terms of radio wave generation and transmission has been done for us. Plug in the Arduino without the base shield and upload the following code.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//All the libraries used
#include <WiFiEsp.h>
#include "SoftwareSerial.h"

//Socket of the WiFi module
SoftwareSerial Serial1(A5, A4); // RX, TX

//IP and broadcasting varibles
IPAddress apIP = IPAddress(192, 168, 111, 111);

//WiFi network settings
char ssid[] = "homebase";            // your network SSID (name)
char pass[] = "password";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status


void setup() {
  // initialize serial for debugging
  Serial.begin(9600);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // Connect serial to Wifi module
  WiFi.init(&Serial1);
  WiFi.configAP(apIP);

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    // Make a network
    status =  WiFi.beginAP(ssid, 10, pass, ENC_TYPE_WPA2_PSK);
  }

  //Print WiFi status
  Serial.println("Network Created");
}

void loop() {
  // put your main code here, to run repeatedly:

}

Observe

Open up the Serial Monitor. The Arduino will start up the WiFi module then our network will be created. Check on a phone or computer that you see our network called homebase.

Modify

You can change the ssid[] or name of the network as well as the pass[] or password of the network.

Receiver

With this code we can see the network we made but it doesn't do anything. The code below adds sections for the network and transport layer. You can see that we have defined our IP address as well as set up a listener for any UDP packets.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//All the libraries used
#include <WiFiEsp.h>
#include <WiFiEspUdp.h>
#include "SoftwareSerial.h"

//Socket of the WiFi module
SoftwareSerial Serial1(A5, A4); // RX, TX
//Create UDP object
WiFiEspUDP Udp;

//IP and broadcasting varibles
IPAddress apIP = IPAddress(192, 168, 111, 111);

//WiFi network settings
char ssid[] = "homebase";            // your network SSID (name)
char pass[] = "password";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

//Ports to be used
unsigned int listeningPort = 10003;  // local port to listen on
unsigned int sendingPort = 10002;  // local port to listen on
char packetBuffer[255];          // buffer to hold incoming packet
char ReplyBuffer[] = "ACK";


void setup() {
  // initialize serial for debugging
  Serial.begin(9600);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // Connect serial to Wifi module
  WiFi.init(&Serial1);
  WiFi.configAP(apIP);

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    // Make a network
    status =  WiFi.beginAP(ssid, 10, pass, ENC_TYPE_WPA2_PSK);
  }

  //Print WiFi status
  Serial.println("Network Created");

  // Set up UDP listener on port
  Udp.begin(listeningPort);

  // Print out which port
  Serial.print(F("Listening on port "));
  Serial.println(listeningPort);
}

void loop() {
  // if there's data available, read the packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remoteIp = Udp.remoteIP();
    Serial.print(remoteIp);
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    int len = Udp.read(packetBuffer, 255);
    if (len > 0) {
      packetBuffer[len] = 0;
    }
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }

}

Observe

Open up the Serial Monitor. The Arduino will start up the WiFi module then our network will be created. We can now see an additional step. The Arduino is waiting to pick up and report any UDP packets.

A Simple Sender 

With our network created and receiver ready for information, we need something to send data. Unplug the Arduino without the shield and replace it with the Arduino with the shield.

Send on Press

This used the button as a trigger for sending a packet. When the button is pressed a message is converted into a UDP packet. Upload the following code.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//All the libraries used
#include <OneButton.h>
#include <WiFiEsp.h>
#include <WiFiEspUdp.h>
#include "SoftwareSerial.h"

//Definable sockets for modules
#define buttonSocket 4

//Socket of the WiFi module
SoftwareSerial Serial1(8, 9); // RX, TX
//Create UDP object
WiFiEspUDP Udp;

//IP and broadcasting varibles
IPAddress apIP = IPAddress(192, 168, 111, 111);
IPAddress stationIP = IPAddress(192, 168, 111, 112);


//WiFi network settings
char ssid[] = "homebase";        // your network SSID (name)
char pass[] = "password";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

//Ports to be used
unsigned int listeningPort = 10002;  // local port to listen on
unsigned int sendingPort = 10003;  // local port to sent on

//Arrays for storing sent and received packets
char packetBuffer[255];          // buffer to hold incoming packet
char ReplyBuffer[] = "ACK";      // a string to send back
char messageToSend[] = "Hello World";

//Creates button object
OneButton button = OneButton(buttonSocket, false);

void setup() {
  // initialize serial for debugging
  Serial.begin(9600);
  // initialize serial for WiFi module
  Serial1.begin(9600);
  // Connect serial to Wifi module
  WiFi.init(&Serial1);
  WiFi.config(stationIP);

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    // Print which SSID it's trying to connect to
    Serial.print(F("Attempting to connect to WPA SSID: "));
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  //Print WiFi status
  Serial.println(F("Connected to wifi"));
  printWifiStatus();

  // Set up UDP listener on port
  Udp.begin(listeningPort);

  // Print out which port
  Serial.print(F("Listening on port "));
  Serial.println(listeningPort);

  // Print out which port
  Serial.print(F("Sending on port "));
  Serial.println(sendingPort);

  // Create button event functions
  button.attachClick(singlePress);
}

void loop() {
  // Update button events
  button.tick();

}

void singlePress() {

  //Print out what the WiFi Module is sending
  Serial.println(F("Sending Packets"));
  Serial.print(F("Sending to: "));
  Serial.println(apIP);
  Serial.print(F("On Port:  "));
  Serial.println(sendingPort);
  Serial.print(F("Message is:  "));
  Serial.println(messageToSend);

  //Send the message
  Udp.beginPacket(apIP, sendingPort);
  Udp.write(messageToSend);
  Udp.endPacket();
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Observe

Open the Serial Monitor. This Arduino will connect to the network we made before. Only connected you can press the button to send a message. When you see that the message Hello World was sent then we know it's working. Swap the Arduino so that the one without the base board is connected to the computer. Open the Serial Monitor again. Wait for the network to come online. Now press the button on the other Arduino. The message will send and be delivered to the other Arduino before being displayed on the Serial Monitor.

Code uploaded
Code uploaded
About to press
Pressed
check Serial Monitor

A Breather 

This is the most basic set up for sending information over WiFi. In the next section, we'll talk about how to add more features to make this a real two way messaging system.