Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Ethernet.h>
- #include <sha1.h>
- #include <stdlib.h>
- #include <mysql.h>
- #include <Time.h>
- #include <TimeAlarms.h>
- void setup() {
- // Initialize Variables
- int lastRead = HIGH; // The last reading (off)
- long blinks = 0; // Blink counter
- char user[] = "John"; // Username for MySQL Database
- char password[] = "Doe"; // Password for MySQL Database
- Connector my_conn; // Reference for the MySQL Connector
- byte mac_address[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
- IPAddress server_address(10, 10, 10, 10);
- // Pin 13 blinks with each pulse detection
- pinMode (13, OUTPUT);
- // Pin 2 is input from the phototransistor
- pinMode (2, INPUT);
- // Start Serial communication
- Serial.begin (115200);
- // Create timer to activate every minute (60 seconds)
- Alarm.timerRepeat(60, repeats);
- // Start Ethernet adapter
- Ethernet.begin(mac_address);
- delay(1000);
- Serial.println("Connecting...");
- if (my_conn.mysql_connect(server_addr, 3306, user, password)) {
- delay(1000);
- }
- else {
- Serial.println("Connection failed.");
- }
- }
- void loop() {
- int Pin2State = digitalRead(2);
- if (Pin2State != lastRead) {
- lastRead = Pin2State;
- if (Pin2State == LOW) { // A blink has been detected
- blinks++;
- digitalWrite(13, HIGH);
- } else {
- digitalWrite(13, LOW);
- }
- }
- // Allows sketch to check and see if timer has triggered
- Alarm.delay(0);
- }
- void repeats() {
- // When timer triggers, sends blinks counted to PowerShell, resets blink counter
- Serial.println(blinks);
- blinks = 0;
- }
- void do_query(const char *q) {
- column_names *c;
- row_values *r;
- // First, execute query. If it returns a value pointer,
- // we have a result set to process. If not, we exit.
- if (!my_conn.cmd_query(q)) {
- return;
- }
- // Next, we read the column names and display them.
- //
- // NOTICE: You must *always* read the column names even if
- // you do not use them. This is so the connector can
- // read the data out of the buffer. Row data follows the
- // column data and thus must be read first.
- c = my_conn.get_columns();
- for (int i = 0; i < c->num_fields; i++) {
- Serial.print(c->fields[i]->name);
- if (i < c->num_fields - 1) {
- Serial.print(",");
- }
- }
- // Next, we use the get_next_row() iterator and read rows printing
- // the values returned until the get_next_row() returns NULL.
- int num_cols = c->num_fields;
- int rows = 0;
- do {
- r = my_conn.get_next_row();
- if (r) {
- rows++;
- for (int i = 0; i < num_cols; i++) {
- Serial.print(r->values[i]);
- if (i < num_cols - 1) {
- Serial.print(", ");
- }
- }
- Serial.println();
- // Note: we free the row read to free the memory allocated for it.
- // You should do this after you've processed the row.
- my_conn.free_row_buffer();
- }
- } while (r);
- Serial.print(rows);
- Serial.println(" rows in result.");
- // Finally, we are done so we free column buffers
- my_conn.free_columns_buffer();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement