Monday, 11 December 2017

OTA (Over the Air) update of ESP8266 firmware using its AP(access-point) over the HTTP.

Here the idea is, I am using ESP8266 wifi module to control appliances in a room using my home wifi network. But I was facing a issue that if I changed the ssid and wifi-password then I was not able to connect to my wifi. For the solution of this problem I thought of OTA (Over the Air) update of my ESP8266 firmware(code) with updated password in it. For this I thought since I would not be able to connect to my wifi because of password change, let's use the ESP8266's (AP)access-point feature which creates its own hotspot. I can connect to access-point to update my ESP8266's firmware. If you want a web server with static ip in your ESP8266 wifi module which is capable of creating access-point from which you can update firmware of your ESP8266, you can use this arduino code. Here GPIO5 pin of ESP8266-12 module(const int pin = 5;) is used for controlling relay or leds. For that you have to do follwing changes in the code:
  • First thing is, you will have to provide your wifi's ssid and wifipassword in the code.
const char* ssid = "mywifi";
const char* password = "password";
  • Then you will have to provide give a desired ssid and password for connecting to your ESP8266 through AccessPoint to update firmware. It can be anything.
const char *esp_ssid = "your_desired_ssid";
const char *esp_password = "your_desired_password";
  • Now this is for authorization before updating ESP8266's firmware.
const char* www_username = "admin";
const char* www_password = "password";
  • Get your gateway(or router) and subnet, then set the desired ip according to your gateway.
IPAddress ip(192, 168, 1, xx); // where xx is the desired IP Address
IPAddress gateway(192, 168, 1, 254); // set gateway to match your wifi network
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your wifi network
  • Now just use you ip, gateway and subnet to set static ip in void setup().
void setup(){
 ......
 WiFi.config(ip, gateway, subnet);
 ......
}
##Basic Requirements Flash chip size should 2x the size of the sketch for using OTA update for your ESP8266 Wifi module.

How to use:

  1. To control your home appliances, go to your web-browser,
  1. To update firmware(code) of your ESP8266 wifi module,
  • Use http://your-ip/fupdate to update firmware.
    • Give authorized user-name and password which you already set in the code.
    • After authorization ESP8266's access-point will enable.
    • Connect your computer to ESP8266's local hotspot network.
    • Choose your firmware.bin file (example: updateHttpServer.ino.generic.bin) to upload and click Uploadbutton.
    • If firmware will be uploaded successfully, you will get OK message on your browser.
    • After uploading, restart your ESP8266 wifi module.

Note:

  • If you went http://your-ip/fupdate and did authorization successfully but haven't uploaded new firmware then restart the ESP8266 module so the ESP8266's AP(access-point) mode will stop and don't consume unnecessary power.
  • If firmware updation will fail, restart the ESP8266. It will re-run the old code and try to upload firmware again.
  • Sometimes it takes 3-5 attempts to upload firmware successfully.

Tools:

You can download ArdroidHttpClient to send request via HTTP. Or you can download or fork the project from link on git-hub and build apk using Android Studio and install to your android phone. You will have to provide a name(any name), your ESP8266 ip (the static ip) and port(as given in the code port is 80). Now you can control your home appliances using the app.
This android application is not for updating firmware. You can use browser for that purpose.
You can get application source code here:
 https://github.com/Arduino-and-RaspberryPi/ESPWebUpdateHttpServer.

Note:

While using this code with ESP8266 wifi module, you phone or computer should be on same network.

Connections:

ESP Wifi Server with static IP

If you want a web server with static ip in you ESP8266 wifi module, you can use this arduino code. Here GPIO2 pin of ESP8266 module(const int pin = 2;) is used for controlling relay or leds. For that you have to do follwing changes in the code:
  • First thing is, you will have to provide your ssid and wifipassword in the code.
const char* ssid = "mywifi";
const char* password = "password";
  • Get your gateway(or router) and subnet, then set the desired ip according to your gateway.
IPAddress ip(192, 168, 1, xx); // where xx is the desired IP Address
IPAddress gateway(192, 168, 1, 254); // set gateway to match your wifi network
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your wifi network
  • Now just use you ip, gateway and subnet to set static ip in void setup().
void setup(){
 ......
 WiFi.config(ip, gateway, subnet);
 ......
}

Tools:

You can download ArdroidHttpClient to send request via HTTP. Or you can download or fork the project from link on git-hub and build apk using Android Studio and install to your android phone. You will have to provide a name(any name), your ESP8266 ip (the static ip) and port(as given in the code port is 80). Now you can control your home appliances using the app. You can get source code of the project from here https://github.com/Arduino-and-RaspberryPi/ESPWifiServer.

Note:

While using this code with ESP8266 wifi module, you phone or computer should be on same network.

Connections:

Tuesday, 21 November 2017

Basic Apache Kafka Tutorial

This tutorial is more focused on practical rather than theory. For basics of apache kafka watch this video.


Lets start with Simple Kafka producer/consumer application. Repo source apache-kafka-learning.

Getting Started:

(i.) Simple Demo Application

1. Download and setup:

  • Visit the Kafka download page and download the most recent version.
  • Extract downloaded file in ~/workspace/kafka folder.

2. Start ZooKeeper server:

 $ kafka_2.11-0.11.0.0/bin/zookeeper-server-start.sh kafka_2.11-0.11.0.0/config/zookeeper.properties  

3. Start kafka server(broker):

   $ kafka_2.11-0.11.0.0/bin/kafka-server-start.sh kafka_2.11-0.11.0.0/config/server.properties 

4. Creating Topic:

    $ kafka_2.11-0.11.0.0/bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic MyFirstTopic --partitions 2 --replication-factor 1 

5. Start a simple console producer that can publish messages to the test topic:

    $ kafka_2.11-0.11.0.0/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic MyFirstTopic 

6. Start a simple console consumer that can consume messages published to a given topic:

    $ kafka_2.11-0.11.0.0/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic MyFirstTopic #--from-beginning

(ii.) Custom producer/consumer application

1. Setup:

  • Create a maven java application and update the pom.xml file as given in the repo.

2. Create custom producer and consumer classes.

3. Build:

 $ mvn clean compile assembly:single 

4. Run

  • Follow the above steps from 2 to 4 of (i.) Simple Demo Application part.
  • Step 5 starting the producer:
        $ java -cp target/kafka-learning-1.0-SNAPSHOT-jar-with-dependencies.jar producer.Producer MyFirstTopic
  • Step 6 starting the consumer:
        $ java -cp target/Kafka-learning-1.0-SNAPSHOT-jar-with-dependencies.jar consumer.Consumer MyFirstTopic group1
    • Type exit to close the application
Note: Repo source apache-kafka-learning.