Need help with simple Arduino<>Firmata app (VB application) [SOLVED]

Hello,
I tried to look on the forum but I couldn't find a simple solution to my problem.
I am writing a VB app which would receive data from Arduino (using Firmata library).

I was able successfully to write a small app which would read analog values from Arduino.

#include <Firmata.h>

void setup()
{
    Firmata.setFirmwareVersion(0, 2);
    Firmata.begin(57600);
}

void loop()
{
    while(Firmata.available())
    Firmata.processInput();
    delay(10);
    Firmata.sendAnalog(0, analogRead(0));   
}
Imports Firmata.FirmataVB

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FirmataVB1.Connect("COM3", 57600)
        Timer1.Enabled = True
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        TextBox1.AppendText(FirmataVB1.AnalogRead(0) & vbCrLf)
    End Sub
End Class

The timer in the VB is set to 100 and basically outputs whatever value Arduino Analog(0) reads.

But what I actually wanted is much simpler and I am having difficult time finding a simple solution.
My app in Arduino does some simple calculations. It monitors the vibration of the oil-boiler (I wanted to find out if it is running or not & for how long).
I have a sensor attached and taking sample data every 10ms. If certain conditions will be met the LED on the board lights up. That is the moment I would like the VB app to be notified about this event. So the app can run for 2 hours, and no data might be sent and when the boiler will start burning oil the alert will be send to the VB app. My VB app calls Oracle database for further processing.
I would like the VB app to be notified also when the boiler will turn off. So this is the second event.

Below is the Arduino code. I marked those 2 places when I would like Arduino to call VB (using Firmata I suppose?).
If someone could help me with this one I would appreciate it.
I need to know the command which would basically send a string or a digit (0 or 1) to VB.
I also need to know with what command I can receive that signal in the VB app. Do I have use the timer also?
I am afraid I will run into problems when I will have one timer on Arduino board, and another timer in the VB. Would be great to have some synchronization - but that's probably for another day.

#include <Firmata.h>

int r1 = 0;
int r2 = 0;
int alert1 = 0;
int alert2 = 0;
int alert3 = 0;
int run = 1;
int redlight = 0;

void setup()
{
    pinMode(13, OUTPUT);
    pinMode(12, OUTPUT);
    pinMode(11, OUTPUT);
    Serial.begin(9600);
}

void loop()
{
   r1 = analogRead(A0);
   delay(10);
   r2 = analogRead(A0);
   delay(10);   

if (run == 1)
{
   if (r1>r2) { if((r1-r2)>20) { alert1 = 1; } else { alert1 = 0; } }
   if (r2>r1) { if((r2-r1)>20) { alert1 = 1; } else { alert1 = 0; } }
   run = run + 1;
}
else if (run == 2)
{
   if (r1>r2) { if((r1-r2)>20) { alert2 = 1; } else { alert2 = 0; } }
   if (r2>r1) { if((r2-r1)>20) { alert2 = 1; } else { alert2 = 0; } }  
   run = run + 1;
}
else if (run == 3)
{
   if (r1>r2) { if((r1-r2)>20) { alert3 = 1; } else { alert3 = 0; } }
   if (r2>r1) { if((r2-r1)>20) { alert3 = 1; } else { alert3 = 0; } }    
   run = 1;
}
else
{
  run = 1; 
}

// Going from sleep mode
if( redlight == 0)
{
   if (alert1 == 1 and alert2 == 1 and alert3 == 1)
   {
     redlight = 1;
     // call VB app that the value changed from 0 to 1 ********************************** <----- 1st Event
   }
   else
   {
     redlight = 0; 
   }
}
// Going from active mode
if( redlight == 1)
{
   if (alert1 == 0 and alert2 == 0 and alert3 == 0)
   {
     redlight = 0;
     // call VB app that the value changed from 1 to 0 ********************************** <----- 2nd Event
   }
   else
   {
     redlight = 1; 
   }
}
  
   if(redlight == 0)
   {
     digitalWrite(11, HIGH); 
     digitalWrite(12, LOW); 
     digitalWrite(13, LOW); 
   }
   if(redlight == 1)
   {
     digitalWrite(11, LOW); 
     digitalWrite(12, HIGH); 
     digitalWrite(13, LOW);    
   } 
   
   delay(1000);
}

using Firmata I suppose?).

No. Firmata use in VB is meant to be a pull process. VB pulls information from the Arduino, which is running the standard Firmata sketch and nothing else.

So, you can't use Firmata.

You can, however, have your VB app connect directly to the serial port and exchange data with the Arduino.

Or, dump VB altogether, and use a real programming language like C#.

PaulS:
You can, however, have your VB app connect directly to the serial port and exchange data with the Arduino.
Or, dump VB altogether, and use a real programming language like C#.

So if I understood correctly I need to setup a listener for the Serial port and then go from there? VB can't do that I suppose?... :~
I'm not by any means an expert in programming... did some C++ in the past. Only touched VB.net recently because it is very easy to write a quick app (but slow as hell..) to do something simple.
I was hoping to find a quick solution because this is one of those one-time projects and don't want to spend too much time learning new stuff. Don't get me wrong - I would love to read about it but life/kids/family/work cycle is time consuming as it is :sleeping:

So your suggestion is to look for some C# examples on listening to the Serial Port?
Thanks for the reply :slight_smile:

Solved!!!
I got it working by using Firmata after all. :slight_smile:
Basically the vb.net app is now able to display analog data from Arduino.

Below is the code for anyone interested. Thanks for your help!!

#include <Firmata.h>

void setup()
{
    Firmata.setFirmwareVersion(0, 2);
    Firmata.begin(115200);
}

void loop()
{
    while(Firmata.available())    
    Firmata.processInput();    
    Firmata.sendAnalog(0, analogRead(0));       
    delay(100);
}
Public Class Main

    Dim data As Integer = 0

    Private AnalogPins As New Hashtable

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FirmataVB1.Connect("COM3", 115200)
        For Each ctrl As System.Windows.Forms.Control In Me.Controls
            Dim ThisCtrl As Firmata.AnalogPinControl = Nothing
            If TypeOf ctrl Is Firmata.AnalogPinControl Then
                ThisCtrl = ctrl
                AnalogPins.Add(CStr(ThisCtrl.PinNumber), ThisCtrl)
                AddHandler ThisCtrl.AnalogOnOff_Changed, AddressOf AnalogPinControl_AnalogOnOff_Changed
            End If
        Next

    End Sub

    Private Sub AnalogPinControl_AnalogOnOff_Changed(ByVal PinNumber As Integer, ByVal OnOff As Integer)
        FirmataVB1.AnalogPinReport(PinNumber, OnOff)
    End Sub

    Private Sub DataReceived(ByVal pin As Integer, ByVal value As Integer) Handles FirmataVB1.AnalogMessageReceieved
        Dim AnalogPin As Firmata.AnalogPinControl
        AnalogPin = AnalogPins(CStr(pin))
        AnalogPin.SetAnalogValue(value)
        data = value        
    End Sub

    Private Sub displayValue()
        TextBox1.Text = data
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        displayValue()
    End Sub

End Class