CometMeWindLoggingProject

= Synopsys = This project started as a means to copy data from a sd accessible device to the interwebs.

The sd carrying device (SCD) logs data every set amount of seconds about wind direction, power, and battery voltage. The problem was getting that data from the SCD to a website.

Flashair->OpenWRT->3G->Website>

= rsync for flashair = openwrt on wrt701 connected to flashair, with 3g modem connected to internet


 * sd & wireless card combo (wifisd, flashair)
 * http server on port 80, with whole filesystem contents.
 * Files are stored at: SD CARD/CSVFILES/LOG
 * 192.168.0.1/24
 * (development on ethernet, 192.168.1.1/24)
 * periodically connect to a remote server via ssh

Flows:
 * communication:
 * flashair -> openwrt wr703n w/ 3g modem -> host-on-cloud
 * communication paths
 * logger [sd slot]-> flashair sd card [flashair wifi]<-> openwrt [usb socket]-> usb extension cable 10m (hub + 2x5m) [usb socket]-> 3g modem [gsm]->[internet]->[ssh]-> host on cloud
 * physical
 * logger
 * flashair sd card
 * TL-WR703N
 * power adapter
 * usb extension 5 m
 * hub
 * usb extension 5 m
 * 3g modem

Flashair
We use class 10 card, 16 GB.
 * http://www.toshiba-components.com/FlashAir/
 * https://www.flashair-developers.com/en/about/overview/

= Setup of a new TL-WR703N router with openwrt preinstalled =
 * 1) Start with an empty SLboat_Mod TP-LINK TL-WR703N
 * 2) connect ethernet to laptop, laptop uses dhcp
 * 3) remove ~/.ssh/known_hosts offending lines for ssh access
 * 4) ssh into it. default password is ? by default it isn't set. set it and add ssh pub key via: http://192.168.1.1/cgi-bin/luci/;stok=050e6ecb7ab80178f16ec1b9aa83b690/admin/system/admin (just an example, stok will be different)
 * 5) setup networking via host
 * 6) If host is connected via wireless, and in the hackerspace, and host has IP of 192.168.1.239:
 * 7) host: masquerade_via_wlp3s0 (see below)
 * 8) host: sudo iptables -F (fixme)
 * 9) wrt: route add default gw 192.168.1.239
 * 10) wrt: echo nameserver 10.81.2.1 > /etc/resolv.conf
 * 11) change hostname to melogger: admin/system/system
 * 12) set timezone to Beirut: admin/system/system
 * 13) opkg update
 * 14) opkg install packages for gsm and lua:
 * 15) opkg remove usb-uhci
 * 16) opkg install usb-modeswitch usb-modeswitch-data comgt kmod-usb-serial kmod-usb-serial-option kmod-usb-acm luci-proto-3g luasocket luaposix
 * 17) opkg install usb-ohci kmod-usb-serial-wwan
 * 18) reboot. perhaps not required, but I had problems with uhci/ohci or this. Need to redo instructions to test this point.
 * 19) setup flashair card wireless as client
 * 20) use wizard: scan, choose flashair, use password. called wwan (the default)
 * 21) go to interfaces, select wwan, advanced, disable both "Use default gateway" and "Use DNS servers advertised by peer".
 * 22) connect gsm dongle, insert sim
 * 23) setup gsm networking
 * 24) create new interface called umts, for cellcom APN is internetg, the rest is empty (no username, no password)
 * 25) move it to "wan" zone
 * 26) open ssh on wan zone by adding the following in /etc/config/firewall before the final 'include' line:
 * 27) config rule
 * 28)         option name  'accept ssh on wan'
 * 29)         option src              wan
 * 30)         option dest_port        22
 * 31)         option target           ACCEPT
 * 32)         option proto            tcp
 * 33) mkdir /root/.ssh
 * 34) chmod 700 /root/.ssh
 * 35) dropbearkey -f /root/.ssh/id_rsa -t rsa
 * 36) copy over the public key to cometme server at /.ssh/authorized_keys
 * 37) test that you can ssh to cometme: ssh -l user -i /root/.ssh/id_rsa server
 * 38) host: git clone git://gitorious.org/air-sd-logger/air-sd-logger.git
 * 39) host: cd air-sd-logger
 * 40) host: make update (assumes ip of router is 192.168.1.1)
 * 41) copies over main executable to /usr/bin/syncsdtocomet and config file to /root/config.lua
 * 42) test: run executable locally once.
 * 43) syncsdtocomet /root/config.lua
 * 44) setup cron job:
 * 45) echo '30 0 * * * /usr/bin/syncsdtocomet /root/config.lua > /root/syncsdtocomet.last.log' > /etc/crontabs/root
 * 46) /etc/init.d/cron start
 * 47) verify via logread that no parsing errors occured in /etc/crontabs/root
 * 48) verify symlink in /etc/rc.d
 * 49) test cron job (check the output, setup time close to it, wait)
 * 50) date 00:29
 * 51) logread -f
 * 52) wait 60 seconds
 * 53) ^C
 * 54) you should see a line: Nov 12 00:30:01 melogger cron.info crond[2727]: crond: USER root pid 2762 cmd /usr/bin/syncsdtocomet /root/config.lua > /root/syncsdtocomet.last.log
 * 55) verify everything is running after a reboot
 * 56) verify crond is running, there is an internet connection, and an sd card connection.
 * 57) verify everything is running without ethernet connection
 * 58) remove ethernet cable.
 * 59) reboot forcibly (remove power).
 * 60) wait 60 seconds.
 * 61) verify: modem is blinking green (ZTE 190), wrt led is solid blue.
 * 62) setup dynamic dns
 * 63) create an account at duckdns.org
 * 64) follow wrt instructions
 * 65) opkg install ddns-scripts luci-app-ddns
 * 66) update configuration via luci: services->dynamic dns. The config file:
 * 67) config service 'duckdns'
 * 68)        option enabled '1'
 * 69)        option domain ' '
 * 70)        option username 'NA'
 * 71)        option password ' '
 * 72)        option force_interval '72'
 * 73)        option force_unit 'hours'
 * 74)        option check_interval '10'
 * 75)        option check_unit 'minutes'
 * 76)        option update_url 'http://www.duckdns.org/update?domains=[DOMAIN]&token=[PASSWORD]&ip=[IP]'
 * 77)        option interface 'umts'
 * 78)        option ip_source 'interface'
 * 79)        option ip_interface '3g-umts'
 * 80) setup constant ip for cellcom:
 * 81) change APN to statreal in /etc/config/network or via luci

Helper: Masquerade script
if [ $UID != 0 ]; then exec sudo $0 fi BASE=`basename $0` TARGET=${BASE#masquerade_via_} echo 1 > /proc/sys/net/ipv4/ip_forward echo masquerading anything to $TARGET iptables -t nat -F iptables -t nat -A POSTROUTING -o $TARGET -j MASQUERADE
 * 1) !/bin/bash
 * 2) symlink me to "masquerade_via_INTERFACE", i.e. masquerade_via_em1 if you are connected to ethernet, masquerade_via_wlp3s0 if wlp3s0 is your wireless interface name.

Troubleshooting

 * Modem should blink green when connected. It's two faulty modes are:
 * Solid Red. That means the sim didn't register. It is a sim problem, not an openwrt one. Try a different sim. Try sim in phone. Contact gsm provider if nothing works and you can reproduce in a phone.
 * Solid Green. Sim is registered but not in data mode. This is an openwrt configuration error. Try rebooting. Then use logread to look for ppd, or ps. Use ubus call network.interface.umts status for more details. Try removing usb-uhci and installing usb-uhci, and rebooting after.

= Notes =

Dropbear
dropbear (minimal disk space implementation):
 * dropbearkey
 * https://yorkspace.wordpress.com/2009/04/08/using-public-keys-with-dropbear-ssh-client/
 * comet-me server is hosted weirdly, the root is at /, so you need to place the public keys in /.ssh/authorized_keys (permissions 700 for directory, 600 for file)

Cronjob
Copying is done 1 per minute with a cronjob


 * * * * * /root/pushlog

Wireless connection as client on openwrt
The flashair client wireless connection was created with the "wizard" from the scanned access points and then manually set to
 * 1) ignore routes
 * 2) ignore dns

This is important since it takes precedence during name resolution since it appears *last*, i.e. the reverse of the usual name resolution order (as by glibc). This is a dnsmasq-ism. The actual resolv.conf is in /tmp/resolv.conf.auto I believe (writing from memory)

dropbear scp client doesn't have an option to set the user name, and since the user name contains an at sign (@) I used cat (8 bit clean when ssh is used without a pty, i.e. running a command, at least I hope - only tested text files..)

Secondly, ssh of dropbear needs an explicit identity to do public key authentication. The whole /root/pushlog is:

wget -o - -q http://192.168.0.1/CSVFILES/LOG | ssh -i ~/.ssh/id_rsa -l user@name example.com "cat > $HOME/LOG"
 * 1) /bin/sh

= GDM/3G Modem with Antenna = One of the requirements is to have the modem in a cave, and the antenna external. One solution would be a long usb cable, but it looks like it would be less robust to random errors, so we are going with a long antenna.

ZTE 190MF
That's what we currently use, supported well with openwrt.

Huawei 3131
Relatively new (2012), at Huawei

Final cart:
 * 15ft RG58 External Antenna Extension Coax RF Jumper Cable FME male to FME female
 * 5dbi 900MHz GSM Antenna cable with FME female magnetic
 * FME plug TO TS9 right angle connector with 15cm cable antenna HUAWEI ZTE adapter pigtail cable customize free shipping
 * plug == Male (pin in the middle).

Purchase links:
 * Modem@aliexpress
 * Antenna@amazon
 * Adapter Cable for external antenna (contradicts antenna above)
 * CRC9 to FME-male Right-Angle Patch Cable

= Reference = SMA, RPSMA
 * http://www.amphenolrf.com/products/CatalogPages/SMA.pdf
 * https://en.wikipedia.org/wiki/SMA_connector
 * https://www.sparkfun.com/pages/RF_Conn_Guide

CRC9, TS9 - TODO

FME
 * https://en.wikipedia.org/wiki/FME_connector

= SD Carc with Wifi = This is hopefully identical to what Noam has:
 * 4GB SD (Secure Digital) Eye-Fi card - Includes Wifi module that allows wireless data transfer to any wifi-enabled device - Easy setup and photo sharing with included software - Automatic file upload frees memory card space - Device compatibility with SDHC format - Included USB adapter - Class 4 Speed rating
 * 206 nis in plonter

= openwrt setup = TargetVendor= 0x12d1 TargetProduct= 0x14db MessageContent="55534243123456780000000000000011062000000100000000000000000000"
 * starting from OpenWrt_SLboat_Mod - second attempt. Thanks Yair!
 * wifi left for last, first setup usb gsm dongle
 * adding ssh key via system->administration menu item (/etc/config/somewhere FILLME)
 * using Huawei 3131
 * get working internet. since wifi is off got it via masquerade from laptop.
 * Installing extra packages per http://wiki.openwrt.org/doc/recipes/3gdongle
 * cannot see device (/sys/kernel/debug/usb/devices shows the device with (None) driver)
 * installing kmod-usb-net-cdc-ether (saw fedora use it)
 * installed modeswitch (needed too according to fedora)
 * Created /etc/usb_modeswitch.d/12d1:14db (copied from 14d1)
 * 1) Huawei E3131

= 3g (umts) setup =
 * after looking at everything of huawei it seems the MessageContent is almost always the same.
 * APN for rami levy: http://www.unlockit.co.nz/mobilesettings/settings.php?id=698
 * APN/user/pass: internet.rl / 	rl@3g / rl
 * unneeded: MMC 425 / MNC 03


 * cdc_ether works, but now it isn't appropriate for all the gcom (serial) oriented howto's.
 * ifconfig -a shows eth1 with hw addr (static?) 58:2C:80:13:92:63
 * attempting to do ifconfig up does a reboot. not a good sign. maybe reuse my old stick for now.

switched back to cdc_acm using modem (ZTE190M ?) Sep 8 18:02:16 OpenWrt_SLboat_Mod local2.info chat[19013]: AT+CGDCONT=1,"IP","internet.rl"^M^M Sep 8 18:02:16 OpenWrt_SLboat_Mod local2.info chat[19013]: OK Sep  8 18:02:16 OpenWrt_SLboat_Mod local2.info chat[19013]:  -- got it Sep  8 18:02:16 OpenWrt_SLboat_Mod local2.info chat[19013]: send (ATD*99***1#^M) Sep 8 18:02:16 OpenWrt_SLboat_Mod local2.info chat[19013]: expect (CONNECT) Sep 8 18:02:16 OpenWrt_SLboat_Mod local2.info chat[19013]: ^M Sep 8 18:02:16 OpenWrt_SLboat_Mod local2.info chat[19013]: ATD*99***1#^M^M Sep 8 18:02:16 OpenWrt_SLboat_Mod local2.info chat[19013]: ERROR
 * logread for log debugging. Showing failure:

This was fixed by seemingly removing and reinserting the dongle. So it had some state which I don't know how to query or reset yet. If I can figure it out I can add it to the chat script. In the debugging process verified it works on my main machine by "systemctl stop ModemManager; systemctl stop NetworkManager"; remove and reinsert dongle to my machine. use minicom to isue the above commands: one that isn't shown, and AT+CGDCONT=1,"IP","internet.rl" plus ATD*99***1# and got CONNECT 7200000 (maybe an extra zero there).

= Networking setup on the OpenWRT =

Two zones:
 * lan: contains umts + lan (yes, lan is the zone label and a network label)
 * wan: actually wrongly named, this contains just wwan (again wrongly named), the wireless flashair (Client mode) connection.

Zone firewall config - the masquerade is for using the internet connection (since I didn't have two) and can be turned off, but since it requires a lan connection to be effective it doesn't pose a security problem, so better leave it for convenience when debugging.
 * lan: Input accept, Output accept, Forward accept, Masquerade on, MSS clamping off.
 * wan: Input reject, Output accept, Forward reject, Masquerade off, MSS clamping off.

Networks: we have three, one bridge (pointless but there), pay attention:
 * LAN: bridge is br-lan, it contains the ethernet interface (eth0) only.
 * UMTS: contains 3g-umts.
 * WWAN: contains Client flashair. The client was created via the "scan" button on the interface.

A quick way to create all of these is just to ignore the above and dump the following files inside /etc/config:

/etc/config/network: config interface 'loopback' option ifname 'lo' option proto 'static' option ipaddr '127.0.0.1' option netmask '255.0.0.0' config interface 'lan' option ifname 'eth0' option type 'bridge' option proto 'static' option ipaddr '192.168.1.1' option netmask '255.255.255.0' config interface 'umts' option proto '3g' option apn 'internet.rl' option username 'rl@3g' option password 'rl' option device '/dev/ttyUSB2' option service 'umts' config interface 'wwan' option proto 'dhcp' option defaultroute '0' option peerdns '0'

Notice ttyUSB2! the modem I'm using comes up with 3 serial interfaces after usb modeswitch finishes turning it from a storage device to a modem, and only the third is usable as a modem (maybe the others are for firmware upload or a non AT interface or debugging).

/etc/config/wireless:

config wifi-device 'radio0' option type 'mac80211' option hwmode '11ng' option macaddr '8c:21:0a:ee:f4:50' option htmode 'HT20' list ht_capab 'SHORT-GI-20' list ht_capab 'SHORT-GI-40' list ht_capab 'RX-STBC1' list ht_capab 'DSSS_CCK-40' option txpower '27' option country 'US' option disabled '0' option channel '6' config wifi-iface option network 'wwan' option ssid 'flashair' option encryption 'psk2' option device 'radio0' option mode 'sta' option bssid 'E8:E0:B7:18:8B:DF' option key ' '
 * 1) cat /etc/config/wireless

= Copying things over from SD to an ssh server somewhere = I would really like to use rsync, but for that I would need to have a sort of http filesystem. Barring that, poor man's rsync:
 * generate a list of files on card, and their dates in memory
 * for each file, record last time it was synced (/root/sdsync/file_name is an ascii file containing that time - or just touch it with that time, even better)
 * if that is older then current date of file then sync.
 * to optimize sync, do it in one go by using something like uuencode: (for file in files; uuencode file -) | ssh uudecode_all_files

Helpers
wget -O - -q http://192.168.0.1/CSVFILES/LOG | grep -e '^wlansd\[' | sed -e 's/wlansd\[[0-9]*\]="//' | sed -e 's/";//'
 * 1) cat /usr/bin/listsd
 * 2) !/bin/sh

Lua

 * splitting and joining strings: http://lua-users.org/wiki/SplitJoin
 * popen implementation that doesn't block, maybe:
 * serialization to lua: http://www.lua.org/pil/12.1.1.html
 * using load(function) for lua 5.1 compatibility (as opposed to load(string)

Sync SD to Comet
https://gitorious.org/air-sd-logger/air-sd-logger/

= Visualization = D3: http://tonygarcia.me/slides/d3chartintro/

Or maybe python: https://github.com/ContinuumIO/Bokeh Also see this completely unrelated post on python becoming the defacto language for scientific data processing