Skip to content

Instantly share code, notes, and snippets.

@sorz
sorz / MMA7455L.py
Last active Dec 17, 2015
Read acceleration from MMA7455L via I2C by Python.
View MMA7455L.py
#!/usr/bin/env python
import smbus
import time
class Accel():
def __init__(self, dev=1, g=2):
self.bus = smbus.SMBus(dev)
self.set_range(g)
@sorz
sorz / DHT11.ino
Created May 11, 2013
DHT11 on Arduino. Send temperature and humidity by serial.
View DHT11.ino
#define DHT11_PIN 2
void setup() {
pinMode(DHT11_PIN, INPUT_PULLUP);
Serial.begin(115200, SERIAL_8E1);
}
void loop() {
if (Serial.available() > 0) {
byte in = Serial.read();
@sorz
sorz / gfwlist2regex.py
Last active Jul 13, 2021
Download and convert GFWList to url regex which compatible with Squid.
View gfwlist2regex.py
#!/usr/bin/env python
#encoding: utf-8
import urllib2
from base64 import b64decode
LIST_URL = 'https://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt'
BLACK_FILE = 'gfw.url_regex.lst'
WHITE_FILE = 'cn.url_regex.lst'
@sorz
sorz / hw_smsd.py
Last active Dec 23, 2016
Forward SMS to E-mail via Huawei datacard. See https://blog.sorz.org/p/sms2email/ for detial.
View hw_smsd.py
#!/usr/bin/env python
#encoding: utf-8
# Copyright (C) 2013 @XiErCh
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
@sorz
sorz / DHT22.ino
Created Jul 19, 2013
DHT22 on Arduino. Send temperature and humidity via serial.
View DHT22.ino
/*
* Author: @xierch
* License: MIT
* Known Issues: can't throw timeout exception if DHT22 broken.
*/
#define DHT22_PIN 2
void setup() {
pinMode(DHT22_PIN, INPUT_PULLUP);
@sorz
sorz / DS1307.py
Last active Sep 5, 2019
Read and wite datetime on DS1307 via I2C by Python.
View DS1307.py
#!/usr/bin/env python
#encoding: utf-8
# Copyright (C) 2013 @XiErCh
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
@sorz
sorz / flash-led.sh
Created Sep 4, 2013
Making the LED on router flash with traffic. http://sorz.org/flashled/
View flash-led.sh
#!/bin/sh
#set -x
export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
IFNAME="eth0"
FULLSPEED=1200 # KiB/s
LED='/sys/class/leds/tp-link:blue:system/brightness'
while [ True ]
do
@sorz
sorz / sendudp.py
Created Sep 28, 2013
Send UDP packets from any custom port (using libnet). Used for UDP hole punching on linux servers (when the local UDP port is being used).
View sendudp.py
#!/usr/bin/env python
#encoding: utf-8
import libnet
from libnet.constants import RAW4, RESOLVE, IPV4_H, UDP_H, IPPROTO_UDP
IFACE = 'wlan2' # Sending via the interface.
def sendto(sport, address):
l = libnet.context(RAW4, IFACE)
View getportd.py
#!/usr/bin/python
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('0.0.0.0', 6000))
while True:
data, addr = s.recvfrom(1024)
s.sendto(str(addr[1]), addr)
View matchFloat.js
function matchFloat(s) {
f = s.replace(/[^\d]+/g, '');
g = s.split(/\d/);
if (g.length == 0)
return f;
else if (g[0].contains('-'))
f = '-' + f;
for (var i=0; i<g.length - 1; i++)