0

所以我正在做一个项目,为自行车制作安全和监控系统,并将监控数据发送到运行良好的 Blynk。我试图在触发值时发送短信,我通过 Clicksend 接收短信,但我不知道如何将我的值发送到 IFTTT,以便它可以在警报短信中写入这些代码:

#include <SoftwareSerial.h>
#include <RH_NRF24.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266HTTPClient.h>
char auth[] = "AUTH_ID";
char ssid[] = "SSID";
char pass[] = "PASS";
const char* iftttURL = "http://maker.ifttt.com/trigger/{event}/with/key/{key}";
BlynkTimer timer;

// Singleton instance of the radio driver

RH_NRF24 nrf24(2, 4);                //D4,D2 on esp                       //nrf24L01
int led = 15;                        //D8 on esp
int acc;
int touch;
int headtemp;

static const int RXPin = 5, TXPin = 16;    //D1,D2 on esp       //gps
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
float lati;
float lon;

const int analogInPin = A0;               //Pt100  Bike Temp
const int SensorValueLow = 463; 
const int SensorValueDiff = 36; // differance between high and low sensor value
const int TempValueDiff = 32; // differance between high and low Temp value
const int TempValueLow = 0;
int sensorValue = 0;
int Temp = 0;

// Calibration: //                        //RPM
const byte PulsesPerRevolution = 10;  // Set how many pulses there are on each revolution. Default: 2.
const unsigned long ZeroTimeout = 100000;  // For high response time, a good value would be 100000
// Calibration for smoothing RPM:
const byte numReadings = 2;  // Number of samples for smoothing. The higher, the more smoothing, but it's going to
// Variables:
/////////////
unsigned long kmh;
int d=50.8;                                //diameter of wheel in cm
volatile unsigned long LastTimeWeMeasured;  // Stores the last time we measured a pulse so we can calculate the period.
volatile unsigned long PeriodBetweenPulses = ZeroTimeout+1000;  // Stores the period between pulses in microseconds.
volatile unsigned long PeriodAverage = ZeroTimeout+1000;  // Stores the period between pulses in microseconds in total, if we are taking multiple pulses.
unsigned long FrequencyRaw;  // Calculated frequency, based on the period. This has a lot of extra decimals without the decimal point.
unsigned long FrequencyReal;  // Frequency without decimals.
unsigned long RPM;  // Raw RPM without any processing.
unsigned int PulseCounter = 1;  // Counts the amount of pulse readings we took so we can average multiple pulses before calculating the period.
unsigned long PeriodSum; // Stores the summation of all the periods to do the average.
unsigned long LastTimeCycleMeasure = LastTimeWeMeasured;  // Stores the last time we measure a pulse in that cycle.
unsigned long CurrentMicros = micros();  // Stores the micros in that cycle.
unsigned int AmountOfReadings = 1;
unsigned int ZeroDebouncingExtra;  // Stores the extra value added to the ZeroTimeout to debounce it.
// Variables for smoothing tachometer:
unsigned long readings[numReadings];  // The input.
unsigned long readIndex;  // The index of the current reading.
unsigned long total;  // The running total.
unsigned long average;  // The RPM value after applying the smoothing.


void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V1, average);
  Blynk.virtualWrite(V2, kmh);
  Blynk.virtualWrite(V3, Temp);
}

WidgetMap myMap(V5);
void setup()
{
  Serial.begin(9600);        
   Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
  
  ss.begin(GPSBaud);                          //GPS
  Serial.println(F("DeviceExample.ino"));
  Serial.println(F("A simple demonstration of TinyGPS++ with an attached GPS module"));
  Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  Serial.println(F("by Mikal Hart"));
  Serial.println();

  pinMode(led, OUTPUT); //D8 of node mcu       //nrf24L01
  Serial.begin(9600);
  while (!Serial) 
    ; // wait for serial port to connect. Needed for Leonardo only
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");

 attachInterrupt(digitalPinToInterrupt(0), Pulse_Event, RISING);    //RPM     // Enable interruption pin 2 when going from LOW to HIGH.
}

void loop()
{
    Blynk.run();
    timer.run();
    smsonaccident();
    
    while (ss.available() > 0)              //GPS
    if (gps.encode(ss.read()))
      displayInfo();                        //GPS function
   

  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    while(true);
  }
  
      NRF24L01();                          //NRF24L01 Function

      BikeTemp();                          //Bike Temprature Function
  
      BikeRPM();                           //Bike Wheel RPM
 

  
}

void NRF24L01()
{
  if (nrf24.available())                //nrf24L01
  {
    // Should be a message for us now   
    uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (nrf24.recv(buf, &len))
    {
      Serial.println("*****Got Signal*****");
      acc = buf[0];
      touch = buf[1];
      Serial.print("Accelerometer State: ");
      Serial.print(buf[0]);
      Serial.print("  ,  Touch State:  ");
      Serial.print(buf[1]);
      if(touch == 1 || acc ==1)
      {digitalWrite(led,HIGH);}
      else
      {digitalWrite(led,LOW);}
      
    }
  }
}

void BikeTemp()
{
  sensorValue = analogRead(analogInPin);      //Pt100 BikeTemp
  Temp = sensorValue-SensorValueLow;
  Temp = Temp/SensorValueDiff;
  Temp = Temp*TempValueDiff;
  Temp = Temp+TempValueLow;
  Serial.print("Temp= ");
  Serial.println(Temp);

}

void BikeRPM()
{
   LastTimeCycleMeasure = LastTimeWeMeasured;  // RPM+Km/h
  CurrentMicros = micros();  // Store the micros() in a variable.
  if(CurrentMicros < LastTimeCycleMeasure)
  {
    LastTimeCycleMeasure = CurrentMicros;
  }
  // Calculate the frequency:
  FrequencyRaw = 10000000000 / PeriodAverage;  // Calculate the frequency using the period between pulses.  
  // Detect if pulses stopped or frequency is too low, so we can show 0 Frequency:
  if(PeriodBetweenPulses > ZeroTimeout - ZeroDebouncingExtra || CurrentMicros - LastTimeCycleMeasure > ZeroTimeout - ZeroDebouncingExtra)
  {  // If the pulses are too far apart that we reached the timeout for zero:
    FrequencyRaw = 0;  // Set frequency as 0.
    ZeroDebouncingExtra = 2000;  // Change the threshold a little so it doesn't bounce.
  }
  else
  {
    ZeroDebouncingExtra = 0;  // Reset the threshold to the normal value so it doesn't bounce.
  }
  FrequencyReal = FrequencyRaw / 10000;  // Get frequency without decimals.
  // Calculate the RPM:
  RPM = FrequencyRaw / PulsesPerRevolution * 60;  // Frequency divided by amount of pulses per revolution multiply by
  RPM = RPM / 10000;  // Remove the decimals.
  // Smoothing RPM:
  total = total - readings[readIndex];  // Advance to the next position in the array.
  readings[readIndex] = RPM;  // Takes the value that we are going to smooth.
  total = total + readings[readIndex];  // Add the reading to the total.
  readIndex = readIndex + 1;  // Advance to the next position in the array.
  if (readIndex >= numReadings)  // If we're at the end of the array:
  {
    readIndex = 0;  // Reset array index.
  }
  // Calculate the average:
  average = total / numReadings;  // The average value it's the smoothed result.
  kmh = d*average*0.001885;    // calculate km/h ,where d(in cm) is diameter of wheel
  Serial.print("RPM: ");
  Serial.print(average);
  Serial.print("   ,  KM/h: ");
  Serial.println(kmh);

}

void displayInfo()
{
  Serial.print(F("Location: ")); 
  if (gps.location.isValid())
  {
   // Serial.print(gps.location.lat(), 6);
    lati = gps.location.lat() ;
    Serial.print(lati, 6);
    Serial.print(F(","));
   // Serial.print(gps.location.lng(), 6);
    lon = gps.location.lng() ;
     Serial.print(lon, 6);
  }
  else
  {
    Serial.print(F("Invalid"));
  }
  int index = 5;
  myMap.location(index, lati, lon, "Bike location");

  Serial.print(F("   Date/Time: "));
  if (gps.date.isValid())
  {
    Serial.print(gps.date.day());
    Serial.print(F("/"));
    Serial.print(gps.date.month());
    Serial.print(F("/"));
    Serial.print(gps.date.year());
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F(" "));
  if (gps.time.isValid())
  {
    if (gps.time.hour() < 10) Serial.print(F("0"));
    Serial.print(gps.time.hour());
    Serial.print(F(":"));
    if (gps.time.minute() < 10) Serial.print(F("0"));
    Serial.print(gps.time.minute());
    Serial.print(F(":"));
    if (gps.time.second() < 10) Serial.print(F("0"));
    Serial.print(gps.time.second());
    
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.println();
}

ICACHE_RAM_ATTR void Pulse_Event()  //RPM Data      // The interrupt runs this to calculate the period between pulses:
{
  PeriodBetweenPulses = micros() - LastTimeWeMeasured;  // Current "micros" minus the old "micros" when the last pulse happens.                                                  
  LastTimeWeMeasured = micros();  // Stores the current micros so the next time we have a pulse we would have something to compare with.
  if(PulseCounter >= AmountOfReadings)  // If counter for amount of readings reach the set limit:
  {
    PeriodAverage = PeriodSum / AmountOfReadings;  // Calculate the final period dividing the sum of all readings by the
    PulseCounter = 1;  // Reset the counter to start over. The reset value is 1 because its the minimum setting allowed (1 reading).
    PeriodSum = PeriodBetweenPulses;  // Reset PeriodSum to start a new averaging operation.
    int RemapedAmountOfReadings = map(PeriodBetweenPulses, 40000, 5000, 1, 10);  // Remap the period range to the reading range.
    RemapedAmountOfReadings = constrain(RemapedAmountOfReadings, 1, 10);  // Constrain the value so it doesn't go below or above the limits.
    AmountOfReadings = RemapedAmountOfReadings;  // Set amount of readings as the remaped value.
  }
  else
  {
    PulseCounter++;  // Increase the counter for amount of readings by 1.
    PeriodSum = PeriodSum + PeriodBetweenPulses;  // Add the periods so later we can average.
  }

}

void smsonaccident()
{
  if (acc>=1) // You can write any condition to trigger e-mail sending
  {
    Serial.println("Alert!!! Accident Happens see location. "); // This can be seen in the Serial Monitor
        HTTPClient http;                    // Declare an object of class HTTPClient
        http.begin(iftttURL);               // Specify request destination
        int httpCode = http.GET();          // Send the request
        Serial.println("Done");   
     if (httpCode > 0) {
          String payload = http.getString();   // Get the request response payload
          Serial.println(payload);             // Print the response payload

        }

        http.end();                         // Close connection
        acc=0;
       delay(10000);                        // delay for 5 min if accident happens  
  }    
}

现在我试图找到一种方法,以便我可以将我的 GPS 值发送到“iftttURL”,但是无论我尝试什么或我知道的任何东西都不起作用,要么只是没有收到值,要么如果收到了,那么 SMS 无法发送,因为它不能验证值我需要添加什么或需要更改什么以将我的 GPS 值“lati”和“lon”发送到我的 ifttt url,在那里它可以将其识别为我可以放入警报消息中的值

4

0 回答 0