본문 바로가기

임베디드 보드/STM32

Thingsboard - MQTT RPC Control

728x90

STM32

Ethernet + GPIO Output 32 + (ADC 16 + 9) Channel

 

ESP8266 통신 테스트 to Thingsboard

 

MBED Development

 
 

테스트화면

RPC GPIO 32 통신 제어합니다.

ADC Channel (16 + 9) 값을 신호를 수신합니다.

참조

Mqtt 설치

jolog.tistory.com/12

 

윈도우에서 MQTT(Mosquitto) 설치하기

윈도우에서 MQTT(Mosquitto) 설치하기 1. Mosquitto 홈페이지에서 윈도우 버전 ‘mosquito-1.6.2-install-windows-x64’ 을 다운로드 받습니다. : “mosquito-1.6.2-install-windows-x64” 설치하기 2. 옵..

jolog.tistory.com

mosquitto.org/download/

 

Download

Source mosquitto-2.0.7.tar.gz (319kB) (GPG signature) Git source code repository (github.com) Older downloads are available at https://mosquitto.org/files/ Binary Installation The binary packages li

mosquitto.org

Mqtt Publish

mosquitto_pub -h "192.168.0.113" -t "v1/devices/me/telemetry" -u "ryYvXcIwnpC10f9aC7Ct" -m "{"temperature":100, "humidity":100.0, "active": false}"

#include "mbed.h"
#include "ISM43362Interface.h"
#include "HTS221Sensor.h"
 
/* Private typedef------------------------------------------------------------*/
#define WIFI_NETWORK_NAME       "ZEROWIN"
// Change it with your WiFi password name
#define WIFI_NETWORK_PASSWORD   "zzzzzzzz"
#define WIFI_SECURITY           NSAPI_SECURITY_WPA_WPA2

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

Serial pc(USBTX, USBRX);

volatile int index = 0;
char buffer[32];

volatile bool available = false;

 int32_t Socket = -1;
 uint16_t Datalen;
 
 
void read_data() 
{
    char c;
    
    while(0 < pc.readable())
    {
        c = pc.getc();
        
        buffer[index] = c;
        
        if(c== '\r')
        {
        }
        else {
            index ++;    
        }
        if(c == '\n'/* || c== '\r'*/)
        {
            buffer[index] = '\0';
            printf("%s\r\n", buffer);
            
            available = true;
            index = 0;       
            

        }     
    }
}

#include "TCPSocket.h"
#include "MQTTmbed.h"
#include "MQTTClient.h"

#define MQTT_HOST               "192.168.0.113"
#define MQTT_PORT               1883
#define MQTT_TOPIC              "v1/devices/me/telemetry"

#include "MQTTNetwork.h"

int main()
{
    pc.baud(115200);
    pc.attach(read_data);
    
int count = 0;

    ISM43362Interface wifi(MBED_CONF_APP_WIFI_SPI_MOSI,
            MBED_CONF_APP_WIFI_SPI_MISO,
            MBED_CONF_APP_WIFI_SPI_SCLK,
            MBED_CONF_APP_WIFI_SPI_NSS,
            MBED_CONF_APP_WIFI_RESET,
            MBED_CONF_APP_WIFI_DATAREADY,
            MBED_CONF_APP_WIFI_WAKEUP, false);

    // Scanning WiFi networks ------------------------------------------
#if 0
    WiFiAccessPoint *ap;
    count = wifi.scan(NULL, 0);
    printf("%d networks available.\n", count);

    /* Limit number of network arbitrary to 15 */
    count = count < 15 ? count : 15;

    ap = new WiFiAccessPoint[count];
    count = wifi.scan(ap, count);
    for (int i = 0; i < count; i++) {
        printf("Network: %s RSSI: %hhd\n", ap[i].get_ssid(), ap[i].get_rssi());
    }

    delete[] ap; 
    #endif

    // Connecting to WiFi network --------------------------------------

    printf("\nConnecting to %s...\n", WIFI_NETWORK_NAME);
    int ret = wifi.connect(WIFI_NETWORK_NAME, WIFI_NETWORK_PASSWORD, WIFI_SECURITY);
    if (ret != 0) {
        printf("\nConnection error\n");
        return -1;
    }

    printf("Success\n\n");
    printf("MAC: %s\n", wifi.get_mac_address());
    printf("IP: %s\n", wifi.get_ip_address());
    printf("Netmask: %s\n", wifi.get_netmask());
    printf("Gateway: %s\n", wifi.get_gateway());
    printf("RSSI: %d\n\n", wifi.get_rssi());    
    
    {    
        MQTTNetwork network(&wifi);
        MQTT::Client<MQTTNetwork, Countdown> client(network);
        
        char assess_token[] = "ryYvXcIwnpC10f9aC7Ct";
        
        MQTTPacket_connectData conn_data = MQTTPacket_connectData_initializer;
        conn_data.username.cstring = assess_token;
        
        if (network.connect(MQTT_HOST, MQTT_PORT) < 0) {
            printf("failed to connect to " MQTT_HOST  "\n");
            return -1;
        }
        
        if (client.connect(conn_data) < 0) {
            printf("failed to send MQTT connect message\n");
            return -1;
        }
        
        printf("successfully connect!\n");
        
        {

            uint8_t id;
            DevI2C i2c_2(PB_11, PB_10);
            HTS221Sensor hum_temp(&i2c_2);
            
            hum_temp.init(NULL);
            hum_temp.enable();
            hum_temp.read_id(&id);
            printf("HTS221  humidity & temperature sensor = 0x%X\r\n", id);
            
            // Get data from sensors -----------------------------------------------
            
            for (;;) {
                float temp, humid;
            
                hum_temp.get_temperature(&temp);
                hum_temp.get_humidity(&humid);
                printf("HTS221:  [temp] %.2f C, [hum]   %.2f%%\r\n", temp, humid);
            
                char msg[256];
                int n = snprintf(msg, sizeof(msg),
                    "{\"temperature\":%f, \"humidity\":%f, \"active\": false}",
                    temp, humid);
            
                void *payload = reinterpret_cast<void*>(msg);
                size_t payload_len = n;
            
                printf("publish to: %s %d %s\r\n", MQTT_HOST, MQTT_PORT, MQTT_TOPIC);
            
                if (client.publish(MQTT_TOPIC, payload, n) < 0) {
                    printf("failed to publish MQTT message");
                }
            
                wait_ms(1000);
            }            
            
        }
        
        client.disconnect();
        wifi.disconnect();
        
        printf("\ndone\n");    
    }
    
    while(1) {
        
        
   }
}

 

References

forgge.github.io/B-L475E-IOT01A-thingsboard-read-temperature-and-humidity-using-mbed.html

 

Using thingsboard.io, STM32 B-L475E-IOT01A IoT kit and ARM Mbed to monitor temperature

Sampling and visualizing temperature samples is the key usecase for any set of IoT tools. If your tools can't do it easily, you should probably trow away that tools. In this article I will show you how to use free and open-source frameworks, ThingsBoard an

forgge.github.io