IP
Published © LGPL

Baby-Pram Monitoring System

Real time monitoring variables like temperature, X-Y-Z acceleration, sound and light intensity for baby-pram.

IntermediateFull instructions provided15,827
Baby-Pram Monitoring System

Things used in this project

Hardware components

MMA7260
×1
LM92
×1
Breadboard (generic)
Breadboard (generic)
×1
Electret microphone
×1
Arduino MKR1000
Arduino MKR1000
×1
max4236
×1
Photocell
×1

Software apps and online services

ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Breadboard

Code

Source Code

C/C++
#include <WiFi101.h>    
//#include <WiFiClient.h>
//#include <WiFiServer.h>
//#include <WiFiSSLClient.h>
//#include <WiFiUdp.h>

#include <Wire.h>
#include <SPI.h>

char ssid[] = "Zzzzzzz";      //  your network SSID (name)
char pass[] = "xxxxxxx";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

char lm92address = 0x48; //A1 A0 are grounded
char buffer[2];
int tempcode;
float tempf;
int tempi;
int tempcode2;

int lighti;

int status = WL_IDLE_STATUS;
WiFiServer server(80);

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String APIKey = "WWWWWWWWWWWWWW";             // enter your channel's Write API Key
const int updateThingSpeakInterval = 20 * 1000; // 20 second interval at which to update ThingSpeak

// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;

// Temperature and ligth measurement every 1 min
unsigned long previousMillis = 0;
int interval_1m = 1 * 60 * 1000;     // 1 min

// Initialize Arduino Ethernet Client
WiFiClient client;




//############################## INIT - SETUP ##########################################
void setup() {

  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  // Start Serial for debugging on the Serial Monitor
  Wire.begin();
  Serial.begin(9600);

  // 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);
  }
  // you're connected now, so print out the status:
  printWifiStatus();
}



//################################## MAIN LOOP #######################################
void loop() {
  // read values from pins and store as strings
  //String temp   = String(10); //Just to have a defaul value
  //String light  = String(20); //Just to have a defaul value
  //String sound  = String(30); //Just to have a defaul value
  //String accelX = String(40); //Just to have a defaul value
  //String accelY = String(50); //Just to have a defaul value
  //String accelZ = String(60); //Just to have a defaul value



  //*************** ACCEL A4 A5 A6 *****************
  digitalWrite(7, HIGH);  // Disable SLEEP MOODE
  delay(10);
  String accelX = String(analogRead(A4), DEC); // read X acceleration
  delay(10);
  String accelY = String(analogRead(A5), DEC); // read X acceleration
  delay(10);
  String accelZ = String(analogRead(A6), DEC); // read X acceleration
  delay(10);
  String sound = String(analogRead(A2), DEC); // read mic
  digitalWrite(7, LOW);   // Enable SLEEP MOODE


//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TASK every interval_1m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Measurement of temperature and light %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

unsigned long currentMillis = millis();
 
if(currentMillis - previousMillis > interval_1m) {
    previousMillis = currentMillis;   


//**************** Temperature I2C LM92 ******************      
  Wire.requestFrom(lm92address, 2);
  if (Wire.available())
  {
    for (int i = 0; i < 2; i++) {
      buffer[i] = Wire.read();
            tempcode2=buffer[i];
            //Serial.print("Byte ");
            //Serial.print(i);
            //Serial.print(": ");
           //Serial.print(tempcode2);
            //Serial.println();
    }
    tempcode = ((buffer[0] << 8) + buffer[1]) >> 3;
    tempf = tempcode * 0.0625;
    tempi = int(tempf);
    Serial.println(tempf);
    Serial.println(tempi);
  }
//********************************************************


//****************** Photocell ADC A1 ********************
  digitalWrite(6, HIGH);
  delay(10);
  lighti = analogRead(A1); // read light value
  digitalWrite(6, LOW);
//*******************************************************
  
  }
 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

String temp   = String(tempi);
String light = String(lighti);

  //Print Update Response to Serial Monitor
  if (client.available()) {
    char c = client.read();
    //    Serial.print(c);
  }
  // Disconnect from ThingSpeak
  if (!client.connected() && lastConnected) {
    Serial.println("...disconnected");
    Serial.println();
    client.stop();
  }
  

// Just for debugging porpose
Serial.print(light);
    Serial.print(",");
    Serial.print(temp);
    Serial.print(",");
    Serial.print(sound);
    Serial.print(",");
    Serial.print(accelX);
    Serial.print(",");
    Serial.print(accelY);
    Serial.print(",");
    Serial.println(accelZ);

  
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% TASK every updateThingSpeakInterval 20 sec %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Update ThingSpeak Channel %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  if (!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval)) {
    updateThingSpeak("field1=" + light + "&field2=" + temp + "&field3=" + sound + "&field4=" + accelX + "&field5=" + accelY + "&field6=" + accelZ);

//    Serial.print(light);
//    Serial.print(",");
//    Serial.print(temp);
//    Serial.print(",");
//    Serial.print(sound);
//    Serial.print(",");
//    Serial.print(accelX);
//    Serial.print(",");
//    Serial.print(accelY);
//    Serial.print(",");
//    Serial.println(accelZ);

  }
  lastConnected = client.connected();
  delay(10);
}
//################################## END MAIN LOOP #######################################





//****************************** Function updateThingSpeak *******************************
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: " + APIKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(tsData.length());
    client.print("\n\n");
    client.print(tsData);
    lastConnectionTime = millis();

    if (client.connected()) {
      Serial.println("Connecting to ThingSpeak...");
      Serial.println();
    }
  }
}
//***************************************************************************************




//****************************** Function printWifiStatus *******************************
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");
}
//***************************************************************************************

Credits

IP

IP

1 project • 4 followers

Comments