ECE4180 Final Project
Dependencies: 4DGL-uLCD-SE ESP8266NodeMCUInterface Motor mbed
Fork of IoT_Blinds_Control by
main.cpp@0:d0cfb841595e, 2016-12-10 (annotated)
- Committer:
- jhooper7
- Date:
- Sat Dec 10 23:40:42 2016 +0000
- Revision:
- 0:d0cfb841595e
- Child:
- 1:d103c1f37bc6
final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jhooper7 | 0:d0cfb841595e | 1 | #include "mbed.h" |
jhooper7 | 0:d0cfb841595e | 2 | #include "TCPSocketConnection.h" |
jhooper7 | 0:d0cfb841595e | 3 | #include "ESP8266Interface.h" |
jhooper7 | 0:d0cfb841595e | 4 | #include "uLCD_4DGL.h" |
jhooper7 | 0:d0cfb841595e | 5 | #include "Motor.h" |
jhooper7 | 0:d0cfb841595e | 6 | |
jhooper7 | 0:d0cfb841595e | 7 | ESP8266Interface wifi(p28, p27, p26);//, "JACK", "ClarentBloodArthur"); |
jhooper7 | 0:d0cfb841595e | 8 | TCPSocketConnection socket; |
jhooper7 | 0:d0cfb841595e | 9 | //Serial pc(USBTX, USBRX); //Used for testing functionality without wifi |
jhooper7 | 0:d0cfb841595e | 10 | Motor m1(p23, p5, p6); //pwm, fwd, rev //this one is for raising/lowering blinds |
jhooper7 | 0:d0cfb841595e | 11 | Motor m2(p24, p7, p8); //this one is for changing the tilt of them. |
jhooper7 | 0:d0cfb841595e | 12 | DigitalOut led1(LED1); |
jhooper7 | 0:d0cfb841595e | 13 | DigitalOut led2(LED2); |
jhooper7 | 0:d0cfb841595e | 14 | DigitalOut led3(LED3); |
jhooper7 | 0:d0cfb841595e | 15 | DigitalOut led4(LED4); |
jhooper7 | 0:d0cfb841595e | 16 | |
jhooper7 | 0:d0cfb841595e | 17 | int heightpos=0; |
jhooper7 | 0:d0cfb841595e | 18 | int tiltpos=0; |
jhooper7 | 0:d0cfb841595e | 19 | float mspeed=1; //can be changed based on rate here; |
jhooper7 | 0:d0cfb841595e | 20 | char inData[5]; |
jhooper7 | 0:d0cfb841595e | 21 | char previnData[5]; |
jhooper7 | 0:d0cfb841595e | 22 | char inChar; |
jhooper7 | 0:d0cfb841595e | 23 | int index=0; |
jhooper7 | 0:d0cfb841595e | 24 | |
jhooper7 | 0:d0cfb841595e | 25 | //CHANGE THESE |
jhooper7 | 0:d0cfb841595e | 26 | const char* wifissid = "SSID"; |
jhooper7 | 0:d0cfb841595e | 27 | const char* password = "Pass"; |
jhooper7 | 0:d0cfb841595e | 28 | const char* ipaddress="IP"; |
jhooper7 | 0:d0cfb841595e | 29 | const int port = PORT; |
jhooper7 | 0:d0cfb841595e | 30 | |
jhooper7 | 0:d0cfb841595e | 31 | void closetiltup(){//sets front of blinds to be tilted up |
jhooper7 | 0:d0cfb841595e | 32 | while(tiltpos>-10){ |
jhooper7 | 0:d0cfb841595e | 33 | m2.speed(-mspeed); |
jhooper7 | 0:d0cfb841595e | 34 | wait(mspeed*.02); |
jhooper7 | 0:d0cfb841595e | 35 | tiltpos=tiltpos-(mspeed); |
jhooper7 | 0:d0cfb841595e | 36 | } |
jhooper7 | 0:d0cfb841595e | 37 | m2.speed(0); |
jhooper7 | 0:d0cfb841595e | 38 | } |
jhooper7 | 0:d0cfb841595e | 39 | |
jhooper7 | 0:d0cfb841595e | 40 | void closetiltdown(){ |
jhooper7 | 0:d0cfb841595e | 41 | while(tiltpos<10){ |
jhooper7 | 0:d0cfb841595e | 42 | m2.speed(mspeed); |
jhooper7 | 0:d0cfb841595e | 43 | wait(mspeed*.02); |
jhooper7 | 0:d0cfb841595e | 44 | tiltpos=tiltpos+(mspeed); |
jhooper7 | 0:d0cfb841595e | 45 | } |
jhooper7 | 0:d0cfb841595e | 46 | m2.speed(0); |
jhooper7 | 0:d0cfb841595e | 47 | } |
jhooper7 | 0:d0cfb841595e | 48 | |
jhooper7 | 0:d0cfb841595e | 49 | void tiltto(float pos){ |
jhooper7 | 0:d0cfb841595e | 50 | if(pos>tiltpos){ |
jhooper7 | 0:d0cfb841595e | 51 | m2.speed(mspeed); |
jhooper7 | 0:d0cfb841595e | 52 | while(tiltpos<pos){ |
jhooper7 | 0:d0cfb841595e | 53 | wait(mspeed*.02); |
jhooper7 | 0:d0cfb841595e | 54 | tiltpos=tiltpos+(mspeed); |
jhooper7 | 0:d0cfb841595e | 55 | } |
jhooper7 | 0:d0cfb841595e | 56 | m2.speed(0); |
jhooper7 | 0:d0cfb841595e | 57 | } |
jhooper7 | 0:d0cfb841595e | 58 | else if(pos<tiltpos){ |
jhooper7 | 0:d0cfb841595e | 59 | m2.speed(-mspeed); |
jhooper7 | 0:d0cfb841595e | 60 | while(tiltpos>pos){ |
jhooper7 | 0:d0cfb841595e | 61 | wait(mspeed*.02); |
jhooper7 | 0:d0cfb841595e | 62 | tiltpos=tiltpos-(mspeed); |
jhooper7 | 0:d0cfb841595e | 63 | } |
jhooper7 | 0:d0cfb841595e | 64 | m2.speed(0); |
jhooper7 | 0:d0cfb841595e | 65 | } |
jhooper7 | 0:d0cfb841595e | 66 | else{ |
jhooper7 | 0:d0cfb841595e | 67 | } |
jhooper7 | 0:d0cfb841595e | 68 | } |
jhooper7 | 0:d0cfb841595e | 69 | |
jhooper7 | 0:d0cfb841595e | 70 | void fullup(){ |
jhooper7 | 0:d0cfb841595e | 71 | while(heightpos<10){ |
jhooper7 | 0:d0cfb841595e | 72 | m1.speed(mspeed); |
jhooper7 | 0:d0cfb841595e | 73 | wait(mspeed*2); |
jhooper7 | 0:d0cfb841595e | 74 | heightpos=heightpos+(mspeed); |
jhooper7 | 0:d0cfb841595e | 75 | } |
jhooper7 | 0:d0cfb841595e | 76 | m1.speed(0); |
jhooper7 | 0:d0cfb841595e | 77 | } |
jhooper7 | 0:d0cfb841595e | 78 | |
jhooper7 | 0:d0cfb841595e | 79 | void fulldown(){ |
jhooper7 | 0:d0cfb841595e | 80 | while(heightpos>0){ |
jhooper7 | 0:d0cfb841595e | 81 | m1.speed(-mspeed); |
jhooper7 | 0:d0cfb841595e | 82 | wait(mspeed); |
jhooper7 | 0:d0cfb841595e | 83 | heightpos=heightpos-(mspeed); |
jhooper7 | 0:d0cfb841595e | 84 | } |
jhooper7 | 0:d0cfb841595e | 85 | m1.speed(0); |
jhooper7 | 0:d0cfb841595e | 86 | } |
jhooper7 | 0:d0cfb841595e | 87 | |
jhooper7 | 0:d0cfb841595e | 88 | void moveto(float pos){ |
jhooper7 | 0:d0cfb841595e | 89 | if(pos>heightpos){ |
jhooper7 | 0:d0cfb841595e | 90 | m1.speed(mspeed); |
jhooper7 | 0:d0cfb841595e | 91 | while(heightpos<pos){ |
jhooper7 | 0:d0cfb841595e | 92 | wait(mspeed*1.3); |
jhooper7 | 0:d0cfb841595e | 93 | heightpos=heightpos+(mspeed); |
jhooper7 | 0:d0cfb841595e | 94 | } |
jhooper7 | 0:d0cfb841595e | 95 | m1.speed(0); |
jhooper7 | 0:d0cfb841595e | 96 | } |
jhooper7 | 0:d0cfb841595e | 97 | else if(pos<heightpos){ |
jhooper7 | 0:d0cfb841595e | 98 | m1.speed(-mspeed); |
jhooper7 | 0:d0cfb841595e | 99 | while(heightpos>pos){ |
jhooper7 | 0:d0cfb841595e | 100 | wait(mspeed*.08); |
jhooper7 | 0:d0cfb841595e | 101 | heightpos=heightpos-(mspeed); |
jhooper7 | 0:d0cfb841595e | 102 | } |
jhooper7 | 0:d0cfb841595e | 103 | m1.speed(0); |
jhooper7 | 0:d0cfb841595e | 104 | } |
jhooper7 | 0:d0cfb841595e | 105 | else{ |
jhooper7 | 0:d0cfb841595e | 106 | } |
jhooper7 | 0:d0cfb841595e | 107 | } |
jhooper7 | 0:d0cfb841595e | 108 | |
jhooper7 | 0:d0cfb841595e | 109 | void resettozero(){ |
jhooper7 | 0:d0cfb841595e | 110 | moveto(0); |
jhooper7 | 0:d0cfb841595e | 111 | tiltto(0); |
jhooper7 | 0:d0cfb841595e | 112 | } |
jhooper7 | 0:d0cfb841595e | 113 | |
jhooper7 | 0:d0cfb841595e | 114 | int main() |
jhooper7 | 0:d0cfb841595e | 115 | { |
jhooper7 | 0:d0cfb841595e | 116 | uLCD_4DGL ulcd(p9, p10, p11); // this is for an mbed-side print of current status |
jhooper7 | 0:d0cfb841595e | 117 | float num; |
jhooper7 | 0:d0cfb841595e | 118 | float prevnum; |
jhooper7 | 0:d0cfb841595e | 119 | int n; |
jhooper7 | 0:d0cfb841595e | 120 | wifi.init(); |
jhooper7 | 0:d0cfb841595e | 121 | bool result; |
jhooper7 | 0:d0cfb841595e | 122 | result = wifi.connect(wifissid, password); |
jhooper7 | 0:d0cfb841595e | 123 | if(!result){ |
jhooper7 | 0:d0cfb841595e | 124 | ulcd.printf("error"); |
jhooper7 | 0:d0cfb841595e | 125 | while(!result){ |
jhooper7 | 0:d0cfb841595e | 126 | result=wifi.connect(wifissid, password); |
jhooper7 | 0:d0cfb841595e | 127 | } |
jhooper7 | 0:d0cfb841595e | 128 | } |
jhooper7 | 0:d0cfb841595e | 129 | ulcd.printf("wifi c"); |
jhooper7 | 0:d0cfb841595e | 130 | if(socket.connect(ipaddress, port)<0){ |
jhooper7 | 0:d0cfb841595e | 131 | ulcd.printf("\ndevice nc");//hosted via laptop |
jhooper7 | 0:d0cfb841595e | 132 | while(socket.connect(ipaddress,port)<0){ |
jhooper7 | 0:d0cfb841595e | 133 | } |
jhooper7 | 0:d0cfb841595e | 134 | } |
jhooper7 | 0:d0cfb841595e | 135 | ulcd.printf("\ndevice c"); |
jhooper7 | 0:d0cfb841595e | 136 | //socket.send("1", 1); |
jhooper7 | 0:d0cfb841595e | 137 | while(1){ |
jhooper7 | 0:d0cfb841595e | 138 | /* if(!wifi.is_connected()){ |
jhooper7 | 0:d0cfb841595e | 139 | result = wifi.connect(wifissid, password); |
jhooper7 | 0:d0cfb841595e | 140 | if(!result){ |
jhooper7 | 0:d0cfb841595e | 141 | ulcd.printf("error"); |
jhooper7 | 0:d0cfb841595e | 142 | while(!result){ |
jhooper7 | 0:d0cfb841595e | 143 | result=wifi.connect(wifissid, password); |
jhooper7 | 0:d0cfb841595e | 144 | } |
jhooper7 | 0:d0cfb841595e | 145 | } |
jhooper7 | 0:d0cfb841595e | 146 | } |
jhooper7 | 0:d0cfb841595e | 147 | while(wifi.is_connected()){*/ |
jhooper7 | 0:d0cfb841595e | 148 | led1=!led1; |
jhooper7 | 0:d0cfb841595e | 149 | /* if(!socket.is_connected()){ |
jhooper7 | 0:d0cfb841595e | 150 | while(socket.connect(ipaddress,port)<0){ |
jhooper7 | 0:d0cfb841595e | 151 | led2=!led2; |
jhooper7 | 0:d0cfb841595e | 152 | } |
jhooper7 | 0:d0cfb841595e | 153 | }*/ |
jhooper7 | 0:d0cfb841595e | 154 | n=0; |
jhooper7 | 0:d0cfb841595e | 155 | socket.send("1",1); |
jhooper7 | 0:d0cfb841595e | 156 | // socket.close(); |
jhooper7 | 0:d0cfb841595e | 157 | //while(socket.connect(ipaddress,port)<0){ |
jhooper7 | 0:d0cfb841595e | 158 | led2=!led2; |
jhooper7 | 0:d0cfb841595e | 159 | // } |
jhooper7 | 0:d0cfb841595e | 160 | n=socket.receive(inData, sizeof(inData)); |
jhooper7 | 0:d0cfb841595e | 161 | led3=!led3; |
jhooper7 | 0:d0cfb841595e | 162 | ulcd.cls(); |
jhooper7 | 0:d0cfb841595e | 163 | // ulcd.printf("\n%c%c%c%c%c", inData[0], inData[1], inData[2], inData[3], inData[4]); |
jhooper7 | 0:d0cfb841595e | 164 | prevnum=num; |
jhooper7 | 0:d0cfb841595e | 165 | num = ((inData[3]-48)*10)+(inData[4]-48); |
jhooper7 | 0:d0cfb841595e | 166 | // ulcd.printf("\n%f", num); |
jhooper7 | 0:d0cfb841595e | 167 | if(inData[2]=='-') |
jhooper7 | 0:d0cfb841595e | 168 | num=-num; |
jhooper7 | 0:d0cfb841595e | 169 | if((num>10)or(num<-10)) |
jhooper7 | 0:d0cfb841595e | 170 | num=prevnum; //do nothing if invalid input |
jhooper7 | 0:d0cfb841595e | 171 | index=0; |
jhooper7 | 0:d0cfb841595e | 172 | if(inData[0]=='0') |
jhooper7 | 0:d0cfb841595e | 173 | resettozero(); |
jhooper7 | 0:d0cfb841595e | 174 | else if(inData[0]=='1') |
jhooper7 | 0:d0cfb841595e | 175 | closetiltup(); |
jhooper7 | 0:d0cfb841595e | 176 | else if(inData[0]=='2') |
jhooper7 | 0:d0cfb841595e | 177 | closetiltdown(); |
jhooper7 | 0:d0cfb841595e | 178 | else if(inData[0]=='3') |
jhooper7 | 0:d0cfb841595e | 179 | tiltto(num); |
jhooper7 | 0:d0cfb841595e | 180 | else if(inData[0]=='4') |
jhooper7 | 0:d0cfb841595e | 181 | fullup(); |
jhooper7 | 0:d0cfb841595e | 182 | else if(inData[0]=='5') |
jhooper7 | 0:d0cfb841595e | 183 | fulldown(); |
jhooper7 | 0:d0cfb841595e | 184 | else if(inData[0]=='6'){ |
jhooper7 | 0:d0cfb841595e | 185 | if(num>=0) |
jhooper7 | 0:d0cfb841595e | 186 | moveto(num); |
jhooper7 | 0:d0cfb841595e | 187 | } |
jhooper7 | 0:d0cfb841595e | 188 | ulcd.locate(0,0); |
jhooper7 | 0:d0cfb841595e | 189 | ulcd.printf("Height is %i\n Tilt is %i\n", heightpos, tiltpos); |
jhooper7 | 0:d0cfb841595e | 190 | //if(n>-1){ |
jhooper7 | 0:d0cfb841595e | 191 | socket.close(); |
jhooper7 | 0:d0cfb841595e | 192 | // if(!socket.is_connected()){ |
jhooper7 | 0:d0cfb841595e | 193 | // ulcd.printf("\ndevice nc");//hosted via laptop |
jhooper7 | 0:d0cfb841595e | 194 | while(socket.connect(ipaddress,port)<0){ |
jhooper7 | 0:d0cfb841595e | 195 | led4=!led4; |
jhooper7 | 0:d0cfb841595e | 196 | } |
jhooper7 | 0:d0cfb841595e | 197 | ulcd.printf("\ndevice rc"); |
jhooper7 | 0:d0cfb841595e | 198 | //} |
jhooper7 | 0:d0cfb841595e | 199 | //} |
jhooper7 | 0:d0cfb841595e | 200 | }//} |
jhooper7 | 0:d0cfb841595e | 201 | socket.close(); |
jhooper7 | 0:d0cfb841595e | 202 | wifi.disconnect(); |
jhooper7 | 0:d0cfb841595e | 203 | } |
jhooper7 | 0:d0cfb841595e | 204 |