Skip to content

Instantly share code, notes, and snippets.

View cpq's full-sized avatar
🎭
Купатися чи не купатись?

Sergey Lyubka cpq

🎭
Купатися чи не купатись?
View GitHub Profile
@cpq
cpq / init.js
Last active November 22, 2017 17:01
apa120
load('api_spi.js');
load('api_timer.js');
load('api_math.js');
let spi = SPI.get();
let light = function(ar) {
let frame = '\x00\x00\x00\x00';
for (let i = 0; i < ar.length; i++) {
print(typeof(ar[i]), ar[i]);
@cpq
cpq / func.js
Created October 17, 2017 10:20
JS args
//////// old
function old1() {
};
function old2() {
old1();
}
old2();
////////// new
@cpq
cpq / init.js
Last active August 13, 2017 17:11
Mongoose OS IoT Kit - DHT22 sensor tutorial
load('api_dht.js');
load('api_mqtt.js');
load('api_timer.js');
Timer.set(1000, true, function() {
let data = { temperature: DHT.getTemp(), humidity: DHT.getHumidity() };
MQTT.pub('dev/1234', JSON.stringify(data), 1);
}, null);
load('api_aws.js');
load('api_gpio.js');
load('api_mqtt.js');
let state = { on: false, counter: 0 }; // device state: shadow metadata
GPIO.set_button_handler(0, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
AWS.Shadow.update(0, {desired: {on: state.on, counter: state.counter + 1}});
}, null);
// Compiling and starting server:
// $ cc ian1.c mongoose/mongoose.c frozen/frozen.c -I mongoose/ -I frozen -o /tmp/a
// $ /tmp/a
// Starting web server on port 8001
//
// Running the client:
// $ curl -d '' 127.0.0.1:8001
// {"error": {"code": -1, "message": "Expected a, b"}}
// $ curl -d '{"b": 34, "a": 12}' 127.0.0.1:8001
// {"result": 46}
@cpq
cpq / ws.js
Last active December 2, 2016 19:52
WebSocket client
var reconnect = function() {
var url = 'ws://' + location.host + '/ws';
ws = new WebSocket(url);
ws.onopen = function(ev) { console.log(ev); };
ws.onclose = function(ev) { console.log(ev); setTimeout(reconnect, 1000); };
ws.onmessage = function(ev) { console.log('message', ev); };
ws.onerror = function(ev) { console.log('error', ev); ws.close(); };
};
@cpq
cpq / package.json
Last active August 18, 2016 13:48
web server skeleton
{
"name": "web_server",
"description": "web server skeleton",
"version": "1.0.0",
"private": false,
"dependencies": {
"express": "3.x",
"sqlite3": "*",
"minimist": "*",
"supervisor": "*",
@cpq
cpq / mess.c
Last active May 27, 2016 15:13
Marshall arbitrary C structures into JSON
// $ cc -W -Wall ~/tmp/mess.c && ./a.out
// {"a": 9, "c": "i am bar", "d": [{"a": 3, "b": null}, {"a": 7, "b": "oops"}]}
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include "mess.h"
#define STRUCT_NAME foo
#define STRUCT_FIELDS ATTR(int, a) ATTR(char *, b)
@cpq
cpq / overflow.c
Created April 4, 2016 12:33
Simple web server with buffer overflow vulnerability
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define LISTENING_PORT 8000
static void serve_new_connection(FILE *fp) {
char method[10], uri[100], protocol[20];
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>GitHub Stars</title>
</head>
<body>
hi
</body>
</html>