Skip to content

7. How our Arduino eavesdrops on ONA

Armand Emamdjomeh edited this page Sep 24, 2015 · 11 revisions

We've got a card that can measure sound, now let's walk through how it posts signals to the server.

Take a look at https://github.com/anthonyjpesce/ona15-arduino-server/blob/master/soundtracker/sketches/post.ino

Woah, that's a lot of code! Don't worry, we'll walk through some of the important parts.

These lines define the wifi network, network name, and security type. These settings should work for our wifi network here at ONA15 (as long as the network doesn't crash, a common occurrence at ONAs.)

#define WLAN_SSID       "_ONA15" // wifi network name
#define WLAN_PASS       "onlinenews" // wifi network password
#define WLAN_SECURITY   WLAN_SEC_WPA2

You'll want to change the following line, to the ID of your robot

String postdata = "robot_id=1&volt=";

The first line below is only for show. It's just to keep track of the server IP in a string format. The Arduino library uses the integer of the IP, however. Again, we've set things up for our server at ONA15, but if you want to try this on your own, you'll need your own settings.

char server[] = "52.26.235.236";
const uint32_t ip = 874179564;

In the loop() function, the Arduino takes a peak-to-peak measurement of the signal every second, and assigns it to the volts variable.

    // collect data every second
    while (millis() - startMillis < sampleWindow)
    {
      sample = analogRead(0);
      if (sample < 1024)  // toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;  // save just the max levels
         }
         else if (sample < signalMin)
         {
            signalMin = sample;  // save just the min levels
         }
      }
    }

    peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
    double volts = (peakToPeak * 3.3) / 1024;  // convert to volts

The client then tries to connect to the server -- hopefully sucessfully.

    Serial.print(F("OK\r\nConnecting to server..."));
    t = millis();
    do {
       client = cc3000.connectTCP(ip, 80);
    } while((!client.connected()) &&
          ((millis() - t) < connectTimeout));

If it's successful, the client then creates a POST request to our server. The voltage is appended to the postdata string and sent to the server. The server then creates and records the signal for the assigned robot in robot_id in the querystring.

   if (client.connected()) {
     Serial.println("connecting...");

     client.println("POST /signal-submit/ HTTP/1.1");
     // EDIT: 'Host' to match your domain
     client.println("Host: 192.168.0.6");
     client.println("User-Agent: Arduino/1.0");
     client.println("Connection: close");
     client.println("Content-Type: application/x-www-form-urlencoded;charset=utf-8");
     client.print("Content-Length: ");
     client.println(postdata.length() + 3);
     client.println();
     client.print(postdata);
     client.print(volts);
   }
Clone this wiki locally