Franco van Gastel
Published © CC0

WiFi Messenger

Calling my son from his room using 2 ESP8266 modules, instead of shouting.

IntermediateShowcase (no instructions)8 hours4,319
WiFi Messenger

Things used in this project

Hardware components

ESP8266 ESP-12E
Espressif ESP8266 ESP-12E
×2
Rotary Encoder with Push-Button
Rotary Encoder with Push-Button
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×2
Oled display 0,96 inch
×1
Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1
LED (generic)
LED (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Laser cutter (generic)
Laser cutter (generic)

Story

Read more

Custom parts and enclosures

Receiver housing Wood

Dxf-files used for laser cutter.
Receiver box made of 3mm plywood.

Receiver Housing glass

Transparent plastic 3 mm. Pmma, plexiplass.

Sender housing

Consists only of 3 mm plywood.

Receiver Housing

Result

Sender Housing

Result

Schematics

Sender hardware

Receiver hardware

Code

Sender Software

Arduino
The software of the wifi sender.
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

// wifi connection variables
const char* ssid = "xxxxxxx";
const char* password = "xxxxxxxxx";
boolean     wifiConnected = false;

// berichten             0123456789012345
const char bericht0[] = "                ";
const char bericht1[] = "Eten !          ";
const char bericht2[] = "Komen nu!       ";
const char bericht3[] = "Kom je zo?      ";
const char bericht4[] = "GSM aan lader   ";
const char bericht5[] = "Wij zijn ff weg ";
const char bericht6[] = "We gaan !!      ";
const char bericht7[] = "Kom je tv kijken?";
const char bericht8[] = "Laat maar !!!   ";

const char* berichten[]= { bericht0, bericht1, bericht2, bericht3, bericht4, bericht5, bericht6, bericht7,bericht8}; 
const byte Max_berichten = 9;

// pin definitions
byte LedPin = 2;
byte Button1 = 13;

byte RotA = 12;
byte RotB = 14;
byte RotS = 16;

// Rotary enc
static volatile int globalCounter = 0 ;
byte flag;
byte Last_RoB_Status;
byte Current_RoB_Status;

// LCD
LiquidCrystal_I2C lcd(0x27,16,2);

// UDP variables
unsigned int  localPort = 8888;
WiFiUDP       UDP;
boolean       udpConnected = false;
char          packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char          SendBuffer[256] ; // a string to send back
char          delivered[] = "Ack";
char          uitgezet[]  = "uitgezet";
IPAddress     ip(192, 168, 0, 102); 


void setup() {
  Serial.begin(115200);

  // initialize LCD
  lcd.begin();
  lcd.backlight();
  lcd.clear();
  
  // Initialise wifi connection
  wifiConnected = connectWifi();
  // only proceed if wifi connection successful
  if (wifiConnected) {
    udpConnected = connectUDP();
    if (udpConnected) {
      // nothing all is done in functions
    }
  }
  // initialise pins
  pinMode(LedPin, OUTPUT);
  pinMode(Button1, INPUT_PULLUP);
  pinMode(RotA, INPUT);
  pinMode(RotB, INPUT);
  pinMode(RotS, INPUT);

  lcd.setCursor(0,0);
  lcd.println("Wifi verbinding ");
}


void loop() {
  // Handle Rotary Encode input
  rotaryDeal();
  rotaryClear();

  // Handle Wifi
  if (wifiConnected) {
    if (udpConnected) {
      // if there’s data available, read a packet
      int packetSize = UDP.parsePacket();
      if (packetSize)
      {
         UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
         int i=0;
         // check if previous message was received
         while(i<3 && packetBuffer[i] == delivered[i]) 
         {
          Serial.print(packetBuffer[i]);
          i++;
         }
         if(i==3)
         {
          lcd.setCursor(0,1);
          lcd.print("Ontvangen");
          Serial.print("Ontvangen");
         }
         else
         {  // other packet 
            // check if tone is turned off
          i=0;
          while(i<3 && packetBuffer[i] == uitgezet[i]) 
          {
            Serial.print(packetBuffer[i]);
            i++;
          }
          if(i==3)
          { 
            lcd.setCursor(0,1);
            lcd.print("Uitgezet ");
            Serial.print("Uitgezet");
          }
         }
      }

      // Send the selected message on button press
      if(!digitalRead(Button1))
      {
        if(globalCounter==0)
        {
          lcd.setCursor(0,0);
          lcd.print("geen bericht ");
          lcd.setCursor(0,1);
          lcd.print("gekozen !!!");
        }
        else
        {
          byte l = strlen(berichten[globalCounter]);
          SendBuffer[0] = globalCounter;
          SendBuffer[1] = l;
          for(int i = 0; i<l; i++)
          {
            SendBuffer[i+2] = berichten[globalCounter][i];
          }
          
          bool success = UDP.beginPacket(ip,8888);
          if(success) Serial.println("Succes ...");
  
          UDP.write(SendBuffer,l+2);
            if(UDP.endPacket())
            {
              Serial.print("Send succes : ");
            }
            else
            {
              Serial.println("Not Sent : ");
            }
           lcd.setCursor(0,1);
           lcd.print("               ");
        }
      }
    }
  }
}

void rotaryClear(void)
{ // reset rotary encode when pressed
  if(digitalRead(RotS) == 0)
  {
    globalCounter = 0;
    Serial.println(globalCounter);
    delay(1000);
    
    lcd.setCursor(0,0);
    lcd.print(berichten[globalCounter]);
    lcd.setCursor(0,1);
    lcd.print(berichten[globalCounter]);
  }
}


void rotaryDeal(void)
{ // handle turning the rotary encode
  Last_RoB_Status = digitalRead(RotB);

  while(!digitalRead(RotA)){
    Current_RoB_Status = digitalRead(RotB);
    flag = 1;
  }

  if(flag == 1){
    flag = 0;
    if((Last_RoB_Status == 0)&&(Current_RoB_Status == 1)){
      globalCounter ++;
    }
    if((Last_RoB_Status == 1)&&(Current_RoB_Status == 0)){
      globalCounter --;
    }
  
    if(globalCounter<0) globalCounter = Max_berichten-1;
    if(globalCounter>=Max_berichten) globalCounter = 0;
    lcd.setCursor(0,0);
    lcd.print(berichten[globalCounter]);
  }
}


// connect to wifi – returns true if successful or false if not
// came from standaard example
boolean connectWifi() {
  boolean state = true;
  int i = 0;
  WiFi.begin(ssid, password);
  Serial.println("");
  Serial.println("Connecting to WiFi");

  // Wait for connection
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i > 10) {
      state = false;
      break;
    }
    i++;
  }
  if (state) {
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("");
    Serial.println("Connection failed.");
  }
  return state;
}

// connect to UDP – returns true if successful or false if not
// came from standard example
boolean connectUDP() {
  boolean state = false;

  Serial.println("");
  Serial.println("Connecting to UDP");

  if (UDP.begin(localPort) == 1) {
    Serial.println("Connection successful");
    state = true;
  }
  else {
    Serial.println("Connection failed");
  }
  return state;
}

Receiver code

Arduino
Software of the receiver part.
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <gfxfont.h>

#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

// oled display
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306   display(OLED_RESET);

// Pin definities
byte Pin_LEDcon    = 0;
byte Pin_Button    = 2;
byte Pin_Sound     = 12;
byte Pin_Msg       = 14;

// wifi connection variables
const char* ssid ="xxxxxxxx";
const char* password = "xxxxxxxxx";
boolean     wifiConnected = false;

// UDP variables
unsigned int localPort = 8888;
WiFiUDP UDP;
boolean udpConnected = false;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "Ack"; // a string to send back
char str[20];



// LCD
LiquidCrystal_I2C lcd(0x27,16,2);

// Flow
bool      bSoundState  = false;
IPAddress ip;
int       port=8888;

// LED blink
long int timer = 0;
int ledState = HIGH;


void setup() {
  // Initialise Serial connection
  Serial.begin(115200);

  // Initalise IO-pins
  pinMode(Pin_LEDcon, OUTPUT);
  pinMode(Pin_Sound, OUTPUT);
  pinMode(Pin_Msg, OUTPUT);
  pinMode(Pin_Button, INPUT_PULLUP);
  digitalWrite(Pin_Msg, HIGH);
  
  // Initialise the LCD
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("...");

  // oled display
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Connecting");
  display.display();
  
  // Initialise wifi connection
  wifiConnected = connectWifi();

  // only proceed if wifi connection successful
  if (wifiConnected) {
    udpConnected = connectUDP();
    if (udpConnected) {
      // initialise pins
      digitalWrite(Pin_LEDcon,HIGH);
      lcd.setCursor(0,0);
      lcd.print("Connected");
      display.setCursor(0, 10);
      display.println("connected");
      display.setCursor(0,20);
      display.println(WiFi.localIP());
      display.display();
    }
  }
}

void loop() {
  // check if the WiFi and UDP connections were successful
  if (wifiConnected) {
    if (udpConnected) {

      // if there’s data available, read a packet
      int packetSize = UDP.parsePacket();
      if (packetSize)
      {
        Serial.print("Received packet of size ");
        Serial.println(packetSize);
        Serial.print("From ");
        IPAddress remote = UDP.remoteIP();
        for (int i = 0; i < 4; i++)
        {
          Serial.print(remote[i], DEC);
          if (i < 3)
          {
            Serial.print(".");
          }
        }
        Serial.print(", port ");
        Serial.println(UDP.remotePort());
        
        // read the packet into packetBufffer
        UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
        int Size = packetBuffer[1];
        lcd.setCursor(0,0);

        for(int i=0; i<Size; i++)
        {
          lcd.print(packetBuffer[2+i]);
          str[i] = packetBuffer[2+i];
        }
        display.clearDisplay();
        display.display();
        display.setCursor(0,0);
        display.setTextSize(2);
        display.println(str);
        display.display();
        Serial.println(str);
        StartSound();

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

      // if sound is turned on 
      if(bSoundState)
      { //check button state
        if(!digitalRead(Pin_Button))
        {   // sound was turned off, let the sender know....
            UDP.beginPacket(ip, port);
            UDP.write("uitgezet");
            UDP.endPacket();
            StopSound();  // Stop sound
        }
        if((timer+250)<millis())
        {
          ledState = (ledState== HIGH) ? LOW : HIGH;
          digitalWrite(Pin_Msg,ledState);
          timer = millis();
        }
      }
    }
  }
}

void  StartSound()
{ // Start sound and turn on LED
  bSoundState=true;
  timer = millis();
  ledState = LOW;
  digitalWrite(Pin_Msg,ledState);
  Serial.println("startsound");
  analogWrite(Pin_Sound,80);
}

void StopSound()
{ // Stop sound and turn off LED
  ledState = HIGH;
  digitalWrite(Pin_Msg,ledState);
  bSoundState=false;
  Serial.println("stopsound");
  analogWrite(Pin_Sound,0);
}

// connect to UDP – returns true if successful or false if not
boolean connectUDP() {
  boolean state = false;

  Serial.println("");
  Serial.println("Connecting to UDP");

  if (UDP.begin(localPort) == 1) {
    Serial.println("Connection successful");
    state = true;
  }
  else {
    Serial.println("Connection failed");
  }

  return state;
}
// connect to wifi – returns true if successful or false if not
boolean connectWifi() {
  boolean state = true;
  int i = 0;
  WiFi.begin(ssid, password);
  Serial.println("");
  Serial.println("Connecting to WiFi");

  // Wait for connection
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    display.setCursor(0,80);
    display.print(".");
    display.display();
    if (i > 10) {
      state = false;
      break;
    }
    i++;
  }
  if (state) {
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("");
    Serial.println("Connection failed.");
    display.setCursor(0,10);
    display.println("Connection failed.");
    display.display();
  }
  return state;
}

Credits

Franco van Gastel

Franco van Gastel

0 projects • 4 followers
Teacher ICT, science, programming CAD-design and enginering. Author of education books.

Comments