SUNGTAE KIM
/
LTECatM1BLDC
LTECatM1BLDC
main.cpp@0:9a05acec5135, 2019-02-27 (annotated)
- Committer:
- stkim92
- Date:
- Wed Feb 27 03:43:16 2019 +0000
- Revision:
- 0:9a05acec5135
LTE Cat.M1 Woorinet_BLDC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stkim92 | 0:9a05acec5135 | 1 | // Sweep the motor speed from full-speed reverse (-1.0) to full speed forwards (1.0) |
stkim92 | 0:9a05acec5135 | 2 | |
stkim92 | 0:9a05acec5135 | 3 | #include "mbed.h" |
stkim92 | 0:9a05acec5135 | 4 | #include "Motor.h" |
stkim92 | 0:9a05acec5135 | 5 | #include <string> |
stkim92 | 0:9a05acec5135 | 6 | //Motor m(D10, D9, D8); // pwm, fwd, rev |
stkim92 | 0:9a05acec5135 | 7 | |
stkim92 | 0:9a05acec5135 | 8 | PwmOut Motor(D10); |
stkim92 | 0:9a05acec5135 | 9 | DigitalOut direction(D9); |
stkim92 | 0:9a05acec5135 | 10 | Serial pc(USBTX, USBRX); // tx, rx |
stkim92 | 0:9a05acec5135 | 11 | Serial lora(PA_13, PA_14); |
stkim92 | 0:9a05acec5135 | 12 | Timer timer; |
stkim92 | 0:9a05acec5135 | 13 | |
stkim92 | 0:9a05acec5135 | 14 | char buffer[200]= {0, }; |
stkim92 | 0:9a05acec5135 | 15 | char ch; |
stkim92 | 0:9a05acec5135 | 16 | int status_flag = 0; |
stkim92 | 0:9a05acec5135 | 17 | int change_flag = 0; |
stkim92 | 0:9a05acec5135 | 18 | |
stkim92 | 0:9a05acec5135 | 19 | |
stkim92 | 0:9a05acec5135 | 20 | bool control_something_on(void) |
stkim92 | 0:9a05acec5135 | 21 | { |
stkim92 | 0:9a05acec5135 | 22 | if(status_flag == 1) { |
stkim92 | 0:9a05acec5135 | 23 | pc.printf("\r\n [Already ON] \r\n"); |
stkim92 | 0:9a05acec5135 | 24 | return false; |
stkim92 | 0:9a05acec5135 | 25 | } else { |
stkim92 | 0:9a05acec5135 | 26 | pc.printf("\r\n [Fan ON] \r\n"); |
stkim92 | 0:9a05acec5135 | 27 | status_flag = 1; |
stkim92 | 0:9a05acec5135 | 28 | |
stkim92 | 0:9a05acec5135 | 29 | Motor.write(0); |
stkim92 | 0:9a05acec5135 | 30 | |
stkim92 | 0:9a05acec5135 | 31 | return true; |
stkim92 | 0:9a05acec5135 | 32 | } |
stkim92 | 0:9a05acec5135 | 33 | /*for (float s= 0.3; s < 1.0 ; s += 0.01) { |
stkim92 | 0:9a05acec5135 | 34 | m.speed(s); |
stkim92 | 0:9a05acec5135 | 35 | if( s > 0.9 ){ |
stkim92 | 0:9a05acec5135 | 36 | s = 0; |
stkim92 | 0:9a05acec5135 | 37 | } |
stkim92 | 0:9a05acec5135 | 38 | wait(0.02); |
stkim92 | 0:9a05acec5135 | 39 | }*/ |
stkim92 | 0:9a05acec5135 | 40 | } |
stkim92 | 0:9a05acec5135 | 41 | |
stkim92 | 0:9a05acec5135 | 42 | bool control_something_off(void) |
stkim92 | 0:9a05acec5135 | 43 | { |
stkim92 | 0:9a05acec5135 | 44 | if( status_flag == 0 ) { |
stkim92 | 0:9a05acec5135 | 45 | pc.printf("\r\n [Already OFF] \r\n"); |
stkim92 | 0:9a05acec5135 | 46 | return false; |
stkim92 | 0:9a05acec5135 | 47 | } else { |
stkim92 | 0:9a05acec5135 | 48 | pc.printf("\r\n [Fan OFF] \r\n"); |
stkim92 | 0:9a05acec5135 | 49 | status_flag = 0; |
stkim92 | 0:9a05acec5135 | 50 | Motor.write(1); |
stkim92 | 0:9a05acec5135 | 51 | |
stkim92 | 0:9a05acec5135 | 52 | return true; |
stkim92 | 0:9a05acec5135 | 53 | |
stkim92 | 0:9a05acec5135 | 54 | //m.speed(0); |
stkim92 | 0:9a05acec5135 | 55 | } |
stkim92 | 0:9a05acec5135 | 56 | } |
stkim92 | 0:9a05acec5135 | 57 | |
stkim92 | 0:9a05acec5135 | 58 | void recv(void) |
stkim92 | 0:9a05acec5135 | 59 | { |
stkim92 | 0:9a05acec5135 | 60 | string str; |
stkim92 | 0:9a05acec5135 | 61 | lora.gets(buffer,49); |
stkim92 | 0:9a05acec5135 | 62 | str = buffer; |
stkim92 | 0:9a05acec5135 | 63 | |
stkim92 | 0:9a05acec5135 | 64 | pc.printf(buffer); //Debug for recv |
stkim92 | 0:9a05acec5135 | 65 | while(1) { |
stkim92 | 0:9a05acec5135 | 66 | if(!(lora.readable())) { |
stkim92 | 0:9a05acec5135 | 67 | break; |
stkim92 | 0:9a05acec5135 | 68 | } |
stkim92 | 0:9a05acec5135 | 69 | ch = lora.getc(); |
stkim92 | 0:9a05acec5135 | 70 | } |
stkim92 | 0:9a05acec5135 | 71 | if(strstr(buffer, "\"led\":0") != NULL) { |
stkim92 | 0:9a05acec5135 | 72 | pc.printf("\r\n RECV CONTROL MSG [OFF]\r\n"); |
stkim92 | 0:9a05acec5135 | 73 | if( control_something_off() == true ) { |
stkim92 | 0:9a05acec5135 | 74 | lora.printf("AT+SKTPDAT=1,telemetry,0,{\"FAN\":\"off\"}\r\n"); |
stkim92 | 0:9a05acec5135 | 75 | } |
stkim92 | 0:9a05acec5135 | 76 | |
stkim92 | 0:9a05acec5135 | 77 | } |
stkim92 | 0:9a05acec5135 | 78 | if(strstr(buffer, "\"led\":1") != NULL) { |
stkim92 | 0:9a05acec5135 | 79 | pc.printf("\r\n RECV CONTROL MSG [ON]\r\n"); |
stkim92 | 0:9a05acec5135 | 80 | if( control_something_on() == true ) { |
stkim92 | 0:9a05acec5135 | 81 | lora.printf("AT+SKTPDAT=1,telemetry,0,{\"FAN\":\"on\"}\r\n"); |
stkim92 | 0:9a05acec5135 | 82 | } |
stkim92 | 0:9a05acec5135 | 83 | } |
stkim92 | 0:9a05acec5135 | 84 | |
stkim92 | 0:9a05acec5135 | 85 | |
stkim92 | 0:9a05acec5135 | 86 | } |
stkim92 | 0:9a05acec5135 | 87 | |
stkim92 | 0:9a05acec5135 | 88 | |
stkim92 | 0:9a05acec5135 | 89 | int main() |
stkim92 | 0:9a05acec5135 | 90 | { |
stkim92 | 0:9a05acec5135 | 91 | pc.baud(115200); |
stkim92 | 0:9a05acec5135 | 92 | lora.baud(115200); |
stkim92 | 0:9a05acec5135 | 93 | |
stkim92 | 0:9a05acec5135 | 94 | unsigned int curr_Time1; |
stkim92 | 0:9a05acec5135 | 95 | unsigned int curr_Time2; |
stkim92 | 0:9a05acec5135 | 96 | |
stkim92 | 0:9a05acec5135 | 97 | direction.write(1); // fan |
stkim92 | 0:9a05acec5135 | 98 | Motor.period_ms(1); // fan |
stkim92 | 0:9a05acec5135 | 99 | Motor.write(1); |
stkim92 | 0:9a05acec5135 | 100 | lora.printf("AT*RNDISDATA=1\r\n"); |
stkim92 | 0:9a05acec5135 | 101 | wait(1); |
stkim92 | 0:9a05acec5135 | 102 | |
stkim92 | 0:9a05acec5135 | 103 | pc.printf("Hello World!\n\r"); |
stkim92 | 0:9a05acec5135 | 104 | timer.start(); |
stkim92 | 0:9a05acec5135 | 105 | curr_Time1 = timer.read(); |
stkim92 | 0:9a05acec5135 | 106 | |
stkim92 | 0:9a05acec5135 | 107 | //lora.printf("AT+SKTPCON=1,MQTT,218.53.242.111,1883,120,1,simple_v1.0,0e768bc0370e11e8a3f1,ServiceID,Device2\r\n"); |
stkim92 | 0:9a05acec5135 | 108 | lora.printf("AT+SKTPCON=1,MQTT,211.234.246.112,1883,120,1,simple_v1.0,795a3b00ac1411e8aaea,smartfarm1,smartfarmFAN\r\n"); |
stkim92 | 0:9a05acec5135 | 109 | pc.printf("[SKTPCON OK]\n\r"); |
stkim92 | 0:9a05acec5135 | 110 | lora.attach(&recv,Serial::RxIrq); |
stkim92 | 0:9a05acec5135 | 111 | } |