RX for Temperature Controlled Fan
Dependencies: 4DGL-uLCD-SE EthernetInterface HTTPClient Motor mbed-rtos mbed
main.cpp@0:b007e0de7dbc, 2016-04-29 (annotated)
- Committer:
- TimothyY
- Date:
- Fri Apr 29 01:07:58 2016 +0000
- Revision:
- 0:b007e0de7dbc
FinalRx
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TimothyY | 0:b007e0de7dbc | 1 | #include "mbed.h" |
TimothyY | 0:b007e0de7dbc | 2 | #include "rtos.h" |
TimothyY | 0:b007e0de7dbc | 3 | #include "Motor.h" |
TimothyY | 0:b007e0de7dbc | 4 | #include "uLCD_4DGL.h" |
TimothyY | 0:b007e0de7dbc | 5 | #include "EthernetInterface.h" |
TimothyY | 0:b007e0de7dbc | 6 | #include "HTTPClient.h" |
TimothyY | 0:b007e0de7dbc | 7 | |
TimothyY | 0:b007e0de7dbc | 8 | #define Room_TempC 23.0 |
TimothyY | 0:b007e0de7dbc | 9 | #define Room_TempF 78.0 |
TimothyY | 0:b007e0de7dbc | 10 | //#define Normal_BTempC 30.0 |
TimothyY | 0:b007e0de7dbc | 11 | #define Normal_BTempF ((9.0/5.0)*Normal_BTempC) + 32 |
TimothyY | 0:b007e0de7dbc | 12 | |
TimothyY | 0:b007e0de7dbc | 13 | Motor m(p21, p19, p20); // pwm, fwd, rev |
TimothyY | 0:b007e0de7dbc | 14 | |
TimothyY | 0:b007e0de7dbc | 15 | DigitalOut myled(LED1); |
TimothyY | 0:b007e0de7dbc | 16 | DigitalOut myLED2(LED2); |
TimothyY | 0:b007e0de7dbc | 17 | DigitalOut myLED3(LED3); |
TimothyY | 0:b007e0de7dbc | 18 | DigitalOut myLED4(LED4); |
TimothyY | 0:b007e0de7dbc | 19 | |
TimothyY | 0:b007e0de7dbc | 20 | InterruptIn pb1(p30); |
TimothyY | 0:b007e0de7dbc | 21 | InterruptIn pb2(p29); |
TimothyY | 0:b007e0de7dbc | 22 | |
TimothyY | 0:b007e0de7dbc | 23 | uLCD_4DGL uLCD(p13,p14,p11); |
TimothyY | 0:b007e0de7dbc | 24 | |
TimothyY | 0:b007e0de7dbc | 25 | Serial pc(USBTX, USBRX); |
TimothyY | 0:b007e0de7dbc | 26 | Serial xbee1(p9, p10); |
TimothyY | 0:b007e0de7dbc | 27 | DigitalOut rst1(p8); |
TimothyY | 0:b007e0de7dbc | 28 | |
TimothyY | 0:b007e0de7dbc | 29 | EthernetInterface ethif; |
TimothyY | 0:b007e0de7dbc | 30 | HTTPClient http; |
TimothyY | 0:b007e0de7dbc | 31 | |
TimothyY | 0:b007e0de7dbc | 32 | enum Statetype { ac_off = 0, ac_on }; |
TimothyY | 0:b007e0de7dbc | 33 | volatile int sensor_data; |
TimothyY | 0:b007e0de7dbc | 34 | volatile int Normal_BTempC = 30; |
TimothyY | 0:b007e0de7dbc | 35 | |
TimothyY | 0:b007e0de7dbc | 36 | void receiver_thread(void const * argument){ |
TimothyY | 0:b007e0de7dbc | 37 | |
TimothyY | 0:b007e0de7dbc | 38 | rst1 = 0; //Set reset pin to 0 |
TimothyY | 0:b007e0de7dbc | 39 | myled = 0; |
TimothyY | 0:b007e0de7dbc | 40 | Thread::wait(1); |
TimothyY | 0:b007e0de7dbc | 41 | rst1 = 1; //Set reset pin to 1 |
TimothyY | 0:b007e0de7dbc | 42 | Thread::wait(1); |
TimothyY | 0:b007e0de7dbc | 43 | |
TimothyY | 0:b007e0de7dbc | 44 | uLCD.printf("Set Temp:"); |
TimothyY | 0:b007e0de7dbc | 45 | uLCD.locate(0,7); |
TimothyY | 0:b007e0de7dbc | 46 | uLCD.printf("Your Temp:"); |
TimothyY | 0:b007e0de7dbc | 47 | |
TimothyY | 0:b007e0de7dbc | 48 | while (1) { |
TimothyY | 0:b007e0de7dbc | 49 | Thread::wait(500); |
TimothyY | 0:b007e0de7dbc | 50 | myled = 1; |
TimothyY | 0:b007e0de7dbc | 51 | if(xbee1.readable()){ |
TimothyY | 0:b007e0de7dbc | 52 | xbee1.scanf("%d\n", &sensor_data); |
TimothyY | 0:b007e0de7dbc | 53 | //pc.printf("data received %d\n\r", sensor_data); |
TimothyY | 0:b007e0de7dbc | 54 | uLCD.text_height(2); |
TimothyY | 0:b007e0de7dbc | 55 | uLCD.text_width(2); |
TimothyY | 0:b007e0de7dbc | 56 | uLCD.locate(2,2); |
TimothyY | 0:b007e0de7dbc | 57 | uLCD.printf("%d C", Normal_BTempC); |
TimothyY | 0:b007e0de7dbc | 58 | uLCD.locate(2,6); |
TimothyY | 0:b007e0de7dbc | 59 | uLCD.printf("%d C", sensor_data); |
TimothyY | 0:b007e0de7dbc | 60 | Thread::wait(300); |
TimothyY | 0:b007e0de7dbc | 61 | myled = 0; |
TimothyY | 0:b007e0de7dbc | 62 | Thread::wait(1000); |
TimothyY | 0:b007e0de7dbc | 63 | } |
TimothyY | 0:b007e0de7dbc | 64 | } |
TimothyY | 0:b007e0de7dbc | 65 | } |
TimothyY | 0:b007e0de7dbc | 66 | |
TimothyY | 0:b007e0de7dbc | 67 | void changeBTemp_thread(void const *argument) { |
TimothyY | 0:b007e0de7dbc | 68 | while (1) { |
TimothyY | 0:b007e0de7dbc | 69 | pb1.mode(PullUp); |
TimothyY | 0:b007e0de7dbc | 70 | pb2.mode(PullUp); |
TimothyY | 0:b007e0de7dbc | 71 | if (pb1 == 0) {Normal_BTempC++;}; |
TimothyY | 0:b007e0de7dbc | 72 | if (pb2 == 0) {Normal_BTempC--;}; |
TimothyY | 0:b007e0de7dbc | 73 | Thread::wait(500); |
TimothyY | 0:b007e0de7dbc | 74 | } |
TimothyY | 0:b007e0de7dbc | 75 | } |
TimothyY | 0:b007e0de7dbc | 76 | |
TimothyY | 0:b007e0de7dbc | 77 | void network_thread(void const *argument) { |
TimothyY | 0:b007e0de7dbc | 78 | |
TimothyY | 0:b007e0de7dbc | 79 | char urlBuffer[256]; |
TimothyY | 0:b007e0de7dbc | 80 | char str[512]; |
TimothyY | 0:b007e0de7dbc | 81 | |
TimothyY | 0:b007e0de7dbc | 82 | ethif.init(); //Use DHCP |
TimothyY | 0:b007e0de7dbc | 83 | ethif.connect(60000); |
TimothyY | 0:b007e0de7dbc | 84 | pc.printf("connected ethernet\n"); |
TimothyY | 0:b007e0de7dbc | 85 | |
TimothyY | 0:b007e0de7dbc | 86 | while (1) |
TimothyY | 0:b007e0de7dbc | 87 | { |
TimothyY | 0:b007e0de7dbc | 88 | // initialize ethernet hardware |
TimothyY | 0:b007e0de7dbc | 89 | pc.printf("Begin While loop"); |
TimothyY | 0:b007e0de7dbc | 90 | |
TimothyY | 0:b007e0de7dbc | 91 | |
TimothyY | 0:b007e0de7dbc | 92 | urlBuffer[0] = 0; |
TimothyY | 0:b007e0de7dbc | 93 | str[0]=0; |
TimothyY | 0:b007e0de7dbc | 94 | sprintf(urlBuffer, "http://api.thingspeak.com/update?key=LRZ7ZLXUI7LKC470&field1=%d", sensor_data); |
TimothyY | 0:b007e0de7dbc | 95 | |
TimothyY | 0:b007e0de7dbc | 96 | http.get(urlBuffer, str,128); |
TimothyY | 0:b007e0de7dbc | 97 | //HTTPResult res = http.get(urlBuffer, str,128); |
TimothyY | 0:b007e0de7dbc | 98 | //if(res==0) { |
TimothyY | 0:b007e0de7dbc | 99 | //pc.printf("sent get to thingspeak\n"); |
TimothyY | 0:b007e0de7dbc | 100 | //} |
TimothyY | 0:b007e0de7dbc | 101 | //else { |
TimothyY | 0:b007e0de7dbc | 102 | // pc.printf("get failed to send"); |
TimothyY | 0:b007e0de7dbc | 103 | // } |
TimothyY | 0:b007e0de7dbc | 104 | // wait(16); // 1 minute |
TimothyY | 0:b007e0de7dbc | 105 | // led=!led; |
TimothyY | 0:b007e0de7dbc | 106 | Thread::wait(10000); |
TimothyY | 0:b007e0de7dbc | 107 | |
TimothyY | 0:b007e0de7dbc | 108 | } |
TimothyY | 0:b007e0de7dbc | 109 | } |
TimothyY | 0:b007e0de7dbc | 110 | int main() { |
TimothyY | 0:b007e0de7dbc | 111 | |
TimothyY | 0:b007e0de7dbc | 112 | Thread thread1(receiver_thread); |
TimothyY | 0:b007e0de7dbc | 113 | Thread::wait(5000); |
TimothyY | 0:b007e0de7dbc | 114 | Thread thread2(changeBTemp_thread); |
TimothyY | 0:b007e0de7dbc | 115 | Thread::wait(5000); |
TimothyY | 0:b007e0de7dbc | 116 | Thread thread3(network_thread); |
TimothyY | 0:b007e0de7dbc | 117 | Statetype state = ac_off; |
TimothyY | 0:b007e0de7dbc | 118 | |
TimothyY | 0:b007e0de7dbc | 119 | while(1){ |
TimothyY | 0:b007e0de7dbc | 120 | myLED3 = !myLED3; |
TimothyY | 0:b007e0de7dbc | 121 | //uLCD.printf("Set Temp: %d\n\r", Normal_BTempC); |
TimothyY | 0:b007e0de7dbc | 122 | switch (state) { |
TimothyY | 0:b007e0de7dbc | 123 | case ac_off: |
TimothyY | 0:b007e0de7dbc | 124 | |
TimothyY | 0:b007e0de7dbc | 125 | m.speed(0.0); |
TimothyY | 0:b007e0de7dbc | 126 | myled = 1; |
TimothyY | 0:b007e0de7dbc | 127 | myLED2 =0; |
TimothyY | 0:b007e0de7dbc | 128 | myLED3 =1; |
TimothyY | 0:b007e0de7dbc | 129 | myLED4 =0; |
TimothyY | 0:b007e0de7dbc | 130 | |
TimothyY | 0:b007e0de7dbc | 131 | Thread::wait(3000); |
TimothyY | 0:b007e0de7dbc | 132 | |
TimothyY | 0:b007e0de7dbc | 133 | (sensor_data >= Normal_BTempC)? state = ac_on : state = ac_off; |
TimothyY | 0:b007e0de7dbc | 134 | //pc.printf("This is off \n\r"); |
TimothyY | 0:b007e0de7dbc | 135 | break; |
TimothyY | 0:b007e0de7dbc | 136 | |
TimothyY | 0:b007e0de7dbc | 137 | case ac_on: |
TimothyY | 0:b007e0de7dbc | 138 | |
TimothyY | 0:b007e0de7dbc | 139 | myled = 1; |
TimothyY | 0:b007e0de7dbc | 140 | myLED2 =1; |
TimothyY | 0:b007e0de7dbc | 141 | myLED3 =1; |
TimothyY | 0:b007e0de7dbc | 142 | myLED4 =1; |
TimothyY | 0:b007e0de7dbc | 143 | |
TimothyY | 0:b007e0de7dbc | 144 | while (sensor_data >= 1.3*Normal_BTempC) { |
TimothyY | 0:b007e0de7dbc | 145 | m.speed(1.0); |
TimothyY | 0:b007e0de7dbc | 146 | } |
TimothyY | 0:b007e0de7dbc | 147 | while ((sensor_data >= 1.2*Normal_BTempC)&&(sensor_data < 1.3*Normal_BTempC)) { |
TimothyY | 0:b007e0de7dbc | 148 | m.speed(0.5); |
TimothyY | 0:b007e0de7dbc | 149 | Thread::wait(500); |
TimothyY | 0:b007e0de7dbc | 150 | m.speed(0.0); |
TimothyY | 0:b007e0de7dbc | 151 | Thread::wait(2000); |
TimothyY | 0:b007e0de7dbc | 152 | } |
TimothyY | 0:b007e0de7dbc | 153 | while ((sensor_data >= 1.1*Normal_BTempC)&&(sensor_data < 1.2*Normal_BTempC)) { |
TimothyY | 0:b007e0de7dbc | 154 | m.speed(0.5); |
TimothyY | 0:b007e0de7dbc | 155 | Thread::wait(500); |
TimothyY | 0:b007e0de7dbc | 156 | m.speed(0.0); |
TimothyY | 0:b007e0de7dbc | 157 | Thread::wait(3000); |
TimothyY | 0:b007e0de7dbc | 158 | } |
TimothyY | 0:b007e0de7dbc | 159 | while ((sensor_data >= Normal_BTempC)&&(sensor_data < 1.1*Normal_BTempC)) { |
TimothyY | 0:b007e0de7dbc | 160 | m.speed(0.5); |
TimothyY | 0:b007e0de7dbc | 161 | Thread::wait(500); |
TimothyY | 0:b007e0de7dbc | 162 | m.speed(0.0); |
TimothyY | 0:b007e0de7dbc | 163 | Thread::wait(3500); |
TimothyY | 0:b007e0de7dbc | 164 | } |
TimothyY | 0:b007e0de7dbc | 165 | |
TimothyY | 0:b007e0de7dbc | 166 | //m.speed(1.0); |
TimothyY | 0:b007e0de7dbc | 167 | (sensor_data < Normal_BTempC)? state = ac_off : state = ac_on; |
TimothyY | 0:b007e0de7dbc | 168 | //pc.printf("This is on \n\r"); |
TimothyY | 0:b007e0de7dbc | 169 | break; |
TimothyY | 0:b007e0de7dbc | 170 | } |
TimothyY | 0:b007e0de7dbc | 171 | Thread::wait(1000); |
TimothyY | 0:b007e0de7dbc | 172 | } |
TimothyY | 0:b007e0de7dbc | 173 | } |