element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Sudden Impact Wearables Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Sudden Impact Wearables Design Challenge
  • More
  • Cancel
Sudden Impact Wearables Design Challenge
Blog EasyConfigure - BASH scripting
  • Blog
  • Forum
  • Documents
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
Author: tomaja
Date Created: 11 Jan 2015 11:50 PM
Views: 132
Likes: 0
Comments: 0
  • mqtt
  • easy_configure
  • sudden_impact
  • raspberry-pi
  • bash
  • mosquitto
Related
Recommended

EasyConfigure - BASH scripting

tomaja
tomaja
11 Jan 2015

Previous posts:

EasyConfigure - Modular/Configurable System Intro

EasyConfigure - Hardware Components

 

1. Introduction

Time is getting shorter for this challenge so it seems like a good idea to use tools that will help in making the progress faster.

Since I'm still waiting for the contestant's kit, the only way I can work right now is by working on the software side and on hardware components that I already have.

I decided to make parts of this project with simple BASH scripts. BASH scripts are speeding up the prototyping phase very much.

These scripts are running on a Raspberry Pi.

 

2. Scripts

scan_and_configure.sh - Scans for WiFi access points and connects to those that follow the naming convention selected for remote units (list of predefined unit names is also an option).

For now, all remote units will initially run as APs with open access.

After Raspberry Pi connects to remote unit as client, it sends a configuration string using netcat. For now, this string contains the main access point ESSID and MQTT broker IP.

After that, switch_to_client command is sent. I'm still waiting for my WiFi modules that I've ordered on eBay so this script is not fully functional.

Please, disregard this many unbuffer commands, I had too much trouble with buffering so I put them everywhere without too much thinking - I'll sort it out later.

#!/bin/bash
# Element14 - SuddenImpact design challenge
# http://www.element14.com/community/community/design-challenges/sudden-impact
#
# This file is part of submission made for Modular&EasyConfigure project (tomaja).
#.
# v000 - January 10th 2015.
#      Initial version. Scans for available wireless network access points.
#      Loops through the list and connects to all available remote units to send configuration data.
#

# Hardcoded values. Will be passed through the command line.
MAIN_AP=tomaje
BROKER_IP=192.168.1.94

# I will filter APs here using regex. For now, connect to any AP
# ESSIDs will be written in remote unit flash memory.
# ESSID example: MEC_deadbeef1234
# Device reset button will restore the AP mode for device so it can be initialized again with different main AP ESSID and broker address.
aps=`sudo unbuffer iwlist wlan0 scan | grep ESSID | sed 's/[ ][ ]*ESSID:\(\)/\1/g' | sed 's/"//g'`

count=`unbuffer echo "$aps" | wc -l`

if [ $count != 0 ]; then
    unbuffer echo Pairing with remote units...
    unbuffer echo Number of detected devices: $count

    for ap in $aps ; do
      echo Setting up device $ap
      echo -e "\tConnecting to $ap ..."
      sudo unbuffer iwconfig wlan0 mode Managed &
      sudo unbuffer iwconfig wlan0 essid $ap &
      echo -e "\tSending configuration ..."
# Sending configuration data. For now, just send some dummy settings to localhost (Test server: nc -k -l 12345)
      echo "AP:$MAIN_AP IP:$BROKER_IP" | nc localhost 12345
      echo "switch_to_client" | nc localhost 12345
      echo -e "\tDONE"
    done
else
    echo "Could not find SuddentImpact remote units. Please make sure that units are prepared for pairing."
fi

 

subscribe.sh - Subscribe to MQTT broker and act upon messages received.

I have a An Open Source MQTT v3.1 Broker mosquitto package installed on my RPi (using install method from their site).

Also, mosquitto-clients package was installed, since I use some CLI tools like mosquitto_pub and mosquitto_sub from that package.

For now, only update-units command is being handled in this script (line 15). This command triggers the previous script. Publishing will be done from monitoring applications (usually run on handheld devices).

 

#!/bin/bash
# Element14 - SuddenImpact design challenge
# http://www.element14.com/community/community/design-challenges/sudden-impact
#
# This file is part of submission made for Modular&EasyConfigure project (tomaja).
#
# v010 - January 11th 2015.
#      Initial version. Subscribe to MQTT broker and act upon messages received.
#

mosquitto_sub -t suddenimpact/commands |
while IFS= read -r line
    do
          if [ $line = "update-units" ]; then
              sudo ./scan_and_configure.sh
          fi
done

 

Demo output looks like this (tomaje was the only WiFi AP available):

pi@malina ~/SuddenImpact $ sudo ./subscribe.sh
Pairing with remote units...
Number of detected devices: 1
Setting up device tomaje
  Connecting to tomaje ...
  Sending configuration ...
  DONE

 

The above output is triggered by publishing update-unit to suddenimpact/commands topic like this:

pi@malina ~ $ mosquitto_pub -t suddenimpact/commands -m update-units

 

Similarly, I will make a script to store remote units sensor data into a database. If this turns out to be too slow, I will make a dedicated C++ application. Assessment of performance can be easily done by running an arbitrary number of publishers from another computer.

 

I received Arduino Micro that I plan to use as the basis for remote units. I'm still to receive the ESP8266 WiFi module. I will also try using one of Microchip's WiFi modules available from element14 (unlike ESP8266, Microchip modules are certified for usage in Europe and USA at least).

 

Thanks,

Dragan

Anonymous
Element14

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2022 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube