Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make library compatible with NINA WiFi module #15

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions examples/WiFi_Nina_OTA/WiFi_Nina_OTA.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*

This example connects to an WPA encrypted WiFi network.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This example connects to an WPA encrypted WiFi network.
This example connects to a WPA encrypted WiFi network.

Then it prints the MAC address of the Wifi shield,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Then it prints the MAC address of the Wifi shield,
Then it prints the MAC address of the WiFi shield,

the IP address obtained, and other network details.
It then polls for sketch updates over WiFi, sketches
can be updated by selecting a network port from within
the Arduino IDE: Tools -> Port -> Network Ports ...

Circuit:
* WiFi shield attached
* SD shield attached

created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
modified 16 January 2017
by Sandeep Mistry
*/

#include <Arduino_WiFiOTA.h>

// SAMD boards use a second stage bootloader approach and have enough RAM to retrieve the update firmware by themselves
// Uno WiFi Rev2 needs some little help, so the Nina module takes care of dowloading and flashing the board

#ifdef ARDUINO_ARCH_SAMD
#include <SNU.h>
#define STORAGE NINAStorageRaw
#else
#define STORAGE NINAStorage
#endif


#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// Wifi Settings ///////
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password

int status = WL_IDLE_STATUS;

void setup() {
//Initialize serial:
Serial.begin(9600);

// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
}

// start the WiFi OTA library with Nina based storage
WiFiOTA.begin("Arduino", "password", STORAGE);

// you're connected now, so print out the status:
printWifiStatus();
}

void loop() {
// check for WiFi OTA updates
WiFiOTA.poll();

// add your normal loop code below ...
}

void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Serial.print("signal strength (RSSI):");
Serial.print("signal strength (RSSI): ");

Serial.print(rssi);
Serial.println(" dBm");
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
*/

#include <SPI.h>
#include <WiFi101.h>
#include <WiFi101OTA.h>
#include <Arduino_WiFiOTA.h>

#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

#include <SPI.h>
#include <SD.h>
#include <WiFi101.h>
#include <WiFi101OTA.h>
#include <Arduino_WiFiOTA.h>
#include <SDU.h>

#include "arduino_secrets.h"
Expand Down
3 changes: 3 additions & 0 deletions examples/WiFi_SD_OTA/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SECRET_SSID ""
#define SECRET_PASS ""

3 changes: 3 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ WiFiOTA KEYWORD1

InternalStorage KEYWORD1
SDStorage KEYWORD1
NINAStorage KEYWORD1
NINAStorageRaw KEYWORD1
SerialFlashStorage KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
Expand Down
6 changes: 3 additions & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=WiFi101OTA
version=1.0.2
name=Arduino_WiFiOTA
version=1.1.0
author=Arduino
maintainer=Arduino <[email protected]>
sentence=Update sketches on your board over WiFi
paragraph=Requires an Arduino/Genuino SAMD board

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should read "SAMD or MEGA AVR"

category=Other
url=http://www.arduino.cc/en/Reference/WiFi101OTA
architectures=samd
architectures=samd,megaavr
27 changes: 13 additions & 14 deletions src/WiFi101OTA.cpp → src/Arduino_WiFiOTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,7 @@

#include <Arduino.h>

#include "WiFi101OTA.h"

#if defined(ARDUINO_SAMD_ZERO)
#define BOARD "arduino_zero_edbg"
#elif defined(ARDUINO_SAMD_MKR1000)
#define BOARD "mkr1000"
#elif defined(ARDUINO_SAMD_MKRZERO)
#define BOARD "mkrzero"
#else
#error "Unsupported board!"
#endif
#include "Arduino_WiFiOTA.h"

#define BOARD_LENGTH (sizeof(BOARD) - 1)

Expand Down Expand Up @@ -82,7 +72,7 @@ void WiFiOTAClass::begin(const char* name, const char* password, OTAStorage& sto

_server.begin();

_mdnsSocket.beginMulti(IPAddress(224, 0, 0, 251), 5353);
_mdnsSocket.beginMulticast(IPAddress(224, 0, 0, 251), 5353);
}

void WiFiOTAClass::poll()
Expand Down Expand Up @@ -297,15 +287,24 @@ void WiFiOTAClass::pollServer()
}

long read = 0;
String url;

while (client.connected() && read < contentLength) {
while (client.available()) {
read++;

_storage->write((char)client.read());
char c = (char)client.read();
if (_storage->hasDownloadAPI()) {
url += c;
} else {
_storage->write(c);
}
}
}

if (_storage->hasDownloadAPI()) {
_storage->download(url);
}

_storage->close();

if (read == contentLength) {
Expand Down
28 changes: 27 additions & 1 deletion src/WiFi101OTA.h → src/Arduino_WiFiOTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,39 @@

#include <Arduino.h>

#include "WiFi101.h"
#if defined(ARDUINO_SAMD_ZERO)
#include "WiFi101.h"
#define BOARD "arduino_zero_edbg"
#elif defined(ARDUINO_SAMD_MKR1000)
#include "WiFi101.h"
#define BOARD "mkr1000"
#elif defined(ARDUINO_SAMD_MKRZERO)
#include "WiFi101.h"
#define BOARD "mkrzero"
#elif defined(ARDUINO_SAMD_MKRWIFI1010)
#include "WiFiNINA.h"
#define BOARD "mkrwifi1010"
#elif defined(ARDUINO_SAMD_NANO_33_IOT)
#include "WiFiNINA.h"
#define BOARD "nano_33_iot"
#elif defined(ARDUINO_SAMD_MKRVIDOR4000)
#include "WiFiNINA.h"
#define BOARD "mkrvidor4000"
#elif defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_MEGAAVR_UNO_WIFI_REV2)
#include "WiFiNINA.h"
#define BOARD "uno2018"
#else
#error "Unsupported board!"
#endif

#include "WiFiUdp.h"

#include "OTAStorage.h"
#include "SDStorage.h"
#include "InternalStorage.h"
#include "SerialFlashStorage.h"
#include "NINAStorage.h"
#include "NINAStorageRaw.h"

class WiFiOTAClass {
public:
Expand Down
4 changes: 4 additions & 0 deletions src/InternalStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef __AVR__

#include <Arduino.h>

#include "InternalStorage.h"
Expand Down Expand Up @@ -119,3 +121,5 @@ long InternalStorageClass::maxSize()
}

InternalStorageClass InternalStorage;

#endif
87 changes: 87 additions & 0 deletions src/NINAStorage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright (c) 2018 Arduino LLC. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "NINAStorage.h"

#ifdef HAS_NINA

#define UPDATE_FILE "/fs/UPDATE.BIN"

static inline void reboot() {
#ifdef __AVR__
/* Write boot request */
USERROW.USERROW31 = 0xEB;
_PROTECTED_WRITE_SPM(NVMCTRL.CTRLA, NVMCTRL_CMD_PAGEERASEWRITE_gc);
while(NVMCTRL.STATUS & NVMCTRL_EEBUSY_bm);

_PROTECTED_WRITE(RSTCTRL.SWRR, RSTCTRL_SWRE_bm);
#else
NVIC_SystemReset();
#endif
}

int NINAStorageClass::open(int contentLength)
{
if (WiFiStorage.exists(UPDATE_FILE)) {
WiFiStorage.remove(UPDATE_FILE);
}

_file = new WiFiStorageFile(UPDATE_FILE);

return 1;
}

size_t NINAStorageClass::write(uint8_t b)
{
int ret = _file->write(&b, 1);
return ret;
}

void NINAStorageClass::close()
{
_file->close();
}

void NINAStorageClass::clear()
{
WiFiStorage.remove(UPDATE_FILE);
}

void NINAStorageClass::apply()
{
WiFiDrv::applyOTA();
reboot();
}

void NINAStorageClass::download(String url)
{
WiFiStorage.download(url, "UPDATE.BIN");
}

long NINAStorageClass::maxSize()
{
#ifdef __AVR__
return (0xFFFF - 0x3FFF - 0x100);
#else
return ((256 * 1024) - 0x2000);
#endif
}

NINAStorageClass NINAStorage;

#endif
56 changes: 56 additions & 0 deletions src/NINAStorage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright (c) 2017 Arduino LLC. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef _NINA_STORAGE_H_INCLUDED
#define _NINA_STORAGE_H_INCLUDED

#ifdef __has_include
#if __has_include(<WiFiStorage.h>)
#include <WiFiStorage.h>
#define HAS_NINA 1
#endif
#else
#include <WiFiStorage.h>
#define HAS_NINA 1
#endif

#ifdef HAS_NINA

#include "OTAStorage.h"

class NINAStorageClass : public OTAStorage {
public:
virtual int open(int length);
virtual size_t write(uint8_t);
virtual void close();
virtual void clear();
virtual void apply();
virtual long maxSize();
virtual void download(String url);
virtual bool hasDownloadAPI() {
return true;
}

private:
WiFiStorageFile* _file;
};

extern NINAStorageClass NINAStorage;

#endif
#endif
Loading