#include <SoftwareSerial.h>
SoftwareSerial mySerial(12, 13); // RX, TX
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include "WiFiManager.h" //https://github.com/tzapu/WiFiManager
int count=0,i,m,j,k;
int t,t1,t2,t3;
int outputpin= A0;//ds18b20 int onoff=0;
//float tempc; //variable to store temperature in degree Celsius
//float tempf; //variable to store temperature in Fahreinheit //float vout;//temporary variable to hold sensor reading
#define switch1 D6 // your relay switch
//////////////////////////////////////// ALL DECLARATIONS for CLOUD //////////////////////////////
const char* host = "api.thingsio.ai"; // OR host = devapi2.thethingscloud.com
const char* post_url = "/devices/deviceData"; // OR /api/v2/thingscloud2/_table/data_ac
const char* time_server = "baas.thethingscloud.com"; //this is to convert timestamp
const int httpPort = 80;
char timestamp[10];
WiFiClient client;
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void configModeCallback (WiFiManager *myWiFiManager) { Serial.println("Entered config mode"); //*-*-*-*-*-*-*-*-*-*-*-*-*-*if control enters this function then net is not connected Serial.println(WiFi.softAPIP()); // "WiFi.softAPIP() is for AP" , "WiFi.localIP() is for STA", Serial.println(myWiFiManager->getConfigPortalSSID()); //if you used auto generated SSID, print it
}
/////////////////////////////////////// TIMESTAMP CALCULATION function///////////////////////////////////////
int GiveMeTimestamp()
{ unsigned long timeout = millis();
while (client.available() == 0) { if (millis() - timeout > 50000) { client.stop(); return 0; } }
while (client.available()) { String line = client.readStringUntil('\r'); //indexOf() is a funtion to search for smthng , it returns -1 if not found int pos = line.indexOf("\"timestamp\""); //search for "\"timestamp\"" from beginning of response got and copy all data after that , it'll be your timestamp if (pos >= 0) { int j = 0; for(j=0;j<10;j++) { timestamp[j] = line[pos + 12 + j]; } } }
} ////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() { pinMode(switch1,OUTPUT); 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("DDIK Makadia","kidd123456789")) //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 //reset and try again, or maybe put it to deep sleep ESP.reset(); delay(1000); } //if you come here you have connected to the WiFi Serial.println("connected..."); }
void loop() { int sensorValue = analogRead(outputpin); { /////////////////////////////////////// SEND THE QUERY AND RECEIVE THE RESPONSE/////////////////////// /* t1=(analogValue/1024.0) *5000; Serial.print("temp: "); Serial.println(t1);
t2=(t1/10); Serial.print("celc: "); Serial.println(t2); if(t2<30) //set your temp { digitalWrite(switch,LOW); onoff=0; Serial.println("OFF"); } else if(t2>30) // set your temp { digitalWrite(switch,HIGH); onoff=1; Serial.println("ON"); } */ if (sensorValue <= 50) //as per your wish { Serial.println("LED 1 ON"); Serial.println(sensorValue); digitalWrite(switch1, HIGH); onoff=1; delay(1000); } if (sensorValue > 50) { Serial.println("LED 1 OFF"); Serial.println(sensorValue); digitalWrite(switch1, LOW); onoff=0; delay(1000); } Serial.print("connecting to "); Serial.println(host); //defined upside :- host = devapi2.thethingscloud.com or 139.59.26.117
///////////////////////////////////// TIMESTAMP CODE SNIPPET /////////////////////////...
Read more »