Skip to content

Commit

Permalink
Changed types to uint whenever possible + created task for the wifi-h…
Browse files Browse the repository at this point in the history
…andler
  • Loading branch information
fabseman committed Apr 26, 2024
1 parent 431d050 commit 8cd4d3f
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 74 deletions.
17 changes: 10 additions & 7 deletions include/cars.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

/***************************** Include files *******************************/

#include "Arduino.h"

/***************************** Defines *******************************/

/***************************** Constants *******************************/
Expand All @@ -32,15 +34,16 @@

class Cars {
private:
int car_id, velocity, date_id, car_type_id, traffic_light_id;
uint8_t car_id, velocity, date_id, car_type_id, traffic_light_id;

public:
void init(int id, int speed, int date, int type, int traffic_light);
int return_id();
int return_velocity();
int return_date();
int return_type();
int return_traffic_id();
void init(uint8_t id, uint8_t speed, uint8_t date, uint8_t type,
uint8_t traffic_light);
uint8_t return_id();
uint8_t return_velocity();
uint8_t return_date();
uint8_t return_type();
uint8_t return_traffic_id();
};

/***************************** Functions *******************************/
Expand Down
2 changes: 1 addition & 1 deletion include/pcnt.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void setup_pcnt();
******************************************************************************/

void initialize_pcnt(pcnt_unit_t pcnt_unit, pcnt_channel_t pcnt_channel,
int input_pin);
uint8_t input_pin);
/*****************************************************************************
* Input : PCNT-unit, PCNT-channel & input-pin
* Output : -
Expand Down
17 changes: 9 additions & 8 deletions include/traffic_lights.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@

class TrafficLights {
private:
int traffic_light_id, queue_size;
String state, direction, location;
uint8_t traffic_light_id, queue_size;
String state, direction, location;

public:
void init(int id, String mode, String placement, String area, int queue);
int return_id();
String return_state();
String return_direction();
String return_location();
int return_queue_size();
void init(uint8_t id, String mode, String placement, String area,
uint8_t queue);
uint8_t return_id();
String return_state();
String return_direction();
String return_location();
uint8_t return_queue_size();
};

/***************************** Functions *******************************/
Expand Down
8 changes: 7 additions & 1 deletion include/wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,19 @@ void wifi_setup(String ssid, String password);
* Function : Initialize a WiFi-connection
******************************************************************************/

void test_connection();
void test_connection(void *pvParameters);
/*****************************************************************************
* Input : -
* Output : -
* Function : Tests connection to web-server by sending dummy data
******************************************************************************/

void start_wifi();
/*****************************************************************************
* Input : -
* Output : -
* Function : Starts task for communicating with the web-server
******************************************************************************/

/****************************** End Of Module *******************************/

Expand Down
14 changes: 8 additions & 6 deletions src/cars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/***************************** Include files *******************************/

#include "cars.h"
#include "Arduino.h"

/***************************** Defines *******************************/

Expand All @@ -28,7 +29,8 @@

/***************************** Functions *******************************/

void Cars::init(int id, int speed, int date, int type, int traffic_light) {
void Cars::init(uint8_t id, uint8_t speed, uint8_t date, uint8_t type,
uint8_t traffic_light) {
/*****************************************************************************
* Function : See module specification (.h-file)
*****************************************************************************/
Expand All @@ -40,14 +42,14 @@ void Cars::init(int id, int speed, int date, int type, int traffic_light) {
traffic_light_id = traffic_light;
}

int Cars::return_id() { return car_id; }
uint8_t Cars::return_id() { return car_id; }

int Cars::return_velocity() { return velocity; }
uint8_t Cars::return_velocity() { return velocity; }

int Cars::return_date() { return date_id; }
uint8_t Cars::return_date() { return date_id; }

int Cars::return_type() { return car_type_id; }
uint8_t Cars::return_type() { return car_type_id; }

int Cars::return_traffic_id() { return traffic_light_id; }
uint8_t Cars::return_traffic_id() { return traffic_light_id; }

/****************************** End Of Module *******************************/
1 change: 0 additions & 1 deletion src/gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ void setup_gpio() {
pinMode(GPIO_NUM_4, OUTPUT);
pinMode(GPIO_NUM_5, OUTPUT);
pinMode(GPIO_NUM_6, OUTPUT);
Serial.println("GPIO has been set up");
}

/****************************** End Of Module *******************************/
9 changes: 4 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ void setup() {
setup_gpio();
setup_timer0();
setup_pcnt();
// wifi_setup("WIFI_SSID_CHANGE_ME", "WIFI_PASSWORD_CHANGE_ME");
}

void loop() {
wifi_setup("WIFI_SSID_CHANGE_ME", "WIFI_PASSWORD_CHANGE_ME");
start_pcnt();
// test_connection();
start_wifi();
}

void loop() {}

/****************************** End Of Module *******************************/
6 changes: 3 additions & 3 deletions src/pcnt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pcnt_isr_handle_t user_isr_handle = NULL;
/***************************** Variables *******************************/

int16_t pulse_counter = RESET;
int overflow_counter = RESET;
uint8_t overflow_counter = RESET;

/***************************** Objects *******************************/

Expand All @@ -66,7 +66,7 @@ void setup_pcnt() {
};

void initialize_pcnt(pcnt_unit_t pcnt_unit, pcnt_channel_t pcnt_channel,
int input_pin) {
uint8_t input_pin) {
pcnt_config_t pcntFreqConfig;
pcntFreqConfig.pulse_gpio_num = input_pin;
pcntFreqConfig.pos_mode = PCNT_COUNT_INC;
Expand Down Expand Up @@ -99,7 +99,7 @@ void print_pcnt(void *pvParameters) {

while (1) {

static int range = 1;
static uint8_t range = 1;

switch (range) {
case NO_CAR:
Expand Down
1 change: 0 additions & 1 deletion src/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ void setup_timer0() {
true); // Fire Interrupt every 1m ticks, so 1s

timerAlarmEnable(timer);
Serial.println("Timer has been set up");
}

/****************************** End Of Module *******************************/
2 changes: 1 addition & 1 deletion src/traffic_cycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void traffic_cycle() {
/*****************************************************************************
* Function : See module specification (.h-file)
*****************************************************************************/
static int traffic_state = NS_READY;
static uint8_t traffic_state = NS_READY;

switch (traffic_state) {
case TL_IDLE:
Expand Down
14 changes: 7 additions & 7 deletions src/traffic_lights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

/***************************** Functions *******************************/

void TrafficLights::init(int id, String mode, String placement, String area,
int queue) {
void TrafficLights::init(uint8_t id, String mode, String placement, String area,
uint8_t queue) {
/*****************************************************************************
* Function : See module specification (.h-file)
*****************************************************************************/
Expand All @@ -42,14 +42,14 @@ void TrafficLights::init(int id, String mode, String placement, String area,
queue_size = queue;
}

int TrafficLights::return_id() { return traffic_light_id; }
uint8_t TrafficLights::return_id() { return traffic_light_id; }

String TrafficLights::return_state() { return state; }
String TrafficLights::return_state() { return state; }

String TrafficLights::return_direction() { return direction; }
String TrafficLights::return_direction() { return direction; }

String TrafficLights::return_location() { return location; }
String TrafficLights::return_location() { return location; }

int TrafficLights::return_queue_size() { return queue_size; }
uint8_t TrafficLights::return_queue_size() { return queue_size; }

/****************************** End Of Module *******************************/
76 changes: 43 additions & 33 deletions src/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,58 +39,68 @@ void wifi_setup(const String ssid, const String password) {
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(5);
}

Serial.println("Connected to WiFi");
}

void test_connection() {
void test_connection(void *pvParameters) {
/*****************************************************************************
* Function : See module specification (.h-file)
*****************************************************************************/

if (WiFi.status() == WL_CONNECTED) {

HTTPClient http;
while (1) {
if (WiFi.status() == WL_CONNECTED) {

Cars car1;
car1.init(20, 30, 2, 1, 2);
HTTPClient http;

const String server = "http://192.168.0.200:3000";
Cars car1;
car1.init(20, 30, 2, 1, 2);

const String car_url = server + "/vehicles/insert" +
"?car_id=" + car1.return_id() +
"&velocity=" + car1.return_velocity() +
"&date_id=" + car1.return_date() +
"&car_type_id=" + car1.return_type() +
"&traffic_light_id=" + car1.return_traffic_id();
const String server = "http://192.168.0.200:3000";

http.begin(car_url);
const String car_url = server + "/vehicles/insert" +
"?car_id=" + car1.return_id() +
"&velocity=" + car1.return_velocity() +
"&date_id=" + car1.return_date() +
"&car_type_id=" + car1.return_type() +
"&traffic_light_id=" + car1.return_traffic_id();

http.POST("");
http.begin(car_url);

TrafficLights traffic_light1;
traffic_light1.init(30, "Green", "South", "Faaborgvej 23, 5610 Assens", 20);
http.POST("");

const String traffic_light_url =
server + "/trafficlights/insert" +
"?traffic_light_id=" + traffic_light1.return_id() +
"&state=" + traffic_light1.return_state() +
"&direction=" + traffic_light1.return_direction() +
"&location=" + traffic_light1.return_location() +
"&queue_size=" + traffic_light1.return_queue_size();
TrafficLights traffic_light1;
traffic_light1.init(30, "Green", "South", "Faaborgvej 23, 5610 Assens",
20);

http.begin(traffic_light_url);
const String traffic_light_url =
server + "/trafficlights/insert" +
"?traffic_light_id=" + traffic_light1.return_id() +
"&state=" + traffic_light1.return_state() +
"&direction=" + traffic_light1.return_direction() +
"&location=" + traffic_light1.return_location() +
"&queue_size=" + traffic_light1.return_queue_size();

http.POST("");
http.begin(traffic_light_url);

http.end();
http.POST("");

} else {
Serial.println("Get a better internet connection!");
http.end();
}
}
}

void start_wifi() {
/*****************************************************************************
* Function : See module specification (.h-file)
*****************************************************************************/

xTaskCreate(test_connection, // function to be executed
"SQL_Queries", // name of the task
2048, // stack size
NULL, // parameter passed to the function
2, // priority
NULL // task handle
);
}

/****************************** End Of Module *******************************/

0 comments on commit 8cd4d3f

Please sign in to comment.