Hardware components | ||||||
| × | 1 |
A Methane Sensor attached to an Genuino mkr1000 sends notifications to Azure when poop (methane) is detected for a period of time.
#include <SPI.h>
#include <WiFi101.h>
int sensorPin = A1; // select the input pin for the potentiometer
int sensorValue = 0;
int sentcounter = 0;
char ssid[] = "myssid"; // your network SSID (name)
char pass[] = "mykey"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
char server[] = "my.server.com";
WiFiClient client;
void setup() {
sentcounter = 0;
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
}
void loop() {
sensorValue = analogRead(sensorPin);
if(sensorValue<500){
sentcounter = 0;
Serial.print("Low: ");
Serial.print(sensorValue);
Serial.println();
}
else{
Serial.print("Still up but no post ");
Serial.print(sensorValue);
Serial.println();
sentcounter +=1;
if (sentcounter == 10)
{
Serial.print("Post: ");
Serial.print(sensorValue);
Serial.println();
postData();
sentcounter = 0;
}
}
delay(1000);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void postData() {
// Combine yourdatacolumn header (yourdata=) with the data recorded from your arduino
// (yourarduinodata) and package them into the String yourdata which is what will be
// sent in your POST request
String yourdata = "poop=poop";
// If there's a successful connection, send the HTTP POST request
if (client.connect(server, 80)) {
Serial.println("connecting...");
// EDIT: The POST 'URL' to the location of your insert_mysql.php on your web-host
client.println("POST /path/poop.php HTTP/1.1");
// EDIT: 'Host' to match your domain
client.println("Host: my.server.com");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");
client.print("Content-Length: ");
client.println(yourdata.length());
client.println();
client.println(yourdata);
}
else {
// If you couldn't make a connection:
Serial.println("Connection failed");
Serial.println("Disconnecting.");
client.stop();
}
}
Alexios Tzanetopoulos
1 project • 1 follower
I think passion is my entire life.
My first passion was music.
Then I decided to study Computer Science.
Thanks to Alexios Tzanetopolous.
Comments