Skip to content

Instantly share code, notes, and snippets.

@matael
Created February 5, 2013 20:38
Show Gist options
  • Select an option

  • Save matael/4717446 to your computer and use it in GitHub Desktop.

Select an option

Save matael/4717446 to your computer and use it in GitHub Desktop.
Again
#!/usr/bin/env python
#-*- coding: utf8 -*-
import sys
import os
import serial
import time
from json import loads
from urllib.request import urlopen
def main():
api_key = '5H7IGDGF78UQAOF'
base_url = 'http://data.nantes.fr/api/'
command = 'getDisponibiliteParkingsPublics/1.0/'
format = '/?output=json'
# recreate full url
full_url = '{}{}{}{}'.format(
base_url,
command,
api_key,
format
)
s = serial.Serial('/dev/ttyACM2', 9600)
# polling loop
while 1:
# get the data :
data_handle = urlopen(full_url)
data = loads(data_handle.read().decode())
# handle eventual errors
if data['opendata']['answer']['status']['@attributes']['code'] != '0':
print('A error occured :\n\n{}'.format(
data['opendata']['answer']['status']['@attributes']['code']
))
sys.exit(1)
# shorten a bit this fucking hash-ception
parkings = data['opendata']['answer']['data']['Groupes_Parking']['Groupe_Parking']
# get num of opened parking
nb_total = len(parkings)
count_closed = 0
names_closed = []
count_full = 0
names_full = []
for p in parkings:
if p['Grp_exploitation'] == '0':
count_closed += 1
names_closed.append(p['Grp_nom'])
else:
if p['Grp_disponible'] == '0':
count_full += 1
names_full.append(p['Grp_nom'])
# print all that stuff
print('\n')
print('{} parking(s) ouvert(s) (sur {})'.format(nb_total-count_closed,nb_total))
if count_closed:
print('Parking(s) fermé(s) : ')
for i in names_closed:
print('\t- {}'.format(i))
print('\n')
print('{} parking(s) plein(s)'.format(count_full))
if count_full:
print('Parking(s) plein(s) : ')
for i in names_full:
print('\t- {}'.format(i))
print('\n')
s.write(chr(nb_total).encode("utf8"))
s.write(chr(count_closed).encode("utf8"))
s.write(chr(count_full).encode("utf8"))
time.sleep(7*60)
if __name__=='__main__': main()
/*
* File: report.ino
* Author : Mathieu (matael) Gaborit
* Year : 2012
* Licence : WTFPL
* Licence Terms :
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// init serial connection
Serial.begin(9600);
// init LCD
lcd.begin(40,2);
pinMode(13, OUTPUT);
}
void loop() {
if (Serial.available() >= 3) {
int nb_total = Serial.read();
int count_closed = Serial.read();
int count_full = Serial.read();
if (!nb_total) digitalWrite(13, HIGH);
lcd.clear();
lcd.setCursor(1,0);
int i;
char ouvert[40];
char plein[40];
for (i=0; i <40 ; i++) {
ouvert[i] = ' ';
plein[i] = ' ';
}
sprintf(ouvert, " Parkings Ouverts : %d/%d", (nb_total-count_closed), nb_total);
sprintf(plein, "Parking(s) Plein(s) : %d/%d", count_full, nb_total);
lcd.print(ouvert);
lcd.setCursor(0,1); // second row
lcd.print(plein);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment