Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "check_id.h"
- #include "DHT.h"
- #define DHTTYPE DHT21
- #define DHTPIN 33
- DHT dht(DHTPIN, DHTTYPE);
- #include <SPI.h>
- #include <Ethernet.h>
- #define _pin_cs 5
- #define _pin_rst 15
- #define _pin_sensor 34
- #define _pin_limit 22
- #include <HTTPClient.h>
- byte _mac[] = {0xDE, 0x19, 0xCC, 0xEF, 0xFE, 0xED};
- byte _ip[] = {192, 168, 1, 20};
- byte _dns[] = {8, 8, 8, 8};
- EthernetClient client;
- const char server[] = "mainapi.sbumedan.co.id";
- //const char server[] = "google.com";
- const char resource[] = "/api/sensi";
- const int port = 80;
- bool dt_sensor = 0;
- bool dt_limit = 0;
- float hum, temp;
- unsigned long cur_time, old_time;
- unsigned long cur_time_sensor, old_time_sensor;
- unsigned long update_time = 1500;
- bool hold_dt_sensor = false;
- bool hold_dt_limit = false;
- String id;
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- delay(1000);
- id = check_id();
- pinMode(_pin_rst, OUTPUT);
- pinMode(_pin_sensor, INPUT);
- pinMode(_pin_limit, INPUT);
- digitalWrite(_pin_rst, HIGH);
- delay(2000);
- dht.begin();
- Ethernet.init(_pin_cs);
- Ethernet.begin(_mac, _ip, _dns);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- cur_time_sensor = millis();
- if (cur_time_sensor - old_time_sensor >= 1000) {
- baca_sensor();
- if(dt_sensor == true){
- if(hold_dt_sensor == false){
- //push_server();
- hold_dt_sensor = true;
- }
- }else{
- hold_dt_sensor = false;
- }
- //------------------------------------
- if(dt_limit == true){
- if(hold_dt_limit == false){
- push_server();
- hold_dt_limit = true;
- }
- }else{
- hold_dt_limit = false;
- }
- //------------------------------------
- old_time_sensor = cur_time_sensor;
- }
- cur_time = millis();
- if (cur_time - old_time >= update_time) {
- push_server();
- old_time = cur_time;
- update_time = 5*60*1000; //update setiap 5 menit;
- }
- delay(2000);
- }
- void baca_sensor() {
- dt_sensor = digitalRead(_pin_sensor);
- dt_limit = digitalRead(_pin_limit);
- hum = dht.readHumidity();
- temp = dht.readTemperature();
- Serial.print("suhu: ");
- Serial.println(temp);
- // /Serial.print("data Sensor: ");
- // /Serial.println(dt_sensor);
- // /Serial.print("data limit: ");
- // /Serial.println(dt_sensor);
- }
- void push_server() {
- if (init_eth(_mac, _ip, _dns, _pin_cs )) {
- Serial.println("push to server..");
- if (client.connect(server, port)) {
- Serial.println("connected to server");
- String httpRequestData = "pln=" + String(dt_sensor) + "&suhu =" + String(temp,2)+
- "&limit=" +String(dt_limit)+"&lokasi=" + id;
- Serial.print("dt_http_post: ");
- Serial.println(httpRequestData);
- client.print(String("POST ") + resource + " HTTP/1.1\r\n");
- client.print(String("Host: ") + server + "\r\n");
- client.println("Connection: close");
- client.println("Content-Type: application/x-www-form-urlencoded");
- client.print("Content-Length: ");
- client.println(httpRequestData.length());
- client.println();
- client.println(httpRequestData);
- while (client.connected()) {
- if (client.available()) {
- Serial.write(client.read());
- }
- }
- } else {
- Serial.println("disconnected to server");
- }
- }
- }
- bool init_eth(uint8_t mac[5], uint8_t ip[3], uint8_t dns[3], uint8_t pin_cs) {
- bool sts_eth;
- Ethernet.init(pin_cs);
- while (Ethernet.hardwareStatus() == EthernetNoHardware) {
- Serial.println(F("Ethernet shield was not found."));
- delay(1000);
- ESP.restart();
- }
- if (Ethernet.hardwareStatus() == EthernetW5500) {
- Serial.println(F("W5500 Ethernet controller detected."));
- }
- if (Ethernet.linkStatus() == LinkON) {
- Serial.println(F("Link status: On"));
- sts_eth = true;
- }
- if (Ethernet.linkStatus() == LinkOFF) {
- Serial.println(F("Link status: Off"));
- sts_eth = false;
- }
- Serial.print(" DHCP assigned IP ");
- Serial.println(Ethernet.localIP());
- return sts_eth;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement