Atilla Ördög

Personal site of Atilla Ördög AKA Gadratil

Basics of solar power management with arduino

Some thoughts about solar power

Using solar power is a new, booming industry. Solar power is free and available for eternity. The only problem with it is that currently the best solar panels use only around 13% of total incoming solar energy.

Even with the bad rate, more and more people decide to use solar panels because they are clean. The easiest setup of a solar panel as the panel itself and a converter that converts the incoming voltage to AC current.

Why there is a need for controlling the power

Using the simple setup is trivial and most of the people like to implement this one, instead of creating some sort of controlled setup where the power flow is controlled by a logic.

Solar panels are not cheap, especially if someone wants to also get the costly additional equipment that gives control over the energy flow. Also in some places the energy supplying company does not pay for the extra power generated by the solar panels.

There is a way, however, that can prevent extra energy flowing back into the system. The main question is: what to do with the extra power?

What to do with the extra power?

Store in batteries

Usually the energy generated by the solar panels is stored in batteries. During the day the batteries get charged and they supply the needed energy during the night. The problem with batteries is that they have a relatively short lifecycle and there is a lot of energy loss during the charge and discharge of the batteries.

Convert it to heat

A good use for the extra energy is converting it to heat. There are cases when a house is heated with wood. Wood costs a lot, so solar energy converted to heat reduces the need for heat from wood. This could be done by rerouting the energy through a resistor that heats up the water in the boiler.

Roll out the solars from it

In some setups the solar panels rotate during the day to always face the sun. When there is an energy surplus, they can be rotated out from the sun just enough to maintain a steady energy income.

The idea of converting energy to heat

The idea of converting the extra energy to heat is pretty simple. We use a device to check the flow of the current at the entry point from the energy supplier. If the current flows backwards, meaning back into the grid, we start to re-rout energy to a resistor that heats up the water.

A device checks the amount of power flowing backwards and manages the re-routed energy accordingly to maintain a steady control over the consumption. If the water reaches a given temperature, we re-rout the energy surplus to the batteries, charging them.

Technical approach to conversion

In this example we use a cheap approach to controlling our energy flow. We use the famous Arduino device for everything we need to do. With the first device, we measure the current and the voltage to decide how much we need to re-rout.

The second and middle device is just receiving data, converts it and sends out the converted data to different outputs.

The third device is only responsible to manage the energy consumption of the resistor.

Arduino as cheap and reliable instrument

Arduino is a beautiful project. It is a simple microcontroller with analog and digital in- and outputs. Since it is a simple microcontroller, there are few chances of error when using such a device. When controlling energy flows, it is probably the best choice.

To get an arduino or any device needed for such a project, just check out your favourite online store, like https://www.sparkfun.com.

Creating the structure

The structure of the project is also pretty simple. The measurement of the current or voltage can be done easily, for example with open energy monitor.

When we are able to measure the current flow and can decide which way the current flows, we just have to create a code that calculates the rate of re-routed energy. Calculating this value depends on a lot of things. In this example, we map the size of the resistor to a byte (0 – 255). Also, we roughly estimate the maximum output of the solars. Using these values we calculate our variable. For example in case of a 2k resistor:

int ourVal = round((255/2000)*Irms);

Of course, we add some conditions to prevent our value to go below 0 and above 255.

When we are done with the calculations, we simple create an encrypted string with all the data we want to send, and send it over Serial, or I2C communication. Using other than these devices will make the reading slow.

Also, using Serial is better for long-range communication with RS485 shields. It allows you to send data over 1200 meters.

In the middle, also an arduino is used. This arduino sends the control value to the third device over PWM. This is a sure method over short ranges and consumes very little processor steps. Also there is a serial LCD to monitor the current values and a LAN shield to build a small web server that displays the measurements over internet.

The third arduino uses the 50Hz zero interrupt and the control value to decide how much the resistor consumes. It uses the upgoing arc of the sinus wave over a triac to make the resistor consume as much as needed.

Writing the code

I will show you just examples, because this idea can be put into real life in many ways.

We use emonlib for monitoring:

#include "EmonLib.h"

For measurement we use:

float powerFactor     = emon1.powerFactor;      //extract Power Factor into Variable

float Irms            = emon1.Irms;      //extract Irms into Variable

float supplyVoltage   = emon1.Vrms;             //extract Vrms into Variable

float realPower       = emon1.realPower;        //extract Real Power into variable

We calculate the control value:

step = round(sqrt(realPower * realPower) / 10);

if (step < 1) leptek = 1;

if (step > 255) step = 255;

 

if ( realPower < calib )

{

if (controlVal < 255)

{

controlVal += step;

}

else

{

controlVal = 255;

}

delay(10);

}

else

{

if (controlVal > 0)

{

controlVal -= step;

}

else

{

controlVal = 0;

}

delay(10);

 

}

After we have the control value, we just send it through our favorite protocol.

On the second device we can use the following library to display our data on an LCD: serLCD

To have a web server, we can use the built-in library for the LAN shield:

#include <Ethernet.h>

Example

To send the PWM value, we just have to use:

analogWrite(pwmPin, controlVal);

The third device uses an interrupt function to manage the consumption on the resistor. In the setup we attach the interrupt:

attachInterrupt(0, light, FALLING);

After this, we write the function:

void light() {

 

if (dim <= 1) {

//Turn TRIAC completely OFF if dim is 0

digitalWrite(AC_pin, LOW);

}

 

if (dim >= 255) { //Turn TRIAC completely ON if dim is 255

digitalWrite(AC_pin, HIGH);

}

 

if (dim > 0 && dim < 255) {

//Dimming part, if dim is not 0 and not 255

delayMicroseconds(34 * (255 - dim));

digitalWrite(AC_pin, HIGH);

delayMicroseconds(500);

digitalWrite(AC_pin, LOW);

}

}

We just need to use a global variable that is accessible for this function. We read this value on an analog port and we are done.

Final thoughts

Since I am a programmer, I cannot go into details about the electronics. The code itself is simple, the logic behind it, self-explanatory. The idea itself has the main focus, implementing it has many, many ways.

Advertisements

PHP Infinity tree

inline

I created a new library to handle infinite depth trees. I came up with the idea and the necessity because I always struggled to find a good library that can be simply loaded and used for infinite depth category trees, whole sub-node movements, easy node movements and such.

Also my main concern was how the tree gets stored in the database. I came up with a new idea based on MPTT, that stores the data in linear style, not using up much extra space and does not require recursive functionality in the storage. The tree itself is built up by a recursive function that builds the tree using N iterations, no overhead.

screenshot

So, the whole thing is quick, runs smoothly and deals with only the tree, nothing else.

You would ask: how can it be implemented?

This library simply adds a PHP interface to infinite depth trees. That’s it. But since it uses namespaces and replaceable adapters, it can be easily implemented anywhere.

So, let’s see the library:

A fully working example and the full documentation can be found at:

PHP Infinity Tree example and documentation

You can purchase the library here:

PHP Infinity Tree on Gadratil Programming

If you have any questions, just ask. Also, if you need implementation, I would gladly help you.

MySQLi DBHandler

I wrote a nice little library to handle simple database queries easily.

It uses mysqli, so you need to have it enabled on your server.

You can find everything inside a simple adapter needs. Perfect for small projects, especially if you do not want to use huge frameworks just to get some rows from a table.

To use it, just include it and initialize it with required DB connection data.

$db = new DBHandler(array(
'server' => 'localhost',
'user' => 'root',
'pass' => '',
'database' => 'myDB'
));

If you have any questions regarding this library, just comment or write me an email.

https://github.com/atillaordog/MySQLi-DBHandler

PHP Image Handler

I wrote about the image handling library I made. The small code snippet I wrote was poor and hard to understand.

I optimized the code and created example and documentation for my Image Handler PHP library.

Now it can be found on Gadratil Programming in the shop section.

Make your life easier using this library by adding it to any custom framework or CMS.

PHP Image Handler

 

Gadratil programming

Proud to present Gadratil Programming: http://www.gadratilprogramming.net/

This website is my personal website showcasing my programming and things related to it.

You can find a small software marketplace on it, too. You can purchase software made by me or others, or just download the free ones.

ENJOY!

WP Admin Category Search PRO

Since the basic WP Admin Category Search was downloaded more than a hundred times, I decided to make a PRO version of it.

You have all the basic things that are in the simple version, but the PRO version features an autocomplete functionality.

This autocomplete takes the suggestions from the categories and when you select a value, that gets checked automatically.

The other functionality stays, so if you write like 3 letters, you just push the GO button and it will filter the list.

Here is the PRO version: http://www.gadratilprogramming.net/shop/wordpress-plugins/wp-admin-category-search-pro/

Small car make and model handler

I just made a small package that contains a fair amount of car makes and models. The whole thing consists of two files, one is the database and the other is the handling class.

The good thing about this is that the data can be expanded and it is very easy to use.

The idea popped into my mind when I  wanted to make a car wash management application and needed autocomplete car makes and models.

You can buy this product from here:

http://codecanyon.net/item/car-makes-and-models/10960058

A very basic live preview is on:

http://carmodelsandmakes.byethost7.com/

And the documentation to it is on:

http://carmodelsandmakes.byethost7.com/documentation/

Enjoy!

 

Things you would pay a small sum for

I have a question to you all: Is there, or are there things you would pay for or offer something in return for, in the field of IT?

This question means the following: you probably have met things on your computer or in any electrical device that you considered not well made or you would do it differently. Usually the problem is that you do not possess the required knowledge and skill set to make that thing as you would like. Now how does it sound that you could do it or you could have it, by only asking it? You would of course somehow show your appreciation, usually this means financials, but it could mean anything.

 

I am asking this question, because I like to solve small and fun problems and tasks regarding IT but not limited to. And I would like you guys to give me ideas, I can solve them and we can come to a deal regarding the payment.

Everybody  would benefit from this 🙂

 

Also, by answering the question, you could point me in the modern simple interest of people, thus offering me a possibility to create something that everybody wants.

Thanks in advance, comments are welcome 🙂

Basic SEO

In my days as a linksearcher I had some insight into how search engine optimization works.

It is clear to me that the easiest way to optimize a site is to pay to the search engine to put your site in a good position.

However, there are other ways, too, simple things.

 

What I want to talk about in this article is link directories. Every new website needs to have some back-links to start with. Link directories are the best way to provide a push for starting websites, especially if the site is small and needs a way to start its life. Big sites have huge marketing, they do not need link directories, but if the site is for example a small gallery of some work a person does, it can really come to life if linked to it properly.

Now proper linking means selecting 2-3 keywords that best describe the site and use these keywords in texts that host the link toward the site. Link directories know this and they are built exactly the way to satisfy this need.

All someone needs is 50-100 links from different sources mainly to the home page, but also to inner pages, and the site has a basic back-link structure to start its life with.

If the site grows, of course, these links can be deleted one by one to make way for the bigger, better links.

I also did this job, so if you have any questions regarding my experience in this just fire away.

Server Messaging made easy

I will talk in this article about a library I made for making easy server side messaging.

It is called ServerMessage and is a library for creating and sending messages on server. By default it saves the messages in DB, but the storage can be changed by implementing the storage interface and passing the new storage when creating the instance.

Also it has a very basic validation that can also be changed in the way described above.

It is a very flexible library, it can be used for a lot of things and in a lot of ways.
A functionality I am proud of is the filtering, currently it has two filter options added, one filters out emails and the other filters out urls. Other filters can be added, too, by implementing the filter interface and passing the filter when filtering.

A very basic example of usage:

include('autoload.php');

// Instantiate the class using explicit values
$message = new ServerMessage(
array(
'db' => array(
'server' => 'localhost',
'user' => 'root',
'pass' => '',
'database' => 'test',
'table_name' => 'messages'
)
)
);

// Check if storage exists and create it if does not
if ( !$message->storage_exists() )
{
$message->install_storage();
}

// Create message
$message->set_subject('Testing message abc@bcd.com');
$message->set_body('Body of the message, also containing filterable things like http://www.filterurl.dev or abcd@efgh.dev');
$message->set_sender(1, 'user');
$message->set_reciever(1, 'support');
$message->set_meta(array('meta_key' => 'meta_value'));
$message->send();

// Now the message is also set in the inner container, thus we can change one thing and send the message again.
$message->set_reciever(2, 'support');
$message->send();

// You can also update the message
$message->set_subject('Changed');
$message->update();

// To reset inner message, you can call
$message->reset_inner_message();

// Filtering messages is easy like
$message->filter_message();

It can be used in a lot of ways. Here is the full list of functions with docx descriptions for reference:

/**
* Constructor.
* @param array $config Can overwrite default config upon creation
* If no storage class is provided, the config default will be used
* @param ServerMessageInterfacesStorage $storage
* @param ServerMessageInterfacesValidation $validation The validation class that validates the message, can be set externally
*/
public function __construct(Array $config = array(), StorageInterface $storage = null, ValidationInterface $validation = null)

public function set_subject($subject = '')

public function set_body($body = '')

public function set_sender($sender_id = 0, $sender_type = '')

public function set_reciever($reciever_id = 0, $reciever_type = '')

public function set_meta($meta)

/**
* Set the status of one or more messages
* @param array|int $id
* @param int $status Must be one from the predefined statuses
* @return boolean
*/
public function change_status($id, $status)

/**
* Set one or more messages to read or unread
* @param array|int $id
* @param boolean $read True or false
* @return boolean
*/
public function set_read($id, $read)

/**
* After the parts of the message have been set, saves to storage
* If validation is set to true in config, validates before sending
* @return boolean
*/
public function send()

/**
* Updates the loaded message in the storage
* It will validate the message if set in config
* @return boolean
*/
public function update()

/**
* If sending was not successful, we get the validation errors
* @return array
*/
public function get_validation_errors()

/**
* Gets the inbox of a given object
* @param int $obj_id The id of the reciever
* @param string $obj_type The type of the reciever
* @param int $status Optional, taken in consideration if not null
* @param int $limit
* @param int $offset
* @return array Returns an array with message objects
*/
public function get_inbox($obj_id, $obj_type, $status = null, $limit = null, $offset = null)

/**
* Gets the number of messages in inbox
* @param int $obj_id The id of the reciever
* @param string $obj_type The type of the reciever
* @param int $status (Optional) Status filter
* @param int $read (Optional) Read filter
* @return int
*/
public function get_total_inbox($obj_id, $obj_type, $status = null, $read = null)

/**
* Gets the outbox of a given object
* @param int $obj_id The id of the sender
* @param string $obj_type The type of the sender
* @param int $status Optional, taken in consideration if not null
* @param int $limit
* @param int $offset
* @return array Returns an array with message objects
*/
public function get_outbox($obj_id, $obj_type, $status = null, $limit = null, $offset = null)

/**
* Gets the number of unapproved messages
* @param int $obj_id The id of the reciever
* @param string $obj_type The type of the reciever
* @param int $status (Optional) Status filter
* @return int
*/
public function get_total_outbox($obj_id, $obj_type, $status = null)

/**
* Get all the messages, pagination dependent
* @param int $limit
* @param int $offset
* @return array
*/
public function get_all($limit = null, $offset = null)

/**
* Get the total number of messages
*/
public function get_total_messages()

/**
* Get a single message by ID or the current message set
* @param int $message_id If null, returns current message
* @return ServerMessageEntityMessage
*/
public function get_single($message_id = null)

/**
* Deletes one or more messages
* @param array | int $message_id
* @return boolean
*/
public function delete_message($message_id)

/**
* Checks if storage exists and can be used
* @return boolean
*/
public function storage_exists()

/**
* Creates storage if that does not exist
*/
public function install_storage()

/**
* Clear out the storage created for messages
*/
public function remove_storage()

/**
* Filters the given message
* @param ServerMessageEntityMessage $message Optional, if not set, the inner message will be used
* @param array $filters Optional, can be used to add filters (Ex. array('facebook' => new FacebookFilter()))
* The filters need to extend the filter interface
* @param boolean $subject_only Filter only the subject
* @param boolean $delete_found Remove the found matches from the message
* @param boolean $save If true, saves the message back to storage (used when deleting threats)
* @return array Returns an associative array with the filtered message and the found matches
*/
public function filter_message(MessageEntity $message = null, Array $filters = array(), $subject_only = false, $delete_found = false, $save = false)

/**
* Return the inner statuses set in config
*/
public function get_statuses()

/**
* We need to reset our message entity sometimes, so we do that with this function
*/
public function reset_inner_message()

The source code can be found here:

https://github.com/atillaordog/ServerMessage