-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
150 lines (123 loc) · 4.36 KB
/
main.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h> //MQTT Broker Server
#include <iostream>
#include<string>
#include<sstream>
const char* ssid = "FRITZ!Box 6490 Cable";
const char* pass = "honisoitquimalypense!";
const char* brokerUser = "[email protected]"; //login info on dioty.co
const char* brokerPass = "32ec3f29";
const char* broker = "mqtt.dioty.co";
const char* inTopic = "/[email protected]/esptest";
const char* outTopic = "/[email protected]/esptestreply";
const int lockPin(3);
const int wifiIndicatorPin(19);
const int loginToDioty(18);
const int ledPin25(5); //pin 8 = GPIO05
const int ledPin50(17); //pin 9 = GPIO17
const int ledPin75(16); //pin 10 = GPIO16
const int ledPin100(4); // pin 11 = GPIO04
char messages[50];
WiFiClient espClient; // NOT sure what that does
PubSubClient client(espClient); // Nor this.., after this: reconnect function for server connection
int getValueFromMessage(std::string msg, std::string id);
int percentageToNumberOfLeds(int value);
void lightProcessLEDs(int numberOfLeds, int ledPin1, int ledPin2, int ledPin3, int ledPin4);
void identifyMessage(std::string msg){
// see what kind of message we receive
std::string substring = msg.substr(0,2);
if(substring == "pe"){
const int value(getValueFromMessage(msg, "pe"));
const int numberOfLeds = percentageToNumberOfLeds(value);
lightProcessLEDs(numberOfLeds, ledPin25, ledPin50, ledPin75, ledPin100);
const char * b = (char*)value;
client.publish(outTopic, "Process LEDs updated to:");
// SEEMS TO CRASH client.publish(outTopic, b);
}
else if(msg.substr(0,4) == "lock"){
digitalWrite(lockPin, LOW);
client.publish(outTopic, "box now locked.");
}
else if(msg.substr(0,6) == "unlock") {
digitalWrite(lockPin, HIGH);
client.publish(outTopic, "box now unlocked.");
}
}
void setupWifi(){
delay(100);
Serial.print("\nConnecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while(WiFi.status() != WL_CONNECTED){
delay(100);
Serial.print("-");
digitalWrite(loginToDioty, LOW);
delay(100);
digitalWrite(loginToDioty, HIGH);
}
Serial.print("\nConnected to ");
Serial.println(ssid);
}
void reconnect(){
while(!client.connected()){
Serial.print("\nConnecting to ");
Serial.println(broker);
if(client.connect("dev0001", brokerUser, brokerPass)){
Serial.print("\nConnected to ");
Serial.println(broker);
digitalWrite(wifiIndicatorPin, HIGH);
client.subscribe(inTopic);
} else {
Serial.println("\nTrying to connect again...");
//delay(5000); // instead: for-loop for flashing LED as long as not connected..
for (int a = 0; a < 33; a++) {
digitalWrite(wifiIndicatorPin, HIGH);
delay(50);
digitalWrite(wifiIndicatorPin, LOW);
delay(100);
}
}
}
}
void callback(char* topic, byte* payload, unsigned int length){ //callback function to receive data from the server
Serial.print("Received message: ");
//Serial.print(topic);
char message_buff[100]; //http://blue-pc.net/2014/10/15/mqtt-nachrichten-mit-dem-arduino-empfangen-senden/
int i = 0;
Serial.println("Message arrived: topic: " + String(topic));
Serial.println("Length: " + String(length,DEC));
for(i=0; i<length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff); // Converting message to string
//Serial.println("Payload: " + msgString);
//Serial.println(msgString[0]);
// ------- MESSAGE PARSE --------
identifyMessage(msgString.c_str());
if(msgString[0]=='t' || msgString[0]=='f')
client.publish(outTopic, "try either 'pex', where x is a value between 0 and 100, or 'lock' / 'unlock'");
}
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT); //ESP32 WROOM board: blaue SMD LED
pinMode(wifiIndicatorPin, OUTPUT);
pinMode(loginToDioty, OUTPUT); // server connection status: flashing = connecting, on = connected
pinMode(lockPin, OUTPUT); // controlled Light by MQTT message
pinMode(ledPin25, OUTPUT);
pinMode(ledPin50, OUTPUT);
pinMode(ledPin75, OUTPUT);
pinMode(ledPin100, OUTPUT);
setupWifi();
client.setServer(broker, 1883);
client.setCallback(callback);
}
void loop()
{
// put your main code here, to run repeatedly:
if (!client.connected()){
reconnect();
}
client.loop();
}