|
|
Node MCU master
|
|
|
Reading data from gas-sensor and ultrasonic-sensor-i2c-arduino-slave.
|
|
|
Posting data to firebase.
|
|
|
```
|
|
|
#include <Arduino.h>
|
|
|
#include <Wire.h> // <-- remove spaces
|
|
|
#include <DHT.h>
|
|
|
#include <ESP8266WiFi.h>
|
|
|
#include <ESP8266WiFiMulti.h>
|
|
|
#include <ESP8266HTTPClient.h>
|
|
|
#include <WiFiClientSecureBearSSL.h>
|
|
|
// Fingerprint for demo URL, expires on June 2, 2019, needs to be updated well before this date
|
|
|
//const uint8_t fingerprint[20] = {0x5A, 0xCF, 0xFE, 0xF0, 0xF1, 0xA6, 0xF4, 0x5F, 0xD2, 0x11, 0x11, 0xC6, 0x1D, 0x2F, 0x0E, 0xBC, 0x39, 0x8D, 0x50, 0xE0};
|
|
|
|
|
|
//https://circuits4you.com/2019/02/08/esp8266-nodemcu-https-secured-post-request/
|
|
|
const char fingerprint[] PROGMEM = "34 44 5B 8A 19 2C CD EA F5 3E 3C DC B0 EB D8 7F 7E A9 DE B0";
|
|
|
|
|
|
ESP8266WiFiMulti WiFiMulti;
|
|
|
DHT dht;
|
|
|
char count;
|
|
|
|
|
|
void setup() {
|
|
|
// this constant won't change. It's the pin number of the sensor's output:
|
|
|
dht.setup(D0); // data pin D0 for humidity
|
|
|
Serial.begin(9600);
|
|
|
Wire.begin(D1, D2); /* join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
|
|
|
|
|
|
delay(500);
|
|
|
// Serial.setDebugOutput(true);
|
|
|
Serial.println();
|
|
|
Serial.println();
|
|
|
Serial.println();
|
|
|
|
|
|
for (uint8_t t = 4; t > 0; t--) {
|
|
|
Serial.printf("[SETUP] WAIT %d...\n", t);
|
|
|
Serial.flush();
|
|
|
delay(1000);
|
|
|
}
|
|
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
//WiFiMulti.addAP("SeeknFind", "nill1124");
|
|
|
WiFiMulti.addAP("Workshop", "VIGYAN@0023");
|
|
|
Serial.println();
|
|
|
Serial.print("MAC: ");
|
|
|
Serial.println(WiFi.macAddress());
|
|
|
}
|
|
|
|
|
|
void loop() {
|
|
|
// wait for WiFi connection
|
|
|
if ((WiFiMulti.run() == WL_CONNECTED)) {
|
|
|
|
|
|
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
|
|
|
|
|
|
client->setFingerprint(fingerprint);
|
|
|
//client->setInsecure(); //https://buger.dread.cz/simple-esp8266-https-client-without-verification-of-certificate-fingerprint.html
|
|
|
|
|
|
HTTPClient https;
|
|
|
// get sensor data
|
|
|
String gas = String(analogRead(A0));
|
|
|
String hum = String(100L * dht.getHumidity() / 1024L);
|
|
|
String temp = String(100L * dht.getTemperature() / 1024L);
|
|
|
// read count
|
|
|
String s;
|
|
|
char c;
|
|
|
Wire.requestFrom(8, 4); /* request & read data of size 4 from slave */
|
|
|
while (Wire.available()) {
|
|
|
c = Wire.read();
|
|
|
s += c;
|
|
|
//Serial.print(c);
|
|
|
}
|
|
|
Serial.print("Count:");
|
|
|
Serial.print(s);
|
|
|
Serial.println();
|
|
|
delay(100);
|
|
|
|
|
|
Wire.beginTransmission(8); /* begin with device address 8 */
|
|
|
Wire.write("0"); /*count reset to 0*/
|
|
|
Wire.endTransmission(); /* stop transmitting */
|
|
|
|
|
|
s.trim();
|
|
|
String count = s;
|
|
|
|
|
|
Serial.print("[HTTPS] begin...\n");
|
|
|
String url = "https://manojsahukar.xyz/api/data?gas=" + gas + "&hum=" + hum + "&temp=" + temp + "&count=" + count;
|
|
|
if (https.begin(*client, url)) { // HTTPS
|
|
|
|
|
|
Serial.print("[HTTPS] GET...\n");
|
|
|
Serial.println(url);
|
|
|
// start connection and send HTTP header
|
|
|
int httpCode = https.GET();
|
|
|
|
|
|
// httpCode will be negative on error
|
|
|
if (httpCode > 0) {
|
|
|
// HTTP header has been send and Server response header has been handled
|
|
|
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
|
|
|
|
|
|
// file found at server
|
|
|
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
|
|
|
String payload = https.getString();
|
|
|
Serial.println(payload);
|
|
|
}
|
|
|
} else {
|
|
|
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
|
|
|
}
|
|
|
|
|
|
https.end();
|
|
|
} else {
|
|
|
Serial.printf("[HTTPS] Unable to connect\n");
|
|
|
}
|
|
|
}
|
|
|
Serial.println("Wait 5s before next round...");
|
|
|
delay(5000);
|
|
|
}
|
|
|
``` |
|
|
\ No newline at end of file |