#define LED RED_LED
#define LED2 GREEN_LED
#include <aJSON.h>
#include "WiFi.h"
#endif
#include <WiFi.h>
#include <Wire.h>
#include <stdlib.h>
//thingspeak settings
char Thinkspeak_id[] = "api.thingspeak.com";
char thinkspeak_paid[] = "7X619FO176BRQNGH";
const int update_interval = 1000*16;
//wifi settings
//buffer for float to string
char buffer[25];
// your network name also called SSID
char ssid[] = "internetid";
// your network password
char password[] = "password";
// initialize the library instance:
WiFiClient client;
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to xively.com
int failedCounter = 0;
char fieldName1[] = "aaaaa"; // Stream you want to push to temp
char fieldName2[] = "bbbbb"; // Stream you want to push to hum
char fieldName3[] = "ddddd"; // Stream you want to push to baby
char fieldName4[] = "eeeee"; // Stream you want to push to buzzer
char fieldName5[] = "fffff"; // Stream you want to push to roomlight
int Temperature;
int Humidity;
int sample;
int buz, rooml;
const int threshold = 600;
void setup() {
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
Serial.begin(9600);
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to Network named: ");
// print the network name (SSID);
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.begin(ssid, pass);
while ( WiFi.status() != WL_CONNECTED) {
// print dots while we wait to connect
Serial.print(".");
delay(300);
}
Serial.println("\nYou're connected to the network");
Serial.println("Waiting for an ip address");
while (WiFi.localIP() == INADDR_NONE) {
// print dots while we wait for an ip addresss
Serial.print(".");
delay(300);
}
Serial.println("\nIP Address obtained");
// you're connected now, so print out the status
printWifiStatus();
}
void loop() {
Serial.print("\nInfant monitoring system:\n");
Temperature = analogRead(6); //temp
Humidity = analogRead(2); //humi
int sample = analogRead(24); //mic
Serial.print("Temperature : ");
int temp = ((Temperature/100)/2)*5;
Serial.println(temp);
int humi = (Humidity/100)*2.5;
Serial.print("Humidity : ");
Serial.println(humi);
Serial.print("Baby status : ");
if (sample ==0)
{
Serial.println("Fine");
}
else
{
Serial.println("Crying");
}
// Serial.println(sample);
if (temp > 27)
{
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED2, LOW);
buz = 1;
rooml = 0;
delay(100);
}
else if (humi > 75)
{
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
buz = 1;
delay(100);
}
else if (sample > threshold)
{
digitalWrite(LED, HIGH);
buz = 1;
// delay(1000);
}
else {
digitalWrite(LED, LOW);
buz = 0;
digitalWrite(LED2, HIGH);
rooml = 1;
}
Serial.println(sample);
int response = updateThingSpeak(temp);
int response2 = updateThingSpeak(humi);
int response4 = updateThingSpeak(sample);
int response5 = updateThingSpeak(buz);
int response6 = updateThingSpeak(rooml);
// Serial.println(response);
delay(1000);
}
void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
Serial.println(">>TSDATALength=" + tsData.length());
client.print("\n\n");
client.print(tsData);
Serial.println(">>TSDATA=" + tsData);
lastConnectionTime = millis();
if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}
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);
}