Skip to content

Commit

Permalink
Add wifi projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Ines333 committed Feb 20, 2024
1 parent a53f90a commit c766623
Show file tree
Hide file tree
Showing 38 changed files with 560 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Examples/SwiftIOPlayground/11WiFi/JoiningWiFi/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "JoiningWiFi",
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/madmachineio/SwiftIO.git", branch: "main"),
.package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"),
.package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "JoiningWiFi",
dependencies: [
"SwiftIO",
"MadBoards",
// Use specific library name rather than "MadDrivers" would speed up the build procedure.
.product(name: "ESP32ATClient", package: "MadDrivers")
]),
]
)
33 changes: 33 additions & 0 deletions Examples/SwiftIOPlayground/11WiFi/JoiningWiFi/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import SwiftIO
import MadBoard
import ESP32ATClient

let rst = DigitalOut(Id.D24, value: true)
let uart = UART(Id.UART1, baudRate: 115200)
let esp = ESP32ATClient(uart: uart, rst: rst)

do {
// If reset failed, you might need to adjust the baudrate.
try esp.reset()
print("ESP32 status: \(esp.esp32Status)")

// Only in 'Station' or 'Station+SoftAP' mode can a connection to an AP be established.
var wifiMode = ESP32ATClient.WiFiMode.station
_ = try esp.setWiFiMode(wifiMode)

wifiMode = try esp.getWiFiMode()
print("ESP32 WiFi mode: \(wifiMode)")

// Fill the SSID and password below.
try esp.joinAP(ssid: "TP-LINK_CD1C", password: "q1w2e3r4", timeout: 20000)
print("ESP32 WiFi status: \(esp.wifiStatus)")

let ipInfo = try esp.getStationIP()
print(ipInfo)
} catch {
print("Error: \(error)")
}

while true {
sleep(ms: 1000)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "TemperatureDataLogger",
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/madmachineio/SwiftIO.git", branch: "main"),
.package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"),
// .package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "TemperatureDataLogger",
dependencies: [
"SwiftIO",
"MadBoards",
// Use specific library name rather than "MadDrivers" would speed up the build procedure.
.product(name: "ESP32ATClient", package: "MadDrivers"),
.product(name: "SHT3x", package: "MadDrivers"),
]),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import SwiftIO
import MadBoard
import ESP32ATClient
import SHT3x

let rst = DigitalOut(Id.D24, value: true)
let uart = UART(Id.UART1, baudRate: 115200)
let esp = ESP32ATClient(uart: uart, rst: rst)

let i2c = I2C(Id.I2C0)
let humiture = SHT3x(i2c)

do {
// If reset failed, you might need to adjust the baudrate.
try esp.reset()
print("ESP32 status: \(esp.esp32Status)")

// Only in 'Station' or 'Station+SoftAP' mode can a connection to an AP be established.
var wifiMode = ESP32ATClient.WiFiMode.station
_ = try esp.setWiFiMode(wifiMode)

wifiMode = try esp.getWiFiMode()
print("ESP32 WiFi mode: \(wifiMode)")

// Fill the SSID and password below.
try esp.joinAP(ssid: "TP-LINK_CD1C", password: "q1w2e3r4", timeout: 20000)
print("ESP32 WiFi status: \(esp.wifiStatus)")

let ipInfo = try esp.getStationIP()
print(ipInfo)
} catch {
print("Error: \(error)")
}

while true {
sleep(ms: 30_000)
if esp.wifiStatus == .ready {
do {
// Read temperature and humidity values from the sensor.
let temp = humiture.readCelsius()
let humidity = humiture.readHumidity()
// Send the values to ThingSpeak using HTTP POST requests to visualize them.
_ = try esp.httpPost(url: "https://api.thingspeak.com/update?api_key=WCGQWXCBJA2WS03F&field1=\(temp)&field2=\(humidity)", headers: ["Content-Type: application/x-www-form-urlencoded"], timeout: 20000)
} catch {
print("Http POST Error: \(error)")
}
} else {
_ = try? esp.readLine(timeout: 1000)
print("WiFi status: \(esp.wifiStatus)")
}
}
28 changes: 28 additions & 0 deletions Examples/SwiftIOPlayground/11WiFi/Weather/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Weather",
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/madmachineio/SwiftIO.git", branch: "main"),
.package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"),
.package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"),
.package(url: "https://github.com/swift-extras/swift-extras-json.git", branch: "main"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "Weather",
dependencies: [
"SwiftIO",
"MadBoards",
// Use specific library name rather than "MadDrivers" would speed up the build procedure.
.product(name: "ESP32ATClient", package: "MadDrivers"),
.product(name: "ExtrasJSON", package: "swift-extras-json"),
]),
]
)
124 changes: 124 additions & 0 deletions Examples/SwiftIOPlayground/11WiFi/Weather/Sources/WeatherInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Structs used to decode the JSON data from the weather service.
struct Coordinate: Decodable {
let longitude: Float
let latitude: Float

enum CodingKeys: String, CodingKey {
case longitude = "lon"
case latitude = "lat"
}
}

struct WeatherConditions: Decodable {
let id: Int
let main: String
let description: String
let icon: String
}

struct MainInfo: Decodable {
let temp: Float
let feelsLikeTemp: Float
let minTemp: Float
let maxTemp: Float
let pressure: Int
let humidity: Int
let seaLevel: Int
let groundLevel: Int

enum CodingKeys: String, CodingKey {
case temp
case feelsLikeTemp = "feels_like"
case minTemp = "temp_min"
case maxTemp = "temp_max"
case pressure
case humidity
case seaLevel = "sea_level"
case groundLevel = "grnd_level"
}
}

struct Wind: Decodable {
let speed: Float
let degree: Int
let gust: Float

enum CodingKeys: String, CodingKey {
case speed
case degree = "deg"
case gust
}
}

struct Clouds: Decodable {
let cloudiness: Int

enum CodingKeys: String, CodingKey {
case cloudiness = "all"
}
}

struct Rain: Decodable {
let rain1h: Float?
let rain3h: Float?

enum CodingKeys: String, CodingKey {
case rain1h = "1h"
case rain3h = "3h"
}
}

struct Snow: Decodable {
let snow1h: Float?
let snow3h: Float?

enum CodingKeys: String, CodingKey {
case snow1h = "1h"
case snow3h = "3h"
}
}

struct Sys: Decodable {
let type: Int?
let id: Int?
let country: String
let sunrise: Int
let sunset: Int
}

struct WeatherInfo: Decodable {
let coordinate: Coordinate
let weatherConditions: [WeatherConditions]
let base: String
let mainInfo: MainInfo
let visibility: Int
let wind: Wind
let clouds: Clouds
let rain: Rain?
let snow: Snow?
let dt: Int
let sys: Sys
let timezone: Float
let cityId: Int
let cityName: String
let cod: Int


enum CodingKeys: String, CodingKey {
case coordinate = "coord"
case weatherConditions = "weather"
case base
case mainInfo = "main"
case visibility
case wind
case clouds
case rain
case snow
case dt
case sys
case timezone
case cityId = "id"
case cityName = "name"
case cod
}
}
60 changes: 60 additions & 0 deletions Examples/SwiftIOPlayground/11WiFi/Weather/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import SwiftIO
import MadBoard
import ESP32ATClient
import ExtrasJSON

let rst = DigitalOut(Id.D24, value: true)
let uart = UART(Id.UART1, baudRate: 115200)
let esp = ESP32ATClient(uart: uart, rst: rst)

do {
// If reset failed, you might need to adjust the baudrate.
try esp.reset()
print("ESP32 status: \(esp.esp32Status)")

// Only in 'Station' or 'Station+SoftAP' mode can a connection to an AP be established.
var wifiMode = ESP32ATClient.WiFiMode.station
_ = try esp.setWiFiMode(wifiMode)

// Print current Wi-Fi mode.
wifiMode = try esp.getWiFiMode()
print("ESP32 WiFi mode: \(wifiMode)")

// Fill the SSID and password below.
try esp.joinAP(ssid: "", password: "", timeout: 20000)
print("ESP32 WiFi status: \(esp.wifiStatus)")

// Print the assigned IP address.
let ipInfo = try esp.getStationIP()
print(ipInfo)
} catch {
print("Error: \(error)")
}

sleep(ms: 1000)

if esp.wifiStatus == .ready {
do {
// Send request to the weather service to obtain current weather.
// Update the URL with your API key and your city name.
let response = try esp.httpGet(url: "https://api.openweathermap.org/data/2.5/weather?q=metric&q=YourCity&appid=YourAPIKey", timeout: 30000)

// Decode the JSON data and print the weather info.
let weatherInfo = try XJSONDecoder().decode(WeatherInfo.self, from: Array(response.utf8))
print("City: \(weatherInfo.cityName)")
print("Weather: \(weatherInfo.weatherConditions[0].main)")
print("Temp: \(weatherInfo.mainInfo.temp)C")
print("Humidity: \(weatherInfo.mainInfo.humidity)")
} catch let error as DecodingError {
print("JSON Decoding Error: \(error)")
} catch {
print("Http GET Error: \(error)")
}
} else {
_ = try? esp.readLine(timeout: 1000)
print("WiFi status: \(esp.wifiStatus)")
}

while true {
sleep(ms: 1000)
}
26 changes: 26 additions & 0 deletions Examples/SwiftIOPlayground/12MoreProjects/LifeGame/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "LifeGame",
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/madmachineio/SwiftIO.git", branch: "main"),
.package(url: "https://github.com/madmachineio/MadBoards.git", branch: "main"),
.package(url: "https://github.com/madmachineio/MadDrivers.git", branch: "main"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "LifeGame",
dependencies: [
"SwiftIO",
"MadBoards",
// Use specific library name rather than "MadDrivers" would speed up the build procedure.
.product(name: "ST7789", package: "MadDrivers")
]),
]
)
Loading

0 comments on commit c766623

Please sign in to comment.