Advertisement
Guest User

So far...

a guest
Apr 30th, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.24 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <sha1.h>
  4. #include <stdlib.h>
  5. #include <mysql.h>
  6. #include <Time.h>
  7. #include <TimeAlarms.h>
  8.  
  9. void setup() {
  10.    // Initialize Variables
  11.    int lastRead = HIGH;            // The last reading (off)
  12.    long blinks = 0;                // Blink counter
  13.    char user[] = "John";   // Username for MySQL Database
  14.    char password[] = "Doe";     // Password for MySQL Database
  15.    Connector my_conn;              // Reference for the MySQL Connector
  16.    byte mac_address[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  17.    IPAddress server_address(10, 10, 10, 10);
  18.  
  19.    // Pin 13 blinks with each pulse detection
  20.    pinMode (13, OUTPUT);
  21.    
  22.    // Pin 2 is input from the phototransistor
  23.    pinMode (2, INPUT);
  24.    
  25.    // Start Serial communication
  26.    Serial.begin (115200);
  27.    
  28.    // Create timer to activate every minute (60 seconds)
  29.    Alarm.timerRepeat(60, repeats);
  30.    
  31.    // Start Ethernet adapter
  32.    Ethernet.begin(mac_address);
  33.    
  34.    delay(1000);
  35.    Serial.println("Connecting...");
  36.    if (my_conn.mysql_connect(server_addr, 3306, user, password)) {
  37.       delay(1000);
  38.    }
  39.    else {
  40.       Serial.println("Connection failed.");
  41.    }
  42. }
  43.  
  44. void loop() {
  45.  
  46.    int Pin2State = digitalRead(2);
  47.  
  48.    if (Pin2State != lastRead) {
  49.       lastRead = Pin2State;
  50.       if (Pin2State == LOW) { // A blink has been detected
  51.          blinks++;
  52.          digitalWrite(13,  HIGH);
  53.       } else {
  54.          digitalWrite(13,  LOW);
  55.       }
  56.    }
  57.    
  58.    // Allows sketch to check and see if timer has triggered
  59.    Alarm.delay(0);
  60. }
  61.  
  62. void repeats() {
  63.    // When timer triggers, sends blinks counted to PowerShell, resets blink counter
  64.    Serial.println(blinks);
  65.    
  66.    blinks = 0;
  67. }
  68.  
  69. void do_query(const char *q) {
  70.    column_names *c;
  71.    row_values *r;
  72.    
  73.    // First, execute query. If it returns a value pointer,
  74.    // we have a result set to process. If not, we exit.
  75.    if (!my_conn.cmd_query(q)) {
  76.       return;
  77.    }
  78.    
  79.    // Next, we read the column names and display them.
  80.    //
  81.    // NOTICE: You must *always* read the column names even if
  82.    //         you do not use them. This is so the connector can
  83.    //         read the data out of the buffer. Row data follows the
  84.    //         column data and thus must be read first.
  85.    
  86.    c = my_conn.get_columns();
  87.    
  88. for (int i = 0; i < c->num_fields; i++) {
  89. Serial.print(c->fields[i]->name);
  90. if (i < c->num_fields - 1) {
  91. Serial.print(",");
  92. }
  93. }
  94.    
  95.    // Next, we use the get_next_row() iterator and read rows printing
  96.    // the values returned until the get_next_row() returns NULL.
  97.    
  98.    int num_cols = c->num_fields;
  99.    int rows = 0;
  100.    do {
  101.       r = my_conn.get_next_row();
  102.       if (r) {
  103.          rows++;
  104.          
  105. for (int i = 0; i < num_cols; i++) {
  106. Serial.print(r->values[i]);
  107. if (i < num_cols - 1) {
  108. Serial.print(", ");
  109. }
  110. }
  111. Serial.println();
  112.          
  113.          // Note: we free the row read to free the memory allocated for it.
  114.          // You should do this after you've processed the row.
  115.          
  116.          my_conn.free_row_buffer();
  117.       }
  118.    } while (r);
  119.    
  120. Serial.print(rows);
  121. Serial.println(" rows in result.");
  122.    
  123.    // Finally, we are done so we free column buffers
  124.    
  125.    my_conn.free_columns_buffer();
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement