임베디드 보드/STM32

mbed programming - ADC & Ethernet UDP

ZEROWIN.TECH 2021. 2. 17. 22:53
728x90

/* mbed Microcontroller Library
 * Copyright (c) 2019 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "platform/mbed_thread.h"
#include "EthernetInterface.h"
#include "LWIPStack.h"

// Blinking rate in milliseconds
#define BLINKING_RATE_MS                                                    100

DigitalOut led2(LED2);
DigitalOut led3(LED3);

Thread thread;
Thread thread3;

AnalogIn adc_A0(A0);
AnalogIn adc_A1(A1);

EthernetInterface eth;
TCPSocket socket;

typedef struct {
    uint32_t secs;         // Transmit Time-stamp seconds.
} ntp_packet;

//Semaphore one_slot(1);

void led2_thread() {
    
    UDPSocket sock;
    sock.open(&eth);
    
    while (true) {
        led2 = !led2;
        SocketAddress sockAddr;
        
        //one_slot.acquire();

    // Bring up the ethernet interface
    // printf("UDP Socket example\n");
    //if (0 != eth.connect()) {
    //    printf("Error connecting\n");
    //    wait(4);
    //    continue;
    //}

    // Show the network address
    //eth.get_ip_address(&sockAddr);
    //printf("IP address is: %s\n", sockAddr.get_ip_address() ? sockAddr.get_ip_address() : "No IP");
/*

    eth.gethostbyname("time.nist.gov", &sockAddr);
    sockAddr.set_port(37);

    char out_buffer[] = "time";
    if (0 > sock.sendto(sockAddr, out_buffer, sizeof(out_buffer))) {
        printf("Error sending data\n");
        
        //one_slot.release();
        
        wait(4);
        continue;
    }

    ntp_packet in_data;
    sock.recvfrom(&sockAddr, &in_data, sizeof(ntp_packet));
    in_data.secs = ntohl(in_data.secs) - 2208988800;      // 1900-1970
    printf("Time Received %lu seconds since 1/01/1900 00:00 GMT\n",
           (uint32_t)in_data.secs);
    printf("Time = %s", ctime((const time_t *)&in_data.secs));

    printf("Time Server Address: %s Port: %d\n\r",
           sockAddr.get_ip_address(), sockAddr.get_port());

    // Close the socket and bring down the network interface
    // sock.close();
    //one_slot.release();
    
//    eth.disconnect(); */

        //one_slot.acquire();

        SocketAddress td_addr("192.168.0.104", 50000);

        char temp[32] = { 0x00, };
        sprintf(temp, "THREAD2 # DATA");
    
        if (0 > sock.sendto(td_addr, temp, sizeof(temp))) {
            printf("Error sending data\n");
            //one_slot.release();
            wait(4);
            
            continue;
        }
        
    // sock.close();
    //one_slot.release();

        
        led2 = !led2;
        wait(1);
    }
}

void led3_thread() {
        
        UDPSocket sock;
        sock.open(&eth);
        
    while (true) {
        

        printf("%f, %f\n", adc_A0.read()*1024, adc_A1.read()*1024);
        
        //one_slot.acquire();

        SocketAddress td_addr("192.168.0.104", 50000);

        char temp[32] = { 0x00, };
        sprintf(temp, "%f, %f", adc_A0.read()*1024, adc_A1.read()*1024);
    
    if (0 > sock.sendto(td_addr, temp, sizeof(temp))) {
        printf("Error sending data\n");
        //one_slot.release();
        wait(4);
        
        continue;
    }
    
    // sock.close();
    //one_slot.release();

        
        led3 = !led3;
        wait(1);
    }
}
 
 
#define IP         "192.168.0.177"
#define GATEWAY    "192.168.0.1"
#define MASK       "255.255.255.0" 

int main()
{
    Serial s(USBTX, USBRX);
    s.baud(115200);
   
    
    // Initialise the digital pin LED1 as an output
    DigitalOut led(LED1);
    
    printf("START\r\n");
   
    eth.disconnect();
    // int i=eth.set_network(IP,MASK,GATEWAY);
    int i=eth.connect();
    printf("set IP status: %i \r\n",i);
    i=eth.connect();
    printf("connect status: %i \r\n",i);
    const char *ip = eth.get_ip_address();
    const char *mac = eth.get_mac_address();
    printf("IP address is: %s\n\r", ip ? ip : "No IP");
    printf("MAC address is: %s\n\r", mac ? mac : "No MAC");
    
    thread.start(led2_thread);
    thread3.start(led3_thread);
    
    while (true) {
        led = !led;
        thread_sleep_for(BLINKING_RATE_MS);
    }
}