Smart Garbage Collection Using Node Mcu
Smart Garbage Collection Using Node Mcu
* For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing,
* then waits for the pin to go LOW and stops timing. Returns the length of the pulse in
microseconds.
* LED is used here to give an alarm when the remaining level of tank is less than or equals to 20
cm.
* by Pankaj Kumar,
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(12, 13); // RX, TX
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiClientSecure.h>
int count=0,i,m,j,k;
//int t,t1,t2,t3;
// defines variables
long duration;
float distance;
char timestamp[10];
WiFiClient client;
/////////////////////////////////////////////////////////////////////////////////////////////////////
////
int GiveMeTimestamp()
while (client.available() == 0)
{
if (millis() - timeout > 50000)
client.stop();
return 0;
while (client.available())
if (pos >= 0)
int j = 0;
for(j=0;j<10;j++)
/////////////////////////////////////////////////////////////////////////////////////////////////////
///
void setup()
{
Serial.begin(115200); //(19200,SERIAL_8E1) - data size = 8 bits , parity = Even , stop bit = 1bit
mySerial.begin(115200);
WiFiManager wifiManager;
wifiManager.setAPCallback(configModeCallback);
if(!wifiManager.autoConnect("iPankaj","80518051"))
//wifiManager.autoConnect("AP-NAME", "AP-PASSWORD"); (OR) wifiManager.autoConnect("AP-
NAME"); only ID no password (OR) wifiManager.autoConnect(); this will generate a ID by itself
Serial.println("failed to connect and hit timeout"); //control comes here after long time of
creating Access point "NodeMCU" by NodeMCU and still it has not connected
ESP.reset();
delay(1000);
Serial.println("connected...yeey :");
}
void loop()
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
distance= duration*0.034/2;
Serial.println(distance);
delay(10000);
if(distance<= 20)
digitalWrite(LED, HIGH);
Serial.print("connecting to ");
if (!client.connect(time_server, httpPort))
return; //*-*-*-*-*-*-*-*-*-*
client.println("Host: baas.thethingscloud.com");
client.println("Cache-Control: no-cache");
client.println("Postman-Token: ea3c18c6-09ba-d049-ccf3-369a22a284b8");
client.println();
GiveMeTimestamp(); //it'll call the function which will get the timestamp response
from the server
Serial.println("timestamp receieved");
Serial.println(timestamp);
///////////////////////////////////////////////////////////////////////////////
Serial.println("inside ThingsCloudPost");
Serial.println(PostValue);
WiFiClientSecure client;
if (!client.connect(server, 443)){
Serial.println("Connection failed!");
} else {
Serial.println("Connected to server!");
//client.println("Connection: close");
client.println("Content-Type: application/json");
client.println("cache-control: no-cache");
client.println("Authorization: Bearer
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.IjVhMzBkZDFkN2QwYjNhNGQzODkwYzQ4OSI.kaY6OMj5
cYlWNqC2PNTkXs9PKy6_m9tdW5AG7ajfVlY");
client.print("Content-Length: ");
client.println(PostValue.length());
client.println();
client.println(PostValue);
//////////////////////////////////POSTING the data on to the cloud is done and now get the
response form cloud server//////////////////
while (!client.available()){
delay(50); //
Serial.print(".");
while (client.available()) {
char c = client.read();
Serial.write(c);
if (!client.connected()) {
Serial.println();
Serial.println("Server disconnected");
client.stop();
delay(3000);