Close

Raspberry Pi zero solar powered server

A project log for An outdoor lab in our family vineyard

An experimental space for open DIY technology, sustainable agriculture, creative community, art, and connection between people and land

sotiris-barlakassotiris barlakas • 07/21/2025 at 13:49•0 Comments

After managing to have a sensor node based on ESP32 running using solar power and battery during nights and constantly gathering data, the next step is to set up a "brain" for our vineyard. A central server that can gather data from one ore more distributed sensor nodes and store that data or upload them to the cloud. As the vineyard hasn't internet access, for now the server will just save data locally in order for them to be accessed directly by reading the SD card.

The current endeavour is split in these parts:

1) Setup Raspberry Pi (initially AC powered)

2) Create a python server script that receives and saves data 

3) Create code for the ESP32 to connect to the server and send data from the sensor connected to it.

4) Finally, power the Raspberry Pi using solar power

Raspberry Pi Setup 

I had never worked with a raspberry before so my first goal was to set up a raspberry pi zero by installing an operating system to am SD card, and boot into it.  To my embarrassment and surprise I realized quickly that you can't just buy a raspberry pi alone, press a button and begin working with it. You need some peripherals. 

1) An hdmi cable to access it through a monitor. 

2) An micro hdmi adapter (because the hub in the pi is micro HDMI type)

3) An SD card to install the OS

4) An SD card USB adapter to plug in a laptop, and upload the downloaded OS to the SD card

5) Keyboard and mouse

6) USB hub because raspberry pi zero has only 2 micro-usb  ports (and one is taken for powering)

So after purchasing the parts and following an online tutorial to install the OS, I was finally in. 

To be sincere I didn't have a spare keyboard and I didn't want to buy one so I needed to find an alternative.

At first I activated from the settings the display keyboard function but writing code with that keyboard would soon turn my life into hell. So after a bit of searching I decided to set up a headless (meaning remote) connection to the raspberry that would enable me to control the raspberry from any device. In Linux this is commonly done after establishing a SSH connection. To do so, the raspberry pi and the device that you try to connect should be on the same Wi-Fi  network.  Alternatively the device must be connected to the Wi-Fi hotspot of the raspberry pi. I found that more generic and straightforward to implement so I started creating a hotspot from the raspberry pi.

This is very straightforward from the GUI of the pi, and all you have to do is to set a name, a password, and activate it. 

Then from another device, I connected to that Wi-Fi hotspot. Then, in order to connect to the raspberry pi and gain access to it, we have to find its IP address.  As we have access to the Pi from the monitor and mouse, we can find its IP by opening a terminal and typing:

hostname -I

 Then we can connect to the raspberry pi by entering in the terminal of the other device:

ssh {pi_username}@{ip_address}

We are in!!

Now we can use terminal commands to do whatever we want with the Raspberry Pi. We don't longer need the mouse and the monitor! 

But first we have to make sure that the Pi will activate the hotspot after every boot. We can set this up easily from the settings of the hotspot.

Create a python server

Now that the raspberry pi is set up and we can access it from any device to program it or inspect we are ready to actually start using it. 

First thing that we are going to try is to create a very simple python server using the Flask library. The functionality of the server initially will be a simple Hello World message print. Later, the server must be able to accept data from the connected client (aka the ESP32) and save them locally.

The code for the basic server setup is this:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, World!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)
 

The code for the server that receives data and saves them locally is:

import csv
from datetime import datetime
from flask import Flask

app = Flask(__name__)
@app.route('/data', methods=['POST'])
def receive_data():
    data = request.json
    print(f"Data received: {data}")

    with open('/home/dev/data_log.csv', 'a', newline='') as f:
        writer = csv.writer(f)
        # Write header if file is empty
        if f.tell() == 0:
            writer.writerow(['timestamp'] + list(data.keys()))
        writer.writerow([datetime.now().isoformat()] + list(data.values()))

    return "Data received successfully!", 200

One final step is to make the server script running each time the raspberry pi boots.

To do that we use a systemd service.

1) First type this to create a service file.

sudo nano /etc/systemd/system/myscript.service

 Replace myscript with any name you like.

2) In this new file we paste the following:

[Unit]
Description=Run My Python Script After Boot
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/myscript.py
WorkingDirectory=/home/pi
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Of course the following lines:

/home/pi/myscript.py
WorkingDirectory=/home/pi

Should be changed to match the path and name of your script.

3) Next, ensure that your script is executable.

chmod +x /home/pi/myscript.py

4) Finally enable the service:

sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable myscript.service

We are ready. Reboot the raspberry and check that it works. To check the status of the service (along with logs from our script) 

sudo systemctl status myscript.service

Create code for the ESP32 to connect to the server and send data

What we need next, is to also upload code to the ESP32, to configure it as a client that connects to the server (Raspberry Pi), and sends data in order to be saved locally.

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "pii"; // your hotspot SSID
const char* password = "12345678"; // your hotspot password
const char* serverUrl = "http://10.42.0.1:5000/data"; // <-- use your Pi IP

void setup() {
  Serial.begin(115200);

  WiFi.setMinSecurity(WIFI_AUTH_WPA_PSK);
  WiFi.begin(ssid, password);

  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected!");

  // Send data
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin(serverUrl);
    http.addHeader("Content-Type", "application/json");

    String jsonData = "{\"temperature\":24.5,\"humidity\":60}";
    int httpResponseCode = http.POST(jsonData);

    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);

    String response = http.getString();
    Serial.println("Server response:");
    Serial.println(response);

    http.end();
  } else {
    Serial.println("WiFi not connected!");
  }
}

void loop() {
  // Nothing here for now
}

 The code runs in the solar powered ESP32, that we set up as explained in the previous logs.


Power Raspberry Pi using sun

The system functions just fine, but we have to power the Raspberry Pi using solar power in order to deploy in the field. For that we equipped with a 5W solar panel with a regulator and USB output to make our life easier. Again (as in the ESP32 case) we use the TP4056 module to manage the charging of the battery. We will also need a DC voltage regulator to step the voltage up for the Raspberry as it works at 5V and the TP4056 uses the battery power which nominal value is 3.7 volts.

It's worth mentioning here that in contrary to the ESP32 solar setup, here we didn't connect the load (the Raspberry Pi) to the output pins of TP4056, but directly to the battery poles. This was decided, because we noticed that the TP4056 can't provide with steady power. And we noticed that during the boot of the Raspberry Pi the voltage dropped instantaneously, and the Pi rebooted. The sensitivity of the Raspberry Pi to current spikes during boot suggests adding a 1000 µF, 10 V capacitor in parallel between the voltage regulator and the Raspberry Pi to buffer power and help manage sudden drops in voltage.

This is it! The core of our project is ready and functioning. We have created prototypes of the brain of the vineyard and a sensor node. The next important step is to put everything  in waterproof enclosures, and test the system in the vineyard!

Discussions