Spark fails to upload. Cyan led

my spark fails to upload. led is throbbing cyan. Have done a factory reset, didnt help.
It just says taking al little longer than expected.

Throbbing cyan means that the core is unable to connect to the :spark: cloud.

Please check your firewall or internet connection :smiley:

It connects as tinker works fine. Just can upload new code.

Hi @kiwibird1,

It sounds like you’re having trouble performing an over the air firmware update to your core from the build IDE, is that right? What kind of WiFi network are you connected over (work / home?) / What kind of latency do you have on your WiFi? Does your core disconnect / reset, or does it start to flash purple and then stop? What other steps have you tried besides a factory reset?

Thanks!
David

My wfi is solid. The core never flashes purple anymore. (it used to work)
havent done any more steps, as Im not sure what to do. Tinker still works fine on it.

Hi @kiwibird1,

Hmm, is there anything else that might have changed between when it was working for you before and now? The over the air firmware update is impacted by very high latency, so if you have a ping time to the US of over 6 seconds that could be an issue. Can you make sure your code is compiling without errors on the build IDE, and could you send us a video of the lights on your core when you do an OTA flash?

Thanks!
David

Has the OTA flashing issue been definitively fixed or is this another example of its rather unpredictable success rate?

I am now on 0.31 firmware. My code doesn’t flash unless i factory reset it first.
I think the problem is in my code. Wifi signal should be strong.
blink led is fine

This code converts on 50 hz sine wave with dc offset into rms voltage.

A separate issue but if
If analogue input4 is hard coded to 512, I get 5.24 on the ouput when I should get zero.

//in ana4 input hardcoded to 512, we get 5.24 as first output, then zero
#include "math.h"
//#include <EEPROM.h>
//DFRobot.com
//Compatible with the Arduino IDE 1.0
//Library version:1.1
//#include <Wire.h> 
#include "LiquidCrystal_I2C.h"

LiquidCrystal_I2C lcd(0x27,16,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
int cntr=0;
int temperatureraw = 0;        
double voltage=0;        
double temperature=0;
double oldtmp=0;

double tmp=-1;
double ratioVolts=0;

double temperatureMax=0;
double temperatureMin=999;    
String str;


//Set Voltage and current input pins
int inPinV;
double VCAL = 234.26;
double PHASECAL = 1.7;


// Variables for RMS
int startV;                                       //Instantaneous voltage at start of sample window.
int startvIsZero;

int lastSampleV,sampleV;   //sample_ holds the raw analog read value, lastSample_ holds the last mple
int lastSampleI,sampleI; 

double lastFilteredV,filteredV;                   //Filtered_ is the raw analog value minus the DC fset
double lastFilteredI, filteredI; 

double phaseShiftedV;                             //Holds the calibrated phase shifted voltage.

double sqV,sumV,sqI,sumI,instP,sumP;              //sq = squared, sum = Sum, inst = instantaneous

boolean lastVCross, checkVCross;                  //Used to measure number of times threshold is ossed.
int crossCount;                                   // ''

//Useful value variables
double realPower,
       apparentPower,
       powerFactor,
       Vrms,
       Irms;

// to enable 12-bit ADC resolution on Arduino Due, 
// include the following line in main sketch inside setup() function:
  //analogReadResolution(ADC_BITS);
// otherwise will default to 10 bits, as in regular Arduino-based boards.
#if defined(__arm__)
#define ADC_BITS    12
#else
#define ADC_BITS    10
#endif

#define ADC_COUNTS  (1<<ADC_BITS)


//--------------------------------------------------------------------------------------
// emon_calc procedure
// Calculates realPower,apparentPower,powerFactor,Vrms,Irms,kwh increment
// From a sample window of the mains AC voltage and current.
// The Sample window length is defined by the number of half wavelengths or crossings we choose to asure.
//--------------------------------------------------------------------------------------
void calcVI(int crossings, int timeout)
{
  int SUPPLYVOLTAGE = 3300;


  int crossCount = 0;                             //Used to measure number of times threshold is ossed.
  int numberOfSamples = 0;                        //This is now incremented  

  //-----------------------------------------------------------------------------------------------------------------------
  // 1) Waits for the waveform to be close to 'zero' (500 adc) part in sin curve.
  //-----------------------------------------------------------------------------------------------------------------------
  boolean st=false;                                  //an indicator to exit the while loop

  unsigned long start = millis();    //millis()-start makes sure it doesnt get stuck in the loop if ere is an error.

  while(st==false)                                   //the while loop...
  {
     startV = analogRead(inPinV);                    //using the voltage waveform
     //startV=512;
     if ((startV < (ADC_COUNTS/2+50)) && (startV > (ADC_COUNTS/2-50))) st=true;  //check its within nge
     if ((millis()-start)>timeout) st = true;
  }

  //startvIsZero=startV;//its alwasy 512 on exit
  //-----------------------------------------------------------------------------------------------------------------------
  // 2) Main measurment loop
  //----------------------------------------------------------------------------------------------------------------------- 
  start = millis(); 

  while ((crossCount < crossings) && ((millis()-start)<timeout)) 
  {
    numberOfSamples++;                            //Count number of times looped.

    lastSampleV=sampleV;                          //Used for digital high pass filter
    //lastSampleI=sampleI;                          //Used for digital high pass filter

    lastFilteredV = filteredV;                    //Used for offset removal
    //lastFilteredI = filteredI;                    //Used for offset removal   

    //-----------------------------------------------------------------------------
    // A) Read in raw voltage and current samples
    //-----------------------------------------------------------------------------
    sampleV = analogRead(inPinV);                 //Read in raw voltage signal
       // sampleV=512;
    //sampleI = analogRead(inPinI);                 //Read in raw current signal

    //-----------------------------------------------------------------------------
    // B) Apply digital high pass filters to remove 2.5V DC offset (centered on 0V).
    //-----------------------------------------------------------------------------
    filteredV = 0.996*(lastFilteredV+(sampleV-lastSampleV));

    //filteredI = 0.996*(lastFilteredI+(sampleI-lastSampleI));

    //-----------------------------------------------------------------------------
    // C) Root-mean-square method voltage
    //-----------------------------------------------------------------------------  
    sqV= filteredV * filteredV;                 //1) square voltage values
    sumV += sqV;                                //2) sum

    //-----------------------------------------------------------------------------
    // D) Root-mean-square method current
    //-----------------------------------------------------------------------------   
    //sqI = filteredI * filteredI;                //1) square current values
    //sumI += sqI;                                //2) sum 

    //-----------------------------------------------------------------------------
    // E) Phase calibration
    //-----------------------------------------------------------------------------
    phaseShiftedV = lastFilteredV + PHASECAL * (filteredV - lastFilteredV); 

    //-----------------------------------------------------------------------------
    // F) Instantaneous power calc
    //-----------------------------------------------------------------------------   
    //instP = phaseShiftedV * filteredI;          //Instantaneous Power
    //sumP +=instP;                               //Sum  

    //-----------------------------------------------------------------------------
    // G) Find the number of times the voltage has crossed the initial voltage
    //    - every 2 crosses we will have sampled 1 wavelength 
    //    - so this method allows us to sample an integer number of half wavelengths which increases curacy
    //-----------------------------------------------------------------------------       
    lastVCross = checkVCross;                     
    if (sampleV > startV) checkVCross = true; 
                     else checkVCross = false;
    if (numberOfSamples==1) lastVCross = checkVCross;                  

    if (lastVCross != checkVCross) crossCount++;
  }

  //-----------------------------------------------------------------------------------------------------------------------
  // 3) Post loop calculations
  //----------------------------------------------------------------------------------------------------------------------- 
  //Calculation of the root of the mean of the voltage and current squared (rms)
  //Calibration coeficients applied. 

  double V_RATIO = VCAL *((SUPPLYVOLTAGE/1000.0) / (ADC_COUNTS));
  Vrms = V_RATIO * sqrt(sumV / numberOfSamples); 

  //double I_RATIO = ICAL *((SUPPLYVOLTAGE/1000.0) / (ADC_COUNTS));
  //Irms = I_RATIO * sqrt(sumI / numberOfSamples); 

  //Calculation power values
  //realPower = V_RATIO * I_RATIO * sumP / numberOfSamples;
  //apparentPower = Vrms * Irms;
  //powerFactor=realPower / apparentPower;

  //Reset accumulators
  sumV = 0;
  //sumI = 0;
  //sumP = 0;
//--------------------------------------------------------------------------------------       
}


void setup()
{
    //analogReadResolution(ADC_BITS);
    Serial.begin(9600);

    pinMode(A7, INPUT); //temp1
    pinMode(A4, INPUT);//voltage (not conenct right now. 3.3 volts = 330 volts)

    pinMode(A3, OUTPUT);
pinMode(A2, OUTPUT);



    inPinV = A4;

    lcd.init();                      // initialize the lcd 

    // Print a message to the LCD.
    lcd.backlight();
    lcd.print("Hello, Peter!A");
    temperatureraw = analogRead(A7);    
    voltage = ( (double)temperatureraw * 3.3)/4095;    
tmp    =(voltage-0.5)*100;
temperature=tmp;
oldtmp=temperature;


}

void loop()
{
    //sleep(20);


    // Keep reading the temperature so when we make an API    
    // call to read its value, we have the latest one    
    //0.5 volts = 0 degress celcious    
    temperatureraw = analogRead(A7);    
    voltage = ( (double)temperatureraw * 3.3)/4095;    


tmp    =(voltage-0.5)*100;
//if (tmp-5>oldtmp || tmp+5 <oldtmp)
{
  //  tmp=oldtmp;//discard
}
//oldtmp=tmp;

//}

//if (temperature=-99)

//{
//temperature=tmp;
//oldtmp=tmp;
//}

    temperature      =((temperature*59)+tmp)/60;

    if (temperature>temperatureMax)
    {
        temperatureMax=temperature;
    }

    if (temperature<temperatureMin)
    {
        temperatureMin=temperature;
    }

    //temperature  =(voltage);    
    //temperature  =analogRead(A7);    
    //Serial.println("temperature:tp1");    
    //Serial.print("temperature:");        
    //Serial.println(temperature);        


    //using namespace std::literals::string_literals;

  //  str += "Hello World, ";
 //   str += "nice to see you, ";
 //   str += "or not";
    //str += "Hello World, "_s + std::to_string(my_int) + other_string;


    //str += "Hello World, " "nice to see you, " "or not";
    //calcVI(20,2000);         // Calculate all. No.of half wavelengths :crossings,timeout
    //delay(1000);
    cntr+=1;
//    value = EEPROM.read(0);
    //if (value>cntr)
    {
     //cntr=value;  
    }

     //EEPROM.write(0, cntr);
      //  lcd.clear();
   lcd.setCursor(0,0);
    lcd.print("TeMp:");
    lcd.print(temperature);
    lcd.setCursor(0,1);
    lcd.print("ver 1.03c");
    //lcd.print("MaX:");
    //lcd.print(temperatureMax);
    //lcd.print(" Min:");
    //lcd.print(temperatureMin);

    //lcd.print("Temp:"+temperature);
    //lcd.print("line2");
    //lcd.print("Hello, world!");

    //digitalwrite(A3,LOW); fails
    //digitalwrite(A2,LOW);
    digitalWrite(A3,LOW);//work
      digitalWrite(A2,LOW);
    //delay(2000);
    //***delay(2000);
  //*  delay(2000);

    //   lcd.print("V:");
           //calcVI(20,2000);         // Calculate all. No.of half wavelengths :crossings,timeout
    //lcd.setCursor(0,1);   
    //lcd.print(  "startvIsZero " );   
    //lcd.print(  startvIsZero );   
    ratioVolts=242.6/271.07*241.4/235;

    lcd.setCursor(0,2);
    lcd.print("V.");
    calcVI(20,2000);         // Calculate all. No.of half wavelengths :crossings,timeout
    lcd.print(Vrms*ratioVolts);   
    delay(1000);

    lcd.setCursor(0,2);
    lcd.print("V:");
    calcVI(20,2000);         // Calculate all. No.of half wavelengths :crossings,timeout
    lcd.print(Vrms*ratioVolts);   
    delay(1000);

    lcd.setCursor(0,2);
    lcd.print("V!");
    calcVI(20,2000);         // Calculate all. No.of half wavelengths :crossings,timeout
  lcd.print(Vrms*ratioVolts);   
  delay(1000);

    lcd.setCursor(0,2);
    lcd.print("V#");
    calcVI(20,2000);         // Calculate all. No.of half wavelengths :crossings,timeout
   lcd.print(Vrms*ratioVolts);   
   delay(1000);









    digitalWrite(A3,HIGH);//work
      digitalWrite(A2,LOW);

    lcd.setCursor(0,3);
    lcd.print("  A.");
    calcVI(40,4000);         // Calculate all. No.of half wavelengths :crossings,timeout
    lcd.print(Vrms*240/232.6);   
    lcd.print("   ");
    lcd.setCursor(0,3);
    lcd.print("  A:");
    calcVI(40,4000);         // Calculate all. No.of half wavelengths :crossings,timeout
    lcd.print(Vrms*240/232.6);   
    lcd.print("   ");
    lcd.setCursor(0,3);
    lcd.print("  A!");
    calcVI(40,4000);         // Calculate all. No.of half wavelengths :crossings,timeout
    lcd.print(Vrms*240/232.6);   
    lcd.print("   ");
    lcd.setCursor(0,3);
    lcd.print("  A#");
    calcVI(40,4000);         // Calculate all. No.of half wavelengths :crossings,timeout
    lcd.print(Vrms*240/232.6);   
    lcd.print("   ");
    lcd.setCursor(0,3);
    lcd.print(" #A#");
    calcVI(40,4000);         // Calculate all. No.of half wavelengths :crossings,timeout
    lcd.print(Vrms*240/232.6);   
    lcd.print("   ");




    //digitalwrite(A3,HIGH);//fails
    //digitalWrite(A2,LOW);

      //delay(2000);
      //delay(2000);
    //digitalwrite(a2,LOW);

    //calcVI(40,4000);         // Calculate all. No.of half wavelengths :crossings,timeout
    //calcVI(40,4000);         // Calculate all. No.of half wavelengths :crossings,timeout
  // * calcVI(40,4000);         // Calculate all. No.of half wavelengths :crossings,timeout

    //calcVI(40,4000);         // Calculate all. No.of half wavelengths :crossings,timeout
    //calcVI(40,4000);         // Calculate all. No.of half wavelengths :crossings,timeout


     lcd.setCursor(0,3);
    lcd.print(cntr);
  // delay(2000);
}

Hi @kiwibird1,

I tried to fix the code formatting a bit in your post. It looks like your calcVI function is frequently blocking for a few seconds at a time, making your loop function longer than the ping timeout with the cloud. If you threw a few Spark_WLAN_Loop() calls in there, or I think the new syntax is Spark.process(), then I think OTA would more successful.

Thanks!
David

Hi Dave
I looked at this link here


But not sure how to implement the function in my code.
the CalcVI function once its running, is somewhat time sensitive. I cant afford to wait say a second to Spark.Process()
(I have no idea how long spark.process takes)
I know its UN-related, but is there a adc x20 mode and or differential mode on spark? Attiny44 for example has it.
Whats the correct way to format code I paste on the forums?

Hmm, could you run the Spark.process between calls to CalcVI? The default connection behavior “Automatic” pings the server every 5-10 seconds, and if it doesn’t process messages at least once every 10 seconds, it’ll think it’s disconnected.

You can format code by using three " ` " marks on either side, or by selecting your code and clicking the “</>” icon in the edit window when entering a post. :slight_smile:

Thanks,
David