Guest User

Untitled

a guest
Jun 5th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <SerialStream.h>
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <mysql++.h>
  5. #include <string>
  6. #include <iomanip>
  7. #include <time.h>
  8.  
  9. using namespace std;
  10.  
  11. int string timeToString(time_t t)
  12. {
  13. stringstream ss;
  14. string s;
  15. ss << t;
  16. s = ss.str();
  17.  
  18. return s;
  19. }
  20.  
  21. int main() {
  22. using namespace LibSerial;
  23.  
  24. SerialStream my_serial_stream;
  25. SerialStream my_serial_port;
  26. my_serial_stream.Open( "/dev/ttyS0" );
  27. my_serial_stream.SetBaudRate( SerialStreamBuf::BAUD_19200 );
  28. my_serial_port.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 );
  29. my_serial_port.SetNumOfStopBits(1);
  30. my_serial_port.SetParity( SerialStreamBuf::PARITY_NONE );
  31. my_serial_port.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_SOFT );
  32. my_serial_stream << "Hello, Serial Port." << flush;
  33.  
  34. char next_char;
  35. string temp;
  36. string humi;
  37. string amps;
  38. time_t tsec = time(0);
  39. int i;
  40. for(i = 0; i < 4; i++) {
  41. my_serial_stream >> next_char;
  42. temp += next_char;
  43. }
  44. for(i = 0; i < 4; i++) {
  45. my_serial_stream >> next_char;
  46. humi += next_char;
  47. }
  48. for(i = 0; i < 4; i++) {
  49. my_serial_stream >> next_char;
  50. amps += next_char;
  51. }
  52.  
  53. const char* db = "hydrograzeunit_1", *server = "localhost", *user = "root", *pass = "lightbulb";
  54. mysqlpp::Connection conn(false);
  55. if (conn.connect(db, server, user, pass)) {
  56. string q = "INSERT INTO environmentstats (temperature, humidity, amperage, timestamp) VALUES ('" + temp + "', '" + humi + "', '" + amps + "', '" + timeToString(tsec) + "')";
  57.  
  58. cout << q << endl;
  59. mysqlpp::Query write_query = conn.query(q);
  60. write_query.exec();
  61. return 0;
  62. }
  63. else {
  64. cerr << "DB connection failed: " << conn.error() << endl;
  65. return 1;
  66. }
  67. }
Add Comment
Please, Sign In to add comment