├── NodeMCUcodewithpostget.ino └── stepcontroling.ino /NodeMCUcodewithpostget.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "DHT.h" 7 | 8 | #define DHTPIN 12 // D6 pin 9 | #define DHTTYPE DHT11 10 | //#define microSwitchDoor 16 //D0 pin 11 | const int doorSwitch = D3; //D0 pin 12 | 13 | DHT dht(DHTPIN, DHTTYPE); 14 | 15 | SoftwareSerial myNode (D1, D2); //RX, TX 16 | String git; 17 | int a = 1; 18 | int count = 0; 19 | int respond = 0; 20 | int mailcount =1; 21 | 22 | 23 | //Wi-Fi name. 24 | const char* ssid = "SLT-ADSL-86938"; 25 | //Wi-Fi password 26 | const char* password = "caj2753nmj"; 27 | // Device apikey for identifying the device. Each device has a different API key. 28 | String apiKeyValue = "12345678"; 29 | // Link using GET and POST data to server. 30 | const char* host = "http://cbx5e.localto.net/api/v1/data"; 31 | // Link using Send Email. 32 | const char* ehost= "maker.ifttt.com"; 33 | 34 | 35 | 36 | void setup() { 37 | pinMode(doorSwitch, INPUT); 38 | // Initialize "debug" serial port 39 | Serial.begin(115200); 40 | Serial.println("DHTxx test!"); 41 | dht.begin(); 42 | myNode.begin(9600); 43 | myNode.write(6); 44 | // Connecting to the WiFi. 45 | WiFi.begin(ssid, password); 46 | Serial.println("Connecting"); 47 | 48 | // Trying to connect to WiFi. 49 | while (WiFi.status() != WL_CONNECTED) { 50 | delay(500); 51 | Serial.print("."); 52 | } 53 | 54 | Serial.println(""); 55 | Serial.print("Connected to WiFi network with IP Address: "); 56 | Serial.println(WiFi.localIP()); 57 | // email(); 58 | } 59 | 60 | void email(){ 61 | 62 | if(mailcount ==1){ 63 | mailcount ++; 64 | WiFiClient client; 65 | if (!client.connect(ehost, 80)) { 66 | Serial.println("Connection failed"); 67 | return; 68 | } 69 | String url = "/trigger/securitybreach/with/key/c2dLtgqA9rmZxR-gweIb7T"; 70 | // Create a JSON object 71 | StaticJsonDocument<200> jsonDocument; 72 | jsonDocument["value1"] = "Vending ID 01"; 73 | jsonDocument["value2"] = ""; 74 | jsonDocument["value3"] = ""; 75 | 76 | // Serialize the JSON object to a string 77 | String body; 78 | serializeJson(jsonDocument, body); 79 | 80 | Serial.println("Sending request..."); 81 | 82 | client.print("POST " + url + " HTTP/1.1\r\n"); 83 | client.print("Host: " + String(ehost) + "\r\n"); 84 | client.print("Content-Type: application/json\r\n"); 85 | client.print("Content-Length: " + String(body.length()) + "\r\n"); 86 | client.print("Connection: close\r\n"); 87 | client.print("\r\n"); 88 | client.print(body); 89 | delay(1000); 90 | 91 | while (client.available()) { 92 | String line = client.readStringUntil('\r'); 93 | Serial.print(line); 94 | } 95 | 96 | client.stop(); 97 | } 98 | } 99 | void dhtdata(){ 100 | float h = dht.readHumidity(); 101 | float t = dht.readTemperature(); 102 | // Check if any reads failed and exit early (to try again). 103 | if (isnan(h) || isnan(t)) { 104 | Serial.println("Failed to read from DHT sensor!"); 105 | return; 106 | } 107 | Serial.print("Humidity: "); 108 | Serial.print(h); 109 | Serial.print("% Temperature: "); 110 | Serial.print(t); 111 | Serial.println("°C "); 112 | 113 | WiFiClient client; 114 | HTTPClient http; 115 | http.begin(client, host); 116 | // Specify content-type header 117 | http.addHeader("Content-Type", "application/json"); 118 | DynamicJsonDocument doc(200); 119 | 120 | doc["humidity"] = String(h); 121 | doc["temperature"] = String(t); 122 | // doc["value"] = "6"; 123 | 124 | String httpRequestData =""; 125 | 126 | serializeJson(doc, httpRequestData); 127 | Serial.print("httpRequestData: "); 128 | // post data to server. 129 | Serial.println(httpRequestData); 130 | int httpResponseCode = http.POST(httpRequestData); 131 | 132 | // Getting http response code. 133 | if (httpResponseCode>0) { 134 | Serial.print("HTTP Response code: "); 135 | Serial.println(httpResponseCode); 136 | } 137 | else { 138 | Serial.print("Error code: "); 139 | Serial.println(httpResponseCode); 140 | } 141 | 142 | String payloadGetpart = http.getString(); 143 | Serial.print("Incomming data : "); 144 | Serial.println(payloadGetpart); 145 | http.end(); 146 | } 147 | void loop() { 148 | 149 | //////////////Get_Request//////////////////////////////////////// 150 | while (!myNode.available()) { 151 | delay(100); 152 | } 153 | WiFiClient client; 154 | HTTPClient http; 155 | http.begin(client, host); 156 | 157 | String payloadincomming = http.getString(); 158 | int httpResponseCodeget = http.GET(); 159 | 160 | Serial.println(httpResponseCodeget); 161 | 162 | if (httpResponseCodeget > 0) { 163 | Serial.print("HTTP Response Code: "); 164 | Serial.println(httpResponseCodeget); 165 | payloadincomming = http.getString(); 166 | Serial.print("Incoming data: "); 167 | // git = payloadincomming; 168 | // Serial.println(git); 169 | DynamicJsonDocument jsonDocument(200); 170 | DeserializationError error = deserializeJson(jsonDocument, payloadincomming); 171 | if (error) { 172 | Serial.print("Deserialization error: "); 173 | Serial.println(error.c_str()); 174 | } 175 | else{ 176 | git = jsonDocument["value"].as(); 177 | Serial.println(git); 178 | } 179 | 180 | // Remove any extra characters from the string and convert to integer. 181 | git.trim(); 182 | a = git.substring(1, git.length() - 1).toInt(); 183 | 184 | if (a == 0 && git[1] != '0') { 185 | Serial.println("Conversion error"); 186 | } else { 187 | Serial.print("Number "); 188 | Serial.println(a); 189 | myNode.write(a); 190 | delay(30); 191 | while(respond != 5){ 192 | respond = myNode.read(); 193 | delay(30); 194 | } 195 | respond =0; 196 | } 197 | } 198 | 199 | http.end(); 200 | Serial.println("---------------Send Data---------------"); 201 | Serial.println(""); 202 | delay(4000); 203 | dhtdata(); 204 | Serial.println("---------------End OF Message---------------"); 205 | Serial.println(""); 206 | int doorSwitchState = digitalRead(doorSwitch); 207 | if(doorSwitchState == HIGH){ 208 | Serial.println("------------------------SECURITY BTEACH--------------------"); 209 | email(); 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /stepcontroling.ino: -------------------------------------------------------------------------------- 1 | String command; 2 | 3 | #include 4 | Servo servo; // DS04-NFC motors 5 | Servo clutchservo; 6 | 7 | #include 8 | SoftwareSerial mySerial(12,13); //RX, TX D0->13 D1->12 9 | int movepoint; 10 | int res =5; 11 | int IRSensor = 10; 12 | // Stepper motors pins 13 | #define dirPinVertical 2 14 | #define stepPinVertical 3 15 | #define dirPinHorizontal 4 16 | #define stepPinHorizontal 5 17 | 18 | #define microSwitchV 6 19 | #define microSwitchH 7 20 | 21 | int pos =0; 22 | int sensorStatus; 23 | void setup() { 24 | servo.attach(8); 25 | clutchservo.attach(9); 26 | 27 | Serial.begin(115200); 28 | mySerial.begin(9600); 29 | Serial.print("Welcome..."); 30 | pinMode(dirPinVertical, OUTPUT); 31 | pinMode(stepPinVertical, OUTPUT); 32 | pinMode(dirPinHorizontal, OUTPUT); 33 | pinMode(stepPinHorizontal, OUTPUT); 34 | 35 | 36 | pinMode(microSwitchV, INPUT_PULLUP); 37 | pinMode(microSwitchH, INPUT_PULLUP); 38 | 39 | pinMode(IRSensor, INPUT); 40 | // Horizontal starting position 41 | digitalWrite(dirPinHorizontal, HIGH); 42 | while (true) { 43 | if (digitalRead(microSwitchH) == LOW) { 44 | // moveRight(70); 45 | break; 46 | } 47 | digitalWrite(stepPinHorizontal, LOW); 48 | delayMicroseconds(1500); 49 | digitalWrite(stepPinHorizontal, HIGH); 50 | delayMicroseconds(1500); 51 | } 52 | 53 | // Vertical starting position 54 | digitalWrite(dirPinVertical, HIGH); // Set the stepper to move in a particular direction 55 | while (true) { 56 | if (digitalRead(microSwitchV) == LOW) { // If the micro switch is pressed, move the platfor a little bit up and exit the while loop 57 | moveUp(70); 58 | moveRight(70); 59 | break; 60 | } 61 | // Move the carrier up until the micro switch is pressed 62 | digitalWrite(stepPinVertical, HIGH); 63 | delayMicroseconds(1500); 64 | digitalWrite(stepPinVertical, LOW); 65 | delayMicroseconds(1500); 66 | } 67 | } 68 | 69 | /////////////////////////////////////////////////////////////////////////////////// 70 | void moveUp (int steps) { 71 | digitalWrite(dirPinVertical, LOW); 72 | for (int x = 0; x < steps; x++) { 73 | digitalWrite(stepPinVertical, HIGH); 74 | delayMicroseconds(1000); 75 | digitalWrite(stepPinVertical, LOW); 76 | delayMicroseconds(1000); 77 | } 78 | } 79 | void moveDown (int steps) { 80 | digitalWrite(dirPinVertical, HIGH); 81 | for (int x = 0; x < steps; x++) { 82 | digitalWrite(stepPinVertical, HIGH); 83 | delayMicroseconds(1000); 84 | digitalWrite(stepPinVertical, LOW); 85 | delayMicroseconds(1000); 86 | } 87 | } 88 | void moveLeft (int steps) { 89 | digitalWrite(dirPinHorizontal, HIGH); 90 | for (int x = 0; x < steps; x++) { 91 | digitalWrite(stepPinHorizontal, HIGH); 92 | delayMicroseconds(1000); 93 | digitalWrite(stepPinHorizontal, LOW); 94 | delayMicroseconds(1000); 95 | } 96 | } 97 | void moveRight (int steps) { 98 | digitalWrite(dirPinHorizontal, LOW); 99 | for (int x = 0; x < steps; x++) { 100 | digitalWrite(stepPinHorizontal, HIGH); 101 | delayMicroseconds(1000); 102 | digitalWrite(stepPinHorizontal, LOW); 103 | delayMicroseconds(1000); 104 | } 105 | } 106 | ///////////////////////////////////////////////////////////////////////////////////// 107 | void loop() { 108 | if(mySerial.available()){ 109 | movepoint = mySerial.read(); 110 | Serial.print("The location is "); 111 | Serial.println(movepoint); 112 | if(movepoint==6){ 113 | mySerial.write(res); 114 | } 115 | } 116 | switch(movepoint){ 117 | // Move the container to location 1 118 | case 1: 119 | moveUp(3830); // Move up 4900 steps (Note: the stepper motor is set in Quarter set resolution) 120 | delay(200); 121 | moveRight(820); // Move left 1700 steps 122 | delay(300); 123 | // Rotate the helical coil, discharge the selected item 124 | for (pos =95; pos <=155; pos +=1){ 125 | clutchservo.write(pos); 126 | delay(15); 127 | } 128 | sensorStatus = digitalRead(IRSensor); 129 | while(sensorStatus ==0){ 130 | sensorStatus = digitalRead(IRSensor); 131 | servo.writeMicroseconds(135); // rotate 132 | } 133 | servo.writeMicroseconds(1500); // stop 134 | delay(500); 135 | for (pos =155; pos >=95; pos -=1){ 136 | clutchservo.write(pos); 137 | delay(15); 138 | } 139 | 140 | // Move the container back to starting position 141 | moveLeft(820); 142 | delay(200); 143 | moveDown(3830); 144 | mySerial.write(res); 145 | movepoint =0; 146 | break; 147 | 148 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 149 | 150 | // Move the container to location 2 151 | case 2: 152 | moveUp(40); // Move up 4900 steps (Note: the stepper motor is set in Quarter set resolution) 153 | delay(200); 154 | moveRight(820); // Move left 1700 steps 155 | delay(300); 156 | // Rotate the helical coil, discharge the selected item 157 | for (pos =95; pos <=155; pos +=1){ 158 | clutchservo.write(pos); 159 | delay(15); 160 | } 161 | sensorStatus = digitalRead(IRSensor); 162 | while(sensorStatus ==0){ 163 | sensorStatus = digitalRead(IRSensor); 164 | servo.writeMicroseconds(135); // rotate 165 | } 166 | servo.writeMicroseconds(1500); // stop 167 | delay(500); 168 | for (pos =155; pos >=95; pos -=1){ 169 | clutchservo.write(pos); 170 | delay(15); 171 | } 172 | // Move the container back to starting position 173 | moveLeft(820); 174 | delay(200); 175 | moveDown(40); 176 | mySerial.write(res); 177 | movepoint =0; 178 | break; 179 | 180 | // Move the container to location 3 181 | case 3: 182 | moveUp(3730); // Move up 4900 steps (Note: the stepper motor is set in Quarter set resolution) 183 | delay(200); 184 | moveRight(4070); // Move left 1700 steps 185 | delay(300); 186 | // Rotate the helical coil, discharge the selected item 187 | for (pos =95; pos <=155; pos +=1){ 188 | clutchservo.write(pos); 189 | delay(15); 190 | } 191 | sensorStatus = digitalRead(IRSensor); 192 | while(sensorStatus ==0){ 193 | sensorStatus = digitalRead(IRSensor); 194 | servo.writeMicroseconds(135); // rotate 195 | } 196 | servo.writeMicroseconds(1500); // stop 197 | delay(500); 198 | for (pos =155; pos >=95; pos -=1){ 199 | clutchservo.write(pos); 200 | delay(15); 201 | } 202 | 203 | // Move the container back to starting position 204 | moveLeft(4070); 205 | delay(200); 206 | moveDown(3730); 207 | mySerial.write(res); 208 | movepoint =0; 209 | break; 210 | 211 | 212 | // Move the container to location 4 213 | case 4: 214 | moveUp(40); // Move up 4900 steps (Note: the stepper motor is set in Quarter set resolution) 215 | delay(200); 216 | moveRight(4450); // Move left 1700 steps 217 | delay(300); 218 | // Rotate the helical coil, discharge the selected item 219 | for (pos =95; pos <=155; pos +=1){ 220 | clutchservo.write(pos); 221 | delay(15); 222 | } 223 | sensorStatus = digitalRead(IRSensor); 224 | while(sensorStatus ==0){ 225 | sensorStatus = digitalRead(IRSensor); 226 | servo.writeMicroseconds(135); // rotate 227 | } 228 | servo.writeMicroseconds(1500); // stop 229 | delay(500); 230 | for (pos =155; pos >=95; pos -=1){ 231 | clutchservo.write(pos); 232 | delay(15); 233 | } 234 | 235 | // Move the container back to starting position 236 | moveLeft(4450); 237 | delay(200); 238 | moveDown(40); 239 | mySerial.write(res); 240 | movepoint =0; 241 | break; 242 | 243 | 244 | } 245 | } 246 | 247 | --------------------------------------------------------------------------------