-
Notifications
You must be signed in to change notification settings - Fork 0
/
Master-Node.ino
165 lines (145 loc) · 4.62 KB
/
Master-Node.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <ESPDMX.h>
//bluetooth
#include <SoftwareSerial.h>
#define rxPin D2 // Broche D2 en tant que RX, à raccorder sur TX du HC-05
#define txPin D3 // Broche D3 en tant que TX, à raccorder sur RX du HC-05
#define keyPin D4 // Broche D4 en tant que key, à raccorder sur Key du HC-05
SoftwareSerial BTSerial(rxPin, txPin);
unsigned long horloge;
DynamicJsonDocument functionActuel(1024);
DynamicJsonDocument functionRun(1024);
DynamicJsonDocument receivedData(1024);
DynamicJsonDocument nodes(1024);
//initialization as mode terminate and waiting for the message Discovery to start, 0 for run,1 for discovery, 2 for terminate
int masterMode=1;
//definition wifi
String ssid = "ledzGO";
String psk = "password";
IPAddress local_IP(192,168,4,1);
IPAddress gateway(192,168,4,1);
IPAddress subnet(255,255,255,0);
IPAddress Broadcast(192,168,4,255);
IPAddress dest;
unsigned int localPort = 8888;
boolean hidden = false;
int channel = 10;
int max_connection = 8;
WiFiUDP Udp;
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE + 1]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acked\r\n"; // a string to send back
char testData[]="{\"Data\": {\"Type\": \"Command\",\"NodeID\": 2354,\"Channels\": [ 125, 0, 62, 214 ]}}";
//maintenant tout est en Serial et on va le remplacer après
DMXESPSerial dmx;
void sendCommand(DynamicJsonDocument json){
if(json["Data"]["NodeID"]==WiFi.macAddress().c_str()){
//DMX show
Serial.println("DMX Command");
int RGB[]={json["Data"]["Channels"][0],json["Data"]["Channels"][1],json["Data"]["Channels"][2]};
dmx.write(1,RGB[0]);
dmx.write(2,RGB[1]);
dmx.write(3,RGB[2]);
dmx.update();
}else{
//Udp.beginPacket(nodes[receivedData["Data"]["NodeID"]], localPort);
String macDest=json["Data"]["NodeID"];
String ipDest=nodes[macDest];
dest.fromString(ipDest);
Udp.beginPacket(dest, localPort);
String commandSlave;
serializeJson(json,commandSlave);
Serial.println("Send command");
Serial.println(ipDest);
Serial.println(commandSlave);
Udp.println(commandSlave);
Udp.endPacket();
}
}
void runFunctionActuel(){
Serial.println("run function");
if(functionActuel["Function"][0]==NULL){
horloge=millis();
functionRun["Function"]=functionActuel["Function"];
}
if(millis()-horloge>=functionRun["Function"][0]["time"]){
DynamicJsonDocument command(1024);
DynamicJsonDocument commandJson(1024);
command["NodeID"]=functionRun["Function"][0]["NodeID"];
command["Channels"]=functionRun["Function"][0]["Channels"];
commandJson["Data"]=command;
sendCommand(commandJson);
functionActuel.remove(0);
}
}
void treatJson(DynamicJsonDocument json){
if(json["Data"]["Type"]=="Function"){
Serial.println("Run Function");
//restart horloge
horloge=millis();
functionActuel["Function"]=json["Data"]["Sequence"];
functionRun["Function"]=json["Data"]["Sequence"];
runFunctionActuel();
}else if(json["Data"]["Type"]=="Command"){
sendCommand(json);
}else if(json["Data"]["Type"]=="Terminate"){
masterMode=2;
Serial.println("Terminate Mode");
}else{
Serial.println("Wrong Type of JSON");
}
}
void setup()
{
Serial.begin(9600);
WiFi.softAPConfig(local_IP, gateway, subnet);
Serial.println(WiFi.softAP(ssid, psk, channel, hidden, max_connection) ? "AP ready" : "AP failed");
Udp.begin(localPort);
Serial.println("Begin");
nodes[WiFi.macAddress().c_str()]=WiFi.localIP().toString().c_str();
//bluetooth
// define pin modes for tx, rx pins:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
BTSerial.begin(9600);
Serial.begin(9600);
// setup key pin
pinMode(keyPin, OUTPUT);
digitalWrite(keyPin, LOW); // key pin
horloge=millis();
}
void loop() {
if(masterMode==2){
delay(1000);
}else if(masterMode==0){
//bluetooth
if (BTSerial.available()){
deserializeJson(receivedData,BTSerial.readString());
treatJson(receivedData);
delay(1000);
}
}else{
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.println("DiscoveryMode");
int n = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
packetBuffer[n] = 0;
Serial.println(packetBuffer);
char IP[16];
char Mac[20];
sscanf(packetBuffer,"%s %s",IP,Mac);
Serial.println(IP);
Serial.println(Mac);
nodes[Mac]=IP;
String i=nodes["3C:61:05:D2:BD:4A"];
Serial.println(i);
}
if(millis()-horloge>20000){
masterMode=0;
Serial.println("Run mode");
}
}
}