Go Down

Topic: MKR1000 (Read 727 times) previous topic - next topic

guju

Hi!
I have a sketch, which runs without issues, when i start it while the PC is connected. I have some serial outputs and everything is fine.
Then i have removed all the serial prints and transfer  the sketch to the arduino in order to run it without the pc.

#include <ThingSpeak.h>
#include <SPI.h>
#include <WiFi101.h>
#include "arduino_secrets.h"
//
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;     // the WiFi radio's status
//
//für ThingSpeak können nur daten alle 15s übertragen werden
#define SpeedTime 1000
#define SendIntervall 15000 //15000 für Thingspeak
#define NumberofPulses 2
#define Max_speed 200 //km/h
//
unsigned long myChannelNumber = 1234;
const char * myWriteAPIKey = "xxxx";
WiFiClient  client;
//*****************Arduino anemometer sketch******************************
const byte interruptPin = 1; //anemomter input to digital pin(3 mega und 1 mkr)
volatile unsigned long sTime = 0; //stores start time for wind speed calculation
unsigned long dataTimer = 0; //used to track how often to communicate data
volatile float pulseTime = 0; //stores time between one anemomter relay closing and the next
volatile float culPulseTime = 0; //stores cumulative pulsetimes for averaging
volatile bool start = true; //tracks when a new anemometer measurement starts
volatile unsigned int avgWindCount = 0; //stores anemometer relay counts for doing average wind speed
volatile unsigned int myInterrCount = 0; //stores anemometer relay counts for doing average wind speed
float aSetting = 70.0; //wind speed setting to signal alarm
//
float HSF = 3.013;
float LSF = 1.761;
float Max = 0;
float Min = -1;
float Average = 0;
int iNumAverage = 0;
unsigned long aTime = 0;
//
void setup() {
  delay(2000);//Delay to let system boot
  pinMode(6, OUTPUT); //setup LED pin to signal high wind alarm condition
  pinMode(interruptPin, INPUT_PULLUP); //set interrupt pin to input pullup
  attachInterrupt(interruptPin, anemometerISR, RISING); //setup interrupt on anemometer input pin, interrupt will occur whenever falling edge is detected
  dataTimer = millis(); //reset loop timer
  aTime = millis();
  // check for the presence of the shield:
  // attempt to connect to WiFi network:
  while ( status != WL_CONNECTED) {
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  // wait 10 seconds for connection:
  delay(10000);
  // you're connected now, so print out the data:
  ThingSpeak.begin(client);
}

void loop() {
  unsigned long rTime = millis();
  if ((rTime - sTime) > 2500) pulseTime = 0; //if the wind speed has dropped below 1MPH than set it to zero   
  if ((rTime - dataTimer) > SpeedTime) { //See if it is time to transmit 
    detachInterrupt(interruptPin); //shut off wind speed measurement interrupt until done communication
    float aWSpeed = getAvgWindSpeed(culPulseTime, avgWindCount); //calculate average wind speed
    if (aWSpeed >= aSetting) {
      digitalWrite(6, HIGH);// high speed wind detected so turn the LED on
    } else {
      digitalWrite(6, LOW);//no alarm so ensure LED is off
    }
    culPulseTime = 0; //reset cumulative pulse counter
    avgWindCount = 0; //reset average wind count
    float aFreq = 0; //set to zero initially
    if (pulseTime > 0.0) aFreq = getAnemometerFreq(pulseTime); //calculate frequency in Hz of anemometer, only if pulsetime is non-zero
    //
    // Min/MAX/AVERAGE bilden
    Average = Average + aWSpeed;
    iNumAverage++;
    if (aWSpeed > Max) {
      Max = aWSpeed;
    }
    if (aWSpeed < Min || Min < 0) {
      Min = aWSpeed;
    }
    start = true; //reset start variable in case we missed wind data while communicating current data out
    attachInterrupt(digitalPinToInterrupt(interruptPin), anemometerISR, RISING); //turn interrupt back on
    dataTimer = millis(); //reset loop timer
  }
  // Daten an Thing speed alle SendIntervall schicken
  if ((rTime - aTime) > SendIntervall) {
    Average = Average / iNumAverage;
    // schicke Daten
    // Daten an Thingspeak
    ThingSpeak.setField(1, Max);
    ThingSpeak.setField(2, Min);
    ThingSpeak.setField(3, Average);
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
    //Reset der Zähler
    Max = 0;
    Min = -1;
    Average = 0;
    iNumAverage = 0;
    aTime = millis();
  }
}

//using time between anemometer pulses calculate frequency of anemometer
float getAnemometerFreq(float pTime) {
  return  1 / ( pTime * NumberofPulses); //*2 wegen der Anzahl der Signale pro Umdrehung
}
float getWindMPH(float freq) {
  return LSF / (1 + freq) + HSF * freq;
}
//Calculates average wind speed over given time period
float getAvgWindSpeed(float cPulse, int per) {
  if (per) return getWindMPH(getAnemometerFreq((float)(cPulse / per)));
  else return 0; //average wind speed is zero and we can't divide by zero
}

//Interrupt
void anemometerISR() {
  unsigned long cTime = millis(); //get current time
  if (!start) { //This is not the first pulse and we are not at 0 MPH so calculate time between pulses
    pulseTime = (float)(cTime - sTime) / 1000;
    if (pulseTime > 0.007) {
      culPulseTime += pulseTime;
      avgWindCount++;  
      sTime = cTime;
      start = false;
    }
  } else {
    sTime = cTime;
    start = false;
  }
}

It does not work? Can you please help!!!
Thanks
Juergen

J-M-L

#1
Jan 14, 2018, 07:13 pm Last Edit: Jan 14, 2018, 07:16 pm by J-M-L
how are things powered ? and (important) make sure grounds are connected between your board and external devices

-------
Please correct your post above and add code tags around your code:
[code] // your code is here [/code].

It should look like this:
Code: [Select]
// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

Go Up