This repository was archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
Reduce boilerplate in the sketch #9
Closed
sandeepmistry
wants to merge
1
commit into
Azure:master
from
sandeepmistry:reduce-sketch-boilerplate
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Arduino. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#include <AzureIoTHub.h> | ||
|
||
#include "simplesample_http.h" | ||
|
||
char ssid[] = "yourNetwork"; // your network SSID (name) | ||
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
while(!Serial) { | ||
; | ||
} | ||
|
||
AzureIoTHub.begin(ssid, pass); | ||
} | ||
|
||
void loop() { | ||
simplesample_http_run(); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
####################################### | ||
# Syntax Coloring Map For WiFi | ||
####################################### | ||
|
||
####################################### | ||
# Datatypes (KEYWORD1) | ||
####################################### | ||
|
||
AzureIoTHub KEYWORD1 | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
####################################### | ||
|
||
begin KEYWORD2 | ||
|
||
####################################### | ||
# Constants (LITERAL1) | ||
####################################### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright (c) Arduino. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#include <time.h> | ||
#include <sys/time.h> | ||
|
||
#if defined(ARDUINO_SAMD_FEATHER_M0) | ||
#include <Adafruit_WINC1500.h> | ||
#elif defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) | ||
#include <WiFi101.h> | ||
#elif defined(ARDUINO_ARCH_ESP8266) | ||
#include <ESP8266WiFi.h> | ||
#else | ||
#error "Unsupported platform" | ||
#endif | ||
|
||
#include "util/NTPClient.h" | ||
|
||
#include "AzureIoTHub.h" | ||
|
||
AzureIoTHubClass AzureIoTHub; | ||
|
||
#if defined(ARDUINO_SAMD_FEATHER_M0) | ||
|
||
#define WINC_CS 8 | ||
#define WINC_IRQ 7 | ||
#define WINC_RST 4 | ||
#define WINC_EN 2 // or, tie EN to VCC | ||
|
||
Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST); | ||
#endif | ||
|
||
void AzureIoTHubClass::begin(const char ssid[], const char password[]) | ||
{ | ||
#if defined(ARDUINO_ARCH_ESP8266) | ||
Serial.setDebugOutput(true); | ||
#endif | ||
|
||
setupWiFi(ssid, password); | ||
syncTime(); | ||
} | ||
|
||
void AzureIoTHubClass::setupWiFi(const char ssid[], const char password[]) | ||
{ | ||
#if defined(ARDUINO_SAMD_FEATHER_M0) && defined(WINC_EN) | ||
pinMode(WINC_EN, OUTPUT); | ||
digitalWrite(WINC_EN, HIGH); | ||
#endif | ||
|
||
#if defined(ARDUINO_ARCH_SAMD) | ||
// check for the presence of the shield : | ||
if (WiFi.status() == WL_NO_SHIELD) { | ||
Serial.println("WiFi shield not present"); | ||
// don't continue: | ||
while (true); | ||
} | ||
#endif | ||
|
||
Serial.print("Attempting to connect to SSID: "); | ||
Serial.println(ssid); | ||
|
||
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: | ||
while (WiFi.begin(ssid, password) != WL_CONNECTED) { | ||
delay(500); | ||
Serial.print("."); | ||
} | ||
|
||
Serial.println("Connected to wifi"); | ||
} | ||
|
||
void AzureIoTHubClass::syncTime() | ||
{ | ||
#if defined(ARDUINO_ARCH_SAMD) | ||
time_t epochTime = (time_t)-1; | ||
|
||
NTPClient ntpClient; | ||
ntpClient.begin(); | ||
|
||
while (true) { | ||
epochTime = ntpClient.getEpochTime("0.pool.ntp.org"); | ||
|
||
if (epochTime == (time_t)-1) { | ||
Serial.println("Fetching NTP epoch time failed! Waiting 5 seconds to retry."); | ||
delay(5000); | ||
} else { | ||
Serial.print("Fetched NTP epoch time is: "); | ||
Serial.println(epochTime); | ||
break; | ||
} | ||
} | ||
|
||
ntpClient.end(); | ||
|
||
struct timeval tv; | ||
tv.tv_sec = epochTime; | ||
tv.tv_usec = 0; | ||
|
||
settimeofday(&tv, NULL); | ||
#elif defined(ARDUINO_ARCH_ESP8266) | ||
time_t epochTime; | ||
|
||
configTime(0, 0, "pool.ntp.org", "time.nist.gov"); | ||
|
||
while (true) { | ||
epochTime = time(NULL); | ||
|
||
if (epochTime == 0) { | ||
Serial.println("Fetching NTP epoch time failed! Waiting 2 seconds to retry."); | ||
delay(2000); | ||
} else { | ||
Serial.print("Fetched NTP epoch time is: "); | ||
Serial.println(epochTime); | ||
break; | ||
} | ||
} | ||
#endif | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ladyada there is potential to remove use of
Adafruit_WINC1500
library in this library and just useWiFi101
if the variant for the "Adafruit Feather M0 WiFi" board defines following WINC1500 pin values like the MKR1000: https://github.com/arduino/ArduinoCore-samd/blob/master/variants/mkr1000/variant.h#L137-L143