MechaLib
The robust, all-in-one software library for MechaLeague Robots
Loading...
Searching...
No Matches
comms.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef COMMS_H
3#define COMMS_H
4
5#include <Arduino.h>
6#include <WiFi.h>
7#include <ESPmDNS.h>
8#include <WiFiUdp.h>
9
10#ifndef TEAM_ID
11#define TEAM_ID 0
12#endif
13
20
25
31
32class Comms {
33 protected:
34 // Field
35 const char* fieldSSID = "MechaLeague-Field";
36 const char* fieldPass = "12345678";
37
38 // MDNS
39 const char* mdnsFormat = "mechaleague-bot-"; // mechaleague-bot-<team_id>.local
40
41 // Control sockets
43 const uint controlServerPort = 1010;
44
45 // Telemetry sockets
47 const uint telemetryServerPort = 1011;
48
50 if (TEAM_ID > 0 && TEAM_ID <= 9999) return true;
51 return false;
52 }
53
54 COMMS_CONN_ERR connectToWiFi(const char* ssid, const char* password = NULL) {
55 #ifdef TEAM_ID
56 // Validate team ID
58
59 // Compute IP address if needed
60 // if (localIP) localIP.fromString(generateIPAddress(TEAM_ID));
61
62 // Connect to wifi
63 // WiFi.config(localIP, gateway, subnet, dns1, dns2);
64 WiFi.mode(WIFI_STA);
65 if (password != NULL) WiFi.begin(ssid, password);
66 else WiFi.begin(ssid);
67 WiFi.waitForConnectResult();
68 wl_status_t status = WiFi.status();
69
70 // Start control and telemetry servers
72
73 // Start MDNS
74 startMDNS();
75
76 return status == WL_CONNECTED ? COMMS_CONN_ERR::COMMS_SUCCESS : COMMS_CONN_ERR::COMMS_FAILURE;
77
78 #else
80 #endif
81 }
82
83 public:
84 void startMDNS() {
85 char mdnsName[sizeof(mdnsFormat) + 4]; // Enough space for "mechaleague-bot-9999\0"
86 snprintf(mdnsName, sizeof(mdnsName), "%s%d", mdnsFormat, TEAM_ID); // mechaleague-bot-<team_id>
87 MDNS.begin(mdnsName);
88 }
89
94
98
99 // Singleton
100 private:
101 bool init = false;
102 public:
103 Comms() {};
104 static Comms& getInstance() {
105 static Comms instance;
106 return instance;
107 }
108};
109
110#endif
const uint controlServerPort
Definition comms.hpp:43
WiFiUDP telemetryServer
Definition comms.hpp:46
const char * mdnsFormat
Definition comms.hpp:39
WiFiUDP controlServer
Definition comms.hpp:42
const char * fieldSSID
Definition comms.hpp:35
const uint telemetryServerPort
Definition comms.hpp:47
static Comms & getInstance()
Definition comms.hpp:104
COMMS_CONN_ERR connectToWiFi(const char *ssid, const char *password=NULL)
Definition comms.hpp:54
void startServer()
Definition comms.hpp:90
void startMDNS()
Definition comms.hpp:84
Comms()
Definition comms.hpp:103
bool validateTeamID()
Definition comms.hpp:49
COMMS_CONN_ERR connectToField()
Definition comms.hpp:95
const char * fieldPass
Definition comms.hpp:36
COMMS_CONN_TYPE
Definition comms.hpp:26
@ CONN_BRIDGE
Definition comms.hpp:28
@ CONN_FIELD
Definition comms.hpp:27
@ CONN_AP
Definition comms.hpp:29
COMMS_MDNS_ERR
Definition comms.hpp:21
@ MDNS_FAILURE
Definition comms.hpp:23
@ MDNS_SUCCESS
Definition comms.hpp:22
COMMS_CONN_ERR
Definition comms.hpp:14
@ COMMS_SUCCESS
Definition comms.hpp:15
@ COMMS_NO_TEAM_ID
Definition comms.hpp:17
@ COMMS_FAILURE
Definition comms.hpp:16
@ COMMS_INVALID_TEAM_ID
Definition comms.hpp:18
#define TEAM_ID
Definition comms.hpp:11