String region_code = "ap-in-1"; // Replace "your_region" with the actual region || For india "ap-in-1"
String connection_key = "<-connection-key->"; // fill your connection key; get it from the node description
String device_id = "<-device-id->"; //Fill your 128-bit UUID of the physical device..
String secret_key="<-secret_key->"; // fill your secret key; get it from the node description
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <TimeLib.h>
// Your Wifi credentials
const char* ssid = "";
const char* password = "";
void setup() {
Serial.begin(115200);
delay(4000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to the WiFi network");
anedya_begin();
}
void loop() {
// -------------- Your Working Area -----------------------
int moistureValue = analogRead(analogPin);
data_append("soil-moisture", moistureValue);
// ---------------------------------------------------------
delay(5000);
}
// ----------------------- End -------------------------------
//------------------------ Functions Section -----------------
//Necessary configuration
void anedya_begin() {
String time_url = "https://device." + region_code + ".anedya.io/v1/time";
String connection_url= "https://device." + region_code + ".anedya.io/v1/check";
// <-------------------------Code to sync time --->
int check=1;
while(check){
// Get the device send time
uint64_t deviceSendTime = millis();
// Prepare the request payload
StaticJsonDocument<200> requestPayload;
requestPayload["deviceSendTime"] = deviceSendTime;
String jsonPayload;
serializeJson(requestPayload, jsonPayload);
HTTPClient http;
http.begin(time_url);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(jsonPayload);
// Check if request was successful
if (httpResponseCode != 200) {
Serial.println("Failed to fetch server time!");
delay(2000);
}else if(httpResponseCode == 200){
check=0;
}
// Parse the JSON response
DynamicJsonDocument jsonResponse(200);
deserializeJson(jsonResponse, http.getString());
int64_t serverReceiveTime = jsonResponse["serverReceiveTime"];
int64_t serverSendTime = jsonResponse["serverSendTime"];
// Compute the current time
int64_t deviceRecTime = millis();
int64_t currentTime = (serverReceiveTime + serverSendTime + deviceRecTime - deviceSendTime) / 2;
//Serial.println(currentTime);
int64_t currentTimeSeconds = currentTime / 1000;
// Set device time
setTime(currentTimeSeconds);
}
// <------Code to check the connection-->
HTTPClient http;
http.begin(connection_url);
http.addHeader("Auth-mode", "key");
http.addHeader("Authorization", connection_key);
int httpResponseCode = http.GET();
// Check if request was successful
if (httpResponseCode != 200) {
Serial.println("Device is not authorized");
Serial.println("Authorizing the device....");
String binding_url= "https://device." + region_code + ".anedya.io/v1/check";
String binjsonPayload="{\"deviceid\":\"" + device_id + "\",\"bindingsecret\": \"" + secret_key + "\"}";
HTTPClient http;
http.begin(binding_url);
http.addHeader("Auth-mode", "key");
http.addHeader("Authorization", connection_key);
int httpBinResponseCode = http.POST(binjsonPayload);
if(httpBinResponseCode != 200){
Serial.println(("Unable to bind!"));
String response = http.getString();
Serial.println(response);
}
else{
Serial.println("Binded successfully");
}
}else if(httpResponseCode == 200){
Serial.println("Device is authorized");
}
}
// Function to send the data
void data_append(String datapoint, float sensor_data) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String senddata_url = "https://device." + region_code + ".anedya.io/v1/submitData";
// Get time
uint64_t current_time = now();
uint64_t current_time_milli = current_time *1000;
// Request to send data
http.begin(senddata_url);
http.addHeader("Content-Type", "application/json");
http.addHeader("Accept", "application/json");
http.addHeader("Auth-mode", "key");
http.addHeader("Authorization", connection_key);
String jsonStr = "{\"data\":[{\"variable\": \"" + datapoint + "\",\"value\":" + String(sensor_data) + ",\"timestamp\":" + String(current_time_milli) + "}]}";
Serial.println(jsonStr);
int httpResponseCode = http.POST(jsonStr);
if (httpResponseCode > 0) {
String response = http.getString();
// Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Error in WiFi connection");
}
}