WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Connect to WIFI or create ad-hoc dhcp connection as fallback  (Read 18721 times)

Offline gpulido

  • Jr. Member
  • **
  • Posts: 57
Connect to WIFI or create ad-hoc dhcp connection as fallback
« on: November 05, 2013, 05:04:03 PM »
Hello!,
I would like to allow a raspberry powered networked harware device, to try to connect to a "specific" wifi, and in case it doesn't found any suitable, to create its own wifi accesible network with a DHCP server.
The idea is just the same as in the post from I borrow the name: http://lcdev.dk/2012/11/18/raspberry-pi-tutorial-connect-to-wifi-or-create-an-encrypted-dhcp-enabled-ad-hoc-network-as-fallback/

In that post the solution for raspbian is given, but as tce has its own "way" to do things I'm asking for help to configure such functionallity.
I've been search through the forum, reviewing all piCore threads looking for hints about how to accomplish it.
I also spend some hours trying to adapt the previous post to tce using my limited network knowdlege and I haven't been able to make it work :(
The things I had try are:
1) Use dhcpd alone
2) Use dhcpd with dnsmasq
3) Use ifconfig and iwconfig

To have such "functionallity stack" is amazing to create hardware devices that could be configured with any hardware device that is on the same network, just for having a "ad-hoc" network created, or just to be able to configure the device to connect to a wifi without the need of accesing through ssh or knowing any linux (for end users)

So, if anyone could point me on the right direction, or help me with it I will be eternally grateful.
Also it could be useful for others trying to use the raspi + tcl stack to embeded devices.

Regards
Gabriel

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Connect to WIFI or create ad-hoc dhcp connection as fallback
« Reply #1 on: November 05, 2013, 05:19:13 PM »
Presented solution in the referred article is generic Linux, nothing Raspbian (expect Debian package management). You can do the same, only the ISC DHCP server is missing in the repo and we have a different startup.
« Last Edit: November 05, 2013, 05:22:28 PM by bmarkus »
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline gpulido

  • Jr. Member
  • **
  • Posts: 57
Re: Connect to WIFI or create ad-hoc dhcp connection as fallback
« Reply #2 on: November 05, 2013, 05:50:08 PM »
Hello,
Thank you for answering.

I already have tried it adapting some of the elements.
For example the dhcpd conf, I created one using the format given here: http://forum.tinycorelinux.net/index.php/topic,2338.msg12210.html#msg12210

Also tcl doesn't have /network/interfaces to configure the wifi, and I don't know what is the ISC DHCP for on the article, he seems to install it but after that is not launched (he also removes it from the boot)
I was able to have a wlan0 up, but it wasn't reachable outside tcl.

However, If you could help me, tomorrow I will reproduce the steps on that post, and try to change the things that are not "available" on tcl for others.


Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Connect to WIFI or create ad-hoc dhcp connection as fallback
« Reply #3 on: November 06, 2013, 01:31:16 AM »
Unfortunately I do not have time to play with it as it requires lot of testing. If necessary I can add the ISC DHCP pack to the repo.
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Connect to WIFI or create ad-hoc dhcp connection as fallback
« Reply #4 on: November 06, 2013, 05:01:38 AM »
dhcp.tcz added to 5.x repo which contains the mainstream DHCP server, client and relay.
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline gpulido

  • Jr. Member
  • **
  • Posts: 57
Re: Connect to WIFI or create ad-hoc dhcp connection as fallback
« Reply #5 on: November 06, 2013, 08:43:00 AM »
Hello,
Thank you!,
One question, the dhtpd daemon was already available on the 5 alpha6 that I'm using
Is the new package  analogous to the ISC DHCP ?

Gabriel

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Connect to WIFI or create ad-hoc dhcp connection as fallback
« Reply #6 on: November 06, 2013, 08:50:12 AM »
In the base of Core/piCore common Linux apps are provided by BusyBox instead of their mainstream equivalent. The dhcp.tcz package is not analogous, IT IS the ISC mainstream DHCP.
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline gpulido

  • Jr. Member
  • **
  • Posts: 57
Re: Connect to WIFI or create ad-hoc dhcp connection as fallback
« Reply #7 on: November 06, 2013, 01:21:49 PM »
Hello,
My firsts steps trying to configure the DHCPD:

1) Install the dhcpd.tzc

2) Create a config file on /etc/dhcpd.conf with the following info:

Code: [Select]
DHCPDARGS=wlan0; #args for the dhcpd daemon -> limit DHCP to the wlan0 interface
default-lease-time 600;
max-lease-time 7200;
 
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.1.255;
option domain-name "RPi-network";
option routers 192.168.1.1; #default gateway
 
subnet 192.168.1.0 netmask 255.255.255.0 {
    range 192.168.1.100 192.168.1.200; #IP range to offer
}
 

3) Create the dhcpd.leases file
sudo mkdir -p /var/db
sudo touch /var/db/dhcpd.leases

4) Trying to run dhcpd with the wlan0 DOWN (i.e, it is not showed using the ifconfig)
sudo dhcpd

it throws me:

Code: [Select]
Wrote 0 deleted host decls to leases file.
Wrote 0 new dynamic host decls to leases file.
Wrote 0 leases to leases file.

No subnet declaration for eth0 (192.168.1.26).
** Ignoring requests on eth0.  If this is not what
   you want, please write a subnet declaration
   in your dhcpd.conf file for the network segment
   to which interface eth0 is attached. **


Not configured to listen on any interfaces!

When I search about how to configure to liste any interface, it seems that a parameter must be added to "the defaults"  file of the dhcpd
Some post talk about
 /etc/default/isc-dhcp-server
And others about:
/etc/sysconfig/dhcpd

I have not found any file related to the dhcp where to put the following line:
Code: [Select]
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACES="wlan0"

Any idea where can I found such file (or equivalent) for the tcl installation?

EDITED
Seems that the dhcpd admits the interface as parameter:
sudo dhcpd wlan0

Now I'm having the following message
sudo dhcpd wlan0

Code: [Select]
Internet Systems Consortium DHCP Server 4.2.5-P1
Copyright 2004-2013 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Wrote 0 leases to leases file.
Multiple interfaces match the same subnet: wlan0 eth0
Multiple interfaces match the same shared network: wlan0 eth0
Listening on LPF/wlan0/00:0b:81:87:dc:d3/192.168.1.0/24
Sending on   LPF/wlan0/00:0b:81:87:dc:d3/192.168.1.0/24
Sending on   Socket/fallback/fallback-net

The wifi created is still not reachable

Thanks
Gabriel

« Last Edit: November 06, 2013, 02:03:27 PM by gpulido »

Offline gpulido

  • Jr. Member
  • **
  • Posts: 57
Re: Connect to WIFI or create ad-hoc dhcp connection as fallback
« Reply #8 on: November 06, 2013, 05:44:34 PM »
Some news, I think I'm moving on the right direction.
I create the ad-hoc connection using the following commands:

Code: [Select]
ifconfig wlan0 down
iwconfig wlan0 mode ad-hoc
iwconfig wlan0 key aaaaa11111 #WEP key
iwconfig wlan0 channel 1 essid RPi      #SSID
ifconfig wlan0 192.168.1.1 netmask 255.255.255.0 up

And after that, I started the dhcpd:
dhcp wlan0

With that configuration, I'm able to connect through ad-hoc using an android device, and fixing the ip on the android device. However I'm no capable of ssh on the system.

Using my desktop ubuntu and using the same wlan0 configuration as previous, (changing the ip), the ping between both devices is not working.

So I'm stuck now.

Gabriel




Offline gpulido

  • Jr. Member
  • **
  • Posts: 57
Re: Connect to WIFI or create ad-hoc dhcp connection as fallback
« Reply #9 on: November 06, 2013, 07:02:36 PM »
Hello again.
Sucess! Following the adafruit tutorial http://learn.adafruit.com/setting-up-a-raspberry-pi-as-a-wifi-access-point/install-software
And changing where it is needed I have been able to create a wifi access point with DHCP using tinycorelinux.
It is very straightforward using the adafruit tutorial, but I'm going to write down here all steps adapted for tcl.

1) Install software.

1.1)install the dhcp.tzc package (instead of the isc-dhcp-server)

1.2) Also we are going to need the hostapd compiled binary from adafruit:

Code: [Select]
wget http://www.adafruit.com/downloads/adafruit_hostapd.zip
Uncompress it:
Code: [Select]
unzip adafruit_hostapd.zip
Move the version to a binary directory (I'm not sure here where is the best place on tce for this file)
Code: [Select]
sudo mv hostapd /usr/local/bin
Make it executable
Code: [Select]
sudo chmod 755 /usr/local/bin/hostapd
2)Set up DHCP server
Create a new file /usr/local/etc/dhcp.conf with the following info:
Code: [Select]
authoritative;
subnet 192.168.42.0 netmask 255.255.255.0 {
range 192.168.42.10 192.168.42.50;
option broadcast-address 192.168.42.255;
option routers 192.168.42.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local";
option domain-name-servers 8.8.8.8, 8.8.4.4;
}

(the ip addresses could be changed, but I mantain the adafruit ones)

3) Config hostapd for Access point
Create a file /usr/local/etc/hostapd.conf and paste the following.
The ssid and password can be changed. Also the driver could be another. It depends of the wifi dongle. I have a 8192CU and it worked.
Code: [Select]
interface=wlan0
driver=rtl871xdrv
ssid=Pi_AP
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=Raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

4) Put all together: Set up wlan0 for static IP, run dhcp and hostapd

Create a file /opt/wlan0.sh with the following data:

Code: [Select]
ifconfig wlan0 down
ifconfig wlan0 192.168.42.1 netmask 255.255.255.0 up
/usr/sbin/hostapd /usr/local/etc/hostapd.conf &
touch /usr/local/etc/dhcp.leases
dhcpd wlan0 -cf /usr/local/etc/hcpd.conf -ls /usr/local/etc/dhcp.leases

make it executable
Code: [Select]
sudo chmod 755 /opt/wlan0.sh
4b) Optional. Before test the script, the wpa_supplicant daemon must be killed if it is running
Use ps to find it and sudo kill -9 pid

5) Run the script
Code: [Select]
sudo /opt/wlan0.sh
Then try to connect using another wifi device. The SSID wifi PI_AP should be listed as available networks

After that, if all is working, all files must be persisted on tcl and if we want to run at boot add the /opt/wlan0.sh line to the fileboot.sh file.

The only thing I have to check is to avoid the wpa_supplicant to run at boot.
Also I'm not sure about where to place the config directories.

I hope this guide could be useful.
Final note: I haven't added the NAT part, as I don't need to pass through the eth0 interface to have "internet" capability. Anyone that needs such functionality, please refer to the adafruit post.
 
« Last Edit: March 02, 2014, 06:18:06 AM by gpulido »

Offline mcdudeabides

  • Jr. Member
  • **
  • Posts: 60
Re: Connect to WIFI or create ad-hoc dhcp connection as fallback
« Reply #10 on: November 10, 2013, 11:24:50 PM »
A very interesting technique.  I could envision a use where a generic device is booted for first time.  Provided that a supported wifi adapter is in place, it would default to a local "hotspot" for additional configuration.  One could avoid having to attach KVM or to connect to a wired local network to complete configuration.  I wonder if it can still scan for available wifi access points while in hotspot mode?  This should be interesting to exercise.  Thanks for sharing it.

Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14790
Re: Connect to WIFI or create ad-hoc dhcp connection as fallback
« Reply #11 on: November 10, 2013, 11:35:53 PM »
Move the version to a binary directory (I'm not sure here where is the best place on tce for this file)
The tc convention is /usr/local - i.e. /usr/local/bin

Quote
Also I'm not sure about where to place the config directories.
following the above - /usr/local/etc

You can make a personalised extension (tcz) out of the files in bin/ and etc/ and set a start-up script to copy the config files from somewhere like /usr/local/share/mywifi/files to /usr/local/etc if you need them to be editable in future.

Offline gpulido

  • Jr. Member
  • **
  • Posts: 57
Re: Connect to WIFI or create ad-hoc dhcp connection as fallback
« Reply #12 on: November 11, 2013, 12:56:30 PM »


following the above - /usr/local/etc

You can make a personalised extension (tcz) out of the files in bin/ and etc/ and set a start-up script to copy the config files from somewhere like /usr/local/share/mywifi/files to /usr/local/etc if you need them to be editable in future.

Tahnk you for the folder tips, I will update the guide to follow those conventions, what is better? to modify the post or just to add the info on another post?

About the tcz extension, the problem is that most of the config files are just that: config files that should be adapted to each one needs. Nevertheless if anyone found this specific configuration useful I could try to generate the "tcz" extension.


A very interesting technique.  I could envision a use where a generic device is booted for first time.  Provided that a supported wifi adapter is in place, it would default to a local "hotspot" for additional configuration.  One could avoid having to attach KVM or to connect to a wired local network to complete configuration.  I wonder if it can still scan for available wifi access points while in hotspot mode?  This should be interesting to exercise.  Thanks for sharing it.

This is exactly the original problem to solve: How to allow the user to configure the wifi parameters for a embeded wifi-networked device without using ssh or something strange, just plug-and-work.
My next step is to create a little server webpage that is opened on "AP" mode to allow the user to config the ssid and password of the wifi.

On the next post I will expand some improvements that I have been made on the process

Offline gpulido

  • Jr. Member
  • **
  • Posts: 57
Re: Connect to WIFI or create ad-hoc dhcp connection as fallback
« Reply #13 on: November 11, 2013, 01:23:16 PM »
Hello again,

Following my own steps, there still need some steps to make it work.

After the point 5):

6) Run the script at boot

- Add the line
Code: [Select]
/opt/wlan0.sh to the file /opt/bootlocal.sh

- Persist the change
Code: [Select]
filetoolsh -b
- Reboot

UPDATE 03/02/2014 This seems to not be longer needed with the 5.2-ssh version of tinycore.
After this, the hostapd is not going to work, because it relays on the /dev/random file to generate enough entropy to randomess on the WPA. As TCL is runing "clean" on each boot, this entropy cannot be generated.
I have solved it on a "patchy" way (please if anyone has a better solution, I will be glad to test it). I have changed the /dev/random file by /dev/urandom. Add the following BEFORE calling hostapd on the wlan0.sh script

 
Code: [Select]
mv /dev/random /dev/random_old
        ln -s /dev/urandom /dev/random
[/i]
7) Finally the original idea: Try to connect to a given SSID and if not sucess, fallback to create an AP
Putting together all previous concepts, I have just finished using the following script at the init.
It first try to connect to the given interface-ssid-password, and if it is not posible it creates a new network, so the devices could connect to:
Code: [Select]
#!/bin/sh
. /etc/init.d/tc-functions
interface="wlan0"
ssid="YOUR_SSID"
pass="YOUR PASSWORD"

#try to connect to an existing Pi_AP
wpa_passphrase  ${ssid}  ${pass} > /etc/wpa_supplicant.conf
wpa_supplicant -i  ${interface}  -c /etc/wpa_supplicant.conf -B >/dev/null 2>&1
for i in {1..10}
do
        iwconfig ${interface}| grep -q Not-Associated
        if [ "$?" == 0 ]
        then
                sleep 2
        else
                break
        fi     
done
udhcpc -n -i  ${interface} 2>/dev/null

#check if associate
iwconfig ${interface}| grep -q Not-Associated
if [ "$?" == 0 ]; then
        pkill wpa_supplicant
        mv /dev/random /dev/random_old
        ln -s /dev/urandom /dev/random
        ifconfig wlan0 down
        ifconfig wlan0 192.168.42.1 netmask 255.255.255.0 up
        /opt/hostapd /etc/hostapd.conf &
        dhcpd wlan0
fi

The next step is to start a miniserver with just one page of configuration, that allows the user to fill the SSID and password, and reboot the raspi. Also some modifications to the previous script are need to read those data from a file or something like that.
Any suggestions about what webserver use (I need the tiniest  to no overload the system) would be very helpful.

Some drawbacks
I have been try to use the created network instead of a router to create a mesh of devices that use broadcast on the network to comunicate, but it seems that the created network is not capable of handle the broadcast, and I don't know why. Maybe a configuration of the hostapd? Anyone can help me here?

Thank you in advance.

« Last Edit: March 02, 2014, 06:20:04 AM by gpulido »