Skip to content

Instantly share code, notes, and snippets.

@LuisFDuarte
Created August 11, 2014 20:22
Show Gist options
  • Select an option

  • Save LuisFDuarte/763d94c055320e0b30af to your computer and use it in GitHub Desktop.

Select an option

Save LuisFDuarte/763d94c055320e0b30af to your computer and use it in GitHub Desktop.
#include <WiFiShieldOrPmodWiFi_G.h> // This is for the MRF24WGxx on a pmodWiFi or WiFiShield
#include <DNETcK.h>
#include <DWIFIcK.h>
//Wifi Variables
char * server = "things.ubidots.com"; // server to connect to
unsigned short portServer = 80;
const char * szSsid = "Atom House Medellin";// Specify the SSID
#define USE_WPA2_PASSPHRASE
#if defined(USE_WPA2_PASSPHRASE)
const char * szPassPhrase = "atommed2014";// modify the security key to what you have.
#define WiFiConnectMacro() DWIFIcK::connect(szSsid, szPassPhrase, &status)
#else // no security - OPEN
#define WiFiConnectMacro() DWIFIcK::connect(szSsid, &status)
#endif
//Ubidots and Sensor Variables
String id="53e8f16476254238ae963390";
String token="NBbF3PWPxWc2IaO40aXOKnhIu8tOv92rYN3ibiEc7Jh6GV3KZUUCHtXuNz7Y";
String value="10";
const int sampleWindow=50;
int sample;
TcpClient tcpClient;
void setup() {
Serial.begin(9600);
DNETcK::STATUS status;
int conID = DWIFIcK::INVALID_CONNECTION_ID;
Serial.println("WiFiTCPClient 1.0");
if((conID = WiFiConnectMacro()) != DWIFIcK::INVALID_CONNECTION_ID)
{
Serial.print("Connection Created, ConID = ");
Serial.println(conID, DEC);
}
else
{
Serial.print("Unable to connection, status: ");
Serial.println(status, DEC);
}
DNETcK::begin();// use DHCP to get our IP and network addresses
tcpClient.connect(server, portServer); // Connecto Ubidots Server
}
void loop() {
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(A6);
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
Serial.print("volts"); //Electret_Mic_Sensivity=6.309mV/Pa
Serial.println(volts);
double p1=volts*1000/6.309;
int lp=20*log10(p1*100000/2);
Serial.print("DB:");
Serial.println(lp);
value=String(lp);
if(tcpClient.isConnected())
{
Save_Value();
}
DNETcK::periodicTasks();
}
//Function to save values to ubidots
void Save_Value(){
Serial.println("Sending value to Ubidots");
String var="{\"value\":"+ value + "}";
String le=String(var.length());
tcpClient.println("POST /api/v1.6/variables/"+id+"/values HTTP/1.1");
tcpClient.println("Content-Type: application/json");
tcpClient.println("Content-Length: "+le);
tcpClient.println("X-Auth-Token: "+token);
tcpClient.println("Host: things.ubidots.com");
tcpClient.println();
tcpClient.println(var);
tcpClient.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment