Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compatibility with campan softmod #95

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
12 changes: 12 additions & 0 deletions disco/uavpal/bin/port_mapping.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

nodemcu_ip="`head -1 /data/ftp/uavpal/conf/nodemcu_ip |tr -d '\r\n' |tr -d '\n'`"

echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -t nat -F PREROUTING
iptables -t nat -F POSTROUTING

iptables -t nat -A PREROUTING -p udp --dport 8888 -j DNAT --to-destination $nodemcu_ip:8888
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination $nodemcu_ip:80
iptables -t nat -A POSTROUTING -d $nodemcu_ip -p tcp --dport 80 -j SNAT --to 192.168.42.1
3 changes: 3 additions & 0 deletions disco/uavpal/bin/uavpal_disco.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,8 @@ if [ ! -d "/data/lib/zerotier-one/networks.d" ]; then
fi
done
fi

/data/ftp/uavpal/bin/port_mapping.sh

ulogger -s -t uavpal_drone "*** idle on LTE ***"
} &
2 changes: 2 additions & 0 deletions disco/uavpal/conf/nodemcu_ip
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
192.168.42.25
# your esp32 or nodemcu ip like 192.168.42.25
4 changes: 2 additions & 2 deletions disco/uavpal/conf/zt_networkid
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
0123456789abcdef
# Enter your ZeroTier Network ID on the first line - this is the same value for drone's zt_networkid and skycontroller2's zt_networkid.
ffffffffffffffff
# Enter your ZeroTier Network ID on the first line - this is the same value for disco's zt_networkid and skycontroller2's zt_networkid.
115 changes: 115 additions & 0 deletions esp32/esp32_wifi_udp_servo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include <WiFi.h>
#include <WiFiUdp.h>
#include <Ticker.h>
#include <Servo.h>

const char* ssid = "DISCO-027361";
const char* password = "";
IPAddress local_IP(192, 168, 42, 25);
IPAddress gateway(192, 168, 42, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional

unsigned int localPort = 8888; // local port to listen on
#define UDP_TX_PACKET_MAX_SIZE 64
uint8_t packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
WiFiUDP Udp;

int ledState = LOW;
unsigned long previousMillis = 0;
const long blinkInterval = 1000;

Servo servo1;
const int servo1Pin = 16;
const int servo1Open = 95;
const int servo1Close = 0;

Ticker discoTicker;
int discoCount = 0;
#define ACTION_TIME 1

void setup()
{
Serial.begin(115200);

if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}

Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("ESP Mac Address: ");
Serial.println(WiFi.macAddress());
Serial.print("Subnet Mask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway IP: ");
Serial.println(WiFi.gatewayIP());
Serial.print("DNS: ");
Serial.println(WiFi.dnsIP());

if(Udp.begin(localPort)) {
Serial.print("UDP Listening on IP: ");
Serial.println(WiFi.localIP());
}

servo1.attach(servo1Pin);
servo1.write(servo1Close);

pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
}

void servo1Action(bool action) {
if(action){
servo1.write(servo1Open);
//digitalWrite(LED_BUILTIN, LOW);
}
else{
servo1.write(servo1Close);
//digitalWrite(LED_BUILTIN, HIGH);
}
}

void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= blinkInterval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH; // Note that this switches the LED *off*
} else {
ledState = LOW; // Note that this switches the LED *on*
}
digitalWrite(LED_BUILTIN, ledState);
}

int packetSize = Udp.parsePacket(); // if there's data available, read a packet
if (packetSize) {
discoCount = 0;
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.print("Udp.read: ");
Serial.println((char *)packetBuffer);
if(packetBuffer[0]=='1'){
if(packetBuffer[1]=='1')
servo1Action(true);
else if(packetBuffer[1]=='0')
servo1Action(false);
}
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(*packetBuffer); // send a reply, to the IP address and port that sent us the packet we received
Udp.endPacket();
}
}

Binary file added image/IMG_20200406_112349.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/IMG_20200406_114310.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/IMG_20200406_114418.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/IMG_20200406_114452.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/VID_20200406_113737_0_COMPRESSED.mp4
Binary file not shown.
Binary file added skycontroller2/uavpal/bin/netcat-arm
Binary file not shown.
4 changes: 4 additions & 0 deletions skycontroller2/uavpal/bin/servo1-timmer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
sleep $1
killall servo1.sh
/data/lib/ftp/uavpal/bin/servo1.sh $2 $3 &
47 changes: 47 additions & 0 deletions skycontroller2/uavpal/bin/servo1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/sh
is_substring(){
case "$2" in
*$1*) return 0;;
*) return 1;;
esac
}

servo_state=$1
press_seconds=2
input_dev_settings="/dev/input/event0"
nodemcu_ip="`head -1 /data/lib/ftp/uavpal/conf/nodemcu_ip |tr -d '\r\n' |tr -d '\n'`"
if [ $2 == "lte" ]; then
nodemcu_ip="192.168.42.1"
fi

while true; do
keystep=0
evtest ${input_dev_settings} | while read line; do
if [ $keystep == 0 ]; then
if is_substring "type 1 (EV_KEY), code 293 (BTN_PINKIE), value 1" "$line"; then
killall servo1-timmer.sh
/data/lib/ftp/uavpal/bin/servo1-timmer.sh $press_seconds $servo_state $2 &
servo1_button_timestamp1=$(date "+%s")
keystep=1
fi
else
if is_substring "type 1 (EV_KEY), code 292 (BTN_TOP2), value 0" "$line"; then
servo1_button_timestamp2=$(date "+%s")
if [ $(($servo1_button_timestamp2-$servo1_button_timestamp1)) -le $press_seconds ]; then
if [ $servo_state == 0 ]; then
echo "11" | ./data/lib/ftp/uavpal/bin/netcat-arm -u ${nodemcu_ip} 8888 -w1&
servo_state=1
else
echo "10" | ./data/lib/ftp/uavpal/bin/netcat-arm -u ${nodemcu_ip} 8888 -w1&
servo_state=0
fi
killall servo1-timmer.sh
keystep=0
ulogger -s -t uavpal_sc2 "... servo1 button press event detected"
fi

fi
fi
done
done

39 changes: 24 additions & 15 deletions skycontroller2/uavpal/bin/uavpal_sc2.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh

# functions
first=1

change_led_color()
{
case "$1" in
Expand Down Expand Up @@ -86,12 +87,6 @@ power_btn_listener()
fi
}

conf_read()
{
result=$(head -1 /data/lib/ftp/uavpal/conf/${1})
echo "$result" |tr -d '\r\n' |tr -d '\n'
}

main()
{
# variables
Expand Down Expand Up @@ -134,6 +129,10 @@ main()
else
input_dev_settings="/dev/input/event1"
fi

killall servo1.sh
/data/lib/ftp/uavpal/bin/servo1.sh 0 wifi &

while true; do
# wait for Settings button event
while true; do
Expand Down Expand Up @@ -182,6 +181,9 @@ switch_to_lte()
if [ "$platform" == "mpp" ]; then
ulogger -s -t uavpal_sc2 "... pausing process wifid (mpp platform only)"
killall -SIGSTOP wifid
fi

if [ "$platform" == "mpp" ]; then
ulogger -s -t uavpal_sc2 "... launching process wifid-uavpal in the background (mpp platform only)"
WIFID_DRIVER=bcmdriver /usr/bin/wifid --mode STA --ip 192.168.42.3 --suffix uavpal &
wifid_suffix="--suffix uavpal"
Expand All @@ -190,8 +192,8 @@ switch_to_lte()
fi

for p in `seq 1 $wifi_connection_attempts`; do
ulogger -s -t uavpal_sc2 "... connecting to mobile Wi-Fi hotspot \"$(conf_read ssid)\" (try $p of $wifi_connection_attempts)"
wifid-cli ${wifid_suffix} connect "$(conf_read ssid)" 0 "$(conf_read wpa)"
ulogger -s -t uavpal_sc2 "... connecting to mobile Wi-Fi hotspot (try $p of $wifi_connection_attempts)"
wifid-cli ${wifid_suffix} connect "`head -1 /data/lib/ftp/uavpal/conf/ssid |tr -d '\r\n' |tr -d '\n'`" 0 "`head -1 /data/lib/ftp/uavpal/conf/wpa |tr -d '\r\n' |tr -d '\n'`"

for q in `seq 1 $wifi_connection_timeout_seconds`; do
wifi_connection_status=`wifid-cli ${wifid_suffix} status 2>&1 |grep state |awk '{print $3}'`
Expand Down Expand Up @@ -245,7 +247,7 @@ switch_to_lte()
kill -9 `ps |grep wifid |grep suffix |awk '{print $1}'`
fi

if [ -d "/data/lib/zerotier-one/networks.d" ] && [ ! -f "/data/lib/zerotier-one/networks.d/$(conf_read zt_networkid).conf" ]; then
if [ -d "/data/lib/zerotier-one/networks.d" ] && [ ! -f "/data/lib/zerotier-one/networks.d/$(head -1 /data/lib/ftp/uavpal/conf/zt_networkid |tr -d '\r\n' |tr -d '\n').conf" ]; then
ulogger -s -t uavpal_sc2 "... zerotier config's network ID does not match zt_networkid config - removing zerotier data directory to allow join of new network ID"
rm -rf /data/lib/zerotier-one 2>/dev/null
mkdir -p /data/lib/zerotier-one
Expand All @@ -256,15 +258,15 @@ switch_to_lte()
/data/lib/ftp/uavpal/bin/zerotier-one -d 2>/tmp/zerotier-one_err

if [ ! -d "/data/lib/zerotier-one/networks.d" ]; then
ulogger -s -t uavpal_sc2 "... (initial-)joining zerotier network ID $(conf_read zt_networkid)"
ulogger -s -t uavpal_sc2 "... (initial-)joining zerotier network ID"
while true
do
ztjoin_response=`/data/lib/ftp/uavpal/bin/zerotier-one -q join $(conf_read zt_networkid)`
ztjoin_response=`/data/lib/ftp/uavpal/bin/zerotier-one -q join $(head -1 /data/lib/ftp/uavpal/conf/zt_networkid |tr -d '\r\n' |tr -d '\n')`
if [ "`echo $ztjoin_response |head -n1 |awk '{print $1}')`" == "200" ]; then
ulogger -s -t uavpal_sc2 "... successfully joined zerotier network ID $(conf_read zt_networkid)"
ulogger -s -t uavpal_sc2 "... successfully joined zerotier network ID"
break # break out of loop
else
ulogger -s -t uavpal_sc2 "... ERROR joining zerotier network ID $(conf_read zt_networkid): $ztjoin_response - trying again"
ulogger -s -t uavpal_sc2 "... ERROR joining zerotier network ID: $ztjoin_response - trying again"
sleep 1
fi
done
Expand Down Expand Up @@ -321,10 +323,13 @@ switch_to_lte()

kill9_pid_tree $pow_pid
pow_pid=0

ulogger -s -t uavpal_sc2 "... resuming process mppd"
killall -SIGCONT mppd

killall servo1.sh
/data/lib/ftp/uavpal/bin/servo1.sh 0 lte &

ulogger -s -t uavpal_sc2 "*** idle on LTE ***"
}

Expand All @@ -351,6 +356,10 @@ switch_to_wifi()
ulogger -s -t uavpal_sc2 "... terminating processes required for LTE"
killall zerotier-one
killall udhcpc

killall servo1.sh
/data/lib/ftp/uavpal/bin/servo1.sh 0 wifi &

ulogger -s -t uavpal_sc2 "*** idle on Wi-Fi (or at least trying to connect) ***"
}

Expand Down
Binary file modified skycontroller2/uavpal/bin/zerotier-one
Binary file not shown.
2 changes: 2 additions & 0 deletions skycontroller2/uavpal/conf/nodemcu_ip
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
192.168.42.25
# your esp32 or nodemcu ip like 192.168.42.25
2 changes: 1 addition & 1 deletion skycontroller2/uavpal/conf/ssid
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
my-wifi-ssid-name
xxxxxx
# Enter your mobile hotspot's name (SSID) on the first line. This is required for Skycontroller 2 to connect to the mobile hotspot via Wi-Fi.
2 changes: 1 addition & 1 deletion skycontroller2/uavpal/conf/wpa
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
my-wifi-passphrase
xxxxxx
# Enter your mobile hotspot's Wi-Fi password (WPA Key) on the first line. This is required for Skycontroller 2 to connect to the mobile hotspot via Wi-Fi.
4 changes: 2 additions & 2 deletions skycontroller2/uavpal/conf/zt_networkid
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
0123456789abcdef
# Enter your ZeroTier Network ID on the first line - this is the same value for drone's zt_networkid and skycontroller2's zt_networkid.
ffffffffffffffff
# Enter your ZeroTier Network ID on the first line - this is the same value for disco's zt_networkid and skycontroller2's zt_networkid.
1 change: 1 addition & 0 deletions 糖豆巴布是js小人
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@