shrewdgamer
Posts: 52
Joined: Fri Dec 07, 2018 3:32 pm
Location: India

I want to do this, what programming languages and libraries do I need to know?

Thu May 02, 2019 8:28 pm

I want to run a program or script on Raspberry Pi 3B+, which allows the user to set an amount of time for it to connect to WiFi 1, after the expiry of that time it has to connect another WiFi 2 for another amount of time, after the expiry of this, connect to WiFi 1 again, like this it should keep connecting in cycles.

While connected to these WiFis, it should check every few minutes(which I can set) at a site which gives information about the IP address, and if the location of IP shows a particular area's name, it should reboot the router, to obtain a new IP. This should start on Raspbian boot.

What programming languages and knowledge do I need to learn to accomplish the above task.

Thanks for your valuable suggestions.

P.S: Don't tell me to leave the IP as it is and similar suggestions.
I have:
Raspberry Pi 3B+ with official power supply
Sandisk Class 10 SD card with Raspbian Stretch full

ghans
Posts: 7874
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: I want to do this, what programming languages and libraries do I need to know?

Fri May 03, 2019 6:26 am

If you do not know any programming language, Python is a good start. If you actually are fluent in any other programming language, why don't you use it on the Pi ? Chances are it works already.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

tpyo kingg
Posts: 626
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: I want to do this, what programming languages and libraries do I need to know?

Fri May 03, 2019 7:33 am

The simplest would be to do it in POSIX shell script, the next simplest would in Bash shell. Above that, python, perl, or similar would work but be more complex.

shrewdgamer
Posts: 52
Joined: Fri Dec 07, 2018 3:32 pm
Location: India

Re: I want to do this, what programming languages and libraries do I need to know?

Fri May 03, 2019 11:10 am

ghans wrote:
Fri May 03, 2019 6:26 am
If you do not know any programming language, Python is a good start. If you actually are fluent in any other programming language, why don't you use it on the Pi ? Chances are it works already.

ghans
Consider me absolute beginner, even though I might know C, C++ and Java, my knowledge of them is equivalent to knowing the alphabets. I can't figure out how to do what I want with those languages, so I'm asking here. What specific libraries will help me? It also seems to require me to know the Linux system APIs, command line to accomplish this.

Can you point me to specific programming languages which allow me to accomplish that in as short a time as possible?
Last edited by shrewdgamer on Fri May 03, 2019 11:13 am, edited 1 time in total.
I have:
Raspberry Pi 3B+ with official power supply
Sandisk Class 10 SD card with Raspbian Stretch full

shrewdgamer
Posts: 52
Joined: Fri Dec 07, 2018 3:32 pm
Location: India

Re: I want to do this, what programming languages and libraries do I need to know?

Fri May 03, 2019 11:11 am

tpyo kingg wrote:
Fri May 03, 2019 7:33 am
The simplest would be to do it in POSIX shell script, the next simplest would in Bash shell. Above that, python, perl, or similar would work but be more complex.
I'm hearing about POSIX for the first time from your comment. I'm unable to find any resources for POSIX or BASH. Do you really think what I want to accomplish is possible with shell script?
I have:
Raspberry Pi 3B+ with official power supply
Sandisk Class 10 SD card with Raspbian Stretch full

tpyo kingg
Posts: 626
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: I want to do this, what programming languages and libraries do I need to know?

Fri May 03, 2019 11:28 am

You said you want some user input for a time interval and then to alternately connect to two wireless networks. It's very doable in shell. POSIX is the least common denominator variant and the most portable.

Code: Select all

#!/bin/sh
read -p "interval in seconds: " s;
while true;
do
        echo Connecting to network 1
        # connect to wi-fi network 1
        sleep $s;
        echo Connecting to network 2
        # connect to wi-fi network 2
        sleep $s;
done
The connection could be done several ways, one would be WPA Supplicant.

The finished script can be launched in a terminal from a desktop icon if desired.

As for shell resources Wooledge has some good guides, FAQs, and tips:

https://mywiki.wooledge.org/BashGuide
https://mywiki.wooledge.org/BashFAQ
https://mywiki.wooledge.org/BashPitfalls

shrewdgamer
Posts: 52
Joined: Fri Dec 07, 2018 3:32 pm
Location: India

Re: I want to do this, what programming languages and libraries do I need to know?

Fri May 03, 2019 11:41 am

tpyo kingg wrote:
Fri May 03, 2019 11:28 am
You said you want some user input for a time interval and then to alternately connect to two wireless networks. It's very doable in shell. POSIX is the least common denominator variant and the most portable.

Code: Select all

#!/bin/sh
read -p "interval in seconds: " s;
while true;
do
        echo Connecting to network 1
        # connect to wi-fi network 1
        sleep $s;
        echo Connecting to network 2
        # connect to wi-fi network 2
        sleep $s;
done
The connection could be done several ways, one would be WPA Supplicant.

The finished script can be launched in a terminal from a desktop icon if desired.

As for shell resources Wooledge has some good guides, FAQs, and tips:

https://mywiki.wooledge.org/BashGuide
https://mywiki.wooledge.org/BashFAQ
https://mywiki.wooledge.org/BashPitfalls
Thanks for these resources.
I have:
Raspberry Pi 3B+ with official power supply
Sandisk Class 10 SD card with Raspbian Stretch full

Rabbit_Pi
Posts: 23
Joined: Fri Sep 23, 2016 3:03 am

Re: I want to do this, what programming languages and libraries do I need to know?

Fri May 03, 2019 5:14 pm

Ruby is the way to go. Although python is similar, I find it more glitchy and buggy than Ruby.

Ruby also requires less code to do the same thing. It's easy to use and easy to learn.

Python really is over rated, and it's interpreter can be more touchy than Java, c#, etc. Plus Ruby has tons of resources and can be used with any kind of programming language and on any kind of operating system. Meaning the code you write on your pi will work on Windows and Mac without any special cross platform coding.

Ruby on rails is it's web development, and the most popular use of Ruby (Twitter started out as a purely RoR), and without Ruby the "hashtag" would not exist.

Rabbit_Pi
Posts: 23
Joined: Fri Sep 23, 2016 3:03 am

Re: I want to do this, what programming languages and libraries do I need to know?

Fri May 03, 2019 6:01 pm

Now for this project you will also need to read up on the CLI commands, or the bash shell that the kernel uses. With Ruby, you can not just modify the CLI commands, but completely create your own custom commands.

def cpu0_temp
a = proc{`cat /sys/class/thermal/thermal_zone0/temp'.chomp}
b =proc{ a.call / 1000}
b.call
end

This code will call and store the first cpu's temp in the def. The def name can be "giggle_fart" for all Ruby cares, you can name them anything.

The ` ` will run system commands and the .chomp will take off any useless data attached.
Another method used is Ruby's system command.

def cpu0_temp
a = proc{system "cat /sys/class/thermal/thermal_zone0/temp".chomp}
b =proc{ a.call / 1000}
puts " #{b.call} °C")
end

This second code with system works just like the first with the back ticks. However I added another line with puts. The puts(put to string) will display the output and always on a new line. The print command does the same without starting a new line. In this second example, when you call your command it will look as such....

IRB>cpu0_temp
59 °C
Nil
IRB>

This is very useful because now we can pull up the CPU temp anytime we call that def, we can now manipulate the data however we want like so....

3.times do
Time.now
Cpu0_temp
sleep(1)
end

This will output

13:00:00
49°C
13:00:01
48°C
13:00:02
52°c


So how can we use this data to control our pi? Let's say we want the pi to shut itself off if the core reaches dangerous temps. With our cpu0_temp command this is very easy to do.

Let's make a new definition.

def check_cpu0(without the puts code)
loop do
cpu0_temp
sleep (1)
end

This will tell the pi to run our new command once every second and stores it within bthis def. The second part of this will be the if statement

If check_cpu0 == 80°C
`sudo shutdown -h`.chomp
end

Now that we have our script made, we want the pi to run it when it boots, so now we have to edit the .rc_local file.
We want to use the .rc_local file over the .bash_aliases because .rc_local runs anything at BOOT and .bash_aliases starts scripts when the User log in level.

Now we need to add the line
ruby File/path/to/script/pi_overheat_protect.rb &

The & is VERY Important because it tells the pi to run our script as a background process.

It's that easy.

You can use this same concept for your script. It will work the same way.
Last edited by Rabbit_Pi on Fri May 03, 2019 7:45 pm, edited 1 time in total.

epoch1970
Posts: 3780
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: I want to do this, what programming languages and libraries do I need to know?

Fri May 03, 2019 7:00 pm

Shortest path:
- use wpa-cli from bash to control wpa_supplicant,
- create a systemd unit service to start your script,
- provide a systemd config file the user can edit,
- hit a geoip provider from the script, that will return the public IP and the assumed location of the router used by the AP.

I don't understand the "router reboot" part of the application.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

User avatar
B.Goode
Posts: 8820
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: I want to do this, what programming languages and libraries do I need to know?

Sat May 04, 2019 8:18 am

I don't understand the "router reboot" part of the application.


Check prior posts from the same user.

From recollection, something to do with defeating restrictions applied to certain IP addresses in order to gain a competitive edge in some online game?

Return to “General programming discussion”