Created
June 15, 2012 01:14
-
-
Save twaddington/2934065 to your computer and use it in GitHub Desktop.
Geoloqi Python UDP Client Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
A Geoloqi socket client demo. | |
:author: Tristan Waddington | |
""" | |
import sys | |
import time | |
import socket | |
from struct import pack, unpack | |
HOST = 'localhost' | |
PORT = 43333 | |
def main(): | |
""" | |
Date (*nix), Lat, Lng, Speed, Heading, Altitude, Accuracy, Battery, UUID | |
Encode Latitude (((latitude + 90) / 180) * 4294967296) | |
Encode Longitude (((longitude + 180) / 360) * 4294967296) | |
Python | |
H = unsigned short | |
I = unsigned int | |
The first character '!' indicates network (= big-endian) should be used. | |
""" | |
packet = '\x41' | |
# Date | |
packet += pack('!I', time.time()) | |
# Lat | |
packet += pack('!I', ((45.5246 + 90) / 180) * 4294967296) | |
# Lng | |
packet += pack('!I', ((-122.6843 + 180) / 360) * 4294967296) | |
# Speed | |
packet += pack('!H', 20) | |
# Heading | |
packet += pack('!H', 190) | |
# Altitude | |
packet += pack('!H', 1320) | |
# Accuracy | |
packet += pack('!H', 28) | |
# Battery | |
packet += pack('!H', 13) | |
# UUID | |
packet += '\x12\x34\x56\x78\x90\xAB\xCD\xEF\x12\x34\x56\x78\x90\xAB\xCD\xEF' | |
print 'Sending %s [%s]' % (packet, len(packet)) | |
# Initialize our socket connection | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.connect((HOST,PORT)) | |
# Send our packet | |
sock.sendall(packet) | |
# Get any response | |
data = sock.recv(1024) | |
# Close our socket | |
sock.close() | |
print 'Response: %s' % unpack('!I', data) | |
if __name__ == "__main__": | |
try: | |
sys.exit(main()) | |
except KeyboardInterrupt: | |
print "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment