Skip to content

Instantly share code, notes, and snippets.

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

Sergey Lyubka cpq

🎭
Купатися чи не купатись?
View GitHub Profile
@cpq
cpq / split.html
Last active November 20, 2021 17:28
HTML split pane layout example
<html>
<head>
<style>
* { box-sizing: border-box; }
body, html { padding: 0; margin: 0; }
.main { height: 100vh; }
.content { padding: 1em; }
.split { display: flex; flex-grow: 1; flex-shrink: 1; width: 100%; height: 100%; box-sizing: border-box; }
.split>* { flex-grow: 1; flex-shrink: 1; width: 100%; height: 100%; overflow: auto; -webkit-overflow-scrolling: touch; }
// This file contains two sketches for the end-user networking API that implements
// a simple WS service: WS message with JSON input {"pin": 12, "val": 0}, and JSON response {"status": true}
// WS handler, streaming API (think AVR)
// We don't buffer an incoming WS message. Instead, we feed
// TCP stack, and user handler, byte-by-byte.
// Some important values from the parsed HTTP header gets stored
// and passed to the user handler.
void ws_handler(struct conn *c, int event, size_t content_len, size_t receided_so_far, uint8_t byte) {
if (event == EVENT_INCOMING_BYTE) {
@cpq
cpq / cnip.h
Last active June 4, 2021 10:46
CNIP API suggestion
// The stack has two API: low level and high level
// Low level: used to hook it up to a hardware, to send/receive MAC frames
// High level: used to implement network apps, like HTTP/MQTT client/servers.
// Low level
// A device code does something like this:
//
// void cnip_mac_out(char *buf, size_t len) {
// bitbang_to_ethernet(buf, len); // Push outgoing frame to the network
// }
const port = 8002;
const http = require('http');
const http_handler = function(req, res) {
let request = '', count = 0;
req.on('data', chunk => request += chunk);
req.on('end', function(ev) {
res.writeHead(200);
const f = function() {
res.write('hi\n');
<!DOCTYPE html>
<html lang="en">
<head>
<title>hiii</title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>
</head>
@cpq
cpq / catcher.js
Created September 15, 2020 12:47
// Run this catcher in a following way:
// 1. Make sure to install tmux, nodejs, and `ws` node package
// apt-get install tmux npm
// npm install -g ws
// 2. Start tmux. This is needed to let the program run after logout.
// When you log in again, `tmux attach` attaches an old session.
// 3. Run catcher in an infinite loop:
// while true; do node catcher.js YOUR_API_KEY ; sleep 1; done
const Websocket = require('ws'); // npm install -g ws
@cpq
cpq / devsim.js
Last active September 11, 2020 20:40
// Simulate mdash.net device. To run this script, install ws library first:
// $ npm -g i ws
// $ node devsim.js MDASH_DEVICE_TOKEN
const Websocket = require('ws'); // npm install -g ws
const pass = process.argv[2]; // Device password
const addr = 'wss://mdash.net/api/v2/rpc?access_token=' + pass;
const ws = new Websocket(addr, {origin: addr});
ws.on('error', msg => console.log(msg.toString()));
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
int character = Serial.read();
if (character == '0') digitalWrite(LED_BUILTIN, LOW); // '0' switches LED off
if (character == '1') digitalWrite(LED_BUILTIN, HIGH); // '1' switches LED on
static int BAUD = 115200, RTS = -1, DTR = -1, ESP32_GPIO = 11, ESP32_RESETN = 12;
void setup() {
Serial.begin(baud);
Serial1.begin(baud);
pinMode(ESP32_GPIO0, OUTPUT);
pinMode(ESP32_RESETN, OUTPUT);
// Reset ESP32, put into upload mode
digitalWrite(ESP32_GPIO0, LOW);
const WebSocket = require('ws'); // npm install -g ws
const server = new WebSocket.Server({port: 8000}, function() {
console.log('WS server started');
});
server.on('connection', function connection(ws, req) {
let i = 0;
ws.on('message', function incoming(message) {
console.log('received', message);
});
console.log('connected', req.connection.remoteAddress);