Tutorial 12: Take a Picture with Pi Camera

The Raspberry Pi Foundation had released two cameras designed to plug directly into the Raspberry Pi board: the regular Pi Camera and the Pi NOIR Camera. In the previous tutorial I showed you how to connect the camera and take a picture with it using Bash commands. In this tutorial I show you how to take pictures using Python 3!

This tutorial is a little older (published in 2015!). The main principles are still relevant, so I haven't needed to update this lesson.


DIFFICULTY
DIFFICULT
CIRCUITRY KNOWLEDGE
LITTLE
PYTHON PROGRAMMING
SOME
C++ PROGRAMMING
SOME
ABOUT
0
MINUTES
  • How to connect a nRF24L01+ to a Raspberry Pi
  • How to connect a nRF24L01+ to an Arduino
  • How send data to a Raspberry Pi from an Arduino
You can copy / paste the code below if you’re having issues with typos or want a shortcut. However I recommend that you follow along in the tutorial to understand what is going on!

receiveArduino.py


import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev

GPIO.setmode(GPIO.BCM)

pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]

radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)

radio.setPayloadSize(32)
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MIN)

radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()

radio.openReadingPipe(1, pipes[1])
radio.printDetails()
radio.startListening()

while True:

    while not radio.available(0):
        time.sleep(1/100)

    receivedMessage = []
    radio.read(receivedMessage, radio.getDynamicPayloadSize())
    print("Received: {}".format(receivedMessage))

    print("Translating our received Message into unicode characters...")
    string = ""

    for n in receivedMessage:
        if (n >= 32 and n <= 126):
            string += chr(n)
    print("Our received message decodes to: {}".format(string))

#>

send.ino


#include<SPI.h>
#include<RF24.h>

// ce, csn pins
RF24 radio(9, 10) ;

void setup(void){
  radio.begin() ;
  radio.setPALevel(RF24_PA_MAX) ;
  radio.setChannel(0x76) ;
  radio.openWritingPipe(0xF0F0F0F0E1LL) ;
  radio.enableDynamicPayloads() ;
  radio.powerUp() ;
  
}

void loop(void){
  const char text[] = "Hello World!" ;
  radio.write(&text, sizeof(text)) ;
  delay(1000) ;
  
}

sendreceiveArduino.py


import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev

GPIO.setmode(GPIO.BCM)

pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]

radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)

radio.setPayloadSize(32)
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MIN)

radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()

radio.openWritingPipe(pipes[0])
radio.openReadingPipe(1, pipes[1])
radio.printDetails()
#radio.startListening()

message = list("GETSTRING")
while len(message) < 32:
    message.append(0)

while True:
    start = time.time()
    radio.write(message)
    print("Sent the message: {}".format(message))
    radio.startListening()
    
    while not radio.available(0):
        time.sleep(1/100)
        if time.time() - start > 2:
            print("Timed out.")
            break

    receivedMessage = []
    radio.read(receivedMessage, radio.getDynamicPayloadSize())
    print("Received: {}".format(receivedMessage))

    print("Translating our received Message into unicode characters...")
    string = ""

    for n in receivedMessage:
        if (n >= 32 and n <= 126):
            string += chr(n)
    print("Our received message decodes to: {}".format(string))

    radio.stopListening()
    time.sleep(1)

# >

sendrecv.ino


#include<SPI.h>
#include<RF24.h>

// ce, csn pins
RF24 radio(9, 10) ;

void setup(void){
  while (!Serial) ;
  Serial.begin(9600) ;
  
  radio.begin() ;
  radio.setPALevel(RF24_PA_MAX) ;
  radio.setChannel(0x76) ;
  radio.openWritingPipe(0xF0F0F0F0E1LL) ;
  const uint64_t pipe = 0xE8E8F0F0E1LL ;
  radio.openReadingPipe(1, pipe) ;
  
  radio.enableDynamicPayloads() ;
  radio.powerUp() ;
  
}

void loop(void){
  radio.startListening() ;
  Serial.println("Starting loop. Radio on.") ;
  char receivedMessage[32] = {0} ;
  if (radio.available()){
    radio.read(receivedMessage, sizeof(receivedMessage));
    Serial.println(receivedMessage) ;
    Serial.println("Turning off the radio.") ;
    radio.stopListening() ;
    
    String stringMessage(receivedMessage) ;
    
    if (stringMessage == "GETSTRING"){
      Serial.println("Looks like they want a string!") ;
      const char text[] = "Hello World!" ;
      radio.write(text, sizeof(text)) ;
      Serial.println("We sent our message.") ;
    }
    
  }
  
  delay(100) ;
  
  
}

Tutorial 34

Tutorial 35