Skip to content

Instantly share code, notes, and snippets.

@lifa08
lifa08 / DHT22.c
Last active November 21, 2017 20:34
Use arduino controller and temperature & humidity sensor DHT22 to measure ambient temperature and humidity and send the sensor data to computer through serial port for display and analysis.
#include <DHT22.h>
// Only used for sprintf
#include <stdio.h>
// Data wire is plugged into port 7 on the Arduino
// Connect a 4.7K resistor between VCC and the data pin (strong pullup)
#define DHT22_PIN 7
// Setup a DHT22 instance
DHT22 myDHT22(DHT22_PIN);
@lifa08
lifa08 / CosineSimilarity.py
Last active March 12, 2016 14:13
Write a C program to calculate similarity of databse records. Similarity is calculated according to TF-IDF algorithm and Cosine similarity theory. Because there is no good and well documented c library for TF-IDF algorithm, so use Python library. But I needed it to implemented in C, so use poen to excute python script and parse its output.
#!/usr/bin/python
import sys, getopt
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import mysql.connector
from mysql.connector import errorcode
SID = sys.argv[1]
cnx = mysql.connector.connect(user = 'cuser', password = 'Test*', database = 'SPdb')
@lifa08
lifa08 / Aserver.js
Last active February 28, 2016 13:15
A basic calenar web application
/*
* Server.js
* Author: Lifang Zhang
* Date: 11.10.2015
*/
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var Event = require('./models/event');
@lifa08
lifa08 / proxy.js
Last active February 21, 2016 20:30
Redis cache test: Consists of the main server (server.js) and a proxy server (proxy.js using Redis and http-proxy). Requests to proxy server will be relayed to Redis server incase data can be found in cache. Otherwise data will be fetched from main server and saved in Redis.
var express = require('express');
var app = express();
var httpProxy = require('http-proxy');
var redis = require("redis");
var redisclient = redis.createClient();
// Create a proxy server
var proxy = httpProxy.createProxyServer({});
app.listen(443, function () {
@lifa08
lifa08 / AES-COPA.c
Last active December 13, 2015 19:52
The implementation of AES-COPA
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "aescopa.h"
#include "aes128e.h"
#define NUM_BYTES 16
#define DEBUG 0
@lifa08
lifa08 / AES128.c
Last active December 13, 2015 19:50
The implementation AES 128 encryption algorithm
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "aes128e.h"
void matrixtransform(unsigned char *to, const unsigned char *from);
void matrixhexprint(unsigned char *arr, int length);
void addroundkey(unsigned char *key, unsigned char *c);
void keyexpansion(unsigned char *roundkey, int roundindex);
@lifa08
lifa08 / 1index.js
Last active February 21, 2016 20:54
Lego EV3 remote controller using Node.js and PhoneGap
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
@lifa08
lifa08 / about.html
Last active August 29, 2015 14:09
Address book - frontend implemented with AngularJS, UI Boostrap and less
<div class="jumbotron well well-lg">
<h4 class="text-center"><strong>Welcome to our address book</strong></h4>
<p>With our address book, you can see all the contacts by clicking </strong>Show contacts</strong>, and you can edit your contacts which includes <strong> add </strong>, <strong> modify </strong>, <strong> delete </strong>. Moreoever, you can load your <strong> google </strong> contacts to your contacts. Use it, it is easy and fun, you will definitely enjoy it :)</p>
</div>
@lifa08
lifa08 / client.js
Created November 14, 2014 09:58
Address book - frontend implemented with jQuery and bootstrap
jQuery.noConflict();
jQuery( document ).ready(function() {
// http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
@lifa08
lifa08 / flicker.js
Last active February 21, 2016 20:55
Photo Album -- part of codes related to third party login and search images with Flickr API which is implemented by me
var Image_page =1;
var Image_add = 'http://api.flickr.com/services/rest/?';
var query_str = 'method=flickr.photos.search&per_page=12&api_key=be59384d594d726d0f05c3d3d75045f0&format=rest&tags=';
var Key_word = '';
var url = '';
$(document).ready(function () {
$(document).on('click', '.btn.btn-primary.searchbtn', function (){