Create micro
Browse files
micro
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
## uno
|
| 3 |
+
|
| 4 |
+
#include "DHT.h"
|
| 5 |
+
|
| 6 |
+
#define DHTPIN 2 // D2 for Arduino
|
| 7 |
+
#define DHTTYPE DHT11
|
| 8 |
+
#define BAUD_RATE 9600
|
| 9 |
+
|
| 10 |
+
DHT dht(DHTPIN, DHTTYPE);
|
| 11 |
+
|
| 12 |
+
void setup() {
|
| 13 |
+
Serial.begin(BAUD_RATE); // Set baud rate for serial communication
|
| 14 |
+
dht.begin();
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
void loop() {
|
| 18 |
+
float val = dht.readHumidity();
|
| 19 |
+
float chk = dht.readTemperature();
|
| 20 |
+
|
| 21 |
+
if (isnan(h) || isnan(t)) {
|
| 22 |
+
Serial.println("Failed to read from DHT sensor!");
|
| 23 |
+
} else {
|
| 24 |
+
// Send data to ESP8266
|
| 25 |
+
Serial.print(chk);
|
| 26 |
+
Serial.print(",");
|
| 27 |
+
Serial.println(t);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
delay(2000); // Wait a few seconds between measurements
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
### ٍEsp8266
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
#include <ESP8266WiFi.h>
|
| 38 |
+
#include <WiFiClientSecure.h>
|
| 39 |
+
#include <ESP8266HTTPClient.h>
|
| 40 |
+
|
| 41 |
+
const char* ssid = "WIFI_SSID";
|
| 42 |
+
const char* password = "WIFI_PASSWORD";
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
String API_URL = "API_URL";
|
| 46 |
+
String API_KEY = "API_KEY";
|
| 47 |
+
String TableName = "maintable";
|
| 48 |
+
const int httpsPort = 443;
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
int sendingInterval = 1200;
|
| 53 |
+
|
| 54 |
+
HTTPClient https;
|
| 55 |
+
WiFiClientSecure client;
|
| 56 |
+
|
| 57 |
+
void setup() {
|
| 58 |
+
Serial.begin(9600);
|
| 59 |
+
delay(10);
|
| 60 |
+
|
| 61 |
+
Serial.println();
|
| 62 |
+
Serial.println("Connecting to ");
|
| 63 |
+
Serial.println(ssid);
|
| 64 |
+
|
| 65 |
+
WiFi.begin(ssid, password);
|
| 66 |
+
|
| 67 |
+
while (WiFi.status() != WL_CONNECTED) {
|
| 68 |
+
delay(500);
|
| 69 |
+
Serial.print(".");
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
Serial.println("");
|
| 73 |
+
Serial.println("WiFi connected");
|
| 74 |
+
Serial.println("IP address: ");
|
| 75 |
+
Serial.println(WiFi.localIP());
|
| 76 |
+
|
| 77 |
+
client.setInsecure();
|
| 78 |
+
}
|
| 79 |
+
void loop() {
|
| 80 |
+
if (WiFi.status() == WL_CONNECTED) {
|
| 81 |
+
if (Serial.available() > 0) {
|
| 82 |
+
String data = Serial.readStringUntil('\n');
|
| 83 |
+
float val, chk;
|
| 84 |
+
|
| 85 |
+
sscanf(data.c_str(), "%f,%f", &val, &chk);
|
| 86 |
+
https.begin(client, API_URL + "/rest/v1/" + TableName);
|
| 87 |
+
https.addHeader("Content-Type", "application/json");
|
| 88 |
+
https.addHeader("Prefer", "return=representation");
|
| 89 |
+
https.addHeader("apikey", API_KEY);
|
| 90 |
+
https.addHeader("Authorization", "Bearer " + API_KEY);
|
| 91 |
+
|
| 92 |
+
int httpCode = https.POST("{\"val\":" + String(val) + ",\"chk\":" + String(chk) + "}");
|
| 93 |
+
String payload = https.getString();
|
| 94 |
+
Serial.println(httpCode);
|
| 95 |
+
Serial.println(payload);
|
| 96 |
+
|
| 97 |
+
https.end();
|
| 98 |
+
}
|
| 99 |
+
} else {
|
| 100 |
+
Serial.println("Error in WiFi connection");
|
| 101 |
+
}
|
| 102 |
+
delay(1000 * sendingInterval);
|
| 103 |
+
}
|