Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SerialStream.h>
- #include <iostream>
- #include <stdio.h>
- #include <mysql++.h>
- #include <string>
- #include <iomanip>
- #include <time.h>
- using namespace std;
- int string timeToString(time_t t)
- {
- stringstream ss;
- string s;
- ss << t;
- s = ss.str();
- return s;
- }
- int main() {
- using namespace LibSerial;
- SerialStream my_serial_stream;
- SerialStream my_serial_port;
- my_serial_stream.Open( "/dev/ttyS0" );
- my_serial_stream.SetBaudRate( SerialStreamBuf::BAUD_19200 );
- my_serial_port.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 );
- my_serial_port.SetNumOfStopBits(1);
- my_serial_port.SetParity( SerialStreamBuf::PARITY_NONE );
- my_serial_port.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_SOFT );
- my_serial_stream << "Hello, Serial Port." << flush;
- char next_char;
- string temp;
- string humi;
- string amps;
- time_t tsec = time(0);
- int i;
- for(i = 0; i < 4; i++) {
- my_serial_stream >> next_char;
- temp += next_char;
- }
- for(i = 0; i < 4; i++) {
- my_serial_stream >> next_char;
- humi += next_char;
- }
- for(i = 0; i < 4; i++) {
- my_serial_stream >> next_char;
- amps += next_char;
- }
- const char* db = "hydrograzeunit_1", *server = "localhost", *user = "root", *pass = "lightbulb";
- mysqlpp::Connection conn(false);
- if (conn.connect(db, server, user, pass)) {
- string q = "INSERT INTO environmentstats (temperature, humidity, amperage, timestamp) VALUES ('" + temp + "', '" + humi + "', '" + amps + "', '" + timeToString(tsec) + "')";
- cout << q << endl;
- mysqlpp::Query write_query = conn.query(q);
- write_query.exec();
- return 0;
- }
- else {
- cerr << "DB connection failed: " << conn.error() << endl;
- return 1;
- }
- }
Add Comment
Please, Sign In to add comment