|
|
| ## uno |
|
|
| #include "DHT.h" |
|
|
| #define DHTPIN 2 |
| #define DHTTYPE DHT11 |
| #define BAUD_RATE 9600 |
|
|
| DHT dht(DHTPIN, DHTTYPE); |
|
|
| void setup() { |
| Serial.begin(BAUD_RATE); |
| dht.begin(); |
| } |
|
|
| void loop() { |
| float val = dht.readHumidity(); |
| float chk = dht.readTemperature(); |
|
|
| if (isnan(h) || isnan(t)) { |
| Serial.println("Failed to read from DHT sensor!"); |
| } else { |
| |
| Serial.print(chk); |
| Serial.print(","); |
| Serial.println(t); |
| } |
|
|
| delay(2000); |
| } |
|
|
|
|
| ### ٍEsp8266 |
|
|
|
|
| #include <ESP8266WiFi.h> |
| #include <WiFiClientSecure.h> |
| #include <ESP8266HTTPClient.h> |
|
|
| const char* ssid = "WIFI_SSID"; |
| const char* password = "WIFI_PASSWORD"; |
|
|
|
|
| String API_URL = "API_URL"; |
| String API_KEY = "API_KEY"; |
| String TableName = "maintable"; |
| const int httpsPort = 443; |
|
|
|
|
|
|
| int sendingInterval = 1200; |
|
|
| HTTPClient https; |
| WiFiClientSecure client; |
|
|
| void setup() { |
| Serial.begin(9600); |
| delay(10); |
|
|
| Serial.println(); |
| Serial.println("Connecting to "); |
| Serial.println(ssid); |
|
|
| WiFi.begin(ssid, password); |
|
|
| while (WiFi.status() != WL_CONNECTED) { |
| delay(500); |
| Serial.print("."); |
| } |
|
|
| Serial.println(""); |
| Serial.println("WiFi connected"); |
| Serial.println("IP address: "); |
| Serial.println(WiFi.localIP()); |
|
|
| client.setInsecure(); |
| } |
| void loop() { |
| if (WiFi.status() == WL_CONNECTED) { |
| if (Serial.available() > 0) { |
| String data = Serial.readStringUntil('\n'); |
| float val, chk; |
|
|
| sscanf(data.c_str(), "%f,%f", &val, &chk); |
| https.begin(client, API_URL + "/rest/v1/" + TableName); |
| https.addHeader("Content-Type", "application/json"); |
| https.addHeader("Prefer", "return=representation"); |
| https.addHeader("apikey", API_KEY); |
| https.addHeader("Authorization", "Bearer " + API_KEY); |
| |
| int httpCode = https.POST("{\"val\":" + String(val) + ",\"chk\":" + String(chk) + "}"); |
| String payload = https.getString(); |
| Serial.println(httpCode); |
| Serial.println(payload); |
| |
| https.end(); |
| } |
| } else { |
| Serial.println("Error in WiFi connection"); |
| } |
| delay(1000 * sendingInterval); |
| } |