Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.49 KB | None | 0 0
  1. // BeVolt Telemetry 10-June-19
  2.  
  3. #include "mbed.h"
  4. #include "EthernetInterface.h"
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include "canIds.h"
  8. #include "TCPServer.h"
  9. #include "TCPSocket.h"
  10. #include "SDFileSystem.h"
  11. #include "ds1307.h"
  12.  
  13. #include <sstream>
  14.  
  15. #define TRIT_DC_BASE     0x220
  16. #define TRIT_DC_DRIVE    0x01
  17.  
  18. CAN can(PD_0,PD_1); // for the F429ZI  (Rx, Tx)
  19. Serial          pc(USBTX, USBRX);
  20. DigitalOut      led(LED1);
  21. CANMessage      msg;
  22. Thread canThread;
  23. Thread serverThread;
  24. int counter = 0;
  25. int CanID = 0x0000002;
  26. TCPServer srv;
  27. TCPSocket clt_sock;
  28. SocketAddress clt_addr;
  29. EthernetInterface eth;
  30.  
  31. float volt_buf[31];
  32. float temp_buf[62];
  33. float current;
  34. float bps_trip;
  35. float all_clear;
  36. float bps_off;
  37. float soc;
  38. float wdog;
  39. float can_error;
  40. float command_msg;
  41.  
  42. SDFileSystem sd(PE_6, PE_5, PE_2, PE_4, "sd"); // the pinout on the mbed Cool Components workshop board, for BIG BOARD
  43. DS1307 my1307(D14, D15); // start DS1307 class and give it pins for connections of the DS1307 device
  44.  
  45. int second = 0;
  46. int minute = 30;
  47. int hours = 19;
  48. int day = 0;
  49. int date = 13;
  50. int month = 11;
  51. int year = 2019;
  52. int l=0;
  53.  
  54. #define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
  55.  
  56. #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
  57.  
  58. #define HTTP_MESSAGE_BODY "" \
  59. "<html>" "\r\n" \
  60. "<head>" "\r\n" \
  61. "<meta http-equiv=\"refresh\" content=\"2\">" "\r\n" \
  62. "</head>" "\r\n"    \
  63. " <body style=\"display:flex;text-align:left\">" "\r\n" \
  64. " <div style=\"margin:auto\">" "\r\n" \
  65. " <h1 style=\"color: #cc5500\">BeVolt Telemetry</h1>" "\r\n" \
  66. //" <p>Data: </p>" "\r\n" \
  67. " </div>" "\r\n" \
  68. //" </body>" "\r\n" \
  69. "</html>"
  70.  
  71. #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \
  72.                       HTTP_HEADER_FIELDS "\r\n" \
  73.                       "\r\n" \
  74.                       HTTP_MESSAGE_BODY "\r\n"
  75.                      
  76. char begdiv[] = "<div style=\"margin:auto;text-align:center\">";
  77. char enddiv[] = "</div>";
  78.  
  79. char title[] = "<h1> BeVolt Telemetry </h1>";
  80.  
  81. float bytes2Float(uint8_t* bytes_array) {
  82.     union {
  83.         float f;
  84.         uint8_t b[4];
  85.     } u;
  86.     u.b[3] = bytes_array[0];
  87.     u.b[2] = bytes_array[1];
  88.     u.b[1] = bytes_array[2];
  89.     u.b[0] = bytes_array[3];
  90.     return u.f;
  91. }
  92.  
  93.  
  94.  
  95. float receiveCan(void) {
  96.     uint32_t received;
  97.    
  98.     if (can.read(msg)) {
  99.         received = *((uint32_t*) msg.data);
  100.     } else {
  101.         pc.printf("Could not receive message\n");
  102.         return -1;
  103.     }
  104.     pc.printf("-----------------------\r\n");
  105.  
  106.     if (msg.id == BPS_TRIP) {
  107.         bps_trip = msg.data[0];
  108.         pc.printf("BPS Trip is %d\n", received);
  109.     } else if (msg.id == BPS_ALL_CLEAR) {
  110.         all_clear = msg.data[0];
  111.         pc.printf("BPS All Clear is %d\n", received);
  112.     } else if (msg.id == BPS_OFF) {
  113.         bps_off = msg.data[0];
  114.         pc.printf("BPS Off is %d\n", received);    
  115.     } else if (msg.id == WDOG_TRIGGERED) {
  116.         wdog = msg.data[0];
  117.         pc.printf("WDOG Triggered is %d\n", received);    
  118.     } else if (msg.id == CAN_ERROR) {
  119.         can_error = msg.data[0];
  120.         pc.printf("CAN Error is %d\n", received);    
  121.     } else if (msg.id == VOLTAGE_DATA) {
  122.         // voltage
  123.         float voltage = bytes2Float(&msg.data[1]);
  124.         pc.printf("Voltage is %.3fV from array index %d\n", voltage, msg.data[0]);
  125.         volt_buf[msg.data[0]] = voltage;
  126.        
  127.         return voltage;
  128.     } else if (msg.id == TEMPERATURE_DATA){
  129.         // temperature
  130.         float temp = bytes2Float(&msg.data[1]);
  131.         pc.printf("Temp is %.3f from array index %d\n", temp, msg.data[0]);  
  132.         temp_buf[msg.data[0]] = temp;
  133.        
  134.         return temp;
  135.     } else if(msg.id == SOC_DATA){
  136.         // state of charge
  137.         soc = bytes2Float(msg.data);
  138.         pc.printf("SoC is %.3f%%\n", soc);
  139.         return received;
  140.     } else if(msg.id == CURRENT_DATA){
  141.         // current
  142.         current = bytes2Float(msg.data);
  143.         pc.printf("Current is %.3fA\n", current);
  144.         return current;
  145.     }
  146.     pc.printf("-----------------------\r\n");
  147.     // Thread::wait(100);
  148.     return -1;
  149. }
  150.  
  151. void received(){
  152.     while(1) {
  153.         //pc.printf("In can receive thread\n");
  154.         receiveCan();
  155.         //Thread::yield();
  156.         ThisThread::sleep_for(100); // 100 works
  157.     }
  158. }
  159.  
  160. void serverStuff(){
  161.     char temp_str[30];
  162.     while(1){
  163.         printf("In server thread\n");
  164.            
  165.         srv.accept(&clt_sock, &clt_addr);
  166.         clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE));
  167.        
  168.         // Voltage
  169.         std::string html_volt="<h2>Voltage</h2><br><p class=\"voltage\">";
  170.        
  171.         for(int i=0; i<16; i++){
  172.             //if(i%3==0) {html_volt+="\n";}
  173.             html_volt = html_volt + "A";
  174.             sprintf(temp_str, "%d", i);
  175.             html_volt += temp_str;
  176.             html_volt += ": ";
  177.             sprintf(temp_str, "%f", volt_buf[i]);
  178.             html_volt += temp_str;
  179.             html_volt += " | ";
  180.         }
  181.         for(int i=16; i<31; i++){
  182.             //if(i%3==0) {html_volt+="\n";}
  183.             html_volt = html_volt + "B";
  184.             sprintf(temp_str, "%d", i-16);
  185.             html_volt += temp_str;
  186.             html_volt += ": ";
  187.             sprintf(temp_str, "%f", volt_buf[i]);
  188.             html_volt += temp_str;
  189.             html_volt += " | ";
  190.         }
  191.        
  192.         html_volt += "</p>";
  193.         // convert the string into character array
  194.         int n = html_volt.length();
  195.         char html_char[n+1];
  196.         strcpy(html_char, html_volt.c_str());
  197.         pc.printf("%s\n", html_char);
  198.         clt_sock.send(html_char, n+1);
  199.        
  200.         // Temp
  201.         std::string html_temp="<h2>Temperature</h2><br><p class=\"temperature\">";
  202.         char sub = 'a';
  203.         for(int i=0; i<31; i++) {
  204.             html_temp += "A";
  205.             sprintf(temp_str, "%d", i);
  206.             html_temp += temp_str;
  207.             html_temp += sub;
  208.             if(sub == 'a') {sub = 'b';} else {sub = 'a';}
  209.             html_temp += ": ";
  210.             sprintf(temp_str, "%f", temp_buf[i]);
  211.             html_temp += temp_str;
  212.             html_temp += " | ";
  213.         }
  214.         sub = 'a';
  215.         for(int i=31; i<62; i++) {
  216.             html_temp += "B";
  217.             sprintf(temp_str, "%d", i-31);
  218.             html_temp += temp_str;
  219.             html_temp += sub;
  220.             if(sub == 'a') {sub = 'b';} else {sub = 'a';}
  221.             html_temp += ": ";
  222.             sprintf(temp_str, "%f", temp_buf[i]);
  223.             html_temp += temp_str;
  224.             html_temp += " | ";
  225.         }
  226.        
  227.         html_temp += "</p>";
  228.         // convert the string into character array
  229.         int temp_char_len = html_temp.length();
  230.         char temp_char[temp_char_len+1];
  231.         strcpy(temp_char, html_temp.c_str());
  232.         pc.printf("%s\n", temp_char);
  233.         clt_sock.send(temp_char, temp_char_len+1);
  234.        
  235.         // current
  236.         std::string html_current = "<h2>Current</h2><br><p class=\"current\">";
  237.         sprintf(temp_str, "%f", current);
  238.         html_current += temp_str;
  239.         html_current += "\n</p>";
  240.         // convert str to char array
  241.         temp_char_len = html_current.length();
  242.         char current_char[html_current.length()+1];
  243.         strcpy(current_char, html_current.c_str());
  244.         pc.printf("%s\n", current_char);
  245.         clt_sock.send(current_char, temp_char_len+1);
  246.        
  247.         // soc
  248.         std::string html_soc = "<h2>State of Charge</h2><br><p class=\"soc\">";
  249.         sprintf(temp_str, "%f", soc);
  250.         html_soc += temp_str;
  251.         html_soc += "\n</p>";
  252.         // convert str to char array
  253.         temp_char_len = html_soc.length();
  254.         char soc_char[html_soc.length()+1];
  255.         strcpy(soc_char, html_soc.c_str());
  256.         pc.printf("%s\n", soc_char);
  257.         clt_sock.send(soc_char, temp_char_len+1);
  258.        
  259.         // flags
  260.         std::string flags = "<h2>Flags</h2><br><p class=\"flags\">";
  261.         sprintf(temp_str, "BPS_TRIP: %f\n", bps_trip);
  262.         flags += temp_str;
  263.         sprintf(temp_str, "ALL_CLEAR: %f\n", all_clear);
  264.         flags += temp_str;
  265.         sprintf(temp_str, "BPS_OFF: %f\n", bps_off);
  266.         flags += temp_str;
  267.         sprintf(temp_str, "WDOG: %f\n", wdog);
  268.         flags += temp_str;
  269.         flags += "</p>";
  270.         // convert str to char array
  271.         temp_char_len = flags.length();
  272.         char flag_char[flags.length()+1];
  273.         strcpy(flag_char, flags.c_str());
  274.         pc.printf("%s\n", flag_char);
  275.         clt_sock.send(flag_char, temp_char_len+1);
  276.        
  277.         ThisThread::sleep_for(5000);
  278.     }
  279. }
  280.  
  281. void acceptFunction(){
  282.     while(1){
  283.         pc.printf("Inside web accept\n");
  284.         srv.accept(&clt_sock, &clt_addr);           // Might need this in another thread
  285.         printf("accept %s:%d\r\n", clt_addr.get_ip_address(), clt_addr.get_port());
  286.         clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE));
  287.         // Thread::wait(1000);
  288.         // Thread::yield();
  289.         ThisThread::sleep_for(1000);
  290.     }
  291. }
  292.  
  293. void writeToSd() {
  294.        
  295. }
  296.  
  297.  
  298.  
  299. int main()
  300. {    
  301.  
  302.     char sbuffer[] = "<br>Pedal Position Voltage (uV):  ";
  303.     char motcur[10];
  304.     int alive = 0;  
  305.     char rdata[8];
  306.     int id1 = TRIT_DC_BASE + TRIT_DC_DRIVE;
  307.     int MotCur_int;
  308.  
  309.     CANMessage msg;
  310.     pc.baud(9600);          // set serial speed
  311.     can.frequency(125000);//(150000); // max 1Mbps, usually 150000
  312.    
  313.     union {
  314.     char rcvdata[4];
  315.     float rxdata;
  316.     } urxdata;
  317.    
  318.     printf("BeVolt Telemetry\r\n");
  319.    
  320.     // Set up ethernet connection
  321.     eth.connect();
  322.     printf("The target IP address is '%s'\r\n", eth.get_ip_address());
  323.    
  324.     // Open the server on ethernet stack
  325.     srv.open(&eth);
  326.     // Bind the HTTP port (TCP 80) to the server
  327.     srv.bind(eth.get_ip_address(), 80); // 23 ->80?
  328.     // Can handle 5 simultaneous connections
  329.     srv.listen(3);
  330.    
  331.     Thread canThread(received);
  332.     (&canThread)->set_priority(osPriorityHigh);
  333.     wait(1);
  334.     Thread serverThread(serverStuff);
  335.     (&serverThread)->set_priority(osPriorityHigh);
  336. //  wait(2);
  337. //  Thread acceptThread(acceptFunction);
  338. //  (&acceptThread)->set_priority(osPriorityLow);
  339.  
  340.     // Testing SD Card
  341.     my1307.settime( second, minute, hours, day, date, month, year);
  342.     FILE *fp = fopen("/sd/test.txt", "a+");
  343.    
  344.     while(1) {
  345.         //wait(1); 
  346.         ThisThread::sleep_for(10000);
  347.     }
  348.    
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement