Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include <WiFiClient.h>
- const char* ssid = "";
- const char* password = "";
- unsigned long lastPulse = 0;
- unsigned long between;
- unsigned long lastSend = 0;
- float power;
- String str = "";
- unsigned long cmillis;
- String url = "http://*******/powerinput";
- int httpResponseCode;
- struct tpulse {
- float power;
- unsigned long ms;
- };
- tpulse pulses[20];
- uint8_t pulsesinarr = 0;
- void ICACHE_RAM_ATTR pulse() {
- cmillis = millis();
- if (lastPulse == 0) { //Skipping the first reading
- lastPulse = cmillis;
- return;
- }
- between = cmillis - lastPulse;
- if (between > 70) {
- lastPulse = cmillis;
- power = (1000.0 * 60.0 * 60.0 / (between));
- Serial.print(power);
- Serial.print(" ");
- tpulse p;
- p.power = power;
- p.ms = cmillis;
- if (pulsesinarr < 20) {
- pulses[pulsesinarr] = p;
- pulsesinarr++;
- }
- }
- }
- void sendData() {
- Serial.println("senddata");
- if (WiFi.status() == WL_CONNECTED) {
- Serial.print("wifi status: ");
- Serial.println(WiFi.status());
- WiFiClient client;
- HTTPClient http;
- http.begin(client, url);
- http.setTimeout(1500);
- http.addHeader("Content-Type", "application/json");
- http.addHeader("memsize", String(ESP.getFreeHeap()));
- String output = "[";
- for (int i = 0; i < pulsesinarr; i++) {
- output += "[";
- output += String(pulses[i].power);
- output += ", " + String(pulses[i].ms);
- output += "]";
- if (i < (pulsesinarr - 1)) output += ", ";
- }
- output += "]";
- Serial.println(output);
- pulsesinarr = 0;
- lastSend = millis();
- Serial.println(httpResponseCode);
- http.end();
- } else {
- Serial.println(WiFi.status());
- }
- }
- void setup() {
- Serial.begin(115200);
- pinMode(D5, INPUT);
- attachInterrupt(digitalPinToInterrupt(D5), pulse, FALLING);
- // Connect to Wi-Fi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi..");
- }
- // Print ESP Local IP Address
- Serial.println(WiFi.localIP());
- }
- void loop() {
- if (pulsesinarr > 0 && (millis() - lastSend) > 1700) sendData();
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement