Skip to content

Commit

Permalink
ci: add docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
brushknight committed May 16, 2021
1 parent 710ffbf commit 4f69560
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 32 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release docker image with latest proxy server

on:
release:
types:
- released
tags:
- '*'


env:
TAG: ${{ github.event.release.tag_name }}
DOCKER_HUB_ID: ${{ secrets.DOCKER_HUB_ID }}
DOCKER_HUB_PASSWORD: ${{ secrets.DOCKER_HUB_PASSWORD }}

jobs:
docker:
name: Publish docker image
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ env.DOCKER_HUB_ID }}
password: ${{ env.DOCKER_HUB_PASSWORD }}
- run: make build
- run: make publish
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: build
build:
@docker build --target server -t brushknight/terrarium-proxy:latest -f server/Dockerfile server

.PHONY: publish
publish:
@docker push brushknight/terrarium-proxy:latest
59 changes: 36 additions & 23 deletions esp32/src/climate.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#include "climate.h"


namespace Climate
{



/* schedule
08 - 20 hot max 29.5
Expand All @@ -30,7 +27,7 @@ namespace Climate
#define DHT_COLD_CENTER_PIN 5 // #3
#define DHT_COLD_SIDE_PIN 18 // #4

#define DAY_MAX_TEMP 30//30
#define DAY_MAX_TEMP 30 //30
#define DAY_TEMP_TOLERANCE 1
#define NIGHT_MAX_TEMP 24
#define NIGHT_TEMP_TOLERANCE 1
Expand All @@ -51,27 +48,27 @@ namespace Climate
dht.temperature().getEvent(&event);
if (isnan(event.temperature))
{
Serial.println(F("Error reading temperature!"));
// Serial.println(F("Error reading temperature!"));
}
else
{
data.t = event.temperature;
Serial.print(F("Temperature: "));
Serial.print(event.temperature);
Serial.println(F("°C"));
// Serial.print(F("Temperature: "));
// Serial.print(event.temperature);
// Serial.println(F("°C"));
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity))
{
Serial.println(F("Error reading humidity!"));
// Serial.println(F("Error reading humidity!"));
}
else
{
data.h = event.relative_humidity;
Serial.print(F("Humidity: "));
Serial.print(event.relative_humidity);
Serial.println(F("%"));
// Serial.print(F("Humidity: "));
// Serial.print(event.relative_humidity);
// Serial.println(F("%"));
}
return data;
}
Expand All @@ -80,6 +77,7 @@ namespace Climate
{
relayState = HIGH;
digitalWrite(HEATER_RELAY_PIN, relayState);
Serial.println("turn relay on");
}

void turnRelayOff()
Expand All @@ -97,21 +95,13 @@ namespace Climate
dhtHotCenter.begin();
dhtColdCenter.begin();
dhtColdSide.begin();
Serial.println("turn relay on");
}

Telemetry::TelemteryData climateControl(int hour, int minute)
{

bool isDay = hour >= DAY_START_HOUR && hour < NIGHT_START_HOUR && minute >= DAY_START_MINUTE;

// read all temps

// if hot 1,2 temp is > limit
// turn heating off
// if hot 1,2 temp is < limit - tolerance
// turn heating on

Serial.print("is day: ");
Serial.println(isDay);

Expand All @@ -124,44 +114,67 @@ namespace Climate
Serial.println("4: cold side");
ClimateData coldSide = readTempHumid(dhtColdSide);

Telemetry::TelemteryData telemetryData = Telemetry::TelemteryData();

if (isDay)
{

if (hotSide.t < DAY_MAX_TEMP - DAY_TEMP_TOLERANCE && hotCenter.t < DAY_MAX_TEMP - DAY_TEMP_TOLERANCE)
{
turnRelayOn();
}else{
telemetryData.heater = true;
}
else
{
turnRelayOff();
telemetryData.heater = false;
}

// safety check
if (hotSide.t > DAY_MAX_TEMP || hotCenter.t > DAY_MAX_TEMP)
{
turnRelayOff();
telemetryData.heater = false;
}
}
else
{

// Serial.print("test night condition 1: ");
// Serial.println(hotSide.t < NIGHT_MAX_TEMP - NIGHT_TEMP_TOLERANCE);

// Serial.print("test night condition 2: ");
// Serial.println(hotCenter.t < NIGHT_MAX_TEMP - NIGHT_TEMP_TOLERANCE);

if (hotSide.t < NIGHT_MAX_TEMP - NIGHT_TEMP_TOLERANCE && hotCenter.t < NIGHT_MAX_TEMP - NIGHT_TEMP_TOLERANCE)
{
turnRelayOn();
}else{
telemetryData.heater = true;
}
else
{
turnRelayOff();
telemetryData.heater = false;
}

// safety check
if (hotSide.t > NIGHT_MAX_TEMP || hotCenter.t > NIGHT_MAX_TEMP)
{
turnRelayOff();
telemetryData.heater = false;
}
}

Telemetry::TelemteryData telemetryData = Telemetry::TelemteryData();
telemetryData.hotSide = hotSide;
telemetryData.hotCenter = hotCenter;
telemetryData.coldCenter = coldCenter;
telemetryData.coldSide = coldSide;

telemetryData.climateConfig.dayMaxTemp = DAY_MAX_TEMP;
telemetryData.climateConfig.nightMaxTemp = NIGHT_MAX_TEMP;
telemetryData.climateConfig.dayTempTolerance = DAY_TEMP_TOLERANCE;
telemetryData.climateConfig.nightTempTolerance = NIGHT_TEMP_TOLERANCE;

return telemetryData;
}
}
18 changes: 15 additions & 3 deletions esp32/src/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ namespace Telemetry
http.begin(TELEMETRY_ENDPOINT);
http.addHeader("Content-Type", "application/json");

StaticJsonDocument<200> doc;
StaticJsonDocument<400> doc;

doc["heater"] = telemteryData.heater;

doc.createNestedObject("hot_side");
doc["hot_side"]["H"] = telemteryData.hotSide.h;
Expand All @@ -37,18 +39,28 @@ namespace Telemetry
doc["cold_side"]["H"] = telemteryData.coldSide.h;
doc["cold_side"]["T"] = telemteryData.coldSide.t;


doc.createNestedObject("climate_config");
doc["climate_config"]["day_max_temp"] = telemteryData.climateConfig.dayMaxTemp;
doc["climate_config"]["night_max_temp"] = telemteryData.climateConfig.nightMaxTemp;
doc["climate_config"]["day_temp_tolerance"] = telemteryData.climateConfig.dayTempTolerance;
doc["climate_config"]["night_temp_tolerance"] = telemteryData.climateConfig.nightTempTolerance;


String requestBody;
serializeJson(doc, requestBody);

Serial.println(requestBody);

int httpResponseCode = http.POST(requestBody);

if (httpResponseCode > 0)
{

String response = http.getString();

Serial.println(httpResponseCode);
Serial.println(response);
// Serial.println(httpResponseCode);
// Serial.println(response);
}
else
{
Expand Down
13 changes: 11 additions & 2 deletions esp32/src/telemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@

#include "climate_data.h"


namespace Telemetry
{


using namespace Climate;

class ClimateConfig
{
public:
float dayMaxTemp;
float nightMaxTemp;
float dayTempTolerance;
float nightTempTolerance;
};

class TelemteryData
{
public:
ClimateData hotSide;
ClimateData hotCenter;
ClimateData coldCenter;
ClimateData coldSide;
ClimateConfig climateConfig;
bool heater;
};

void send(TelemteryData telemteryData);
Expand Down
50 changes: 50 additions & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Dockerfile References: https://docs.docker.com/engine/reference/builder/

### Build binary

# Start from the latest golang base image
FROM golang:latest AS builder

# Add Maintainer Info
LABEL maintainer="Grigorii Merkushev <[email protected]>"

# Set the Current Working Directory inside the container
WORKDIR /app

# Copy go mod and sum files
COPY go.mod go.sum ./

# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download

# Copy the source from the current directory to the Working Directory inside the container
COPY server.go .

# Build the Go apps
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o server server.go

###
### Create image with binary
###

# Start from the latest alpine base image
FROM alpine:latest AS server

# Set the Current Working Directory inside the container
WORKDIR /app

# Copy zoneinfo
RUN apk --no-cache add tzdata

# Copy artifacts created in previous step
COPY --from=builder /app/server .

# Add executable permissions
RUN chmod +x ./server

# Command to run the executable
ENTRYPOINT ["./server"]

# Expose port 80 to the outside world
EXPOSE 80

17 changes: 13 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ type ClimateData struct {
H float64 `json:"h"`
}

type ClimateConfig struct {
DayMaxTemp float64 `json:"day_max_temp"`
NightMaxTemp float64 `json:"night_max_temp"`
DayTempTolerance float64 `json:"day_temp_tolerance"`
NightTempTolerance float64 `json:"night_temp_tolerance"`
}

type Data struct {
HotSide ClimateData `json:"hot_side"`
HotCenter ClimateData `json:"hot_center"`
ColdCenter ClimateData `json:"cold_center"`
ColdSide ClimateData `json:"cold_side"`
HotSide ClimateData `json:"hot_side"`
HotCenter ClimateData `json:"hot_center"`
ColdCenter ClimateData `json:"cold_center"`
ColdSide ClimateData `json:"cold_side"`
Heater bool `json:"heater"`
Config ClimateConfig `json:"climate_config"`
}

var errResponse struct {
Expand Down

0 comments on commit 4f69560

Please sign in to comment.