Skip to content

Commit

Permalink
Add ESP32ATClient and examples
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Liu <andy@madmachine.io>
  • Loading branch information
andy0808 committed Dec 6, 2023
1 parent 2f54508 commit fde8c18
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 87 deletions.
3 changes: 2 additions & 1 deletion Examples/ESP32ATClient/HttpGet/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ let package = Package(
// 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/madmachineio/MadDrivers.git", branch: "main"),
.package(path: "../../.."),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
8 changes: 3 additions & 5 deletions Examples/ESP32ATClient/HttpGet/Sources/HttpGet/HttpGet.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Read temperature and relative humidity every second.
import SwiftIO
import MadBoard
import ESP32ATClient
Expand All @@ -22,8 +21,7 @@ public struct HttpGet {
print("ESP32 WiFi mode: \(wifiMode)")

// Fill the SSID and password below.
//_ = try esp.joinAP(ssid: "", password: "", autoConnect: true)
_ = try esp.joinAP(ssid: "Andy_iPhone15Pro", password: "asdfghjkl", autoConnect: true)
_ = try esp.joinAP(ssid: "", password: "", autoConnect: true)
print("ESP32 WiFi status: \(esp.wifiStatus)")

let ipInfo = try esp.getStationIP()
Expand All @@ -36,13 +34,13 @@ public struct HttpGet {
while true {
if esp.wifiStatus == .ready {
do {
let ret = try esp.httpGet(url: "https://arduino.tips/asciilogo.txt")
let ret = try esp.httpGet(url: "https://httpbin.org/get")
print(ret)
} catch {
print("Http GET Error: \(error)")
}
} else {
_ = try? esp.waitURCMessage(timeout: 1000)
_ = try? esp.readLine(timeout: 1000)
print("WiFi status: \(esp.wifiStatus)")
}

Expand Down
File renamed without changes.
25 changes: 25 additions & 0 deletions Examples/ESP32ATClient/HttpPost/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "HttpPost",
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(path: "../../.."),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.executableTarget(
name: "HttpPost",
dependencies: [
"SwiftIO",
"MadBoards",
// use specific library would speed up the compile procedure
.product(name: "ESP32ATClient", package: "MadDrivers")
]),
]
)
50 changes: 50 additions & 0 deletions Examples/ESP32ATClient/HttpPost/Sources/HttpPost/HttpPost.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import SwiftIO
import MadBoard
import ESP32ATClient

@main
public struct HttpPost {
public static func main() {
sleep(ms: 100)

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.
let wifiMode = try esp.getWiFiMode()
print("ESP32 WiFi mode: \(wifiMode)")

// Fill the SSID and password below.
_ = try esp.joinAP(ssid: "", password: "", autoConnect: true)
print("ESP32 WiFi status: \(esp.wifiStatus)")

let ipInfo = try esp.getStationIP()
print(ipInfo)

} catch {
print("Error: \(error)")
}

while true {
if esp.wifiStatus == .ready {
do {
let ret = try esp.httpPost(url: "https://httpbin.org/post", headers: ["Accept: application/json"])
print(ret)
} catch {
print("Http POST Error: \(error)")
}
} else {
_ = try? esp.readLine(timeout: 1000)
print("WiFi status: \(esp.wifiStatus)")
}

sleep(ms: 1000)
}
}
}
3 changes: 2 additions & 1 deletion Examples/ESP32ATClient/JoinWiFi/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ let package = Package(
// 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/madmachineio/MadDrivers.git", branch: "main"),
.package(path: "../../.."),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Read temperature and relative humidity every second.
import SwiftIO
import MadBoard
import ESP32ATClient
Expand Down
3 changes: 2 additions & 1 deletion Examples/ESP32ATClient/Prepare/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ let package = Package(
// 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/madmachineio/MadDrivers.git", branch: "main"),
.package(path: "../../.."),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Read temperature and relative humidity every second.
import SwiftIO
import MadBoard
import ESP32ATClient
Expand Down
Loading

0 comments on commit fde8c18

Please sign in to comment.