B4R Question Do Until Loop in Esp8266

AndroidMadhu

Active Member
Licensed User
Hello,
Currently i am testing workflow sensor YF-S201 for sensing of workflow.
The workflow sensor is working as expected. But when the counter is 0 [Zero], I do not want to send the reading to my Website. Since the Zero reading is wasting of Space in Database. I wanted to send the totalCounter value to the website.

Below is the Code snippet:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Private waterwifi As ESP8266WiFi
    Private D2pin As D1Pins
    Private D1 As Pin
    Private counter As UInt
    Private set= False As Boolean
    Private totalCounter As Double
End Sub

Serial1.Initialize(115200)
    Log("AppStart")
    D1.Initialize(D2pin.D1,D1.MODE_INPUT_PULLUP)
    D1.AddListener("pin2_StateChanged")
    waterwifi.Connect2("xxxxx","xxxx")
    If waterwifi.IsConnected Then
        Log("Router Connected Successfully!!!")
    Else
        Log("Router Not able to connect!!!")
        Return
    End If
    HttpJob.Initialize("Test")
    HttpJob.Download("http://mytestpage.com")

Sub jobdone(Job As JobResult)
Do Until counter>0
        totalCounter=(totalCounter+counter)
        Delay(1000)
        counter=0     'Resetting the Counter
        set= False
    Loop
    Log(totalCounter)   <---- This log not showing in serial
    Dim s As String=JoinStrings(Array As String("http://mytestpage.com/addCounter.php?counter=",totalCounter)
'        'Sending the values to cloud
        HttpJob.Download(s)
End Sub


Private Sub pin2_StateChanged(state As Boolean)
    Select state
        Case True
            counter=counter+1
        Case False
    End Select
End Sub

Am I doing anything wrong in jobdone procedure?
 

janderkan

Well-Known Member
Licensed User
Longtime User
You are setting the counter=0 in the loop, so it will never be >0 and you never exit the loop
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
@janderkan , If I will not resetting the counter=0 then the interrupt pin will hold the old value.
I need to reset the counter=0 so that when the rotor generate the pulse I can get the interrupt pulse.[High].
Now when I resetting the counter out of Do while loop, it is not throwing any value.
B4X:
Do Until counter>0
        Log("the Counter:",counter)
        totalCounter=(totalCounter+counter)
        Delay(1000)
        Log(totalCounter)
    Loop
    counter=0     'Resetting the Counter
    set= False
    
    'Delay(1000)
End Sub

Whereas when I am writing the simple code without Do Until Loop it is providing the value :
B4X:
Log("Counter is :",counter)
counter=0     'Resetting the Counter
set= False

Only I need a Totalcounter value, when the counter is Zero it will not throw any value to Webpage.
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
I am able to calculate only the totalcounter as per the requirements. But I am not able to send the totalcount value to jobdone procedure to send to webpage.
below is the code snippet:
B4X:
Private Sub flowcalc()
    If counter=0 Then
        Return
    Else
        Do While counter>0
            'Log("Simple Counter inside loop:",counter)
            totalCounter=(totalCounter+counter)
            Log("TotalCounter inside loop:",totalCounter)
            counter=0
            set=False
        Loop
    End If
Log("TotalCounter outside loop:",totalCounter)

End Sub

How Do I send the totalCounter value to the below procedure to send to webpage
B4X:
Sub jobdone(Job As JobResult)
    
End Sub

Please advice
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
I don't exactly understand your code
@Erel, What I want to do is to calculate the flow meter counter and send the counter to my webpage using EsP8266.
Below is the code for calculating the Flow Counter. However when there is no counter[0], the code will not send the value, if the counter is greater than Zero then the code will send the value to Webpage. The Code for the calculating the counter as below:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Private waterwifi As ESP8266WiFi
    Private D2pin As D1Pins
    Private D1 As Pin
    Private counter As UInt
    Private set= False As Boolean
    Private totalCounter As uInt
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    D1.Initialize(D2pin.D1,D1.MODE_INPUT_PULLUP)
    D1.AddListener("pin2_StateChanged")
    waterwifi.Connect2("xxxx","xxxx")
    If waterwifi.IsConnected Then
        Log("Router Connected Successfully!!!")
    Else
        Log("Router Not able to connect!!!")
        Return
    End If
    HttpJob.Initialize("Test")
    HttpJob.Download("http://mytestpage.com")
End Sub


Sub jobdone(Job As JobResult)

End Sub

Private Sub flowcalc()
    'This procedure will calculate the total counter before sending to Webpage.
    If counter=0 Then
        Return    'Do nothing when counter is 0
    Else
        Do While counter>0
            totalCounter=(totalCounter+counter)
            'Log("TotalCounter inside loop:",totalCounter)
            counter=0  'Resetting he counter
            set=False
        Loop
    End If
    Log("TotalCounter outside loop:",totalCounter)
    
    
End Sub


Private Sub pin2_StateChanged(state As Boolean)
    Select state
        Case True
            counter=counter+1
        Case False
    End Select
End Sub

I am facing the only problem is , how to send the totalCounter value to JonDone() procedure to send to Webpage.
Please advice
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
Thanks @Erel, But one confusion is how to run the flowcalc() procedure, it has to be run before generating the totalCounter value.
I have run it from jonDone() procedure, but no luck.
May be my confusion is like novice, but I am not able to run the flowcalc(). Please advice....
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
Where counter comes from? If you run flowcalc the counter is not able to change, while you are in a loop. And this loop ends after the first cycle, since counter, which controls the loop, is reset to 0. I think that you must rethink the whole thing.
 
Upvote 0
Top