Difference between revisions of "Setup camera and such"

From Kandos Digital Embassy
Jump to: navigation, search
(Camera sending image to Server)
(= Temp/Humidity Sensor)
Line 5: Line 5:
 
# eth0 connect inet DHCP
 
# eth0 connect inet DHCP
  
=== Temp/Humidity Sensor ==
+
=== Temp/Humidity Sensor + dust ==
 +
 
 +
* do with arduino.  following sketch with DHT11 library:
  
# get pigpio library, install.
 
 
  <nowiki>
 
  <nowiki>
cd ~
+
#include <dht.h>
wget abyz.co.uk/rpi/pigpio/pigpio.zip
+
 
unzip pigpio.zip
+
/*
cd PIGPIO
+
Standalone Sketch to use with a Arduino UNO and a
make
+
Sharp Optical Dust Sensor GP2Y1010AU0F
make install
+
*/
 +
 
 +
//
 +
//    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);
 +
}
 
</nowiki>
 
</nowiki>
# get dht22 example
+
 
# [http://www.raspberrypi.org/forums/viewtopic.php?p=515575 set up  pigpiod at boot]
+
== pi ==
# if using DHT11 the set up different file for DHT11!  [https://www.dropbox.com/s/326si04qiheqfw1/DHT22.py here is replacement to modify call to include (true) for DHT11)]
+
 
# make sensor script file and change chmod 755 for executable
+
# make sensor script senddata.py and change chmod 755 for executable
 
  <nowiki>
 
  <nowiki>
#!/usr/bin/python
+
 
#senddata.py
+
# get basestation
+
import sys
+
import time
+
id = str(sys.argv[1])
+
# set up pi and sensors
+
import os
+
import urllib
+
os.chdir('pigpio_dht22')
+
import pigpio
+
pi = pigpio.pi()
+
import DHT22
+
s = DHT22.sensor(pi, 4)
+
# get data
+
s.trigger()
+
time.sleep(1)
+
print('{:3.2f}'.format(s.temperature() / 1.))
+
print('{:3.2f}'.format(s.humidity() / 1.))
+
# send data, (download results to nowhere)
+
urllib.urlretrieve("http://digitalembassy.mesh/data/sensors/logData.php?basestation="+id+"&type=temp&value="+('%.2f' % s.temperature()), filename="/dev/null")
+
time.sleep(1)
+
urllib.urlretrieve("http://digitalembassy.mesh/data/sensors/logData.php?basestation="+id+"&type=humidity&value="+('%.2f' % s.humidity()), filename="/dev/null")
+
# close
+
#s.cancel()
+
#pi.stop()
+
#exit()
+
 
</nowiki>
 
</nowiki>
 
# set up crontab -e
 
# set up crontab -e

Revision as of 11:38, 10 April 2015

Connect to server

= Set up general

  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

  • 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

  1. make sensor script senddata.py and change chmod 755 for executable


  1. set up crontab -e
crontab -e


* * * * * /home/pi/senddata.py 1 >> /home/pi/cron_out.log


sudo service cron restart

Camera sending image to Server

  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"