Categories
Project Ideas Projects

Problems I faced during Load management System using Arduino and ESP8266

Yesterday, I finished the first prototype of Load management system for a client and due to the short time, I faced some issues, which should be solved pre handed. Today, I am going to summaries what kind of problems I faced during the project and how I shifted and what I didn’t accomplished. So, this post is going to be totally different then the regular tutorial, rather, it is just going to be the pure my observations which I should focus next time I am working on some similar project.

Project Summary

Let’s first discuss what I want to achieve for the prototype and what was the ultimate goal for it. I wanted to build an Efficient source and load prediction system which can predict the source energy generation from Solar and wind energy generators, and it should fetch the total week weather prediction from the Weather API and based on previous history of the energy generation and current and future weather conditions, it should predict how much energy could be produced from solar and how much from National Grid.

Once done, we then required to create a Rule based system, which based on these predictions manage our load and sources efficiently, so that our generators are best utilized, and we could optimize the energy consumption cost purchased from the National Grid.

What kind of Interfaces do we need for data visualizations?

Let’s now discuss what kind of parameters we have to visualize to observe the overall system behavior and what kind of display interfaces do we need to visualize it.

We could add a simple 20×4 character LCD with the Arduino to display the local measured parameters like source voltages, battery voltages, incoming national grid station voltages, consumed current and overall power in KW for our load consumptions.

Next we can display these parameters, remotely on mobile device with Android App and on Web interface for IoT applications. So we used

  • Python for machine learning and Web Interface
  • Flask as website making backend framework
  • HTML CSS and JavaScript for Frontend. ( no JavaScript framework is used)
  • Android Kotlin for Mobile application building
  • Arduino Mega for Power related tasks
  • ESP8266 for IoT related tasks

1. Communication between Arduino Mega and ESP8266

First I have to figure out the problem I was facing the string receiving and parsing in ESP8266 from Arduino Mega. I am still not sure what was the problem which I faced due to which, I wasn’t able to receive the proper string in ESP8266. I didn’t get much time to identify the problem, rather I just tried some different codes from the stack overflow. I lost the link for now but This was the code I used to Receive the String and parse the comma separated list.

while (espSerial.available() > 0)
    {
        char recieved = espSerial.read();
        inData += recieved; 
    
        // Process message when new line character is recieved
        if (recieved == '\n')
        {
            int commaIndex = inData.indexOf(',');

            // Search for the next comma just after the first
            int secondCommaIndex = inData.indexOf(',', commaIndex + 1);
            int thirdCommaIndex = inData.indexOf(',', secondCommaIndex + 1);
            int fourthCommaIndex = inData.indexOf(',', thirdCommaIndex + 1);
            int fifthCommaIndex = inData.indexOf(',', fourthCommaIndex + 1);
            
            String firstValue = inData.substring(0, commaIndex);
            String secondValue = inData.substring(commaIndex + 1, secondCommaIndex);
            String thirdValue = inData.substring(secondCommaIndex + 1, thirdCommaIndex);
            String fourthValue = inData.substring(thirdCommaIndex + 1, fourthCommaIndex);
            String fifthValue = inData.substring(fourthCommaIndex + 1, fifthCommaIndex);
            String sixthValue = inData.substring(fifthCommaIndex + 1); // To the end of the string
            
            int r = firstValue.toInt();//power_i
            int g = secondValue.toInt();//power_w
            int b = thirdValue.toInt();//v1,
            int x = fourthValue.toInt();//v2
            int y = fifthValue.toInt();//v3
            int z = sixthValue.toInt();//v4
           }
   }
Code language: Arduino (arduino)

In this case simple while loop will keep receiving the string and later it was simply with the help of substring method of String class used to fetch individual value from the string.

2. Firebase Strange Behavior for Comma separated List

Next, I tried to send the string as it is to firebase Real time database from ESP8266, but it wasn’t working. Rather it was exhibiting the strange behavior. So, I quickly think of a different solution and format the string with _ underscores rather than ‘,’ commas. Here is the function which I used to format the string from different variables.


String formatValues(int p1, int p2, int v1, int v2, int v3,int v4) {
    String formattedString = "";
    
    formattedString += "p1_" + formatDigit(p1) + "_";
    formattedString += "p2_" + formatDigit(p2) + "_";
    formattedString += "v1_" + floatToString((v1/100),2) + "_";
    formattedString += "v2_" +  floatToString((v2/100), 2) + "_";
    formattedString += "v3_" + floatToString((v3/100), 2) + "_";
    formattedString += "v4_" + floatToString((v4/100), 2) ;
    
    
    
    return formattedString;
}


String formatDigit(int value) {
    if (value < 10) {
        return "00" + String(value);
    } else if (value < 100) {
        return "0" + String(value);
    } else {
        return String(value);
    }
}

String floatToString(float value, int precision) {
    char buffer[20]; // Adjust the buffer size as needed
    dtostrf(value, 0, precision, buffer);
    return String(buffer);
}


Code language: Arduino (arduino)

Once formatted, it could be pushed to firebase Realtime database with simply a function calls.

            String fstring = formatValues(r,g,b,x,y,z);
            if((millis()-preMillis)>7000){
              firebase.setString("inv_vals",fstring);
              preMillis=millis();
            }Code language: JavaScript (javascript)

3. Receiving and Parsing Firebase String in Android Kotlin

Now it was time to receive that pushed string from firebase Realtime database into the Android Kotlin. So this is the code which I used. It was pretty easy in Kotlin to parse the string and it did not make much problem.

val myRef = database.getReference("inv_vals")
        myRef.addValueEventListener(object: ValueEventListener {

            override fun onDataChange(snapshot: DataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                val value = snapshot.value
                if (value != null) {

                    tvStatus.text = value.toString()
                    val dataParts = value.toString().split("_")
                    val p_i = dataParts[1].toInt()
                    val p_w = dataParts[3].toInt()
                    val v_1 = dataParts[5].toFloat()
                    val v_2 = dataParts[7].toFloat()
                    val v_3 = dataParts[9].toFloat()
                    val v_4 = dataParts[11].toFloat()
                    updateLoadList(p_i, p_w)

                    tvPoweri.text = "$p_i W"
                    tvPowerw.text = "$p_w W"
                    tvVolt1.text = "$v_1 v"
                    tvVolt2.text = "$v_2 v"
                    tvVolt3.text = "$v_3 v"
                    tvVolt4.text = "$v_4 v"



                };
                Log.d("TAG", "Value is: $value")
            }

            override fun onCancelled(error: DatabaseError) {
                Log.w("TAG", "Failed to read value.", error.toException())
            }
        })

Code language: Kotlin (kotlin)

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.