/*
Complete project details: https://RandomNerdTutorials.com/esp32-https-requests/
const char* ssid = "Wokwi-GUEST";
const char* password = "";
*/
/*
* HTTP over TLS (HTTPS) example sketch
*
* This example demonstrates how to use
* WiFiClientSecure class to access HTTPS API.
* We fetch and display the status of
* esp8266/Arduino project continuous integration
* build.
*
* Created by Ivan Grokhotkov, 2015.
* This example is in public domain.
*
*. Code explained and commented by Sachin Soni
*
* For tutorial of this code, watch out this video
* https://youtu.be/dsmMzS3Qvg0
*
* visit
* http://www.techiesms.com
* for IoT project tutorials.
*
* #techiesms
* explore | learn | share
*
* ref: https://www.youtube.com/watch?v=dsmMzS3Qvg0 thank you
*
*/
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "DHT.h"
DHT dht(15, DHT11);
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// const char* ssid = "MAHAKAL"; // Name of the Host
// const char* password = "sateri456"; // Password of the corresponding Host
//const char* ssid = "WeWorkLabsHackathon"; // Name of the Host
//const char* password = "Hackathon2023!";
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* host = "agihomesensorapi.atharvapawar.repl.co";
// Server from which data is to be fetched
const int httpsPort = 443; // Default port for HTTPS
// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "82 68 CF 6A D5 98 13 41 53 A9 97 D9 25 18 76 87 9B 78 72 1D"; //replit.co
//Edge computing
#define MoisensorPin 14
int MoisensorValue = 0;
int limit = 55;
#define dhtPin 15
#define ch1_Pin 18
#define ch2_Pin 19
//"/sensorvalue?
// a_id=acc1243
//&sec_code=agihome123
//&smv=45
//&ps=425
//&temp=4225
//&hum=4250
//&ap1=425
//const char* sendData[7] = {"acc1243", "agihome123", "45", "1", "37", "89", "0"}; // dummy data initialization
String account_id = "acc1243";
String secret_code= "agihome123";
int pump = 0;
float temp = 0;
float hum= 0;
int appli_2 = 0;
//sendData[0] = accountid
//sendData[1] = secret code
//sendData[2] = soil moisture data
//sendData[3] = Pump Status(bool)
//sendData[4] = Temperature
//sendData[5] = Humidity
//sendData[6] = application 1(bool)
void displayData(){
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
// Display static text
display.setCursor(0, 10);
display.println("Soil Moisture: "+ String(MoisensorValue));
display.setCursor(0, 30);
display.println("Temp: "+ String(temp));
display.setCursor(0, 50);
display.println("Hum: "+ String(hum));
display.display();
}
void sendDATA(){
WiFiClientSecure client; // Use WiFiClientSecure class to create client instance
client.setInsecure();
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
// establishing connection with the server(api.github.com) at port 443
Serial.println("connection failed");
return;
// this line will return the function to the starting of void setup()
}
// String url = "/sensors/get?N=" + String(random(150, 200)) + "&P=" + String(random(190, 220)) + "&K=" + String(random(150, 200)) + "&temperature=" + String(random(30, 40)) + "&humidity=" + String(random(80, 90)) + "&moisture=" + String(random(90, 100)) + "&wind_speed=" + String(random(50, 70)) + "&altitude=" + String(random(10, 40)) + "&light=" + String(random(23, 35)) + "&rain=" + String(random(200, 300)) + "&wind_direction=" + String(random(0, 1)) + "&pump=" + String(random(0, 1)) + "&earth_x=" + String(random(30, 81)) + "&earth_y=" + String(random(30, 81)) + "&earth_z=" + String(random(30, 81));
String url = "/sensorvalue?a_id=acc1243&sec_code=agihome123&smv="+ String(MoisensorValue) + "&ps=" + String(pump) + "&temp=" + String(temp) + "&hum=" + String(hum) + "&ap1=" + String(appli_2);
//address from which we need to get the data inside the server.
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
/*
* GET /repos/esp8266/Arduino/commits/master/status HTTP/1.1
* Host : api.github.com
* User-Agent : BuildFailureDetectorESP8266
* Connection : close
*/
Serial.println("request sent");
while (client.connected()) { // until the client is connected, read out the response
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
}
void setup() {
Serial.begin(115200); //default baud rate for esp32
// Wifi connection code
Serial.println();Serial.print("connecting to ");Serial.println(ssid);
WiFi.begin(ssid, password); // establish connection with mentioned Host
while (WiFi.status() != WL_CONNECTED) {delay(500);Serial.print(".");}
Serial.println("");Serial.println("WiFi connected");Serial.println("IP address: ");
Serial.println(WiFi.localIP()); // Print out the Local IP assigned by the router to ESP8266
// pin declaration
dht.begin();
pinMode(ch1_Pin, OUTPUT);
pinMode(ch2_Pin, OUTPUT);
digitalWrite(ch1_Pin, LOW);
digitalWrite(ch2_Pin, LOW);
// Oled config
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
}
void loop() {
//Read Soil moisture Sensor value
MoisensorValue = analogRead(MoisensorPin); //sprintf(buffer, "Soil Moisture Raw: %d", MoisensorValue);Serial.println(buffer);
MoisensorValue = map(MoisensorValue, 0, 1023, 0, 100); // 0 to 100%
MoisensorValue = (100-MoisensorValue) - 1; // inverse the value
//controll Pump
if (MoisensorValue<limit){digitalWrite(ch1_Pin, LOW); pump = 1; }
else { digitalWrite(ch1_Pin, HIGH); pump = 0; }
temp = dht.readTemperature();
hum = dht.readHumidity();
// application - 2 : Lamp for indoor plant pot
appli_2 = 0;
digitalWrite(ch2_Pin, LOW);
sendDATA();
displayData();
delay(40000); //40sec
}
// String url = "/sensorvalue?a_id=acc1243&sm="+ String(sendData[0]) + "&ps=" + String(sendData[1]) + "&N=" + String(sendData[2]) + "&P=" + String(sendData[3]) + "&K=" + String(sendData[4]) + "&ph=" + String(sendData[5] + "&temp=" + String(sendData[6]) + "&hum=" + String(sendData[7])
// + "&N=" + String(N) + "&P=" + String(P) + "&K=" + String(K);
// S = 0
// P = 1
// N = 100
// P = 35
// K = 498
// p = 8
// T = 30
// H = 76
// -------------Gatway Node-------
//sendData[0] = N1 soil moisture data
//sendData[1] = N1 pump status
//sendData[2] = N
//sendData[3] = P
//sendData[4] = K
//sendData[5] = pH data
//sendData[6] = Temperature
//sendData[7] = Humidity