Skip to content

Instantly share code, notes, and snippets.

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

Sergey Lyubka cpq

🎭
Купатися чи не купатись?
View GitHub Profile
@cpq
cpq / main.c
Last active July 9, 2025 07:37
Mongoose Wizard - MQTT client with device management
// This MQTT client implements device management with
// a device dashboard, remote firmware update and device control over MQTT.
//
// Usage:
// 1. Visit https://mongoose.ws/wizard. Enable MQTT, generate project
// 2. Copy-paste this code into your main.c
// 3. After mongoose_init() call, add initialization:
// struct mongoose_mqtt_handlers mqtt_handlers = {
// my_mqtt_connect, my_mqtt_tls_init, my_mqtt_on_connect,
// my_mqtt_on_message, my_mqtt_on_cmd,
@cpq
cpq / mongoose_netxduo.md
Last active July 4, 2025 10:46
How to add Mongoose to NetxDuo, STM32 H573
@cpq
cpq / main.c
Last active June 6, 2025 18:32
How to send a JSON REST API response with a list of options
size_t print_entries(void (*out)(char, void *), void *ptr, va_list *ap) {
const char **names = va_arg(*ap, const char **);
size_t i, len = 0;
for (i = 0; names[i] != NULL; i++) {
len += mg_xprintf(out, ptr, "%s%m",
i > 0 ? "," : "", // Comma-separate entries
MG_ESC(names[i])); // JSON-escape string
}
return len;
}
@cpq
cpq / main.c
Created April 29, 2025 14:20
ESP32C6 dashboard
// SPDX-FileCopyrightText: 2024 Cesanta Software Limited
// SPDX-License-Identifier: GPL-2.0-only or commercial
// Generated by Mongoose Wizard, https://mongoose.ws/wizard/
#include "mongoose_glue.h"
#include "wifi.h"
#include "driver/gpio.h"
#include "driver/temperature_sensor.h"
#include "nvs_flash.h"
@cpq
cpq / bsd.c
Last active August 28, 2024 12:24
Final code for the "BSD socket API explained" video
// Source for the https://www.youtube.com/watch?v=pp9AM5A1mDs
// Written & tested on MacOS
// To discuss or report issues, join https://discord.gg/KfR8E6wSds
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
@cpq
cpq / rt1060.sh
Last active December 4, 2023 14:00
Flashing rt1060-evk via vcon SWD
#!/bin/bash
# SWD commands to call rt1060 flash boot rom functions
DEVCALL="curl -su :$VCON_API_KEY https://dash.vcon.io/api/v3/devices/13"
# SWD exec: se COMMANDS. Hex addresses are without leading 0x
se() { $DEVCALL/rpc/swd.exec -d "{\"req\": \"$*\"}" | jq -cr .resp ; }
rm() { se rm,$1 | cut -d, -f2 ; }
dump() { $DEVCALL/rpc/swd.read -d "{\"addr\":$1, \"size\": $2}" | jq -r .data | xxd -r -p | hexdump -C ; }
hex() { node -p "($*).toString(16)"; }
@cpq
cpq / stm32.md
Last active July 27, 2024 12:16
STM32 naming

STM32 naming scheme

Example MCU: STM32 H743ZI

Example Type Description
H Type F: mainstream, L: low power, H: high performance, W: wireless
7 Core 0: M0, 1: M3, 2: M3, 3: M4, 4: M4, 7: M7
23 Line speed, peripherals, silicon process, …
Z Pin count F: 20, G: 28, K: 32, T: 36, S: 44, C: 48, R: 64,66, V: 100, Z: 144, I: 176
// Copyright (c) Cesanta Software Limited
// All rights reserved
// SPDX-License-Identifier: MIT
// Usage example (Arduino):
// char buf[100];
// struct slip slip = {.buf = buf, .size = sizeof(buf) - 1};
// ...
// unsigned char c = Serial.read();
// size_t len = slip_recv(c, &slip);
@cpq
cpq / queue2.c
Created February 15, 2023 09:26
#include <sttdef.h>
// Single producer, single consumer non-blocking queue
//
// Producer:
// void *buf;
// while (mg_queue_space(q, &buf, len) == 0) WAIT(); // Wait for free space
// memcpy(buf, data, len); // Copy data to the queue
// mg_queue_add(q, len); // Advance q->head
//
@cpq
cpq / queue1.c
Created February 14, 2023 16:18
// Single producer, single consumer non-blocking queue
//
// Producer:
// void *buf;
// while (mg_queue_space(q, &buf, len) == 0) WAIT(); // Wait for free space
// memcpy(buf, data, len); // Copy data to the queue
// mg_queue_add(q, len); // Advance q->head
//
// Consumer:
// void *buf;