![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Final version
Dependencies: mbed SparkfunAnalogJoystick LSM9DS1_Library_cal PinDetect
main.cpp
- Committer:
- kzar
- Date:
- 2018-12-05
- Revision:
- 1:0ec1b59239f2
- Parent:
- 0:5ad3d5f99603
- Child:
- 2:da3e288789a4
File content as of revision 1:0ec1b59239f2:
// This mbed code creates a connection with the car mbeds server and reads from it // The car will host the server, the remote controll will send data to the server #include "mbed.h" #include "PinDetect.h" Serial pc(USBTX, USBRX); Serial esp(p28, p27); // tx, rx // Standard Mbed LED definitions DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); // Push button used for testing PinDetect pb1(p21); void pushed(); /* char ssid[32] = "hsd"; // enter WiFi router ssid inside the quotes char pwd [32] = "austin123"; // enter WiFi router password inside the quotes */ // things for sending/receiving data over serial volatile int tx_in=0; volatile int tx_out=0; volatile int rx_in=0; volatile int rx_out=0; const int buffer_size = 4095; char tx_buffer[buffer_size+1]; char rx_buffer[buffer_size+1]; void Tx_interrupt(); void Rx_interrupt(); void read_line(); int DataRX; int update; char cmdbuff[1024]; char replybuff[4096]; char webdata[4096]; // This may need to be bigger depending on WEB browser used char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added void SendCMD(),getreply(); void getConnection(); // Sets up a connection with the car server char rx_line[1024]; int port =80; // set server port int SERVtimeout =5; // set server timeout in seconds in case link breaks. int main() { pc.baud(9600); esp.baud(9600); led1=1,led2=0,led3=0, led4=0; // Setup a serial interrupt function to receive data esp.attach(&Rx_interrupt, Serial::RxIrq); // Setup a serial interrupt function to transmit data esp.attach(&Tx_interrupt, Serial::TxIrq); // Attach isr to pushbutton pb1.mode(PullUp); pb1.attach_asserted(&pushed); // Get connection to server getConnection(); update = 0; // Everything is interrupt driven, infinite loop while(1) { pushed(); //Send all car vals+ if (update) { // Send led for testing // Reset update flag update = 0; } } } void pushed() { strcpy(cmdbuff,"cl:send(\"flip_led1\")\r\n"); SendCMD(); //getreply(); wait(.1); } // Sets up connection with car server void getConnection() { // Reset the ESP8266 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); strcpy(cmdbuff,"node.restart()\r\n"); SendCMD(); wait(2); getreply(); // Disconncet from any potential connections strcpy(cmdbuff,"wifi.sta.disconnect()\r\n"); SendCMD(); getreply(); wait(2); // Set mode (STATION) strcpy(cmdbuff,"wifi.setmode(wifi.STATION)\r\n"); SendCMD(); getreply(); wait(2); // Connect to car server strcpy(cmdbuff,"wifi.sta.config(\"Marlon's iPhone\", \"feelsbadman\")\r\n"); SendCMD(); getreply(); wait(2); // Connect to server strcpy(cmdbuff,"wifi.sta.connect()\r\n"); SendCMD(); getreply(); wait(2); strcpy(cmdbuff,"print(\"Looking for a connection\")\r\n"); SendCMD(); getreply(); wait(2); strcpy(cmdbuff,"tmr.alarm(1,2000,1, function()\r\n"); SendCMD(); getreply(); wait(2); strcpy(cmdbuff,"if(wifi.sta.getip()~=nil) then\r\n"); SendCMD(); getreply(); wait(2); strcpy(cmdbuff,"tmr.stop(1)\r\n"); SendCMD(); getreply(); wait(2); // Print out controller ip address strcpy(cmdbuff,"print(\"Connected!\")\r\n"); SendCMD(); getreply(); wait(2); // Print out controller ip address strcpy(cmdbuff,"print(\"Client IP Address: \",wifi.sta.getip())\r\n"); SendCMD(); getreply(); wait(5); // Create connetion strcpy(cmdbuff,"cl=net.createConnection(net.TCP, 0)\r\n"); SendCMD(); getreply(); wait(2); // Connect the connection strcpy(cmdbuff,"cl:connect(80, \"192.168.4.1\")\r\n"); SendCMD(); getreply(); wait(2); // strcpy(cmdbuff,"tmr.alarm(2, 5000, 1, function()\r\n"); // SendCMD(); // getreply(); // wait(2); // // strcpy(cmdbuff,"cl:send(\"Hello World!\")\r\n"); // SendCMD(); // getreply(); // wait(2); // // strcpy(cmdbuff,"end)\r\n"); // SendCMD(); // getreply(); // wait(2); // Print out controller ip address strcpy(cmdbuff,"else\r\n"); SendCMD(); getreply(); wait(2); // Print out controller ip address strcpy(cmdbuff,"print(\"Connecting...\")\r\n"); SendCMD(); getreply(); wait(2); // Print out controller ip address strcpy(cmdbuff,"end\r\n"); SendCMD(); getreply(); wait(2); // Print out controller ip address strcpy(cmdbuff,"end)\r\n"); SendCMD(); getreply(); wait(2); } // ESP Command data send void SendCMD() { int i; char temp_char; bool empty; i = 0; // Start Critical Section - don't interrupt while changing global buffer variables NVIC_DisableIRQ(UART1_IRQn); empty = (tx_in == tx_out); while ((i==0) || (cmdbuff[i-1] != '\n')) { // Wait if buffer full if (((tx_in + 1) % buffer_size) == tx_out) { // End Critical Section - need to let interrupt routine empty buffer by sending NVIC_EnableIRQ(UART1_IRQn); while (((tx_in + 1) % buffer_size) == tx_out) { } // Start Critical Section - don't interrupt while changing global buffer variables NVIC_DisableIRQ(UART1_IRQn); } tx_buffer[tx_in] = cmdbuff[i]; i++; tx_in = (tx_in + 1) % buffer_size; } if (esp.writeable() && (empty)) { temp_char = tx_buffer[tx_out]; tx_out = (tx_out + 1) % buffer_size; // Send first character to start tx interrupts, if stopped esp.putc(temp_char); } // End Critical Section NVIC_EnableIRQ(UART1_IRQn); return; } // Get Command and ESP status replies void getreply() { read_line(); sscanf(rx_line,replybuff); } // FUNCTIONS BELOW ARE FOR RX AND TX INTERUPTS (NOT WEB STUFF) // Read a line from the large rx buffer from rx interrupt routine void read_line() { int i; i = 0; // Start Critical Section - don't interrupt while changing global buffer variables NVIC_DisableIRQ(UART1_IRQn); // Loop reading rx buffer characters until end of line character while ((i==0) || (rx_line[i-1] != '\r')) { // Wait if buffer empty if (rx_in == rx_out) { // End Critical Section - need to allow rx interrupt to get new characters for buffer NVIC_EnableIRQ(UART1_IRQn); while (rx_in == rx_out) { } // Start Critical Section - don't interrupt while changing global buffer variables NVIC_DisableIRQ(UART1_IRQn); } rx_line[i] = rx_buffer[rx_out]; i++; rx_out = (rx_out + 1) % buffer_size; } // End Critical Section NVIC_EnableIRQ(UART1_IRQn); rx_line[i-1] = 0; return; } // Interupt Routine to read in data from serial port void Rx_interrupt() { DataRX=1; //led3=1; // Loop just in case more than one character is in UART's receive FIFO buffer // Stop if buffer full while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { rx_buffer[rx_in] = esp.getc(); // Uncomment to Echo to USB serial to watch data flow pc.putc(rx_buffer[rx_in]); rx_in = (rx_in + 1) % buffer_size; } //led3=0; return; } // Interupt Routine to write out data to serial port void Tx_interrupt() { //led2=1; // Loop to fill more than one character in UART's transmit FIFO buffer // Stop if buffer empty while ((esp.writeable()) && (tx_in != tx_out)) { esp.putc(tx_buffer[tx_out]); tx_out = (tx_out + 1) % buffer_size; } //led2=0; return; }