Mundarija:
2025 Muallif: John Day | [email protected]. Oxirgi o'zgartirilgan: 2025-01-13 06:58
Bu qo'llanmada men tuproqdagi namlikni esp32 yordamida o'qish va keyin qiymatlarni thingsio.ai IoT bulutli platformasiga yuborish haqida tushuntiraman.
1 -qadam: QOMPONENTLAR
Uskuna komponentlari:
1. esp32 ishlab chiqish taxtasi
2. Tuproq sensori
3. Jumper simlari
Dasturiy ta'minot:
1. Arduino IDE
2. Thingsio.ai
2 -qadam: Ulanishlar
Tuproq namligi sensori ---------------------- esp32 taxta
VCC -------------------------------------- 3V3
GND -------------------------------------- GND
A0 -------------------------------------- VP
3 -qadam: kodlash:
#qo'shing
#qo'shing
#qo'shing
int soni = 0, i, m, j, k;
int t;
int sensor_pin = A0;
int qiymati;
///////////////////////////////////////// BARCHA BELGILAR
CLOUD uchun ///////////////////////////////
const char* host = "api.thingsai.io"; // YoKI host =
devapi2.thethingscloud.com
const char* post_url = "/devices/deviceData"; // YoKI
/api/v2/thingscloud2/_table/data_ac
const char* time_server =
"baas.thethingscloud.com"; // bu vaqt tamg'asini aylantirish uchun
const int httpPort = 80;
const int httpsPort = 443;
const char* server =
"api.thingsai.io"; // Server manzili
char vaqt belgisi [10];
Wi -Fi ko'p tarmoqli WiFi;
// TCP ulanishlarini yaratish uchun WiFiClient sinfidan foydalaning
WiFiClient mijozi;
///////////////////////////////////////// TIMESTAMP
Hisoblash funktsiyasi //////////////////////////////////////////////////////////////////////////
int GiveMeTimestamp ()
{
imzosiz uzoq
vaqt tugashi = millis ();
// WiFiClient mijozi;
vaqt
(client.available () == 0)
{
agar (millis () -
tanaffus> 50000)
{
client.stop ();
qaytarish 0;
}
}
while (client.available ())
{
String qatori =
client.readStringUntil ('\ r'); // indexOf () -smthngni qidirish funktsiyasi, topilmasa -1 qaytaradi
int pos =
line.indexOf ("\" vaqt tamg'asi / ""); // javob boshidan "\" vaqt tamg'asi / "" ni qidiring va shundan so'ng barcha ma'lumotlarni nusxa ko'chiring, bu sizning vaqt tamg'angiz bo'ladi
agar (pos> =
0)
{
int j = 0;uchun (j = 0; j <10; j ++)
{vaqt belgisi [j] = chiziq [pos + 12 + j];
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
bo'sh o'rnatish ()
{Serial.begin (115200);Serial.println ("Sensordan o'qish …");
kechikish (2000);
// Biz boshlaymiz
WiFi tarmog'iga ulanish
WiFiMulti.addAP ("Wifi_name", "Wifi_Password");
Serial.println ();
Serial.println ();Serial.print ("WiFi kuting …");esa (WiFiMulti.run ()! = WL_CONNECTED) {Serial.print (".");
kechikish (500);
}Serial.println ("");Serial.println ("Wi -Fi ulangan");
Serial.println ( IP
manzil: );Serial.println (WiFi.localIP ());
kechikish (500);
}
bo'sh halqa ()
{
{
///////////////////////////////////////////////////////////////////////////////////////////////////// sahifalar
Javobni qabul qiling ///////////////////////
qiymati =
analogRead (sensor_pin);
// qiymati =
xarita (qiymati, 550, 0, 0, 100);Serial.print ("Mositure:");Serial.print (qiymat);Serial.println ("%");kechikish (1000);Serial.print ("ulanish");Serial.println (mezbon); // teskari aniqlangan:- host = devapi2.thethingscloud.com yoki 139.59.26.117
///////////////////////////////////////////////////// TIMESTAMP KODI SNIPPET
/////////////////////////
Serial.println ("ichida vaqt tamg'asini olish / n");
agar
(! client.connect (time_server, {
qaytish;
//*-*-*-*-*-*-*-*-*-*
}client.println ("GET/api/vaqt tamg'asi HTTP/1.1"); // Bu qism nima qilyapti, men tushunmadimclient.println ("Xost: baas.thethingscloud.com");client.println ("Keshni boshqarish: keshsiz");client.println ("Postman-Token: ea3c18c6-09ba-d049-ccf3-369a22a284b8");
mijoz.println ();
GiveMeTimestamp ();
// u serverdan vaqt tamg'asi javobini oladigan funktsiyani chaqiradi
Serial.println ("vaqt tamg'asi qabul qilindi");
Serial.println (vaqt tamg'asi);Serial.println ("ThingsCloudPost ichida");
String PostValue =
"{" device_id / ": 61121695918, \" slave_id / ": 2";
PostValue =
PostValue + ", \" dts / ":" + vaqt tamg'asi;
PostValue =
PostValue +", \" data / ": {" MOIST / ":" +qiymati +"}" +"}";Serial.println (PostValue);
/ * WiFiClientSecure misolini yaratish */
WiFiClientSecure mijozi;Serial.println ("443 -port orqali serverga ulanish");
agar
(! client.connect (server, 443)) {Serial.println ("Ulanish muvaffaqiyatsiz tugadi!");
} boshqa {Serial.println ("Serverga ulandi!");
/* HTTP yaratish
so'rov */client.println ("POST/devices/deviceData HTTP/1.1");client.println ("Xost: api.thingsai.io");//client.println("Ulanish: yopish ");client.println ("Kontent turi: application/json");
client.println ( keshni boshqarish:
kesh yo'q ");client.println ("Avtorizatsiya: egasi eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9. IjVhMzBkZDFkN2QwYjNhNGQzODkwYzQ4OSI.kaY6OMj5cYlWNqC2PNT")client.print ("Content-Length:");
mijoz.println (PostValue.length ());mijoz.println ();client.println (PostValue);
//////////////////////////////////////////////////////////////////////////// qism qismlarga ma'lumotlarni joylashtirish
bulut tugadi va endi bulutli serverning javobini oling /////////////////
Serial.print ( Javob kutilmoqda
);
vaqt
(! mijoz.available ()) {
kechikish (50);
//Serial.print (".");
}
/* agar ma'lumotlar bo'lsa
mavjud, keyin qabul qiling va Terminalda chop eting */
vaqt
(client.available ()) {
char c = client.read ();Serial.write (c);
}
/* bo'lsa
server uzildi, mijozni to'xtating */
agar
(! client.connected ()) {Serial.println ();Serial.println ("Server uzilgan");
client.stop ();
}
}
Serial.println ( ////////////////////// END
///////////////////// );
kechikish (3000);
}
}
4 -qadam: Chiqish:
Kodning chiqishi ko'rsatiladi.
5 -qadam: GRAPH
Bu sensordan o'qiladigan qiymatlarning grafik ko'rinishi.
6 -qadam:
Loyihaning to'liq videosi. Rahmat.