# Camera module 3 connect to raspberry pi using node red to generate a dashboard for streaming purpose

**URL:** https://discourse.nodered.org/t/camera-module-3-connect-to-raspberry-pi-using-node-red-to-generate-a-dashboard-for-streaming-purpose/88064
**Category:** Dashboard
**Created:** [17 May 2024 15:33 UTC](https://discourse.nodered.org/t/camera-module-3-connect-to-raspberry-pi-using-node-red-to-generate-a-dashboard-for-streaming-purpose/88064 "2024-05-17T15:33:46Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Chow0605](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chow0605/32/91058_2.png) [@Chow0605](https://discourse.nodered.org/u/Chow0605)
#### Post date: [17 May 2024 15:33 UTC](https://discourse.nodered.org/t/camera-module-3-connect-to-raspberry-pi-using-node-red-to-generate-a-dashboard-for-streaming-purpose/88064/1 "2024-05-17T15:33:46Z")

</div>

Hi guys, I currently doing a project related to camera module 3, raspberry pi 3 and node red. The coding part i already done and can run in thonny. But when it comes to node red, i am blank because i don't know where to start from. According to my senior's flow, this is what he use to generate the dashboard but he didn't specifies the settings in the node. Need help from u guys.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bdfe5d95c2c3cea9b82ea228283bbbfeadfc2be6.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [17 May 2024 16:07 UTC](https://discourse.nodered.org/t/camera-module-3-connect-to-raspberry-pi-using-node-red-to-generate-a-dashboard-for-streaming-purpose/88064/2 "2024-05-17T16:07:41Z")

</div>

Welcome to the forum @Chow0605

Start by watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

### Author: ![Chow0605](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chow0605/32/91058_2.png) [@Chow0605](https://discourse.nodered.org/u/Chow0605)
#### Post date: [17 May 2024 16:53 UTC](https://discourse.nodered.org/t/camera-module-3-connect-to-raspberry-pi-using-node-red-to-generate-a-dashboard-for-streaming-purpose/88064/3 "2024-05-17T16:53:07Z")

</div>

I have done watching the videos but i still could not understand what i have done wrong in my flow.....

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [17 May 2024 20:56 UTC](https://discourse.nodered.org/t/camera-module-3-connect-to-raspberry-pi-using-node-red-to-generate-a-dashboard-for-streaming-purpose/88064/4 "2024-05-17T20:56:48Z")

</div>

What node is being used to get data from the camera? If you don't know what node has been installed then look in the dropdown menu Manage Pallet to see what nodes have been installed.

---

<div class="post-metadata">

### Author: ![Chow0605](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chow0605/32/91058_2.png) [@Chow0605](https://discourse.nodered.org/u/Chow0605)
#### Post date: [19 May 2024 06:58 UTC](https://discourse.nodered.org/t/camera-module-3-connect-to-raspberry-pi-using-node-red-to-generate-a-dashboard-for-streaming-purpose/88064/5 "2024-05-19T06:58:01Z")

</div>

Hi guys, is it possible for me to insert this coding into the python shell in node red can get the whole system running and display the offset data in dashboard?  
the coding is as below:

```auto
<from picamera2 import Picamera2 
from libcamera import controls 
import cv2 
import numpy as np 
import time 
import csv 

picam2 = Picamera2() 
config = picam2.create_preview_configuration() 
picam2.configure(config) 
picam2.set_controls({"AfMode":controls.AfModeEnum.Continuous}) 
picam2.start() 

regulated_line_position = None 
pixel_to_mm_ratio = 0.027 
start_time = time.time() 
interval = 1  

csv_filename = "offset_data.csv" 
with open(csv_filename, mode='w', newline='') as csv_file: 
    fieldnames = ['Timestamp', 'Distance_mm']  
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames) 
    writer.writeheader() 

while True: 
    with open(csv_filename, mode='a', newline='') as csv_file: 
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames) 
        
        im = picam2.capture_array() 
        gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) 
        edges = cv2.Canny(gray, 50, 150) 

        roi_y = 220 
        roi_height = im.shape[0] // 16 
        roi = edges[roi_y:roi_y + roi_height, :] 
        lines = cv2.HoughLinesP(roi, 1, np.pi / 180, threshold=100, minLineLength=100, maxLineGap=10)
        
        if lines is not None: 
            min_distance = float('inf') 
            for line in lines: 
                x1, y1, x2, y2 = line[0] 
                angle = np.arctan2(y2 - y1, x2 - x1) * 180 / np.pi 

                if 175 <= angle <= 185 or -5 <= angle <= 5 or 176 <= angle <= 186 or -6 <= angle <= 6: 
                    line_center = y2  
                    image_center = im.shape[1] / 2 
                    offset_pixels = line_center - image_center 
                    distance_mm = abs(offset_pixels) * pixel_to_mm_ratio 

                    if distance_mm < min_distance: 
                        min_distance = distance_mm 

                        cv2.rectangle(im, (x1, roi_y + y1), (x2, roi_y + y2), (0, 255, 0), 2) 

            if min_distance != float('inf') and time.time() - start_time >= interval: 
                timestamp = time.strftime('%Y-%m-%d %H:%M:%S') 
                writer.writerow({'Timestamp': timestamp, 'Distance_mm': min_distance}) 
                print("Distance to regulated line (mm):", min_distance) 
                start_time = time.time() 

        cv2.line(im, (0, im.shape[0] // 2), (im.shape[1], im.shape[0] // 2), (255, 0, 0), 1) 

        cv2.imshow("Frame", im) 

        if cv2.waitKey(1) & 0xFF == ord('q'): 
            break 

picam2.release() 
cv2.destroyAllWindows()

```

the flow in node red is as below:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/330d0eb99c6607c5411e215680822dd689c3a839.png)

this should be working according to my collage senior, but i could not get in working, soon as i deploy it, it shows a few lines under the python shell and immediately change to script closed and stop.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [19 May 2024 08:14 UTC](https://discourse.nodered.org/t/camera-module-3-connect-to-raspberry-pi-using-node-red-to-generate-a-dashboard-for-streaming-purpose/88064/6 "2024-05-19T08:14:54Z")

</div>

Seeing that this is a continuation of a thread you opened yesterday, I have moved your post to this thread.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [19 May 2024 08:14 UTC](https://discourse.nodered.org/t/camera-module-3-connect-to-raspberry-pi-using-node-red-to-generate-a-dashboard-for-streaming-purpose/88064/7 "2024-05-19T08:14:59Z")

</div>



---

<div class="post-metadata">

### Author: ![Chow0605](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chow0605/32/91058_2.png) [@Chow0605](https://discourse.nodered.org/u/Chow0605)
#### Post date: [19 May 2024 13:03 UTC](https://discourse.nodered.org/t/camera-module-3-connect-to-raspberry-pi-using-node-red-to-generate-a-dashboard-for-streaming-purpose/88064/9 "2024-05-19T13:03:51Z")

</div>

inject node to inject the whole system, python shell to run the coding, a chart and a gauge to display the data
