Basic AES-128-ECB Encryption/Decryption with PHP

The Key must be 16bit long.

    $key = '1234567890123456';
   
    function encrypt($data,$key) {
        return base64_encode(openssl_encrypt($data, "aes-128-ecb", $key, OPENSSL_RAW_DATA));
    }

    function decrypt($data,$key) {
        return openssl_decrypt(base64_decode($data), "aes-128-ecb", $key, OPENSSL_RAW_DATA);
    }

 

To Encrypt, Simply call

$data = "This is to be encrypted";
echo $encrypted_text = encrypt($data,$key);

 

To Decrypt the above encrypted text

$data = "This is to be encrypted";
$encrypted_text = encrypt($data,$key);

$data = $encrypted_text;
echo $decrypted_text = decrypt($data,$key);

 

Zip directory using Node.JS

Archiving a file/folder using nodejs is very simple. I have used nodejs archiver library to zip directory and fs library to get the file information in this post.

const fs = require('fs');
var archiver = require('archiver');

function zip_directory(filename, copyFrom, copyToPath){
    var copyTo = copyToPath + filename;

    if(!fs.existsSync(copyToPath)){
        fs.mkdirSync(copyToPath);
    }

    var output = fs.createWriteStream(copyTo);
    var archive = archiver('zip', {
      zlib: { level: 9 } // Sets the compression level.
    });

    output.on('close', function () {
        console.log(`\n<< ARCHIVE LOG: TOTAL BYTES ${archive.pointer()} >>`);
        console.log(`\n<< ARCHIVE LOG: archiver has been finalized and the output file descriptor has closed. >>`);
    });

    output.on('end', function() {
	  console.log(`\n<< ARCHIVE LOG: END : Data has been drained. >>`);
	});

    archive.on('warning', function(err) {
	  if (err.code === 'ENOENT') {
	    console.log(`\n<< ARCHIVE LOG: ENOENT ${err} >>`);
	  } else {
	    console.log(`\n<< ARCHIVE LOG: WARNING ${err} >>`);
	  }
	});

    archive.on('error', function(err){
        console.log(`\n<< ARCHIVE LOG: Error : ${err} >>`);
    });

    try {
        archive.pipe(output);
        archive.directory(copyFrom, { name: filename });
        archive.finalize();
    } catch (err) {
        console.log(`\n<< ARCHIVE LOG: Error : ${err} >>`);
    }
}


zip_directory('myzipfile.zip', '/home/', '/opt/zipfiles/');

 

Note: Should install required packages as well as declare require things in code.

Tagged : / /

NodeJs Http Server (Web Server)

Making a simple HTTP server in Node.js is very simple. Node.js provides extremely easy-to-use HTTP APIs, a simple web server.

Let’s take a look at a very simple example:

const http = require('http');

const http_port = 8000;

// Instantiate the HTTP server.
const httpServer = http.createServer((req, res) => {
    if (req.method == 'POST') {
        var jsonString = '';

        req.on('data', function (data) {
    	    try {
                console.log(data.toString());
    	    }
    	    catch(err) {
    		console.log(err);
    	    }
        });

      	req.on('error', (err) => {
            // This prints the error message and stack trace to `stderr`.
            console.log("POST ERROR \n" + err.stack);
      	});

        req.on('end', function () {
             console.log('POST Request ended here.');
        });
    }
    
    res.writeHead(200);
    res.end();
    console.log('Response ended here. \n ');
});


httpServer.listen(http_port, () => {
  console.log("Web server is listening on port %s \n", http_port);
});

Save this in a file called server.js – run node server.js, and your program  will hang there… it’s waiting for connections to respond to, so you’ll have to give it one if you want to see it do anything. If everything has been set up correctly, you should see your server’s request & response log.

Let’s take a more in-depth look at what the above code is doing. Function in http.createServer takes a request object and a response object as parameters.

The request object contains things such as the requested URL, Request Method and data.

The response object is how we send the headers and contents of the response back to the user making the request. Here we return a 200 response code (signaling a successful response) with empty body. Other headers, such as Content-type, would also be set here.

The listen method, which causes the server to wait for incoming requests on the specified port – 8000, in this case.

There you have it – your most basic Node.js HTTP server.

Manage IoT Sensor Network with ISTSOS (Open Source)

IstSOS is Free and Open Source Sensor Observation Service Data Management System (OGC SOS server implementation) written in Python. istSOS allows for managing and dispatch observations from monitoring sensors according to the Sensor Observation Service standard. It provides a Graphical user Interface that allows for easing the daily operations and a RESTFull Web api for automatizing administration procedures. istSOS is released under the GPL License, and runs on all major platforms (Windows, Linux, Mac OS X), even though it has been used in production in linux environment only.

Main Features

  • Offer your data according to the Sensor Observation Service standard from Open Geospatial Consortium.
  • Administer your sensors and your data with a comfortable interface.
  • Use a complete API for accessing functionalities to makes it easy for new clients to use istSOS application.
  • Get notified trough mail, twitter or other social when your sensor data met specific conditions.

How to Install (On Ubuntu 18.04)

Installation on Linux with the Debian package

The easiest way to install istSOS on a Debian distribution is to use the istSOS deb packages.

Download the debian file from the repository

Please go to https://sourceforge.net/projects/istsos/files to get the latest release.

Install the debian file

Open a terminal and move to the folder containing the downloaded deb file.

sudo dpkg -i python3-istsos_2.4.0-RC4.deb;sudo apt-get -f -y install

This command will install all the required dependencies, with the exception of PostgreSQL and PostGIS. In fact it could reside on other servers.

If everything has gone well, you should see the administration page at this address: http://localhost/istsos/admin/

NodeMCU ESP8266 Over The Air (OTA) Programming In Arduino IDE

A fantastic feature of any WiFi-enabled microcontroller like ESP8266 NodeMCU is the ability to update its firmware wirelessly. This is known as Over-The-Air (OTA) programming.

The OTA programming allows updating/uploading a new program to ESP8266 using Wi-Fi instead of requiring the user to connect the ESP8266 to a computer via USB to perform the update.

OTA functionality is extremely useful in case of no physical access to the ESP module. It helps reduce the amount of time spent for updating each ESP module at the time of maintenance.

One important feature of OTA is that one central location can send an update to multiple ESPs sharing same network.

The only disadvantage is that you have to add an extra code for OTA with every sketch you upload, so that you’re able to use OTA in the next update.

The factory image in ESP8266 doesn’t have an OTA Upgrade capability. So, you need to load the OTA firmware on the ESP8266 through serial interface first.

It’s a mandatory step to initially update the firmware, so that you’re able to do the next updates/uploads over-the-air.

The ESP8266 add-on for the Arduino IDE comes with a OTA library & BasicOTA example. You can access it through File > Examples > ArduinoOTA > BasicOTA.

The following code should load. But, before you head for uploading the sketch, you need to make some changes to make it work for you. You need to modify the following two variables with your network credentials, so that ESP8266 can establish a connection with existing network.

const char* ssid = "..........";
const char* password = "..........";

Once you are done, go ahead and upload the sketch.

 

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

const char* ssid = "..........";
const char* password = "..........";

void setup() {
  Serial.begin(115200);
  Serial.println("Booting NodeMCU");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Sorry, Connection Failed! Rebooting NodeMCU...");
    delay(5000);
    ESP.restart();
  }
  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH)
      type = "sketch";
    else 
      type = "filesystem";

    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connecting Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receiving Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  ArduinoOTA.handle();
}

Now, open the Serial Monitor at a baud rate of 115200. And press the RST button on ESP8266. If everything is OK, it will output the dynamic IP address obtained from your router. Note it down.

Upload New Sketch Over-The-Air

Now, let’s upload a new sketch over-the-air.

Remember! you need to add the code for OTA in every sketch you upload. Otherwise, you’ll loose OTA capability and will not be able to do next uploads over-the-air. So, it’s recommended to modify the above code to include your new code.

As an example we will include a simple Blink sketch in the Basic OTA code. Remember to modify the SSID and password variables with your network credentials.

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

const char* ssid = "..........";
const char* password = "..........";

//variabls for blinking an LED with Millis
const int led = D0; // ESP8266 Pin to which onboard LED is connected
unsigned long previousMillis = 0;  // will store last time LED was updated
const long interval = 1000;  // interval at which to blink (milliseconds)
int ledState = LOW;  // ledState used to set the LED
void setup() {
pinMode(led, OUTPUT);
    
  Serial.begin(115200);
  Serial.println("Booting NodeMCU");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting NodeMCU...");
    delay(5000);
    ESP.restart();
  }
  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH)
      type = "sketch";
    else
      type = "filesystem";

    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connecting Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receiving Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  ArduinoOTA.handle();

  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {

  previousMillis = currentMillis;
  ledState = not(ledState);
  digitalWrite(led,  ledState);
  }
}

Once you copy above sketch to your Arduino IDE, go to Tools > Port option and you should see something like this: esp8266-xxxxxx at your_esp_ip_address If you can’t find it, you may need to restart your IDE.

Select the port and click Upload button. Within a few seconds, the new sketch will be uploaded. And you should see the on-board LED blinking.

Smart Switch using NodeMCU – Works with Google Home Assistant

Google assistant is AI (Artificial Intelligence) based voice command service. Using voice, we can interact with google assistant and it can search on the internet, schedule events, set alarms, control appliances, etc. This service is available on smartphones and Google Home devices.

We can control smart home devices including lights, switches, fans, thermostats and many more devices using our Google Assistant.

A lot of commercial devices already exist in the market for such application but,. Here i have explained how to build your own smart switch using NodeMCU (ESP8266) development board and few other electrical components.

Components used;

  • NodeMCU (ESP8266) Development Board.
  • Solid State Relay Module.
  • 6V Power Supply Module.
  • Connection Wires.

Above figure explained very simply about the circuit diagram. Be aware when you connecting this to the main power. If you don’t have enough knowledge about connecting this to the main power system, please get some help from a experienced  technician.

Before you connect using above explained diagram, you must programme the NodeMCU development board. Otherwise you cannot control it using Google Smart Home Assistant with voice commands.

In above diagram, i used D0 – Digital pin to control the input signal connected to the Solid State Relay module. When we want to switch on the light, we need to set it to high voltage state (5V), and when we want to switch of the light, we need to set it to low voltage state (0V).

You can find the source code which needs to be uploaded to the NodeMCU development borad using Arduino IDE here. https://github.com/lahirutm/WebserviceLK-IoT/blob/master/arduino_examples/state_save_light_example.ino

Once you programmed your NodeMCU, you must link it with Google Home Assistant to control using voice commands. Please follow the steps explained here.  https://iot.webservice.lk/how_to

Enjoy using your own smart home devices…

Toroidal Transformers

Transformers are made from a pair of solenoids wrapped around a metal core that is usually a ferrite. Toroidal transformers are two coils wrapped around a metal, such as a ferrite or silicon steel, that is doughnut shaped. The coils are either wrapped in different areas or placed one over the other. They are preferred for RF or radio frequency transformers, where they are used to increase or decrease voltages from power sources, and to isolate different parts in a circuit. RF transformers are also used for impedance matching, which means they help connect input and output parts of different circuits.

Here are five other reasons why toroidal transformers are used for many industrial applications:

  1. Low noise and low stray field – The field generated by magnetization, also known as the stray field, is lower in a toroidal transformer, due to the uniform core windings. Less magnetic interference in toroids results in higher performance.

  2. Easy to mount – Using just one screw, toroidal transformers can be easily mounted in a short time. This convenience helps limit maintenance and downtime.

  3. Low operating temperature – Toroidal transformers operate at lower temperatures than transformers with similar specifications.

  4. Light weight core – The core of a toroidal transformer weighs less than typical transformers due to it being comprised of less raw materials.

  5. More economical – Since toroidal transformer cores are manufactured from fewer materials, they weigh less than conventional transformers and use less energy, providing better cost savings and higher return on investment.

The toroidal transformers efficiency makes them useful for a wide array of machines such as audio/visual equipment, security systems, telecommunication systems, industrial control equipment and power distribution equipment. Cost efficiency is an important key to the widespread use of toroidal transformers, as well as the fact that they can be customized for any diameter and height.

Difference Between MPPT and PWM Charge Controller

 

MPPT Solar Charge Controller

PWM Solar Charge Controller

1. What they do

The PWM controller is in essence a switch that connects a solar array to a battery. The result is that the voltage of the array will be pulled down to near that of the battery.

The MPPT controller is more sophisticated (and more expensive): it will adjust its input voltage to harvest the maximum power from the solar array and then transform this power to supply the varying voltage requirement, of the battery plus load. Thus, it essentially decouples the array and battery voltages so that there can be, for example, a 12 volt battery on one side of the MPPT charge controller and a large number of cells wired in series to produce 36 volts on the other.

As array size increases, cable length will increase. The option to wire more panels in series and thereby decrease the cable cross sectional area with a resultant drop in cost, is a compelling reason to install an MPPT controller as soon as the array power exceeds a few hundred Watts (12 V battery), or several 100s of Watts (24 V or 48 V battery).

PWM

The PWM charge controller is a good low cost solution for small systems only, when solar cell temperature is moderate to high (between 45°C and 75°C).

MPPT

To fully exploit the potential of the MPPT controller, the array voltage should be substantially higher than the battery voltage. The MPPT controller is the solution of choice for higher power systems (because of the lowest overall system cost due to smaller cable cross sectional areas). The MPPT controller will also harvest substantially more power when the solar cell temperature is low (below 45°C), or very high (above 75°C), or when irradiance is very low.

  • Charging Method
    The difference between the PWM controller and the MPPT solar controller is that they charge differently. The PWM is charged in a three-stage charging mode. MPPT is the maximum power tracking technology, and the charging efficiency can be increased to about 30%.
  • Voltage
    When the MPPT solar controller is used, the charging efficiency is more obvious when the high-voltage solar panel is used to charge the low-voltage battery. If the voltage of the solar energy and the battery are the same, for example, it is 12V, then the difference between the practical PWM and the MPPT controller is not very big.
  • Power
    If the power of the solar panel is small, it is more appropriate to select a PWM controller. Relatively speaking, if the power of the solar panel is large, MPPT is selected.Solar controllers, because the cost of MPPT solar controllers is high, if your solar panel power is small, it is a waste of MPPT controller.

Salt Water Batteries

Saltwater batteries are not similar to lithium-ion batteries or lead acid batteries for use in portable devices or any automobiles. This is because they can’t hold as much charge in the same size and weight, or putting it another way, they are less energy-dense. But they hold some powerful advantages in applications in which size and weight are less important.

The electrolyte for a saltwater battery is nothing more than — salt water — hence the device’s name comes from it. The anode can be carbon, and the cathode can be a material such as manganese oxide. (Or anode can be copper and cathode can be zinc or aluminum).

With no hazardous materials in their construction, unlike lithium-ion batteries, they are non-toxic and they cannot explode. There is no need for the complex and troublesome electronic circuitry that every lithium-ion battery needs to ensure that they charge and discharge only within safe parameters. In addition, unlike their lithium-based cousins, saltwater batteries can be more deeply discharged (drained of electrical energy) with no damage to the battery.

The biggest disadvantage of all forms of salt-water batteries is that, to store a given amount of electricity, they are bulkier and heavier than other commercially available batteries. As it turns out, there is one huge potential use for these devices in which that will be no issue at all — smoothing out power from large-scale renewable-energy-generation plants.