Skip to content

Commit

Permalink
more picow fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Jul 25, 2024
1 parent b74df5d commit 995adbe
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ venv
.vscode
docs
bin
pvenv
2 changes: 1 addition & 1 deletion examples/PICOWTestNip01/PICOWTestNip01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void setup() {
nostr::picow::PICOWPlatform::initTime("pool.ntp.org");

Serial.println("Init Nostr");
nostr::picow::PICOWPlatform::initNostr(analogRead(9), true);
nostr::picow::PICOWPlatform::initNostr(analogRead(A0), true);

Serial.println("Ready!");

Expand Down
9 changes: 8 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,18 @@ lib_deps =

[env:PICOWTestNip01]
monitor_speed = 115200

extra_scripts = post:move_firmware.py
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = pico
board = rpipicow
framework = arduino
board_build.core = earlephilhower

build_flags =
-DDEBUG_RP2040_WIRE
-DDEBUG_RP2040_SPI
-DDEBUG_RP2040_CORE
-DPIO_FRAMEWORK_ARDUINO_ENABLE_RTTI
-fexceptions
build_unflags =
-fno-exceptions
Expand All @@ -112,3 +118,4 @@ lib_deps =




5 changes: 3 additions & 2 deletions src/Transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ namespace nostr {
enum class ConnectionStatus { CONNECTED, DISCONNECTED, ERROR };
class Connection {
public:
virtual void addMessageListener(std::function<void(NostrString)> listener);
virtual void addMessageListener(std::function<void(NostrString)> listener) = 0;
virtual void send(NostrString message) = 0;
virtual void disconnect() = 0;
virtual ~Connection() = default;
Connection() = default;
virtual void loop() = 0;
virtual bool isReady() = 0;
virtual void addConnectionStatusListener(std::function<void(ConnectionStatus status)> listener) {};
virtual void addConnectionStatusListener(std::function<void(ConnectionStatus status)> listener) = 0;
};
class Transport {
public:
Expand Down
4 changes: 3 additions & 1 deletion src/esp32/ESP32Platform.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef _NOSTR_ESP32_PLATFORM_H
#define _NOSTR_ESP32_PLATFORM_H 1
#ifdef ESP32
#include "Common.h"

#ifdef _ESP32_BOARD_
#include "NostrString.h"
#include "Utils.h"
#include "bootloader_random.h"
Expand Down
4 changes: 2 additions & 2 deletions src/esp32/ESP32Transport.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "ESP32Transport.h"
#include "Common.h"

#ifdef ESP32

#ifdef _ESP32_BOARD_

using namespace nostr;

Expand Down
4 changes: 3 additions & 1 deletion src/esp32/ESP32Transport.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef _NOSTR_ESP32_TRANSPORT_H
#define _NOSTR_ESP32_TRANSPORT_H 1
#ifdef ESP32
#include "Common.h"

#ifdef _ESP32_BOARD_

#include "ArduinoJson.h"
#include "NostrString.h"
Expand Down
30 changes: 15 additions & 15 deletions src/picow/PICOWPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
#define _NOSTR_PICOW_PLATFORM_H
#ifdef _PICOW_BOARD_

#include <SPI.h>
#include <WiFi.h>
#include "Arduino.h"
#include "NostrString.h"
#include "Utils.h"
#include "picow/PICOWTransport.h"
#include <time.h>

#include <WiFi.h>

namespace nostr {
namespace picow {
namespace PICOWPlatform {
Expand All @@ -34,26 +33,27 @@ inline void serialLogger(const NostrString &str) {
* Initialize the WiFi connection
*/
inline void initWifi(NostrString ssid, NostrString passphrase, int channel = 6) {
WiFi.mode(WIFI_STA);
if (WiFi.status() == WL_NO_MODULE) {
IPAddress dns(8, 8, 8, 8);


if (WiFi.status() == WL_NO_SHIELD) {
while (true){
Serial.println("Communication with WiFi module failed!");
}
}

int status = WL_IDLE_STATUS;

String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}

int wifiStatus = WiFi.begin(ssid.c_str(), passphrase.c_str());
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid.c_str());
status = WiFi.begin(ssid.c_str(), passphrase.c_str());

while (wifiStatus != WL_CONNECTED) {
Serial.printf("Connecting... status: %d\n ", wifiStatus);
wifiStatus = WiFi.status();
delay(500);
delay(10000);
}

WiFi.setDNS(dns);

Serial.println("Connected to WiFi");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Expand Down
14 changes: 9 additions & 5 deletions src/picow/PICOWTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ picow::PICOWConnection::PICOWConnection(PICOWTransport *transport, NostrString u
port = NostrString_toInt(portStr);
host = NostrString_substring(host, 0, NostrString_indexOf(host, ":"));
}
// if (ssl) {
// Utils::log("Connecting to " + host + " : " + port + " with path " + path + " using SSL...");
// ws.beginSSL(host, port, path);
// } else {
if (ssl) {
Utils::log("Connecting to " + host + " : " + port + " with path " + path + " using SSL...");
ws.beginSSL(host.c_str(), port, path.c_str());
} else {
Utils::log("Connecting to " + host + " : " + port + " with path " + path + "...");
ws.begin(host, port, path);
// }
}
ws.setReconnectInterval(5000);
ws.onEvent([this](WStype_t type, uint8_t *payload, size_t length) {
switch (type) {
Expand Down Expand Up @@ -202,4 +202,8 @@ picow::PICOWConnection::~PICOWConnection() {

picow::PICOWTransport::PICOWTransport() {}

void picow::PICOWConnection::addConnectionStatusListener(std::function<void(ConnectionStatus status)> listener) {
connectionListeners.push_back(listener);
}

#endif
11 changes: 7 additions & 4 deletions src/picow/PICOWTransport.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Common.h"
#ifndef _NOSTR_PICOW_TRANSPORT_H
#define _NOSTR_PICOW_TRANSPORT_H
#include "Common.h"

#ifdef _PICOW_BOARD_

#include <WebSocketsClient.h>
Expand Down Expand Up @@ -28,6 +29,8 @@ class PICOWConnection : public Connection {
bool isReady() override;
void addMessageListener(std::function<void(NostrString)> listener) override;
~PICOWConnection() override;
void addConnectionStatusListener(std::function<void(ConnectionStatus status)> listener) override;


protected:
PICOWConnection(PICOWTransport *transport, NostrString url);
Expand All @@ -36,13 +39,13 @@ class PICOWConnection : public Connection {
PICOWTransport *transport;
WebSocketsClient ws;
std::vector<std::function<void(NostrString)>> messageListeners;

std::vector<std::function<void(ConnectionStatus status)>> connectionListeners;
};
class PICOWTransport : public Transport {
public:
void getInvoiceFromLNAddr(NostrString addr, unsigned long long amountMSats, NostrString comment, std::function<void(NostrString)> callback);
void getInvoiceFromLNAddr(NostrString addr, unsigned long long amountMSats, NostrString comment, std::function<void(NostrString)> callback) override;
Connection *connect(NostrString url) override;
void close();
void close() override;
void disconnect(Connection *conn) override;
~PICOWTransport() override;
PICOWTransport();
Expand Down
11 changes: 4 additions & 7 deletions wokwi/picow/diagram.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
{
"version": 1,
"author": "Riccardo Balbo",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"type": "board-pi-pico-w",
"id": "pico",
"top": 0,
"left": 0,
"attrs": {
"env": "arduino-community"
}
}
"attrs": {}
}
],
"connections": [
[
Expand Down

0 comments on commit 995adbe

Please sign in to comment.