Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "WiFi.h"
- #include "WiFiClientSecure.h"
- #include "SoundData.h"
- #include "XT_DAC_Audio.h"
- #define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPSRequest_Generic v2.2.1"
- #define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN 2002001
- #define ASYNC_HTTPS_DEBUG_PORT Serial
- #define _ASYNC_TCP_SSL_LOGLEVEL_ 1
- #define _ASYNC_HTTPS_LOGLEVEL_ 1
- #define HTTPS_REQUEST_INTERVAL 2
- #define HEARTBEAT_INTERVAL 10
- #include "AsyncHTTPSRequest_Generic.h"
- #include "Ticker.h"
- const char* ssid = "Turtle Bay";
- const char* password = "wheretheturtlesstay";
- AsyncHTTPSRequest request;
- Ticker ticker;
- Ticker ticker1;
- int do_open = 0;
- int do_shut = 0;
- int looper;
- // Set web server port number to 80
- WiFiServer server(80);
- // Variable to store the HTTP request
- String header;
- String doorstate = "unknown";
- String localip;
- int transit;
- int post = 0;
- int loopControl = 0;
- XT_Wav_Class AudioRooster(roosterAudio);
- XT_Wav_Class AudioWolf(wolfAudio);
- XT_Wav_Class AudioClunk(clunkAudio);
- XT_Wav_Class AudioTransit(transitAudio);
- XT_DAC_Audio_Class DacAudio(25,0);
- XT_Sequence_Class Sequence;
- const int sensorOpen = 13;
- const int sensorShut = 12;
- const int ch1 = 25;
- const int motorOpen = 14;
- const int motorShut = 27;
- unsigned long currentTime = millis();
- unsigned long previousTime = 0;
- const long timeoutTime = 2000;
- void heartBeatPrint()
- {
- static int num = 1;
- if (WiFi.status() == WL_CONNECTED)
- Serial.print(F("H")); // H means connected to WiFi
- else
- Serial.print(F("F")); // F means not connected to WiFi
- if (num == 80)
- {
- //Serial.println();
- num = 1;
- }
- else if (num++ % 10 == 0)
- {
- //Serial.print(F(" "));
- }
- }
- void requestCB(void *optParm, AsyncHTTPSRequest *request, int readyState)
- {
- (void) optParm;
- if (readyState == readyStateDone)
- {
- AHTTPS_LOGDEBUG0(F("\n**************************************\n"));
- AHTTPS_LOGDEBUG1(F("Response Code = "), request->responseHTTPString());
- if (request->responseHTTPcode() == 200)
- {
- //Serial.println(F("\n**************************************"));
- //Serial.println(request->responseText());
- //Serial.println(F("**************************************"));
- }
- request->setDebug(false);
- }
- }
- void sendRequest()
- {
- static bool requestOpenResult;
- if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone)
- {
- if (digitalRead(sensorShut) == HIGH) {
- requestOpenResult = request.open("POST", "https://my.server.here/api/webhook/-zFgKm53M6rgV2uaKk03" );
- }
- else if ( digitalRead(sensorOpen) == HIGH ) {
- requestOpenResult = request.open("POST", "https://my.server.here/api/webhook/-uWJ_T6V2BKClxQx6u7" );
- }
- else if ( digitalRead(motorShut) == HIGH ) {
- requestOpenResult = request.open("POST", "https://my.server.here/api/webhook/-wxMfFP9D5lkl-AWdXGw" );
- }
- else if ( digitalRead(motorOpen) == HIGH ) {
- requestOpenResult = request.open("POST", "https://my.server.here/api/webhook/-rgYChJjv6fTtGQXITWi" );
- }
- else {
- requestOpenResult = request.open("POST", "https://my.server.here/api/webhook/-4Y0GNgnBQnCizKXiMOj" );
- }
- if (requestOpenResult)
- {
- // Only send() if open() returns true, or crash
- request.send();
- }
- else
- {
- Serial.println(F("Can't send bad request"));
- }
- }
- else
- {
- Serial.println(F("Can't send request"));
- }
- }
- void setup() {
- Serial.begin(115200);
- pinMode(sensorOpen, INPUT); //open safety sensor
- pinMode(sensorShut, INPUT); //closed safety sensor
- pinMode(ch1, OUTPUT); //future speaker pins
- pinMode(motorOpen, OUTPUT); //motor cw
- pinMode(motorShut, OUTPUT); //motor ccw
- // Set outputs to LOW
- digitalWrite(sensorOpen, LOW);
- digitalWrite(sensorShut, LOW);
- digitalWrite(ch1, LOW);
- digitalWrite(motorOpen, LOW);
- digitalWrite(motorShut, LOW);
- // Connect to Wi-Fi network with SSID and password
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected.");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- server.begin();
- request.setDebug(false);
- request.onReadyStateChange(requestCB);
- ticker.attach(HTTPS_REQUEST_INTERVAL, sendRequest);
- ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint);
- // Send first request now
- sendRequest();
- }
- void loop(){
- DacAudio.FillBuffer();
- // if(AudioTransit.Playing == false) {
- // DacAudio.Play(&AudioTransit);
- // }
- WiFiClient client = server.available(); // Listen for incoming clients
- if (client) { // If a new client connects,
- currentTime = millis();
- previousTime = currentTime;
- Serial.println("New Client."); // print a message out in the serial port
- String currentLine = ""; // make a String to hold incoming data from the client
- while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
- currentTime = millis();
- if (client.available()) { // if there's bytes to read from the client,
- char c = client.read(); // read a byte, then
- Serial.write(c); // print it out the serial monitor
- header += c;
- if (c == '\n') { // if the byte is a newline character
- // if the current line is blank, you got two newline characters in a row.
- // that's the end of the client HTTP request, so send a response:
- if (currentLine.length() == 0) {
- // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
- // and a content-type so the client knows what's coming, then a blank line:
- client.println("HTTP/1.1 200 OK");
- client.println("Content-type:text/html");
- client.println("Connection: close");
- client.println();
- if (header.indexOf("GET /gate/open") >= 0) {
- Serial.println("OPEN = Open signal received.");
- do_open = 1;
- }
- if (header.indexOf("GET /gate/shut") >= 0) {
- Serial.println("SHUT = Shut signal received.");
- do_shut = 1;
- }
- break;
- } else { // if you got a newline, then clear currentLine
- currentLine = "";
- }
- } else if (c != '\r') { // if you got anything else but a carriage return character,
- currentLine += c; // add it to the end of the currentLine
- }
- }
- }
- // Clear the header variable
- header = "";
- // Close the connection
- client.stop();
- Serial.println("Client disconnected.");
- Serial.println("");
- }
- if((do_open == 1 && doorstate == "open") || (do_shut == 1 && doorstate == "shut")) {
- Sequence.RemoveAllPlayItems();
- Sequence.AddPlayItem(&AudioClunk);
- DacAudio.Play(&Sequence);
- Serial.println("DOORDONE = 0");
- if(do_open == 1) {
- do_open = 0;
- }
- else {
- do_shut = 0;
- }
- }
- if (digitalRead(sensorOpen) != HIGH && do_open == 1) {
- Sequence.RemoveAllPlayItems();
- Sequence.AddPlayItem(&AudioRooster);
- DacAudio.Play(&Sequence);
- digitalWrite(motorOpen, HIGH);
- digitalWrite(motorShut, LOW);
- Serial.println("OPEN = motorOpen changed to HIGH.");
- Serial.println("OPEN = motorShut changed to LOW.");
- Serial.println("OPEN = Open action begin");
- }
- if (digitalRead(sensorShut) != HIGH && do_shut == 1) {
- Sequence.RemoveAllPlayItems();
- Sequence.AddPlayItem(&AudioWolf);
- DacAudio.Play(&Sequence);
- digitalWrite(motorOpen, LOW);
- digitalWrite(motorShut, HIGH);
- Serial.println("SHUT = motorOpen changed to LOW.");
- Serial.println("SHUT = motorShut changed to HIGH.");
- Serial.println("SHUT = Shut action begin");
- }
- if( digitalRead(motorShut) == HIGH || digitalRead(motorOpen) == HIGH ) {
- doorstate = "transit";
- }
- while (doorstate == "transit" ) {
- if(loopControl == 0) {
- delay(3000);
- Sequence.RemoveAllPlayItems();
- if(Sequence.RepeatForever != true) {
- Sequence.RepeatForever=true;
- Sequence.AddPlayItem(&AudioTransit);
- Serial.println("repeat forever = true, add audio transit");
- }
- DacAudio.Play(&Sequence);
- Serial.println(Sequence.Playing);
- Serial.println("play transit audio sequence");
- loopControl = 1;
- }
- if (digitalRead(sensorOpen) == HIGH && doorstate != "open" && do_open == 1) {
- Serial.println("sensorOpen is HIGH && doorstate is not open.");
- doorstate = "open";
- Serial.println("IFOPEN = Leaving transit state.");
- Serial.println("IFOPEN = Doorstate changed to open.");
- if (digitalRead(motorOpen) == HIGH) {
- if(Sequence.RepeatForever == true) {
- Sequence.RepeatForever = false;
- Sequence.RemoveAllPlayItems();
- loopControl = 0;
- }
- delay(2000);
- digitalWrite(motorOpen, LOW);
- Serial.println("IFOPEN = motorOpen changed to LOW.");
- Serial.println("DOORDONE = 1");
- }
- }
- if (digitalRead(sensorShut) == HIGH && doorstate != "shut" && do_shut == 1) {
- Serial.println("IFSHUT = sensorShut is HIGH && doorstate is not shut.");
- doorstate = "shut";
- Serial.println("IFSHUT = Leaving transit state.");
- Serial.println("IFSHUT = Doorstate changed to shut.");
- if (digitalRead(motorShut) == HIGH) {
- if(Sequence.RepeatForever == true) {
- Sequence.RepeatForever = false;
- Sequence.RemoveAllPlayItems();
- loopControl = 0;
- }
- delay(2000);
- digitalWrite(motorShut, LOW);
- Serial.println("DOORDONE = 1");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement