Skip to content

Commit 79f31d7

Browse files
authored
Add Class OTA (Ameba-AIoT#274)
* Add Class OTA - Update API: change header OTA to Class OTA * Update ota_drv.c - removed default server port and ip address
1 parent 740b05c commit 79f31d7

File tree

6 files changed

+49
-29
lines changed

6 files changed

+49
-29
lines changed

Arduino_package/hardware/libraries/OTA/examples/OTA/OTA.ino

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
#include "ota_thread.h"
1+
#include "OTA.h"
22
#include "WiFi.h"
33

44

5-
char ssid[] = "Network_SSID"; // your network SSID (name)
6-
char pass[] = "Password"; // your network password
5+
char ssid[] = "Network_SSID5"; // your network SSID (name)
6+
char pass[] = "Password"; // your network password
77
int status = WL_IDLE_STATUS;
88

9+
int port = 3000; // your server port number
10+
char* server = "192.168.3.14"; // your server ip address
11+
12+
OTA ota;
13+
914
void setup()
1015
{
1116
Serial.begin(115200);
@@ -19,7 +24,7 @@ void setup()
1924
}
2025

2126
// Set up the threads
22-
start_OTA_threads();
27+
ota.start_OTA_threads(port, server);
2328
}
2429

2530
void loop()

Arduino_package/hardware/libraries/OTA/src/ota_thread.cpp renamed to Arduino_package/hardware/libraries/OTA/src/OTA.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#define ARDUINOJSON_STRING_LENGTH_SIZE_4
22

33
#include <Arduino.h>
4-
#include "ota_thread.h"
4+
#include "OTA.h"
55
#include <WiFi.h>
66
#include <ArduinoJson.h>
77

@@ -16,17 +16,20 @@ char buffer[1024];
1616
uint32_t thread1_id, thread2_id, thread3_id, stack_size1, stack_size2;
1717
int priority1;
1818

19+
OTA::OTA(){};
1920

20-
void sendPostRequest()
21+
OTA::~OTA(){};
22+
23+
void OTA::sendPostRequest()
2124
{
2225
doc["OTA_state"] = g_otaState;
2326
serializeJson(doc, jsonString);
2427
WiFiClient wifiClient;
2528

26-
if (wifiClient.connect(server, PORT)) {
29+
if (wifiClient.connect(_server, _port)) {
2730
// Send POST request
2831
wifiClient.println("POST /api/connectedclients HTTP/1.1");
29-
wifiClient.println("Host: " + String(server));
32+
wifiClient.println("Host: " + String(_server));
3033
wifiClient.println("Content-Type: application/json"); // Use appropriate content type
3134
wifiClient.println("Content-Length: " + String(jsonString.length())); // Specify the length of the content
3235
wifiClient.println("Connection: keep-alive");
@@ -40,14 +43,14 @@ void sendPostRequest()
4043
delay(3000);
4144
}
4245

43-
void thread1_task(const void *argument)
46+
void OTA::thread1_task(const void *argument)
4447
{
4548
while (1) {
4649
sendPostRequest();
4750
}
4851
}
4952

50-
void thread2_task(const void *argument)
53+
void OTA::thread2_task(const void *argument)
5154
{
5255
WiFiServer server(5000);
5356
server.begin();
@@ -79,8 +82,10 @@ void thread2_task(const void *argument)
7982
}
8083

8184

82-
void start_OTA_threads()
85+
void OTA::start_OTA_threads(int port, char *server)
8386
{
87+
_port = port;
88+
_server = server;
8489
priority1 = osPriorityNormal;
8590
stack_size1 = 1024;
8691
thread1_id = os_thread_create_arduino(thread1_task, NULL, priority1, stack_size1);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef OTA_H
2+
#define OTA_H
3+
4+
#include <Arduino.h>
5+
#include <httpd/httpd.h>
6+
#include <WiFi.h>
7+
8+
class OTA {
9+
public:
10+
OTA(void);
11+
~OTA(void);
12+
13+
// To start OTA firmware update process via HTTP
14+
void start_OTA_threads(int port, char *server);
15+
16+
17+
private:
18+
static void thread1_task(const void *argument);
19+
static void thread2_task(const void *argument);
20+
21+
static void sendPostRequest();
22+
};
23+
#endif // OTA_H

Arduino_package/hardware/libraries/OTA/src/ota_drv.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <ota_8735b.h>
66
#include "ota_drv.h"
77

8-
const int PORT = 3000; // MODIFY THIS FOR YOUR HTTP SERVER PORT
9-
const char *server = "192.168.3.4"; // MODIFY THIS FOR YOUR HTTP SERVER IP ADDRESS
8+
int _port;
9+
char *_server;
1010
const char *resource = "api/uploadfile"; // DO NOT MODIFY
1111

1212

@@ -26,7 +26,7 @@ void http_update_ota_task(void *param)
2626

2727
g_otaState = OtaState[2];
2828

29-
ret = http_update_ota((char *)server, PORT, (char *)resource);
29+
ret = http_update_ota((char *)_server, _port, (char *)resource);
3030

3131
g_otaState = OtaState[3];
3232

Arduino_package/hardware/libraries/OTA/src/ota_drv.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
extern const int PORT;
4-
extern const char *server;
3+
extern int _port;
4+
extern char *_server;
55
extern const char *resource;
66

77
extern const char *OtaState[];

Arduino_package/hardware/libraries/OTA/src/ota_thread.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)