KIM YOUNG SEONG / Mbed OS 429_adc_network_adc_16_channel

Dependencies:   MQTT

Committer:
cherrychance
Date:
Mon Feb 22 06:07:17 2021 +0000
Revision:
13:f56c6e844cc3
Parent:
12:138b2b3d041a
stm32_f429zi + adc 16 channel mux + ethernet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:e455fdb56bc8 1 #include "mbed.h"
hudakz 0:e455fdb56bc8 2 #include "EthernetInterface.h"
hudakz 0:e455fdb56bc8 3 #include "TCPSocket.h"
cherrychance 13:f56c6e844cc3 4 #include "MQTTmbed.h"
cherrychance 13:f56c6e844cc3 5 #include "MQTTClient.h"
cherrychance 13:f56c6e844cc3 6
hudakz 0:e455fdb56bc8 7 #include <stdio.h>
hudakz 0:e455fdb56bc8 8 #include <string>
hudakz 9:161bed13b17e 9 using namespace std;
hudakz 9:161bed13b17e 10
cherrychance 13:f56c6e844cc3 11
cherrychance 13:f56c6e844cc3 12
cherrychance 13:f56c6e844cc3 13 #define MQTT_HOST "192.168.0.113"
cherrychance 13:f56c6e844cc3 14 #define MQTT_PORT 1883
cherrychance 13:f56c6e844cc3 15 #define MQTT_TOPIC "v1/devices/me/telemetry"
cherrychance 13:f56c6e844cc3 16
cherrychance 13:f56c6e844cc3 17 #include "MQTTNetwork.h"
cherrychance 13:f56c6e844cc3 18
cherrychance 13:f56c6e844cc3 19 #define IP "192.168.0.181"
cherrychance 13:f56c6e844cc3 20 #define GATEWAY "192.168.0.1"
hudakz 9:161bed13b17e 21 #define NETMASK "255.255.255.0"
hudakz 11:101be77aa5db 22
hudakz 11:101be77aa5db 23 /* */
hudakz 8:47b0cb4b5b7d 24 EthernetInterface* net;
hudakz 9:161bed13b17e 25
cherrychance 13:f56c6e844cc3 26 AnalogIn adc_A0(A0); // PA_3
cherrychance 13:f56c6e844cc3 27 AnalogIn adc_A1(A1); // PC_0
cherrychance 13:f56c6e844cc3 28 AnalogIn adc_A2(A2); // PC_3
cherrychance 13:f56c6e844cc3 29 AnalogIn adc_A3(PF_6);
cherrychance 13:f56c6e844cc3 30 AnalogIn adc_A4(A4); // PF_5
cherrychance 13:f56c6e844cc3 31 AnalogIn adc_A5(A5); // PF_10
cherrychance 13:f56c6e844cc3 32 AnalogIn adc_A6(PF_8);
cherrychance 13:f56c6e844cc3 33 AnalogIn adc_A7(PF_7);
cherrychance 13:f56c6e844cc3 34 AnalogIn adc_A8(PF_9);
cherrychance 13:f56c6e844cc3 35 AnalogIn adc_A9(PF_4);
hudakz 9:161bed13b17e 36
cherrychance 13:f56c6e844cc3 37 DigitalOut led(LED1);
hudakz 9:161bed13b17e 38
cherrychance 13:f56c6e844cc3 39 DigitalOut gpS0(PD_7);
cherrychance 13:f56c6e844cc3 40 DigitalOut gpS1(PD_6);
cherrychance 13:f56c6e844cc3 41 DigitalOut gpS2(PD_5);
cherrychance 13:f56c6e844cc3 42 DigitalOut gpS3(PD_4);
cherrychance 13:f56c6e844cc3 43
cherrychance 13:f56c6e844cc3 44
cherrychance 13:f56c6e844cc3 45 BufferedSerial pc(SERIAL_TX, SERIAL_RX, 115200);
hudakz 9:161bed13b17e 46
cherrychance 13:f56c6e844cc3 47 #define ADC_NUM 9 + 16
cherrychance 13:f56c6e844cc3 48 uint16_t ADCResult[ADC_NUM];
hudakz 9:161bed13b17e 49
cherrychance 13:f56c6e844cc3 50 #define BACKUP_NUM 50
cherrychance 13:f56c6e844cc3 51 uint16_t ADCBackup[ADC_NUM][BACKUP_NUM] = { 0x00, };
hudakz 9:161bed13b17e 52
cherrychance 13:f56c6e844cc3 53 Thread thread;
hudakz 9:161bed13b17e 54
cherrychance 13:f56c6e844cc3 55 #define MUX_MAX_NUM 16
cherrychance 13:f56c6e844cc3 56 int muxChannel[16][4]={
cherrychance 13:f56c6e844cc3 57 {0,0,0,0}, //channel 0
cherrychance 13:f56c6e844cc3 58 {1,0,0,0}, //channel 1
cherrychance 13:f56c6e844cc3 59 {0,1,0,0}, //channel 2
cherrychance 13:f56c6e844cc3 60 {1,1,0,0}, //channel 3
cherrychance 13:f56c6e844cc3 61 {0,0,1,0}, //channel 4
cherrychance 13:f56c6e844cc3 62 {1,0,1,0}, //channel 5
cherrychance 13:f56c6e844cc3 63 {0,1,1,0}, //channel 6
cherrychance 13:f56c6e844cc3 64 {1,1,1,0}, //channel 7
cherrychance 13:f56c6e844cc3 65 {0,0,0,1}, //channel 8
cherrychance 13:f56c6e844cc3 66 {1,0,0,1}, //channel 9
cherrychance 13:f56c6e844cc3 67 {0,1,0,1}, //channel 10
cherrychance 13:f56c6e844cc3 68 {1,1,0,1}, //channel 11
cherrychance 13:f56c6e844cc3 69 {0,0,1,1}, //channel 12
cherrychance 13:f56c6e844cc3 70 {1,0,1,1}, //channel 13
cherrychance 13:f56c6e844cc3 71 {0,1,1,1}, //channel 14
cherrychance 13:f56c6e844cc3 72 {1,1,1,1} //channel 15
cherrychance 13:f56c6e844cc3 73 };
cherrychance 13:f56c6e844cc3 74
cherrychance 13:f56c6e844cc3 75 int readADCMux(int _ch)
cherrychance 13:f56c6e844cc3 76 {
cherrychance 13:f56c6e844cc3 77 // 4개 컨트롤핀 (s0,s1,s2,s3) 핀 설정
cherrychance 13:f56c6e844cc3 78 gpS0 = muxChannel[_ch][0];
cherrychance 13:f56c6e844cc3 79 gpS1 = muxChannel[_ch][1];
cherrychance 13:f56c6e844cc3 80 gpS2 = muxChannel[_ch][2];
cherrychance 13:f56c6e844cc3 81 gpS3 = muxChannel[_ch][3];
hudakz 9:161bed13b17e 82
cherrychance 13:f56c6e844cc3 83 // SIG pin 읽기
cherrychance 13:f56c6e844cc3 84 return adc_A0.read_u16();
cherrychance 13:f56c6e844cc3 85 }
cherrychance 13:f56c6e844cc3 86
hudakz 9:161bed13b17e 87
hudakz 0:e455fdb56bc8 88 /**
hudakz 0:e455fdb56bc8 89 * @brief
hudakz 0:e455fdb56bc8 90 * @note
hudakz 0:e455fdb56bc8 91 * @param
hudakz 0:e455fdb56bc8 92 * @retval
hudakz 0:e455fdb56bc8 93 */
hudakz 9:161bed13b17e 94 int main(void)
hudakz 9:161bed13b17e 95 {
cherrychance 13:f56c6e844cc3 96
cherrychance 13:f56c6e844cc3 97 gpS0 = 1;
cherrychance 13:f56c6e844cc3 98 gpS1 = 1;
cherrychance 13:f56c6e844cc3 99 gpS2 = 1;
cherrychance 13:f56c6e844cc3 100 gpS3 = 1;
cherrychance 13:f56c6e844cc3 101 // printf("\r\n Starting \r\n");
hudakz 9:161bed13b17e 102
hudakz 11:101be77aa5db 103 //ethLed.start(eth_led);
hudakz 11:101be77aa5db 104 //net = NetworkInterface::get_default_instance();
hudakz 8:47b0cb4b5b7d 105 net = new EthernetInterface();
hudakz 8:47b0cb4b5b7d 106
hudakz 11:101be77aa5db 107 //net->set_network("192.168.1.181","255.255.255.0","192.168.1.1"); // use static IP address, netmask, gateway
hudakz 8:47b0cb4b5b7d 108 if (!net) {
hudakz 11:101be77aa5db 109 printf("Error! No network inteface found.\n");
hudakz 8:47b0cb4b5b7d 110 return 0;
hudakz 8:47b0cb4b5b7d 111 }
hudakz 0:e455fdb56bc8 112
hudakz 8:47b0cb4b5b7d 113 //net->set_network (IP, NETMASK, GATEWAY); // include this for using static IP address
hudakz 9:161bed13b17e 114 nsapi_size_or_error_t r = net->connect();
hudakz 11:101be77aa5db 115 if (r != NSAPI_ERROR_OK) {
hudakz 11:101be77aa5db 116 printf("Error! net->connect() returned: %d\n", r);
hudakz 8:47b0cb4b5b7d 117 return r;
hudakz 8:47b0cb4b5b7d 118 }
hudakz 7:02a0635aeeac 119
hudakz 9:161bed13b17e 120 // Show the network address
hudakz 11:101be77aa5db 121 SocketAddress addr;
hudakz 11:101be77aa5db 122 net->get_ip_address(&addr);
cherrychance 13:f56c6e844cc3 123 //printf("IP address: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None");
hudakz 11:101be77aa5db 124 net->get_netmask(&addr);
cherrychance 13:f56c6e844cc3 125 //printf("Netmask: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None");
hudakz 11:101be77aa5db 126 net->get_gateway(&addr);
cherrychance 13:f56c6e844cc3 127 //printf("Gateway: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None");
hudakz 9:161bed13b17e 128
cherrychance 13:f56c6e844cc3 129 int count = 0;
cherrychance 13:f56c6e844cc3 130 while(1)
cherrychance 13:f56c6e844cc3 131 {
cherrychance 13:f56c6e844cc3 132 // printf("percentage: %3.3f%%\n", adc_A0.read()*100.0f);
cherrychance 13:f56c6e844cc3 133 char temp[128] = { 0x00, };
cherrychance 13:f56c6e844cc3 134 for(int i = 0; i < MUX_MAX_NUM; i++)
cherrychance 13:f56c6e844cc3 135 {
cherrychance 13:f56c6e844cc3 136 ADCBackup[i][count % MUX_MAX_NUM] = readADCMux(i);
cherrychance 13:f56c6e844cc3 137 double total = 0;
cherrychance 13:f56c6e844cc3 138 for(int j = 0; j < BACKUP_NUM; j++)
cherrychance 13:f56c6e844cc3 139 {
cherrychance 13:f56c6e844cc3 140 total += ADCBackup[i][j];
cherrychance 13:f56c6e844cc3 141 }
cherrychance 13:f56c6e844cc3 142 ADCResult[i] = total / BACKUP_NUM;
cherrychance 13:f56c6e844cc3 143 }
cherrychance 13:f56c6e844cc3 144
cherrychance 13:f56c6e844cc3 145 sprintf(temp, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d \n",
cherrychance 13:f56c6e844cc3 146 ADCResult[0],
cherrychance 13:f56c6e844cc3 147 ADCResult[1],
cherrychance 13:f56c6e844cc3 148 ADCResult[2],
cherrychance 13:f56c6e844cc3 149 ADCResult[3],
cherrychance 13:f56c6e844cc3 150 ADCResult[4],
cherrychance 13:f56c6e844cc3 151 ADCResult[5],
cherrychance 13:f56c6e844cc3 152 ADCResult[6],
cherrychance 13:f56c6e844cc3 153 ADCResult[7],
cherrychance 13:f56c6e844cc3 154 ADCResult[8],
cherrychance 13:f56c6e844cc3 155 ADCResult[9],
cherrychance 13:f56c6e844cc3 156 ADCResult[10],
cherrychance 13:f56c6e844cc3 157 ADCResult[11],
cherrychance 13:f56c6e844cc3 158 ADCResult[12],
cherrychance 13:f56c6e844cc3 159 ADCResult[13],
cherrychance 13:f56c6e844cc3 160 ADCResult[14],
cherrychance 13:f56c6e844cc3 161 ADCResult[15]
cherrychance 13:f56c6e844cc3 162
cherrychance 13:f56c6e844cc3 163 );
cherrychance 13:f56c6e844cc3 164
cherrychance 13:f56c6e844cc3 165
cherrychance 13:f56c6e844cc3 166 /*
cherrychance 13:f56c6e844cc3 167 for(int i = 0; i < ADC_NUM; i++) {
cherrychance 13:f56c6e844cc3 168
cherrychance 13:f56c6e844cc3 169 switch(i)
cherrychance 13:f56c6e844cc3 170 {
cherrychance 13:f56c6e844cc3 171 case 0: ADCBackup[i][count % BACKUP_NUM] = adc_A0.read_u16(); break;
cherrychance 13:f56c6e844cc3 172 case 1: ADCBackup[i][count % BACKUP_NUM] = adc_A1.read_u16(); break;
cherrychance 13:f56c6e844cc3 173 case 2: ADCBackup[i][count % BACKUP_NUM] = adc_A2.read_u16(); break;
cherrychance 13:f56c6e844cc3 174 case 3: ADCBackup[i][count % BACKUP_NUM] = adc_A3.read_u16(); break;
cherrychance 13:f56c6e844cc3 175 case 4: ADCBackup[i][count % BACKUP_NUM] = adc_A4.read_u16(); break;
cherrychance 13:f56c6e844cc3 176 case 5: ADCBackup[i][count % BACKUP_NUM] = adc_A5.read_u16(); break;
cherrychance 13:f56c6e844cc3 177 case 6: ADCBackup[i][count % BACKUP_NUM] = adc_A6.read_u16(); break;
cherrychance 13:f56c6e844cc3 178 case 7: ADCBackup[i][count % BACKUP_NUM] = adc_A7.read_u16(); break;
cherrychance 13:f56c6e844cc3 179 case 8: ADCBackup[i][count % BACKUP_NUM] = adc_A8.read_u16(); break;
cherrychance 13:f56c6e844cc3 180 case 9: ADCBackup[i][count % BACKUP_NUM] = adc_A9.read_u16(); break;
cherrychance 13:f56c6e844cc3 181 }
cherrychance 13:f56c6e844cc3 182
cherrychance 13:f56c6e844cc3 183 double total = 0;
cherrychance 13:f56c6e844cc3 184 for(int j = 0; j < BACKUP_NUM; j++)
cherrychance 13:f56c6e844cc3 185 {
cherrychance 13:f56c6e844cc3 186 total += ADCBackup[i][j];
cherrychance 13:f56c6e844cc3 187 }
cherrychance 13:f56c6e844cc3 188 ADCResult[i] = total / BACKUP_NUM;
hudakz 11:101be77aa5db 189 }
cherrychance 13:f56c6e844cc3 190
cherrychance 13:f56c6e844cc3 191
cherrychance 13:f56c6e844cc3 192
cherrychance 13:f56c6e844cc3 193 sprintf(temp, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d \n",
cherrychance 13:f56c6e844cc3 194 ADCResult[0],
cherrychance 13:f56c6e844cc3 195 ADCResult[1],
cherrychance 13:f56c6e844cc3 196 ADCResult[2],
cherrychance 13:f56c6e844cc3 197 ADCResult[3],
cherrychance 13:f56c6e844cc3 198 ADCResult[4],
cherrychance 13:f56c6e844cc3 199 ADCResult[5],
cherrychance 13:f56c6e844cc3 200 ADCResult[6],
cherrychance 13:f56c6e844cc3 201 ADCResult[7],
cherrychance 13:f56c6e844cc3 202 ADCResult[8],
cherrychance 13:f56c6e844cc3 203 ADCResult[9]
cherrychance 13:f56c6e844cc3 204 ); */
cherrychance 13:f56c6e844cc3 205
cherrychance 13:f56c6e844cc3 206 /*
cherrychance 13:f56c6e844cc3 207 sprintf(temp, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d \n",
cherrychance 13:f56c6e844cc3 208 adc_A0.read_u16(),
cherrychance 13:f56c6e844cc3 209 adc_A1.read_u16(),
cherrychance 13:f56c6e844cc3 210 adc_A2.read_u16(),
cherrychance 13:f56c6e844cc3 211 adc_A3.read_u16(),
cherrychance 13:f56c6e844cc3 212 adc_A4.read_u16(),
cherrychance 13:f56c6e844cc3 213 adc_A5.read_u16(),
cherrychance 13:f56c6e844cc3 214 adc_A6.read_u16(),
cherrychance 13:f56c6e844cc3 215 adc_A7.read_u16(),
cherrychance 13:f56c6e844cc3 216 adc_A8.read_u16(),
cherrychance 13:f56c6e844cc3 217 adc_A9.read_u16()
cherrychance 13:f56c6e844cc3 218 ); */
cherrychance 13:f56c6e844cc3 219
cherrychance 13:f56c6e844cc3 220 printf(temp);
cherrychance 13:f56c6e844cc3 221
cherrychance 13:f56c6e844cc3 222 if(count % 10 == 0) {
cherrychance 13:f56c6e844cc3 223
cherrychance 13:f56c6e844cc3 224
cherrychance 13:f56c6e844cc3 225 UDPSocket sock;
cherrychance 13:f56c6e844cc3 226 sock.open(net);
cherrychance 13:f56c6e844cc3 227
cherrychance 13:f56c6e844cc3 228 uint16_t port = 50000;
cherrychance 13:f56c6e844cc3 229 const char addr[16] = "192.168.0.104";
cherrychance 13:f56c6e844cc3 230 SocketAddress sAddr(addr, port);
cherrychance 13:f56c6e844cc3 231
cherrychance 13:f56c6e844cc3 232 if(0 > sock.sendto(sAddr, temp, strlen(temp))) {
cherrychance 13:f56c6e844cc3 233 printf("Error sending data\n");
cherrychance 13:f56c6e844cc3 234 break;
cherrychance 13:f56c6e844cc3 235 }
cherrychance 13:f56c6e844cc3 236
cherrychance 13:f56c6e844cc3 237 sock.close();
cherrychance 13:f56c6e844cc3 238 }
cherrychance 13:f56c6e844cc3 239
cherrychance 13:f56c6e844cc3 240 count += 1;
cherrychance 13:f56c6e844cc3 241
cherrychance 13:f56c6e844cc3 242 led = !led;
cherrychance 13:f56c6e844cc3 243 ThisThread::sleep_for(10);
cherrychance 13:f56c6e844cc3 244 }
hudakz 9:161bed13b17e 245
hudakz 9:161bed13b17e 246 }