Atilla Ördög

Personal site of Atilla Ördög AKA Gadratil

Category: Standalone scripts

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.

Advertisement

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

 

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!

 

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

ImageHandler, a small and useful tool

Usually every CMS and Framework has its own image handling class. The problem is between, however, because in a CMS image handling is part of a media library that is big and complex.

In a Framework image handling is not done at all, it just has some classes for helping the developer.

This gap is what I tried to fill by creating a small tool for handling images.

This tool is very lightweight, easy to use and covers a large area in image handling.

It is created in a way that a developer can easily change the storage and validation classes if needed. Currently data is saved into DB using Mysqli and validation checks basic aspects.

Configuration can be passed using the Config class and the config file in the base folder, but config params can be passed upon initialization, too.

The image handling is done by Kohana`s Image library, surely it is a bit modified to fit my needs. This tool uses GD or Imagick library, so you will have to enable that if you want to handle images.

What you will need to run this code: PHP 5+, mysqli, GD or Imagick

Here is a full sample of the usage of this library

include_once('ImageHandler/autoload.php');

// Initialize the class, it loads config from the config file, but configs can be overwritten from here
$imageHandler = new ImageHandler(
array(
'sizes' => array(
'normal' => array('width' => 50, 'height' => 50)
),
'db' => array(
'server' => 'localhost',
'user' => 'root',
'pass' => '',
'database' => 'test',
'table_name' => 'images'
),
'base_path' => dirname(__FILE__) . DIRECTORY_SEPARATOR . 'test_images'
)
);

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

// Check if we have to delete
if ( array_key_exists('delete_image', $_GET) )
{
$imageHandler->delete_image_by(array('object_id' => 1, 'object_type' => 'test'));
}

// Handle POST
if ( $_POST )
{
if ( array_key_exists('image', $_FILES) )
{
// Validate and save image
if ( $imageHandler->validate_image($_FILES['image']) )
{
$imageHandler->save_image($_FILES['image'], 1, 'test', 'test_images/1');
}
else
{
var_dump($imageHandler->get_errors());
}
}
}

echo 'Current image:';

$images = $imageHandler->get_image_by(array('object_id' => 1, 'object_type' => 'test'));

var_dump($images);

You can find this library here: https://github.com/atillaordog/ImageHandler

Validation Wall

I re-imagined data validation in PHP. Why did I do this? I found that current validation techniques are too strict and cannot be used alone.

So, I thought the following: validation is like a filter. A wall that has different doors, each door specific to a set of data. Data comes and in order to reach its destination, it has to go over the door. Now the door has rule-sets, these are the rules specific to a part from the data.

These rule sets are built from rules, that are the basic rules found in any validation system.

A wall can be built easily by simply grouping the rules to fit our needs.

Here is an example:

$title_ruleset = new MainRuleset('title', array( new NotEmpty() ));
$description_ruleset = new MainRuleset('description', array( new NotEmpty() ));
$categories_ruleset = new MainRuleset('categories', array( new NotEmpty() ));

$door = new Door(array($title_ruleset, $description_ruleset, $categories_ruleset));

$vw = new ValidationWall($door);

$post_passes = $vw->pass($post);

In this example we use the NotEmpty rule to check the title, the description and the category.

 

To use ValidationWall, you have to include the autoload file like:

include_once route/to/ValidationWall/folder/autoload.php';

Also you have to use the namespaces like:

use ValidationWallDoorMainDoor as Door;
use ValidationWallRuleSetMainRuleset as MainRuleset;
use ValidationWallRuleNotEmpty as NotEmpty;
use ValidationWallRuleNumeric as RuleNumeric;

After you initialized ValidationWall, you can start building your wall to fit your needs.

Main benefits:

  • It is totally stand-alone, usable anywhere in any script
  • Logically more understandable than general validation
  • Rules are custom, meaning you can build any rule you want, you just have to implement the interface or extends the abstract class
  • Build validation like you are playing Lego

You can find this little code here:

https://github.com/atillaordog/ValidationWall