KIM YOUNG SEONG / Mbed OS 429_adc_network_adc_16_channel

Dependencies:   MQTT

Files at this revision

API Documentation at this revision

Comitter:
cherrychance
Date:
Mon Feb 22 06:07:17 2021 +0000
Parent:
12:138b2b3d041a
Commit message:
stm32_f429zi + adc 16 channel mux + ethernet

Changed in this revision

MQTT.lib Show annotated file Show diff for this revision Revisions of this file
MQTTNetwork.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTT.lib	Mon Feb 22 06:07:17 2021 +0000
@@ -0,0 +1,1 @@
+http://os.mbed.com/teams/mqtt/code/MQTT/#9cff7b6bbd01
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTTNetwork.h	Mon Feb 22 06:07:17 2021 +0000
@@ -0,0 +1,37 @@
+// Network adapter for Paho MQTT library -------------------------------
+
+class MQTTNetwork
+{
+public:
+    MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) {
+        socket = new TCPSocket();
+    }
+
+    ~MQTTNetwork() {
+        delete socket;
+    }
+
+    int read(unsigned char* buffer, int len, int timeout) {
+        return socket->recv(buffer, len);
+    }
+
+    int write(unsigned char* buffer, int len, int timeout) {
+        return socket->send(buffer, len);
+    }
+
+    int connect(const char* hostname, int port) {
+        socket->open(network);
+        // return socket->connect(hostname, port);
+        SocketAddress sAddr(hostname, port);
+        return socket->connect(sAddr); // , sizeof(sAddr));
+        
+    }
+
+    int disconnect() {
+        return socket->close();
+    }
+
+private:
+    NetworkInterface* network;
+    TCPSocket* socket;
+};
\ No newline at end of file
--- a/main.cpp	Tue Oct 27 16:43:25 2020 +0000
+++ b/main.cpp	Mon Feb 22 06:07:17 2021 +0000
@@ -1,223 +1,89 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
 #include "TCPSocket.h"
+#include "MQTTmbed.h"
+#include "MQTTClient.h"
+
 #include <stdio.h>
 #include <string>
 using namespace std;
 
-#define IP      "192.168.1.181"
-#define GATEWAY "192.168.1.1"
+
+
+#define MQTT_HOST               "192.168.0.113"
+#define MQTT_PORT               1883
+#define MQTT_TOPIC              "v1/devices/me/telemetry"
+
+#include "MQTTNetwork.h"
+
+#define IP      "192.168.0.181"
+#define GATEWAY "192.168.0.1"
 #define NETMASK "255.255.255.0"
 
-#define PORT    80
 /* */
 EthernetInterface*  net;
-TCPSocket           server;
-TCPSocket*          client;
-char                httpBuf[1500];
-char                httpHeader[256];
-const int           OFF = 0;
-const int           ON = 1;
-const char          PASSWORD[] = "secret";  // Change as you like
-DigitalOut          output(LED1);
-float               roomTemp = 21.8;        // A temperature sensor output
 
-/**
- * @brief   Analyses the received URL
- * @note    The string passed to this function will look like this:
- *          GET /password HTTP/1.....
- *          GET /password/ HTTP/1.....
- *          GET /password/?sw=1 HTTP/1.....
- *          GET /password/?sw=0 HTTP/1.....
- * @param   url URL string
- * @retval -1 invalid password
- *         -2 no command given but password valid
- *         -3 just refresh page
- *          0 switch off
- *          1 switch on
- */
-int8_t analyseURL(char* url)
-{
-    if (strlen(url) < (5 + strlen(PASSWORD) + 1))
-        return(-1);
+AnalogIn adc_A0(A0); // PA_3
+AnalogIn adc_A1(A1); // PC_0
+AnalogIn adc_A2(A2); // PC_3
+AnalogIn adc_A3(PF_6);
+AnalogIn adc_A4(A4); // PF_5
+AnalogIn adc_A5(A5); // PF_10
+AnalogIn adc_A6(PF_8);
+AnalogIn adc_A7(PF_7);
+AnalogIn adc_A8(PF_9);
+AnalogIn adc_A9(PF_4);
 
-    //if (url.substr(5, PASSWORD.size()) != PASSWORD)
-    if (strncmp(url + 5, PASSWORD, strlen(PASSWORD)) != 0)
-        return(-1);
-
-    uint8_t pos = 5 + strlen(PASSWORD);
-
-    //if (url.substr(pos, 1) != "/")
-
-    if (*(url + pos) != '/')
-        return(-1);
-
-    //if (url.substr(pos++, 1) == " ")
-    if (*(url + pos++) == ' ')
-        return(-2);
+DigitalOut led(LED1);
 
-    //string  cmd(url.substr(pos, 5));
-    *(url + pos + 5) = '\0';    // terminate the cmd string
-    char*   cmd = ((url + pos));
-    if (strcmp(cmd, "?sw=0") == 0)
-        return(0);
-    if (strcmp(cmd, "?sw=1") == 0)
-        return(1);
-    return(-3);
-}
+DigitalOut gpS0(PD_7);
+DigitalOut gpS1(PD_6);
+DigitalOut gpS2(PD_5);
+DigitalOut gpS3(PD_4);
+
+
+BufferedSerial pc(SERIAL_TX, SERIAL_RX, 115200);
 
-/**
- * @brief
- * @note
- * @param
- * @retval
- */
-char* movedPermanently(uint8_t flag)
-{
-    memset(httpBuf, 0, sizeof(httpBuf));
-    if (flag == 1) {
-        strcpy(httpBuf, "/");
-        strcat(httpBuf, PASSWORD);
-        strcat(httpBuf, "/");
-    }
+#define ADC_NUM 9 + 16
+uint16_t ADCResult[ADC_NUM];
 
-    strcat(httpBuf, "<h1>301 Moved Permanently</h1>\r\n");
-    return(httpBuf);
-}
+#define BACKUP_NUM 50
+uint16_t ADCBackup[ADC_NUM][BACKUP_NUM] = { 0x00, };
 
-/**
- * @brief
- * @note
- * @param
- * @retval
- */
-char* showWebPage(int status)
-{
-    char    roomTempStr[10] = { };
-
-    //roomTemp = ds1820.read();
-
-    sprintf(roomTempStr, "%3.1f", roomTemp);
-    memset(httpBuf, 0, sizeof(httpBuf));
+Thread thread;
 
-    /*$off*/
-    strcat
-    (
-        httpBuf,
-        "<head>"
-            "<meta charset=\"utf-8\">"
-            "<meta name=\"viewport\" content=\" initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=0;\"/>"
-            "<title>Smart Home</title>"
-            "<link href='http://fonts.googleapis.com/css?family=Droid+Sans&v1' rel='stylesheet' type='text/css'>"
-            "<style>"
-            ".switch {"
-                "position: relative;"
-                "display: inline-block;"
-                "width: 60px;"
-                "height: 34px;"
-            "}"
-            ".switch input {display:none;}"
-            ".slider {"
-                "position: absolute;"
-                "cursor: pointer;"
-                "top: 0;"
-                "left: 0;"
-                "right: 0;"
-                "bottom: 0;"
-                "border-radius: 34px;"
-                "background-color: #ccc;"
-                "-webkit-transition: .4s;"
-                "transition: .4s;"
-            "}"
-            ".slider:before {"
-                "position: absolute;"
-                "content: \"\";"
-                "height: 26px;"
-                "width: 26px;"
-                "left: 4px;"
-                "bottom: 4px;"
-                "border-radius: 50%;"
-                "background-color: white;"
-                "-webkit-transition: .4s;"
-                "transition: .4s;"
-            "}"
-            "input:checked + .slider {"
-                "background-color: #8ce196;"
-            "}"
-            "input:focus + .slider {"
-                "box-shadow: 0 0 1px #8ce196;"
-            "}"
-            "input:checked + .slider:before {"
-                "-webkit-transform: translateX(26px);"
-                "-ms-transform: translateX(26px);"
-                "transform: translateX(26px);"
-            "}"
-            "</style>"
-        "</head>"
+#define MUX_MAX_NUM 16
+int muxChannel[16][4]={
+    {0,0,0,0}, //channel 0
+    {1,0,0,0}, //channel 1
+    {0,1,0,0}, //channel 2
+    {1,1,0,0}, //channel 3
+    {0,0,1,0}, //channel 4
+    {1,0,1,0}, //channel 5
+    {0,1,1,0}, //channel 6
+    {1,1,1,0}, //channel 7
+    {0,0,0,1}, //channel 8
+    {1,0,0,1}, //channel 9
+    {0,1,0,1}, //channel 10
+    {1,1,0,1}, //channel 11
+    {0,0,1,1}, //channel 12
+    {1,0,1,1}, //channel 13
+    {0,1,1,1}, //channel 14
+    {1,1,1,1}  //channel 15
+  };
+  
+int readADCMux(int _ch)
+{
+  // 4개 컨트롤핀 (s0,s1,s2,s3) 핀 설정
+  gpS0 = muxChannel[_ch][0];
+  gpS1 = muxChannel[_ch][1];
+  gpS2 = muxChannel[_ch][2];
+  gpS3 = muxChannel[_ch][3];
 
-        "<body>"
-            "<h2><a href=\".\" title=\"Click to refresh the page\">Smart Home</a></h2>"
-            "<pre>Temperature:\t"
-    );
-    strcat(httpBuf, roomTempStr);
-    strcat(httpBuf, "&deg;C</pre>");
-    strcat
-    (
-       httpBuf,
-       "<pre>Heating:\t"
-    );
-    if(status == ON) {
-       strcat
-       (
-           httpBuf,
-           "<a href=\"./?sw=0\" class=\"switch\"> "
-           "<input type=\"checkbox\" checked>"
-       );
-    }
-    else {
-       strcat
-       (
-           httpBuf,
-           "<a href=\"./?sw=1\" class=\"switch\"> "
-           "<input type=\"checkbox\">"
-       );
-    }
-    strcat
-    (
-       httpBuf,
-           "<div class=\"slider\"></div>"
-           "</a>"
-           "</pre>"
-           "<hr>"
-           "<pre>2017 ARMmbed</pre>"
-       "</body>"
-    );
-    /*$on*/
-    return httpBuf;
-}
-
-/**
- * @brief
- * @note
- * @param
- * @retval
- */
-void sendHTTP(TCPSocket* client, char* header, char* content)
-{
-    char    content_length[10] = { };
-    sprintf(content_length, "%u\r\n", strlen(content));
-    strcat(header, "\r\nContent-Type: text/html\r\n");
-    strcat(header, "Content-Length: ");
-    strcat(header, content_length);
-    strcat(header, "Pragma: no-cache\r\n");
-    strcat(header, "Connection: About to close\r\n\r\n");
-
-    char    c = content[0];
-    memmove(httpBuf + strlen(header), httpBuf, strlen(content));    // make room for the header
-    strcpy(httpBuf, header);                                        // copy the header on front of the content
-    httpBuf[strlen(header)] = c;
-    client->send((uint8_t*)httpBuf, strlen(httpBuf));
-}
+  // SIG pin 읽기
+  return adc_A0.read_u16();
+}   
+  
 
 /**
  * @brief
@@ -227,7 +93,12 @@
  */
 int main(void)
 {
-    printf("\r\n Starting \r\n");
+    
+    gpS0 = 1;
+    gpS1 = 1;
+    gpS2 = 1;
+    gpS3 = 1;
+    // printf("\r\n Starting \r\n");
 
     //ethLed.start(eth_led);
     //net = NetworkInterface::get_default_instance();
@@ -249,79 +120,127 @@
     // Show the network address
     SocketAddress   addr;
     net->get_ip_address(&addr);
-    printf("IP address: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None");
+    //printf("IP address: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None");
     net->get_netmask(&addr);
-    printf("Netmask: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None");
+    //printf("Netmask: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None");
     net->get_gateway(&addr);
-    printf("Gateway: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None");
-
-    /* Open the server on ethernet stack */
-    server.open(net);
-
-    /* Bind the HTTP port (TCP 80) to the server */
-    server.bind(PORT);
-
-    /* Listen for clients */
-    server.listen();
+    //printf("Gateway: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None");
 
-    printf("=========================================\r\n");
-    printf("Ready to serve clients.\r\n");
-    net->get_ip_address(&addr);
-    printf("Usage: Type http:\/\/%s\/%s\/  into your web browser and hit ENTER\r\n", addr.get_ip_address(), PASSWORD);
-    while (true) {
-        //listening for http GET request
-        client = server.accept();
-        if (client) {
-            client->getpeername(&addr);
-            printf("Connection succeeded!\n\rIP: %s\n\r", addr.get_ip_address());
-            client->recv(httpBuf, 1500);
-            if (strncmp(httpBuf, "GET", 3) != 0) {
-                strcpy(httpHeader, "HTTP/1.0 200 OK");
-                strcpy(httpBuf, "<h1>200 OK</h1>");
-                sendHTTP(client, httpHeader, httpBuf);
-            }
-            else
-            if ((strncmp(httpBuf, "GET", 3) == 0) && (strncmp(httpBuf + 3, " / ", 3 == 0))) {
-                strcpy(httpHeader, "HTTP/1.0 200 OK");
-                strcpy(httpBuf, "<p>Usage: http://host_or_ip/password</p>\r\n");
-                sendHTTP(client, httpHeader, httpBuf);
+    int count = 0;
+    while(1)
+    {
+        // printf("percentage: %3.3f%%\n", adc_A0.read()*100.0f);
+        char temp[128] = { 0x00, };
+        for(int i = 0; i < MUX_MAX_NUM; i++)
+        {
+            ADCBackup[i][count % MUX_MAX_NUM] = readADCMux(i);
+                double total = 0;
+                for(int j = 0; j < BACKUP_NUM; j++)
+                {
+                    total += ADCBackup[i][j];
+                }
+                ADCResult[i] = total / BACKUP_NUM;            
+        }
+        
+                sprintf(temp, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d \n", 
+                    ADCResult[0], 
+                    ADCResult[1], 
+                    ADCResult[2], 
+                    ADCResult[3], 
+                    ADCResult[4], 
+                    ADCResult[5], 
+                    ADCResult[6], 
+                    ADCResult[7], 
+                    ADCResult[8], 
+                    ADCResult[9], 
+                    ADCResult[10], 
+                    ADCResult[11], 
+                    ADCResult[12], 
+                    ADCResult[13], 
+                    ADCResult[14], 
+                    ADCResult[15] 
+                    
+                    );       
+  
+        
+            /*
+            for(int i = 0; i < ADC_NUM; i++) {
+                
+                switch(i)
+                {
+                    case 0: ADCBackup[i][count % BACKUP_NUM] = adc_A0.read_u16(); break;
+                    case 1: ADCBackup[i][count % BACKUP_NUM] = adc_A1.read_u16(); break;
+                    case 2: ADCBackup[i][count % BACKUP_NUM] = adc_A2.read_u16(); break;
+                    case 3: ADCBackup[i][count % BACKUP_NUM] = adc_A3.read_u16(); break;
+                    case 4: ADCBackup[i][count % BACKUP_NUM] = adc_A4.read_u16(); break;
+                    case 5: ADCBackup[i][count % BACKUP_NUM] = adc_A5.read_u16(); break;
+                    case 6: ADCBackup[i][count % BACKUP_NUM] = adc_A6.read_u16(); break;
+                    case 7: ADCBackup[i][count % BACKUP_NUM] = adc_A7.read_u16(); break;
+                    case 8: ADCBackup[i][count % BACKUP_NUM] = adc_A8.read_u16(); break;
+                    case 9: ADCBackup[i][count % BACKUP_NUM] = adc_A9.read_u16(); break;
+                    }
+                    
+                double total = 0;
+                for(int j = 0; j < BACKUP_NUM; j++)
+                {
+                    total += ADCBackup[i][j];
+                }
+                ADCResult[i] = total / BACKUP_NUM;
             }
-            else {
-                int cmd = analyseURL(httpBuf);
-                switch (cmd) {
-                    case -3:
-                        // update webpage
-                        strcpy(httpHeader, "HTTP/1.0 200 OK");
-                        sendHTTP(client, httpHeader, showWebPage(output));
-                        break;
-
-                    case -2:
-                        // redirect to the right base url
-                        strcpy(httpHeader, "HTTP/1.0 301 Moved Permanently\r\nLocation: ");
-                        sendHTTP(client, httpHeader, movedPermanently(1));
-                        break;
+            
+            
+            
+                sprintf(temp, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d \n", 
+                    ADCResult[0], 
+                    ADCResult[1], 
+                    ADCResult[2], 
+                    ADCResult[3], 
+                    ADCResult[4], 
+                    ADCResult[5], 
+                    ADCResult[6], 
+                    ADCResult[7], 
+                    ADCResult[8], 
+                    ADCResult[9] 
+                    ); */
+               
+    /*
+            sprintf(temp, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d \n", 
+                adc_A0.read_u16(), 
+                adc_A1.read_u16(),
+                adc_A2.read_u16(),
+                adc_A3.read_u16(),
+                adc_A4.read_u16(),
+                adc_A5.read_u16(),
+                adc_A6.read_u16(),
+                adc_A7.read_u16(),
+                adc_A8.read_u16(),
+                adc_A9.read_u16()
+                ); */
+                
+        printf(temp);
+            
+        if(count % 10 == 0) {
+            
+            
+            UDPSocket sock;
+            sock.open(net);
+            
+            uint16_t port = 50000;
+            const char addr[16] = "192.168.0.104";        
+            SocketAddress sAddr(addr, port);
+     
+            if(0 > sock.sendto(sAddr, temp, strlen(temp))) {
+                printf("Error sending data\n");
+                break;
+            }
+            
+            sock.close();
+        }
+                
+        count += 1;
+        
+        led = !led;
+        ThisThread::sleep_for(10);
+    }
 
-                    case -1:
-                        strcpy(httpHeader, "HTTP/1.0 401 Unauthorized");
-                        strcpy(httpBuf, "<h1>401 Unauthorized</h1>");
-                        sendHTTP(client, httpHeader, httpBuf);
-                        break;
-
-                    case 0:
-                        output = OFF;   // output off
-                        strcpy(httpHeader, "HTTP/1.0 200 OK");
-                        sendHTTP(client, httpHeader, showWebPage(output));
-                        break;
-
-                    case 1:
-                        output = ON;    // output on
-                        strcpy(httpHeader, "HTTP/1.0 200 OK");
-                        sendHTTP(client, httpHeader, showWebPage(output));
-                        break;
-                }
-            }
-
-            client->close();
-        }
-    }
 }