Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.5 kB
3
Indexable
import psycopg2

def save_data_to_database(data):
    try:
        # Replace the following with your PostgreSQL database configuration
        dbname = "mydatabase"
        user = "your_username"
        password = "your_password"
        host = "localhost"
        port = "5432"

        connection = psycopg2.connect(
            dbname=dbname, user=user, password=password, host=host, port=port
        )
        cursor = connection.cursor()

        # Replace 'column1_value' and 'column2_value' with your data values
        column1_value = data["column1"]
        column2_value = data["column2"]

        insert_query = f"INSERT INTO mytable (column1, column2) VALUES ('{column1_value}', {column2_value});"
        cursor.execute(insert_query)

        connection.commit()
        cursor.close()
        connection.close()
        print("Data saved to the database successfully.")
    except Exception as e:
        print("Error:", e)

if __name__ == "__main__":
    # Example data to save to the database
    data_to_save = {
        "column1": "Value1",
        "column2": 42
    }

    save_data_to_database(data_to_save)




/////

import bluetooth
import threading

def handle_client(client_socket, client_address):
    print("Connected with", client_address)

    while True:
        data = client_socket.recv(1024)
        if not data:
            break
        print("Received from", client_address, ":", data.decode())

    client_socket.close()
    print("Disconnected from", client_address)

def start_server():
    server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    port = 1  # RFCOMM port number, must be the same as the client
    server_socket.bind(("", port))
    server_socket.listen(3)  # Maximum of 3 clients (you can adjust this value)

    print("Waiting for connections...")

    threads = []
    while True:
        client_socket, client_address = server_socket.accept()
        thread = threading.Thread(target=handle_client, args=(client_socket, client_address))
        thread.start()
        threads.append(thread)

    # Wait for all client threads to finish
    for thread in threads:
        thread.join()

    server_socket.close()

if __name__ == "__main__":
    start_server()


///

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"
#include "esp_gattc_api.h"

#define GATTC_TAG "GATTC_DEMO"
#define REMOTE_SERVICE_UUID         0x00001101
#define REMOTE_NOTIFY_CHAR_UUID     0x00001101
#define PROFILE_NUM                 1
#define PROFILE_APP_IDX             0
#define ESP_APP_ID                  0x55
#define ADV_DATA_LEN                31

static const char* TAG = "ESP32_Client";
static esp_gattc_cb_t gattc_cb_param;
static uint8_t notify_data[ADV_DATA_LEN];

static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t* param) {
    if (event == ESP_GAP_BLE_SCAN_RESULT_EVT) {
        esp_ble_gap_cb_param_t* scan_result = (esp_ble_gap_cb_param_t*)param;
        switch (scan_result->scan_rst.search_evt) {
            case ESP_GAP_SEARCH_INQ_RES_EVT: {
                if (memcmp(scan_result->scan_rst.bda, YOUR_RASPBERRY_PI_MAC_ADDRESS, ESP_BD_ADDR_LEN) == 0) {
                    esp_ble_gap_stop_scanning();
                    esp_ble_gattc_open(gattc_cb_param.reg.app_id, scan_result->scan_rst.bda, scan_result->scan_rst.ble_addr_type, true);
                }
                break;
            }
            case ESP_GAP_SEARCH_INQ_CMPL_EVT: {
                break;
            }
            default:
                break;
        }
    }
}

static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* param) {
    switch (event) {
        case ESP_GATTC_REG_EVT:
            esp_ble_gap_start_scanning(10);
            break;
        case ESP_GATTC_OPEN_EVT: {
            if (param->open.status != ESP_GATT_OK) {
                break;
            }
            ESP_LOGI(GATTC_TAG, "Connected to the server.");
            break;
        }
        case ESP_GATTC_DISCONNECT_EVT: {
            ESP_LOGI(GATTC_TAG, "Disconnected from the server.");
            esp_ble_gap_start_scanning(10);
            break;
        }
        default:
            break;
    }
}

static void send_data_task(void* arg) {
    while (1) {
        vTaskDelay(5000 / portTICK_PERIOD_MS); // Send data every 5 seconds

        // Replace the example data with the data you want to send
        const char* dataToSend = "Hello, Raspberry Pi!";
        esp_ble_gattc_send_char_descr_by_uuid(gattc_cb_param.gattc_if,
            gattc_cb_param.conn_id,
            REMOTE_SERVICE_UUID,
            REMOTE_NOTIFY_CHAR_UUID,
            strlen(dataToSend),
            (uint8_t*)dataToSend,
            ESP_GATT_WRITE_TYPE_RSP,
            ESP_GATT_AUTH_REQ_NONE);
    }
}

void app_main() {
    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    esp_err_t ret;

    ret = esp_bt_controller_init(&bt_cfg);
    if (ret) {
        ESP_LOGE(TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
        return;
    }

    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
    if (ret) {
        ESP_LOGE(TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
        return;
    }

    ret = esp_bluedroid_init();
    if (ret) {
        ESP_LOGE(TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
        return;
    }

    ret = esp_bluedroid_enable();
    if (ret) {
        ESP_LOGE(TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
        return;
    }

    esp_ble_gap_register_callback(gap_event_handler);
    esp_ble_gattc_register_callback(gattc_event_handler);

    esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
    if (local_mtu_ret) {
        ESP_LOGE(GATTC_TAG, "set local  MTU failed, error code = %x", local_mtu_ret);
    }

    ret = esp_ble_gattc_app_register(ESP_APP_ID);
    if (ret) {
        ESP_LOGE(GATTC_TAG, "gattc app register error, error code = %x", ret);
        return;
    }

    xTaskCreate(send_data_task, "send_data_task", 2048, NULL, 5, NULL);
}