-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase-robot.ino
138 lines (104 loc) · 3.22 KB
/
base-robot.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*******************************************************************
Connect to local MQTT server with a Bot
ESP8266 library from https://github.com/esp8266/Arduino
Created for noycebru www.twitch.tv/noycebru
*******************************************************************/
#include "robot.h"
#include "robot_wifi.h"
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
//------------------------------
WiFiClient wiFiClient;
PubSubClient client(wiFiClient); // MQTT client
// Put your setup code here, to run once:
void setup() {
setupSerial();
setupPins();
setupWIFI();
setupMQTT();
}
void setupSerial() {
Serial.begin(115200);
Serial.println();
}
void setupPins() {
pinMode(LED_PIN, OUTPUT);
}
void setupWIFI() {
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);
}
void setupMQTT() {
client.setServer(MQTT_BROKER.c_str(), MQTT_PORT);
client.setCallback(callback);// Initialize the callback routine
}
void loop() {
// Check to make sure we are connected to the mqtt server
reconnectClient();
// Tell the mqtt client to process its loop
client.loop();
}
// Reconnect to client
void reconnectClient() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attempt to connect
if(client.connect(MQTT_ID.c_str())) {
Serial.println("Connected!");
for(int i=0;i < MQTT_TOPIC_COUNT;i++){
client.subscribe(MQTT_TOPIC[i].c_str());
Serial.print("Subcribed to: ");
Serial.println(MQTT_TOPIC[i]);
}
} else {
Serial.println(" try again in 5 seconds");
// Wait before retrying
delay(MQTT_RECONNECT_DELAY);
}
Serial.println('\n');
}
}
// Handle incomming messages from the broker
void callback(char* topic, byte* payload, unsigned int length) {
String response;
String msgTopic = String(topic);
Serial.println("topic received message:");
Serial.println(msgTopic);
for (int i = 0; i < length; i++) {
response += (char)payload[i];
}
Serial.print("Message arrived [");
Serial.print(topic);
Serial.println("] ");
Serial.println(response);
// We need to set the default value for the older message format
long activateValue = ACTIVATE_DEFAULT;
// This is quick and dirty with minimal input checking
// We are the only ones sending this data so we shouldn't have to worry
if (response.indexOf(",") != -1) {
// It looks like we are receiving the new format so try and parse the activation time
int delimiterLocation = response.indexOf(",");
activateValue = response.substring(delimiterLocation + 1, response.length()).toFloat();
}
// We need to turn the robot on
activateRobot(activateValue);
}
void activateRobot(long activateValue) {
Serial.print("activateRobot called: ");
Serial.println(activateValue);
// TODO: Add code for new robot here
Serial.println("activateRobot completed!");
Serial.println();
}