Fanatic Series
Published

Network VU Sound Level Meter using C#, Arduino and ESP8266

Network Sound Level meter is capable to send UDP packet over laptop existing WiFi. These packets received by Arduino through ESP8266.

BeginnerFull instructions provided10 hours4,305
Network VU Sound Level Meter using C#, Arduino and ESP8266

Things used in this project

Hardware components

Arduino Micro
Arduino Micro
×1
ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
LilyPad Rainbow LED (strip of 7 colors)
SparkFun LilyPad Rainbow LED (strip of 7 colors)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Visual Studio 2015
Microsoft Visual Studio 2015

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

BreadBoard

Code

Windows Form Application

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;

namespace SoundLevelMeter
{
    public partial class Form1 : Form
    {
        int temp = 0;
        int max_value = 0;
        double divisor;
        String data = "";
        long low_value_counter = 0;
        String packet = "BBBBBBBBBB";
        Socket sock = null;
        System.Net.IPEndPoint endpoint = null;
        String style = "B";
        
        public Form1()
        {
            InitializeComponent();
            NAudio.CoreAudioApi.MMDeviceEnumerator enumerator = new NAudio.CoreAudioApi.MMDeviceEnumerator();
            var devices = enumerator.EnumerateAudioEndPoints(NAudio.CoreAudioApi.DataFlow.All, NAudio.CoreAudioApi.DeviceState.Active);
            comboBox1.Items.AddRange(devices.ToArray());
            try { comboBox1.SelectedIndex = 0; }
            catch { }
            timer2.Start();
            sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            System.Net.IPAddress serverAddr = System.Net.IPAddress.Parse("192.168.1.202");
            endpoint = new System.Net.IPEndPoint(serverAddr, 11000);
            textBox1.Visible = false;
            comboBox1.Visible = false;

        
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                var device = (NAudio.CoreAudioApi.MMDevice)comboBox1.SelectedItem;
                // abc = NAudio.CoreAudioApi.;
                temp = (int)(Math.Round(device.AudioMeterInformation.MasterPeakValue * 100 + 0.5));
                progressBar1.Value = temp;
                if (max_value < temp) { max_value = temp; divisor = max_value / 10; }
                textBox1.Text = temp.ToString();
                
                    if (temp >= 11 * divisor) packet = "AAAAAAAAA";
                    else if (temp >= 10 * divisor) packet = "AAAAAAAA";
                    else if (temp >= 9 * divisor) packet = "AAAAAAAAB";
                    else if (temp >= 8 * divisor) packet = "AAAAAAABB";
                    else if (temp >= 7 * divisor) packet = "AAAAAABBB";
                    else if (temp >= 5 * divisor) packet = "AAAAABBBB";
                    else if (temp >= 4 * divisor) packet = "AAAABBBBB";
                    else if (temp >= 3 * divisor) packet = "AAABBBBBB";
                    else if (temp >= 2 * divisor) packet = "AABBBBBBB";
                    else if (temp >= 1 * divisor) packet = "ABBBBBBBB";

                if (temp < 3) { packet = "BBBBBBBBB"; }

                    data = packet+style;
                    udp_send();

                
            }
        }

        public void udp_send()
        {
            string text = data;
            byte[] send_buffer = Encoding.ASCII.GetBytes(text);
            sock.SendTo(send_buffer, endpoint);


        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            max_value=0;

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Style1_Click(object sender, EventArgs e)
        {
            style = "B";

        }

        private void Style2_Click(object sender, EventArgs e)
        {
            style = "A";
        }

        private void Style3_Click(object sender, EventArgs e)
        {
            style = "C";
        }

        private void Style4_Click(object sender, EventArgs e)
        {
            style = "D";
        }
    }
}

Arduino Pro Micro

Arduino
#define DEBUG true
#define dbg Serial  // USB local debug
int led0 = 2;
int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 7;
int led6 = 8;
int led7 = 9;
int led8 = 10;
String ssid = "ssid";
String pass = "your Password";
String ip = "192.168.1.202";    //ESP 8266 Static IP
String remoteip="192.168.1.4";   // Computer IP
String contype= "UDP";             // Protocal of sending Data

String num = "5";
char output[10]="";
String sendData(String command, const int timeout, boolean debug) {
  String response = "";
  Serial1.print(command); // send the read character to the esp8266
  long int time = millis();
  while ( (time + timeout) > millis()) {
    while (Serial1.available()) {
      // The esp has data so display its output to the serial window
      char c = Serial1.read(); // read the next character.
      response += c;
    }
  }

  if (debug)
  {
    Serial.print(response);
  }

  return response;
}





void setup() {

  // Set baud rates
pinMode(led0, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
  Serial1.begin(115200);
  dbg.begin(115200);
  dbg.println("DEBUG: Running Setup");



  sendData("AT+RST\r\n", 2000, DEBUG); delay(10000);// reset module
  sendData("AT\r\n", 2000, DEBUG); delay(4000);// reset module
  sendData("AT+CWMODE=1\r\n", 1000, DEBUG); delay(4000);// configure as access point


sendData("AT+CWJAP=\"" + ssid + "\",\"" + pass + "\"\r\n", 1000, DEBUG);delay(1000); 
sendData("AT+CIPSTA=\""+ip+"\"\r\n",1000,true); //delay(2000);
sendData("AT+CIPMUX=1\r\n",5000,DEBUG); //delay(4000);

sendData("AT+CIPSTART=4,\"" +contype+ "\",\"" + remoteip + "\",12000,11000,0\r\n",3000,DEBUG);//delay(4000);

}


void loop()
{

 
  
  if(Serial1.available()) // check if the esp is sending a message 
  {

    if(Serial1.find("+IPD,")){
    delay(1000);
    if(Serial1.find(":")){

    for(int i=0;i<=9;i++){
    int inByte = Serial1.read();
    output[i]=inByte;
//    Serial.write(inByte);
    }
      
    }
    
    
    Serial.print(output);
    Serial.println(" ");
if(output[9]=='A'){style1();}
if(output[9]=='B'){style2();}
if(output[9]=='C'){style3();}
if(output[9]=='D'){style4();}



  }
}}

void style1()
{
  //  led0  led1  led2  led3  led4  led5  led6  led7  led8   total-9-leds   >>>>>>>>>>
  //  0     1     2     3     4     5     6     7     8
  if(output[0]=='A'){digitalWrite(led0,HIGH);} if(output[0]!='A'){digitalWrite(led0,LOW);}
  if(output[1]=='A'){digitalWrite(led1,HIGH);} if(output[1]!='A'){digitalWrite(led1,LOW);}
  if(output[2]=='A'){digitalWrite(led2,HIGH);} if(output[2]!='A'){digitalWrite(led2,LOW);}
  if(output[3]=='A'){digitalWrite(led3,HIGH);} if(output[3]!='A'){digitalWrite(led3,LOW);}
  if(output[4]=='A'){digitalWrite(led4,HIGH);} if(output[4]!='A'){digitalWrite(led4,LOW);}
  if(output[5]=='A'){digitalWrite(led5,HIGH);} if(output[5]!='A'){digitalWrite(led5,LOW);}
  if(output[6]=='A'){digitalWrite(led6,HIGH);} if(output[6]!='A'){digitalWrite(led6,LOW);}
  if(output[7]=='A'){digitalWrite(led7,HIGH);} if(output[7]!='A'){digitalWrite(led7,LOW);}
  if(output[8]=='A'){digitalWrite(led8,HIGH);} if(output[8]!='A'){digitalWrite(led8,LOW);}
    
  }

  void style2()
{
  //  led0  led1  led2  led3  led4  led5  led6  led7  led8   total-9-leds       <<<<<<<<<<
  //  8     7     6     5     4     3     2     1     0
  if(output[8]=='A'){digitalWrite(led0,HIGH);} if(output[8]!='A'){digitalWrite(led0,LOW);}
  if(output[7]=='A'){digitalWrite(led1,HIGH);} if(output[7]!='A'){digitalWrite(led1,LOW);}
  if(output[6]=='A'){digitalWrite(led2,HIGH);} if(output[6]!='A'){digitalWrite(led2,LOW);}
  if(output[5]=='A'){digitalWrite(led3,HIGH);} if(output[5]!='A'){digitalWrite(led3,LOW);}
  if(output[4]=='A'){digitalWrite(led4,HIGH);} if(output[4]!='A'){digitalWrite(led4,LOW);}
  if(output[3]=='A'){digitalWrite(led5,HIGH);} if(output[3]!='A'){digitalWrite(led5,LOW);}
  if(output[2]=='A'){digitalWrite(led6,HIGH);} if(output[2]!='A'){digitalWrite(led6,LOW);}
  if(output[1]=='A'){digitalWrite(led7,HIGH);} if(output[1]!='A'){digitalWrite(led7,LOW);}
  if(output[0]=='A'){digitalWrite(led8,HIGH);} if(output[0]!='A'){digitalWrite(led8,LOW);}
    
  }
  void style3()
{
  //  led0  led1  led2  led3  led4  led5  led6  led7  led8   total-9-leds        <<<<<>>>>>>>
  //  7     5     3     1     0     2     4     6     8
  if(output[7]=='A'){digitalWrite(led0,HIGH);} if(output[7]!='A'){digitalWrite(led0,LOW);}
  if(output[5]=='A'){digitalWrite(led1,HIGH);} if(output[5]!='A'){digitalWrite(led1,LOW);}
  if(output[3]=='A'){digitalWrite(led2,HIGH);} if(output[3]!='A'){digitalWrite(led2,LOW);}
  if(output[1]=='A'){digitalWrite(led3,HIGH);} if(output[1]!='A'){digitalWrite(led3,LOW);}
  if(output[0]=='A'){digitalWrite(led4,HIGH);} if(output[0]!='A'){digitalWrite(led4,LOW);}
  if(output[2]=='A'){digitalWrite(led5,HIGH);} if(output[2]!='A'){digitalWrite(led5,LOW);}
  if(output[4]=='A'){digitalWrite(led6,HIGH);} if(output[4]!='A'){digitalWrite(led6,LOW);}
  if(output[6]=='A'){digitalWrite(led7,HIGH);} if(output[6]!='A'){digitalWrite(led7,LOW);}
  if(output[8]=='A'){digitalWrite(led8,HIGH);} if(output[8]!='A'){digitalWrite(led8,LOW);}
    
  }
  void style4()
{
  //  led0  led1  led2  led3  led4  led5  led6  led7  led8   total-9-leds        >>>>        <<<<<
  //  0     2     4     6     8     7     5     3     1
  if(output[0]=='A'){digitalWrite(led0,HIGH);} if(output[0]!='A'){digitalWrite(led0,LOW);}
  if(output[2]=='A'){digitalWrite(led1,HIGH);} if(output[2]!='A'){digitalWrite(led1,LOW);}
  if(output[4]=='A'){digitalWrite(led2,HIGH);} if(output[4]!='A'){digitalWrite(led2,LOW);}
  if(output[6]=='A'){digitalWrite(led3,HIGH);} if(output[6]!='A'){digitalWrite(led3,LOW);}
  if(output[8]=='A'){digitalWrite(led4,HIGH);} if(output[8]!='A'){digitalWrite(led4,LOW);}
  if(output[7]=='A'){digitalWrite(led5,HIGH);} if(output[7]!='A'){digitalWrite(led5,LOW);}
  if(output[5]=='A'){digitalWrite(led6,HIGH);} if(output[5]!='A'){digitalWrite(led6,LOW);}
  if(output[3]=='A'){digitalWrite(led7,HIGH);} if(output[3]!='A'){digitalWrite(led7,LOW);}
  if(output[1]=='A'){digitalWrite(led8,HIGH);} if(output[1]!='A'){digitalWrite(led8,LOW);}
    
  }

Credits

Fanatic Series
5 projects • 24 followers
Electrical Engineer, who has worked on numerous HW/SW projects. He has worked as a Lead Researcher, Designer & Developer
Contact

Comments

Please log in or sign up to comment.