idk what a fork is
Dependencies: 4DGL-uLCD-SE EthernetInterface HTTPClient Motor mbed-rtos mbed
Fork of 2036lab7_template by
Diff: main.cpp
- Revision:
- 1:739ba18153a4
- Parent:
- 0:df4d7c0a1594
diff -r df4d7c0a1594 -r 739ba18153a4 main.cpp --- a/main.cpp Mon Apr 06 12:28:01 2015 +0000 +++ b/main.cpp Thu Apr 28 18:53:22 2016 +0000 @@ -1,93 +1,165 @@ -#include "mbed.h" -// Need include below to add the RTOS -#include "rtos.h" -//#include "EthernetInterface.h" //needed for Extra Credit -//#include "NTPClient.h" -#include "SDFileSystem.h" + #include "mbed.h" + #include "rtos.h" + #include "Motor.h" #include "uLCD_4DGL.h" -#include "TMP36.h" -#include "wave_player.h" -// Setup four builtin leds for use by threads -DigitalOut led1(LED1); -DigitalOut led2(LED2); -DigitalOut led3(LED3); -DigitalOut led4(LED4); +#include "EthernetInterface.h" +#include "HTTPClient.h" + + #define Room_TempC 23.0 + #define Room_TempF 78.0 + #define Normal_BTempC 30.0 + #define Normal_BTempF ((9.0/5.0)*Normal_BTempC) + 32 + +Motor m(p21, p19, p20); // pwm, fwd, rev + +DigitalOut myled(LED1); +DigitalOut myLED2(LED2); +DigitalOut myLED3(LED3); +DigitalOut myLED4(LED4); -AnalogOut DACout(p18); // used to play sound on speaker - -//wave player plays a *.wav file to D/A and a PWM -wave_player waver(&DACout); - -uLCD_4DGL uLCD(p28,p27,p29); // serial tx, serial rx, reset pin; - -SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup - +uLCD_4DGL uLCD(p13,p14,p11); + +Serial pc(USBTX, USBRX); +Serial xbee1(p9, p10); +DigitalOut rst1(p8); -// Setup function code for three new threads to run. -// Put in a while loop so that threads run forever. -// Thread::wait will force at least a "x" millisecond -// wait before the thread runs again. During this delay -// the other threads will run -// DO NOT use wait() with the RTOS!!!!! -// wait just burns processor time and no other threads run -void led2_thread(void const *argument) -{ - led2 = 1; - while (true) { - led2 = !led2; - Thread::wait(2000); - } -} -void led3_thread(void const *argument) -{ - led3 = 1; - while (true) { - led3 = !led3; - Thread::wait(4000); - } -} -void led4_thread(void const *argument) -{ - TMP36 myTMP36(p20); - led4 = 1; - while (true) { - led4 = !led4; - Thread::wait(8000); +EthernetInterface ethif; +HTTPClient http; + +enum Statetype { ac_off = 0, ac_on }; +volatile int sensor_data; + +void receiver_thread(void const * argument){ + rst1 = 0; //Set reset pin to 0 + myled = 0; + Thread::wait(1); + rst1 = 1; //Set reset pin to 1 + Thread::wait(1); + ethif.init(); //Use DHCP + ethif.connect(60000); + pc.printf("connected ethernet\n"); + + char urlBuffer[256]; + char str[512]; + + while (1) { + Thread::wait(1000); + myled = 1; + if(xbee1.readable()){ + xbee1.scanf("%d\n", &sensor_data); + pc.printf("data received %d \n\r ", sensor_data); + Thread::wait(300); + myled = 0; + Thread::wait(2000); + } + // initialize ethernet hardware + pc.printf("Begin While loop"); + + urlBuffer[0] = 0; + //str[0]= 0; + //sprintf(urlBuffer, "http://api.thingspeak.com/update?key=LRZ7ZLXUI7LKC470&field1=%d", 15); + sprintf(urlBuffer, "http://api.thingspeak.com/update?key=LRZ7ZLXUI7LKC470&field1=%d", sensor_data); + + HTTPResult res = http.get(urlBuffer, str,128); + if(res==0) { + pc.printf("sent get to thingspeak\n"); + } + else { + pc.printf("get failed to send"); + } } } -int main() -{ - led1 = 1; -// code to set time in extra credit option goes here -// - uLCD.baudrate(3000000); //jack up baud rate to max for fast display - uLCD.text_width(2); //2x size text - uLCD.text_height(2); -// Create 3 new thread objects thread1, thread2, and thread3 -// The RTOS will immediately start running them - Thread thread1(led2_thread); - Thread thread2(led3_thread); - Thread thread3(led4_thread); -// Main continues to run and is actually the first thread. -// So a total of four threads are running now. -// Each thread blinks an LED, but at a different rate -// because of the different values used in Thread::wait(). -// -// Set time in seconds since Jan 1 1970 (Unix style) -// must set time to start Real Time clock running - set_time(1286729737); - char buffer[12]; - time_t seconds; - while (true) { -// reads time structure - seconds = time(NULL); -// converts time structure to a string - strftime(buffer, 12, "%T", localtime(&seconds)); -// print time HH:MM:SS - uLCD.locate(0,2); - uLCD.printf("%s\n\r", buffer); - led1 = !led1; - Thread::wait(1000); +/*void network_thread(void const * argument) { + ethif.init(); //Use DHCP + ethif.connect(60000); + pc.printf("connected ethernet\n"); + + char urlBuffer[256]; + char str[512]; + + while (1) + { + wait(30); + // initialize ethernet hardware + pc.printf("Begin While loop"); + + urlBuffer[0] = 0; + //str[0]= 0; + //sprintf(urlBuffer, "http://api.thingspeak.com/update?key=LRZ7ZLXUI7LKC470&field1=%d", 15); + sprintf(urlBuffer, "http://api.thingspeak.com/update?key=LRZ7ZLXUI7LKC470&field1=%d", 26); + + HTTPResult res = http.get(urlBuffer, str,128); + if(res==0) { + pc.printf("sent get to thingspeak\n"); + } + else { + pc.printf("get failed to send"); + } + //led=!led; + Thread::wait(2000); + } } + */ + +int main() { + Thread thread1(receiver_thread); + //Thread thread2(network_thread); + Statetype state = ac_off; + + + while(1){ + myLED3 = !myLED3; + switch (state) { + case ac_off: + + m.speed(0.0); + myled = 1; + myLED2 =0; + myLED3 =1; + myLED4 =0; + + (sensor_data >= Normal_BTempC)? state = ac_on : state = ac_off; + // pc.printf("This is off %d: ",sensor_data); + break; + + case ac_on: + + myled = 1; + myLED2 =1; + myLED3 =1; + myLED4 =1; + + /* while (sensor_data >= 1.3*Normal_BTempC) { + m.speed(1.0); + } + while ((sensor_data >= 1.2*Normal_BTempC)&&(sensor_data < 1.3*Normal_BTempC)) { + m.speed(0.5); + Thread::wait(500); + m.speed(0.0); + Thread::wait(2000); + } + while ((sensor_data >= 1.1*Normal_BTempC)&&(sensor_data < 1.2*Normal_BTempC)) { + m.speed(0.5); + Thread::wait(500); + m.speed(0.0); + Thread::wait(3000); + } + while ((sensor_data >= Normal_BTempC)&&(sensor_data < 1.1*Normal_BTempC)) { + m.speed(0.5); + Thread::wait(500); + m.speed(0.0); + Thread::wait(3500); + }*/ + + m.speed(1.0); + (sensor_data < Normal_BTempC)? state = ac_off : state = ac_on; + pc.printf("This is on %d: ",sensor_data); + break; + } + + Thread::wait(1000); + + } +}