WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Headless RPi Zero on new networks  (Read 2018 times)

Offline hamak

  • Newbie
  • *
  • Posts: 26
Headless RPi Zero on new networks
« on: February 07, 2019, 10:47:10 AM »
Hello there
Can anyone tell me if it is an easier way to do a headless connection to new wifi networks than this?

I made this today because I want to be able to create a wifi.txt file in the fat32 partition of PiCore
from any PC that contains the wifi SSID PASSWORD WPA, but I do not want
to mount and unmount /mnt/mmcblk0p1 if the network is OK!
This is tested with notepad and works great except for the mount unmount stuff

wifi.txt is the same format as wifi.db
SSID[TAB]PASSWORD[TAB]WPA
No space, only TAB's

this is the /opt/bootlocal.sh
Code: [Select]
mount /dev/mmcblk0p1 /mnt/mmcblk0p1
if [ -f /mnt/mmcblk0p1/wifi.txt ]; then
  cp /mnt/mmcblk0p1/wifi.txt /home/tc/wifi.db
  rm /mnt/mmcblk0p1/wifi.txt
  filetool.sh -b
  sudo reboot
fi
umount /dev/mmcblk0p1 /mnt/mmcblk0p1

I was thinking about checking for a hidden file on /home/tc/ but I think it is a messy code

Code: [Select]
if [ ! -f /home/tc/.wifi ]; then
mount /dev/mmcblk0p1 /mnt/mmcblk0p1
fi
if [ -f /mnt/mmcblk0p1/wifi.txt ]; then
  cp /mnt/mmcblk0p1/wifi.txt /home/tc/wifi.db
  rm /mnt/mmcblk0p1/wifi.txt
  touch /home/tc/.wifi
  filetool.sh -b
  sudo reboot
fi
umount /dev/mmcblk0p1 /mnt/mmcblk0p1

The Critical Problem with this is that the .wifi will exist even if you change networks and then you have a problem! NO SSH!
as you see i still will perform a unmount even if this is not mounted and that is messy...

To find the new PiCore I run arp -a on windows and then ping with broadcast like example: 192.168.1.255 and run arp -a again to see what ip address is newin the list, but if you have ip scanner or something that will work too
« Last Edit: February 07, 2019, 10:51:51 AM by hamak »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11695
Re: Headless RPi Zero on new networks
« Reply #1 on: February 07, 2019, 11:32:29 AM »
Hi hamak
Quote
... as you see i still will perform a unmount even if this is not mounted and that is messy...
You could test if the drive is mounted first. I think this should work:
Code: [Select]
if ! grep -q /mnt/mmcblk0p1 /etc/mtab
then
umount /mnt/mmcblk0p1
fi

/etc/mtab  is a link to  /proc/mounts  which is a list of all currently mounted devices.  grep returns a  0 (zero)  if it finds the
search term. Placing the  !  in front inverts the the return code so the  if  statement becomes  true  when  grep  succeeds.

Offline hamak

  • Newbie
  • *
  • Posts: 26
Re: Headless RPi Zero on new networks
« Reply #2 on: February 07, 2019, 08:43:12 PM »
Yes thank you :-) 
I think that solves one of the problems but I still need to stick with the first script to be able to connect and reconfigure the wifi on the fly.

Thank you Rich!