Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.95 KB | None | 0 0
  1. #include "WiFi.h"
  2. #include "WiFiClientSecure.h"
  3. #include "SoundData.h"
  4. #include "XT_DAC_Audio.h"
  5. #define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPSRequest_Generic v2.2.1"
  6. #define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN 2002001
  7. #define ASYNC_HTTPS_DEBUG_PORT Serial
  8. #define _ASYNC_TCP_SSL_LOGLEVEL_ 1
  9. #define _ASYNC_HTTPS_LOGLEVEL_ 1
  10. #define HTTPS_REQUEST_INTERVAL 2
  11. #define HEARTBEAT_INTERVAL 10
  12. #include "AsyncHTTPSRequest_Generic.h"
  13. #include "Ticker.h"
  14.  
  15. const char* ssid = "Turtle Bay";
  16. const char* password = "wheretheturtlesstay";
  17.  
  18. AsyncHTTPSRequest request;
  19. Ticker ticker;
  20. Ticker ticker1;
  21. int do_open = 0;
  22. int do_shut = 0;
  23. int looper;
  24.  
  25. // Set web server port number to 80
  26. WiFiServer server(80);
  27.  
  28. // Variable to store the HTTP request
  29. String header;
  30.  
  31. String doorstate = "unknown";
  32. String localip;
  33. int transit;
  34. int post = 0;
  35. int loopControl = 0;
  36. XT_Wav_Class AudioRooster(roosterAudio);
  37. XT_Wav_Class AudioWolf(wolfAudio);
  38. XT_Wav_Class AudioClunk(clunkAudio);
  39. XT_Wav_Class AudioTransit(transitAudio);
  40. XT_DAC_Audio_Class DacAudio(25,0);
  41. XT_Sequence_Class Sequence;
  42.  
  43. const int sensorOpen = 13;
  44. const int sensorShut = 12;
  45. const int ch1 = 25;
  46. const int motorOpen = 14;
  47. const int motorShut = 27;
  48.  
  49. unsigned long currentTime = millis();
  50.  
  51. unsigned long previousTime = 0;
  52. const long timeoutTime = 2000;
  53.  
  54. void heartBeatPrint()
  55. {
  56. static int num = 1;
  57.  
  58. if (WiFi.status() == WL_CONNECTED)
  59. Serial.print(F("H")); // H means connected to WiFi
  60. else
  61. Serial.print(F("F")); // F means not connected to WiFi
  62.  
  63. if (num == 80)
  64. {
  65. //Serial.println();
  66. num = 1;
  67. }
  68. else if (num++ % 10 == 0)
  69. {
  70. //Serial.print(F(" "));
  71. }
  72. }
  73.  
  74. void requestCB(void *optParm, AsyncHTTPSRequest *request, int readyState)
  75. {
  76. (void) optParm;
  77.  
  78. if (readyState == readyStateDone)
  79. {
  80. AHTTPS_LOGDEBUG0(F("\n**************************************\n"));
  81. AHTTPS_LOGDEBUG1(F("Response Code = "), request->responseHTTPString());
  82.  
  83. if (request->responseHTTPcode() == 200)
  84. {
  85. //Serial.println(F("\n**************************************"));
  86. //Serial.println(request->responseText());
  87. //Serial.println(F("**************************************"));
  88. }
  89.  
  90. request->setDebug(false);
  91. }
  92. }
  93.  
  94. void sendRequest()
  95. {
  96. static bool requestOpenResult;
  97.  
  98. if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone)
  99. {
  100. if (digitalRead(sensorShut) == HIGH) {
  101. requestOpenResult = request.open("POST", "https://my.server.here/api/webhook/-zFgKm53M6rgV2uaKk03" );
  102. }
  103. else if ( digitalRead(sensorOpen) == HIGH ) {
  104. requestOpenResult = request.open("POST", "https://my.server.here/api/webhook/-uWJ_T6V2BKClxQx6u7" );
  105. }
  106. else if ( digitalRead(motorShut) == HIGH ) {
  107. requestOpenResult = request.open("POST", "https://my.server.here/api/webhook/-wxMfFP9D5lkl-AWdXGw" );
  108. }
  109. else if ( digitalRead(motorOpen) == HIGH ) {
  110. requestOpenResult = request.open("POST", "https://my.server.here/api/webhook/-rgYChJjv6fTtGQXITWi" );
  111. }
  112. else {
  113. requestOpenResult = request.open("POST", "https://my.server.here/api/webhook/-4Y0GNgnBQnCizKXiMOj" );
  114. }
  115. if (requestOpenResult)
  116. {
  117. // Only send() if open() returns true, or crash
  118. request.send();
  119. }
  120. else
  121. {
  122. Serial.println(F("Can't send bad request"));
  123. }
  124. }
  125. else
  126. {
  127. Serial.println(F("Can't send request"));
  128. }
  129. }
  130.  
  131. void setup() {
  132. Serial.begin(115200);
  133.  
  134. pinMode(sensorOpen, INPUT); //open safety sensor
  135. pinMode(sensorShut, INPUT); //closed safety sensor
  136. pinMode(ch1, OUTPUT); //future speaker pins
  137. pinMode(motorOpen, OUTPUT); //motor cw
  138. pinMode(motorShut, OUTPUT); //motor ccw
  139. // Set outputs to LOW
  140. digitalWrite(sensorOpen, LOW);
  141. digitalWrite(sensorShut, LOW);
  142. digitalWrite(ch1, LOW);
  143. digitalWrite(motorOpen, LOW);
  144. digitalWrite(motorShut, LOW);
  145. // Connect to Wi-Fi network with SSID and password
  146. Serial.print("Connecting to ");
  147. Serial.println(ssid);
  148. WiFi.begin(ssid, password);
  149. while (WiFi.status() != WL_CONNECTED) {
  150. delay(500);
  151. Serial.print(".");
  152. }
  153. Serial.println("");
  154. Serial.println("WiFi connected.");
  155. Serial.println("IP address: ");
  156. Serial.println(WiFi.localIP());
  157. server.begin();
  158. request.setDebug(false);
  159. request.onReadyStateChange(requestCB);
  160. ticker.attach(HTTPS_REQUEST_INTERVAL, sendRequest);
  161. ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint);
  162. // Send first request now
  163. sendRequest();
  164. }
  165.  
  166. void loop(){
  167. DacAudio.FillBuffer();
  168. // if(AudioTransit.Playing == false) {
  169. // DacAudio.Play(&AudioTransit);
  170. // }
  171. WiFiClient client = server.available(); // Listen for incoming clients
  172. if (client) { // If a new client connects,
  173. currentTime = millis();
  174. previousTime = currentTime;
  175. Serial.println("New Client."); // print a message out in the serial port
  176. String currentLine = ""; // make a String to hold incoming data from the client
  177. while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
  178. currentTime = millis();
  179. if (client.available()) { // if there's bytes to read from the client,
  180. char c = client.read(); // read a byte, then
  181. Serial.write(c); // print it out the serial monitor
  182. header += c;
  183. if (c == '\n') { // if the byte is a newline character
  184. // if the current line is blank, you got two newline characters in a row.
  185. // that's the end of the client HTTP request, so send a response:
  186. if (currentLine.length() == 0) {
  187. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  188. // and a content-type so the client knows what's coming, then a blank line:
  189. client.println("HTTP/1.1 200 OK");
  190. client.println("Content-type:text/html");
  191. client.println("Connection: close");
  192. client.println();
  193. if (header.indexOf("GET /gate/open") >= 0) {
  194. Serial.println("OPEN = Open signal received.");
  195. do_open = 1;
  196. }
  197. if (header.indexOf("GET /gate/shut") >= 0) {
  198. Serial.println("SHUT = Shut signal received.");
  199. do_shut = 1;
  200. }
  201. break;
  202. } else { // if you got a newline, then clear currentLine
  203. currentLine = "";
  204. }
  205. } else if (c != '\r') { // if you got anything else but a carriage return character,
  206. currentLine += c; // add it to the end of the currentLine
  207. }
  208. }
  209. }
  210. // Clear the header variable
  211. header = "";
  212. // Close the connection
  213. client.stop();
  214. Serial.println("Client disconnected.");
  215. Serial.println("");
  216. }
  217. if((do_open == 1 && doorstate == "open") || (do_shut == 1 && doorstate == "shut")) {
  218. Sequence.RemoveAllPlayItems();
  219. Sequence.AddPlayItem(&AudioClunk);
  220. DacAudio.Play(&Sequence);
  221. Serial.println("DOORDONE = 0");
  222. if(do_open == 1) {
  223. do_open = 0;
  224. }
  225. else {
  226. do_shut = 0;
  227. }
  228. }
  229. if (digitalRead(sensorOpen) != HIGH && do_open == 1) {
  230. Sequence.RemoveAllPlayItems();
  231. Sequence.AddPlayItem(&AudioRooster);
  232. DacAudio.Play(&Sequence);
  233. digitalWrite(motorOpen, HIGH);
  234. digitalWrite(motorShut, LOW);
  235. Serial.println("OPEN = motorOpen changed to HIGH.");
  236. Serial.println("OPEN = motorShut changed to LOW.");
  237. Serial.println("OPEN = Open action begin");
  238. }
  239. if (digitalRead(sensorShut) != HIGH && do_shut == 1) {
  240. Sequence.RemoveAllPlayItems();
  241. Sequence.AddPlayItem(&AudioWolf);
  242. DacAudio.Play(&Sequence);
  243. digitalWrite(motorOpen, LOW);
  244. digitalWrite(motorShut, HIGH);
  245. Serial.println("SHUT = motorOpen changed to LOW.");
  246. Serial.println("SHUT = motorShut changed to HIGH.");
  247. Serial.println("SHUT = Shut action begin");
  248. }
  249. if( digitalRead(motorShut) == HIGH || digitalRead(motorOpen) == HIGH ) {
  250. doorstate = "transit";
  251. }
  252. while (doorstate == "transit" ) {
  253. if(loopControl == 0) {
  254. delay(3000);
  255. Sequence.RemoveAllPlayItems();
  256. if(Sequence.RepeatForever != true) {
  257. Sequence.RepeatForever=true;
  258. Sequence.AddPlayItem(&AudioTransit);
  259. Serial.println("repeat forever = true, add audio transit");
  260. }
  261. DacAudio.Play(&Sequence);
  262. Serial.println(Sequence.Playing);
  263. Serial.println("play transit audio sequence");
  264. loopControl = 1;
  265. }
  266. if (digitalRead(sensorOpen) == HIGH && doorstate != "open" && do_open == 1) {
  267. Serial.println("sensorOpen is HIGH && doorstate is not open.");
  268. doorstate = "open";
  269. Serial.println("IFOPEN = Leaving transit state.");
  270. Serial.println("IFOPEN = Doorstate changed to open.");
  271. if (digitalRead(motorOpen) == HIGH) {
  272. if(Sequence.RepeatForever == true) {
  273. Sequence.RepeatForever = false;
  274. Sequence.RemoveAllPlayItems();
  275. loopControl = 0;
  276. }
  277. delay(2000);
  278. digitalWrite(motorOpen, LOW);
  279. Serial.println("IFOPEN = motorOpen changed to LOW.");
  280. Serial.println("DOORDONE = 1");
  281. }
  282. }
  283. if (digitalRead(sensorShut) == HIGH && doorstate != "shut" && do_shut == 1) {
  284. Serial.println("IFSHUT = sensorShut is HIGH && doorstate is not shut.");
  285. doorstate = "shut";
  286. Serial.println("IFSHUT = Leaving transit state.");
  287. Serial.println("IFSHUT = Doorstate changed to shut.");
  288. if (digitalRead(motorShut) == HIGH) {
  289. if(Sequence.RepeatForever == true) {
  290. Sequence.RepeatForever = false;
  291. Sequence.RemoveAllPlayItems();
  292. loopControl = 0;
  293. }
  294. delay(2000);
  295. digitalWrite(motorShut, LOW);
  296. Serial.println("DOORDONE = 1");
  297. }
  298. }
  299. }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement