int sendRequest (byte* host, unsigned short port, char* response, unsigned short responseSize, boolean keepAlive) { if (myClient.connected() || myClient.connect(host, port)) { uint32_t startTime = millis(); myClient.write((const uint8_t *)mainbuffer, strlen(mainbuffer)); myClient.flush(); while(!myClient.available() && (millis() - startTime) < 5000){ SPARK_WLAN_Loop(); }; while(myClient.available()) { readbytes = myClient.read((uint8_t*) response, responseSize); if (readbytes == -1) break; } myClient.flush(); if(!keepAlive) { myClient.stop(); } } else { // unable to connect return 1; } return 0; }
void setup() { Serial.begin(9600); while (!Serial.available()) // Wait here until the user presses ENTER SPARK_WLAN_Loop(); // in the Serial Terminal. Call the BG Tasks // Begin tmp006 measuring tmp.begin(); Serial.println("TMP006 Example"); }
void loop() { uint32_t t; double r; Serial.println("Type any character to start"); while (!Serial.available()) SPARK_WLAN_Loop(); while (Serial.available()) Serial.read(); // open or create file - truncate existing file. if (!file.open(&root, "BENCH.DAT", O_CREAT | O_TRUNC | O_RDWR)) { error("open failed"); } // fill buf with known data for (uint16_t i = 0; i < (BUF_SIZE-2); i++) { buf[i] = 'A' + (i % 26); } buf[BUF_SIZE-2] = '\r'; buf[BUF_SIZE-1] = '\n'; Serial.print("File size "); Serial.print(FILE_SIZE_MB); Serial.println(" MB"); uint32_t n = FILE_SIZE/sizeof(buf); Serial.println("Starting write test. Please wait up to a minute"); // do write test t = millis(); for (uint32_t i = 0; i < n; i++) { if (file.write(buf, sizeof(buf)) != sizeof(buf)) { error("write failed"); } } t = millis() - t; file.sync(); r = (double)file.fileSize()/t; Serial.print("Write "); Serial.print(r); Serial.println(" kB/sec"); Serial.println(); Serial.println("Starting read test. Please wait up to a minute"); // do read test file.rewind(); t = millis(); for (uint32_t i = 0; i < n; i++) { if (file.read(buf, sizeof(buf)) != sizeof(buf)) { error("read failed"); } } t = millis() - t; r = (double)file.fileSize()/t; Serial.print("Read "); Serial.print(r); Serial.println(" kB/sec"); Serial.println("Done"); file.close(); }
void setup() { // open serial terminal and press ENTER to start Serial.begin(9600); while(!Serial.available()) SPARK_WLAN_Loop(); // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.print("MAX31855 test"); // wait for MAX chip to stabilize delay(500); }
void setup() { //init thermocouple thermocouple.init(); // publish calibration function Spark.function("calibrate",calibrateTherm); // open serial terminal and press ENTER to start Serial.begin(9600); while (!Serial.available()) SPARK_WLAN_Loop(); while (Serial.available()) Serial.read(); //flush serial data Serial.println("MAX31855 test"); // wait for MAX chip to stabilize delay(500); }
void error(const char* str) { Serial.print("error: "); Serial.println(str); if (card.errorCode()) { Serial.print("SD error: "); Serial.print(card.errorCode(), HEX); Serial.print(','); Serial.println(card.errorData(), HEX); } while(1) { SPARK_WLAN_Loop(); }; }
void setup() { pinMode(D7, OUTPUT); digitalWrite(D7, HIGH); likeservo.attach(A7); // attaches the servo on the A7 pin to the servo object likeservo.write(0); // start with servo fully clockwise (down pushup) if(DEBUG_SERIAL) { Serial.begin(115200); while (!Serial.available()) { // After Core D7 LED turns on, open serial monitor and press enter! SPARK_WLAN_Loop(); } } // Force FB to be polled immediately on start lastTime = millis() + 500UL; blinkCount = 19; }
void setup() { startTime = millis(); // capture the time that our app starts pinMode(D0,INPUT); pinMode(D7,OUTPUT); #ifdef SERIAL_DEBUG Serial.begin(9600); // Make sure serial terminal is closed before powering up Core while(!Serial.available()) SPARK_WLAN_Loop(); // Open serial terminal now, and press ENTER #endif // POST to https://api.spark.io/v1/devices/DEVICE_ID/on?access_token=12341234 Spark.function("on", setOn); // call this function to enable the system // POST to https://api.spark.io/v1/devices/DEVICE_ID/off?access_token=12341234 Spark.function("off", setOff); // call this function to disable the system } // end setup()
/* * @brief This should block for a certain number of milliseconds and also execute spark_wlan_loop */ void delay(unsigned long ms) { #ifdef SPARK_WLAN_ENABLE volatile system_tick_t spark_loop_elapsed_millis = SPARK_LOOP_DELAY_MILLIS; spark_loop_total_millis += ms; #endif volatile system_tick_t last_millis = GetSystem1MsTick(); while (1) { KICK_WDT(); volatile system_tick_t current_millis = GetSystem1MsTick(); volatile long elapsed_millis = current_millis - last_millis; //Check for wrapping if (elapsed_millis < 0) { elapsed_millis = last_millis + current_millis; } if (elapsed_millis >= ms) { break; } #ifdef SPARK_WLAN_ENABLE if (!SPARK_WLAN_SETUP || SPARK_WLAN_SLEEP) { //Do not yield for SPARK_WLAN_Loop() } else if ((elapsed_millis >= spark_loop_elapsed_millis) || (spark_loop_total_millis >= SPARK_LOOP_DELAY_MILLIS)) { spark_loop_elapsed_millis = elapsed_millis + SPARK_LOOP_DELAY_MILLIS; //spark_loop_total_millis is reset to 0 in SPARK_WLAN_Loop() do { //Run once if the above condition passes SPARK_WLAN_Loop(); } while (SPARK_FLASH_UPDATE);//loop during OTA update } #endif } }
void SwitchScheduler::tock() { if (configuration == NULL) { DEBUG_PRINT("Configuration not set."); return; } if (lastLoopCheck == 0) { uint32_t tNow = rtc->now(); while (rtc->second(tNow) != 0) { DEBUG_PRINT("Second: "); DEBUG_PRINT(rtc->second(tNow)); DEBUG_PRINT("\n"); DEBUG_PRINT(rtc->ISODateString(rtc->now()) + "\n"); SPARK_WLAN_Loop(); delay(1000); tNow = rtc->now(); } } unsigned long now = millis(); if (now - lastLoopCheck >= checkLoopInterval || lastLoopCheck == 0) { DEBUG_PRINT("Tock..."); DEBUG_PRINT(rtc->ISODateString(rtc->now()) + "\n"); // Sync the time daily. syncTime(); // Try to get the astronomy data if it's time. retrieveAstronomyData(); if (isSchedulerEnabled()) { // Turn on/off the outlet switch if it's time. checkSchedulerTasks(); } lastLoopCheck = now; } }
void setup(void) { Serial.begin(9600); while (!Serial.available()) SPARK_WLAN_Loop(); //wait to connect Serial.println("Adafruit MMA8451 test!"); if (! mma.begin()) { Serial.println("Couldnt start"); while (1); } Serial.println("MMA8451 found!"); mma.setRange(MMA8451_RANGE_2_G); Serial.print("Range = "); Serial.print(2 << mma.getRange()); Serial.println("G"); }
void loop() { // Check if PIR is active if (pirActive() == true) { // Push notify! if (PUMPKIN_ARMED == true) { int status = 0; int attempts = 0; status = pushNotify("Pumpkin", "Trick or Treat"); // If we don't see a successful response, try again 5 more times. while ( status != 200 && attempts++ < 5 ) { // If this failed for some reason, try again in 3 seconds, bail after 5 times. startTime = millis(); while (millis() - startTime < 3000UL) { digitalWrite(D7,HIGH); delay(100); digitalWrite(D7,LOW); delay(100); } SPARK_WLAN_Loop(); // process background tasks to stay connected to Spark Cloud status = pushNotify("Pumpkin", "Trick or Treat"); // retry } } // Do some flashy stuff RGB.control(true); for (uint8_t x=0; x<10; x++) { // BLINK ORANGE RGB.color(255,100,0); // orange delay(200); RGB.color(0,0,0); // off delay(200); } RGB.control(false); } // Make it blinky to indicate everything is running, and not locked up if (millis() - startTime > 1000UL) { startTime = millis(); s = !s; digitalWrite(D7,s); } } // end loop()
void setup() { Serial.begin(115200); while (!Serial.available()) SPARK_WLAN_Loop(); while (Serial.available()) Serial.read(); // initialize the SD card at SPI_FULL_SPEED for best performance. // try SPI_HALF_SPEED if bus errors occur. // Initialize HARDWARE SPI with user defined chipSelect if (!card.init(SPI_FULL_SPEED, chipSelect)) error("card.init failed"); // Initialize SOFTWARE SPI //if (!card.init(mosiPin, misoPin, clockPin, chipSelect)) error("card.init failed"); // initialize a FAT volume if (!volume.init(&card)) error("volume.init failed!"); Serial.print("Type is FAT"); Serial.println(volume.fatType(), DEC); if (!root.openRoot(&volume)) error("openRoot failed"); }