Difference between revisions of "Setup camera and such"

From Kandos Digital Embassy
Jump to: navigation, search
(= Temp/Humidity Sensor + dust)
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
== Connect to server ==
 
== Connect to server ==
  
 +
=== Set up general ==
 +
# change host name = basestation-01, edit in /etc/hosts, and /etc/hostname then "sudo /etc/init.d/hostname.sh"
 
# eth0 connect inet DHCP
 
# eth0 connect inet DHCP
#
 
  
 +
=== Temp/Humidity Sensor + dust ==
  
 +
[[File:Sensor_bb.png|thumbnail]]
 +
 +
* do with arduino.  following sketch with DHT11 library:
  
== Old approach for hosting data (now using server) ==
 
 
  <nowiki>
 
  <nowiki>
sudo apt-get update
+
#include <dht.h>
sudo apt-get upgrade
+
 
sudo apt-get install vlc
+
/*
sudo apt-get install hostapd
+
Standalone Sketch to use with a Arduino UNO and a
sudo apt-get install dnsmasq
+
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);
 +
}
 
</nowiki>
 
</nowiki>
edit the file /etc/network/interfaces and replace the line "iface wlan0 inet ..." with:
+
 
 +
== pi ==
 +
 
 +
# make sensor script senddata.py and change chmod 755 for executable
 
  <nowiki>
 
  <nowiki>
iface wlan0 inet static
+
#!/usr/bin/python
  address 192.168.42.1
+
import sys
  netmask 255.255.255.0
+
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
 
</nowiki>
 
</nowiki>
Edit the file /etc/default/hostapd and change the line:
+
# set up crontab -e
 
  <nowiki>
 
  <nowiki>
DAEMON_CONF="/etc/hostapd/hostapd.conf"
+
* * * * * /home/pi/senddata.py 1 >> /home/pi/cron_out.log
 
</nowiki>
 
</nowiki>
edit the file /etc/hostapd/hostapd.conf
 
 
  <nowiki>
 
  <nowiki>
interface=wlan0
+
sudo service cron restart
ssid=TacticalSpaceLab
+
hw_mode=g
+
channel=6
+
auth_algs=1
+
wmm_enabled=0
+
 
</nowiki>
 
</nowiki>
Change IP and start hostapd
+
 
<nowiki>
+
=== Camera sending image to Server ===
sudo ifconfig wlan0 192.168.42.1
+
 
sudo service hostapd start
+
# install sshpass
</nowiki>
+
  <nowiki>  
Maybe it doesn't work.  Using EDIMAX interface?  Try this version of hostapd (via [http://www.daveconroy.com/turn-your-raspberry-pi-into-a-wifi-hotspot-with-edimax-nano-usb-ew-7811un-rtl8188cus-chipset/ Dave Conroy])
+
sudo apt-get install sshpass
<nowiki>
+
wget http://www.daveconroy.com/wp3/wp-content/uploads/2013/07/hostapd.zip
+
unzip hostapd.zip
+
sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.bak
+
sudo mv hostapd /usr/sbin/hostapd.edimax
+
sudo ln -sf /usr/sbin/hostapd.edimax /usr/sbin/hostapd
+
sudo chown root.root /usr/sbin/hostapd
+
sudo chmod 755 /usr/sbin/hostapd
+
</nowiki>
+
Set up dnsmasq to give clients DHCP-based IP addresses
+
configure /etc/dnsmasq.conf
+
<nowiki>
+
interface=wlan0
+
dhcp-range=192.168.42.50,192.168.42.150,12h
+
</nowiki>
+
restart dnsmasq
+
  <nowiki>
+
sudo /etc/init.d/dnsmasq restart
+
 
</nowiki>
 
</nowiki>
Start video share
+
# make sendimage in /home/pi, with chmod 755
 
  <nowiki>
 
  <nowiki>
sudo modprobe bcm2835-v4l2
+
#!/bin/bash
cvlc v4l2:///dev/video0 --v4l2-width 1920 --v4l2-height 1080 --v4l2-chroma h264 --sout '#standard{access=http,mux=ts,dst=0.0.0.0:12345}'
+
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
 
</nowiki>
 
</nowiki>
 +
# add cron command to take image and upload to server
 +
*/5 * * * * /home/pi/sendimage
 +
# restart cron "sudo service cron restart"

Latest revision as of 02:27, 16 April 2015

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"