Setup camera and such

From Kandos Digital Embassy
Jump to: navigation, search

Connect to server[edit]

= Set up general[edit]

  1. change host name = basestation-01, edit in /etc/hosts, and /etc/hostname then "sudo /etc/init.d/hostname.sh"
  2. eth0 connect inet DHCP

= Temp/Humidity Sensor + dust[edit]

Sensor bb.png
  • do with arduino. following sketch with DHT11 library:
#include <dht.h>

/*
 Standalone Sketch to use with a Arduino UNO and a
 Sharp Optical Dust Sensor GP2Y1010AU0F
*/

//
//    FILE: dht11_test.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// PURPOSE: DHT library test sketch for DHT11 && Arduino
//     URL:
//
// Released to the public domain
//

dht DHT;

#define DHT11_PIN 4
  
int measurePin = 6; //Connect dust sensor to Arduino A0 pin
int ledPower = 12;   //Connect 3 led driver pins of dust sensor to Arduino D2
  
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
  
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
  
void setup(){
  Serial.begin(9600);
  pinMode(ledPower,OUTPUT); // for dust sensor
}
  
void loop(){
    if (Serial.available() > 0) {
      delay(1000);
      sendData();
      // clear buffer
      while(Serial.available())
      {
         char t= Serial.read(); 
      }
    }
}

float getFiltered() {
  getDustLevel();
  delay(200);
  float d = getDustLevel();
  delay(200);
  d += getDustLevel();
  delay(200);
  d += getDustLevel();
    delay(200);
  d += getDustLevel();
  return d/4; 
}

float getDustLevel() {
  digitalWrite(ledPower,LOW); // power on the LED
  delayMicroseconds(samplingTime);
  
  voMeasured = analogRead(measurePin); // read the dust value
  
  delayMicroseconds(deltaTime);
  digitalWrite(ledPower,HIGH); // turn the LED off
  delayMicroseconds(sleepTime);
  
  // 0 - 5V mapped to 0 - 1023 integer values
  // recover voltage
  calcVoltage = voMeasured * (5.0 / 1024.0);
  
  // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
  // Chris Nafis (c) 2012
  dustDensity = 0.17 * calcVoltage - 0.1;
  
  return dustDensity; // unit: mg/m3
}

void sendData()
{
  float dust = getFiltered();
  
  // READ DATA
  float temp = -100;
  float humidity = -100;
  int chk = DHT.read11(DHT11_PIN);
  if (chk == DHTLIB_OK) {
    humidity = DHT.humidity;
    temp = DHT.temperature;
  }
  /*switch (chk)
  {
    case DHTLIB_OK:  
		Serial.print("OK,\t"); 
		break;
    case DHTLIB_ERROR_CHECKSUM: 
		Serial.print("Checksum error,\t"); 
		break;
    case DHTLIB_ERROR_TIMEOUT: 
		Serial.print("Time out error,\t"); 
		break;
    case DHTLIB_ERROR_CONNECT:
        Serial.print("Connect error,\t");
        break;
    case DHTLIB_ERROR_ACK_L:
        Serial.print("Ack Low error,\t");
        break;
    case DHTLIB_ERROR_ACK_H:
        Serial.print("Ack High error,\t");
        break;
    default: 
		Serial.print("Unknown error,\t"); 
		break;
  }*/
  
  // DISPLAY DATA
  Serial.print("dust=");
  Serial.print(dust);
  Serial.print(",");
  Serial.print("humidity=");
  Serial.print(humidity);
  Serial.print(",");
  Serial.print("temp=");
  Serial.println(temp);
}

pi[edit]

  1. make sensor script senddata.py and change chmod 755 for executable
#!/usr/bin/python
import sys
import time
import urllib
import serial
# get basestation
id = str(sys.argv[1])
# set up pi and sensors
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=10)
# turn off serial DTR which resets Arduino 
time.sleep(1)
ser.setDTR(level=0)
time.sleep(1)
# send beepboop to activate sensor reading
ser.write('Yo!')
time.sleep(2)
str =  ser.readline()
print  str
types = str.split(',')
for type in types:
	parts = type.split('=')
	# send data, (download results to nowhere)
	print parts[0] + " = " + parts[1]
	urllib.urlretrieve("http://digitalembassy.mesh/data/sensors/logData.php?basestation="+id+"&type="+parts[0]+"&value="+parts[1]), filename="/dev/null")
	time.sleep(1)

# close

  1. set up crontab -e
* * * * * /home/pi/senddata.py 1 >> /home/pi/cron_out.log


sudo service cron restart

Camera sending image to Server[edit]

  1. install sshpass
 
sudo apt-get install sshpass

  1. make sendimage in /home/pi, with chmod 755
#!/bin/bash
raspistill -vf -hf -o /home/pi/image.jpg
sshpass -p '<password>' scp /home/pi/image.jpg pi@digitalembassy.mesh:/var/www/data/sensors/images/1.jpg
sshpass -p '<password>' scp /home/pi/image.jpg pi@digitalembassy.mesh:/var/www/data/sensors/images/1-$(date +%F-%H:%M).jpg

  1. add cron command to take image and upload to server
  • /5 * * * * /home/pi/sendimage
  1. restart cron "sudo service cron restart"