mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-18 19:31:02 +00:00
move Arduino examples -> tutorials
This commit is contained in:
1 parent
2fe7c8a410
commit
72a45d2f6a
42 files changed
+62
-58
No files matched your search
@@ -0,0 +1,102 @@
|
||||
#include "WiFi.h"
|
||||
#include "mongoose.h"
|
||||
|
||||
#define WIFI_SSID "wifi_network_name" // Change this
|
||||
#define WIFI_PASS "wifi_password" // And this
|
||||
|
||||
#define MQTT_SERVER "mqtt://broker.hivemq.com:1883"
|
||||
#define MQTT_SUB_TOPIC "mg/rx" // Subscribe to this topic
|
||||
#define MQTT_PUB_TOPIC "mg/tx" // Publish to this topic
|
||||
|
||||
#define LED_PIN 0
|
||||
|
||||
struct mg_mgr mgr;
|
||||
struct mg_connection *mqtt_connection;
|
||||
|
||||
void mqtt_publish(const char *message) {
|
||||
struct mg_mqtt_opts opts = {};
|
||||
opts.topic = mg_str(MQTT_PUB_TOPIC);
|
||||
opts.message = mg_str(message);
|
||||
if (mqtt_connection) mg_mqtt_pub(mqtt_connection, &opts);
|
||||
}
|
||||
|
||||
// Crude function to get available RAM, for quick profiling
|
||||
size_t getFreeRAM(void) {
|
||||
size_t size = 0, increment = 100;
|
||||
void *p;
|
||||
while ((p = malloc(size)) != NULL) free(p), size += increment;
|
||||
return size;
|
||||
}
|
||||
|
||||
// Implement LED control over MQTT: "on" and "off" commands
|
||||
void handle_command(struct mg_str msg) {
|
||||
if (msg.len == 3 && memcmp(msg.buf, "off", 3) == 0) {
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
mqtt_publish("done - off");
|
||||
} else if (msg.len == 2 && memcmp(msg.buf, "on", 2) == 0) {
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
mqtt_publish("done - on");
|
||||
}
|
||||
MG_INFO(("Free RAM: %u bytes", getFreeRAM()));
|
||||
}
|
||||
|
||||
static void mqtt_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_MQTT_OPEN) {
|
||||
MG_INFO(("%lu CONNECTED to %s", c->id, MQTT_SERVER));
|
||||
struct mg_mqtt_opts opts = {};
|
||||
opts.topic = mg_str(MQTT_SUB_TOPIC);
|
||||
mg_mqtt_sub(c, &opts);
|
||||
MG_INFO(("%lu SUBSCRIBED to %s", c->id, MQTT_SUB_TOPIC));
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
// Received MQTT message
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||
MG_INFO(("%lu RECEIVED %.*s <- %.*s", c->id, (int) mm->data.len,
|
||||
mm->data.buf, (int) mm->topic.len, mm->topic.buf));
|
||||
handle_command(mm->data);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
MG_INFO(("%lu CLOSED", c->id));
|
||||
mqtt_connection = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void reconnect_if_not_connected(void) {
|
||||
if (mqtt_connection == NULL) {
|
||||
struct mg_mqtt_opts opts = {};
|
||||
opts.clean = true;
|
||||
mqtt_connection =
|
||||
mg_mqtt_connect(&mgr, MQTT_SERVER, &opts, mqtt_ev_handler, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
Serial.begin(115200);
|
||||
while (!Serial) delay(50);
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||
while (WiFi.status() != WL_CONNECTED) Serial.print("."), delay(100);
|
||||
|
||||
Serial.println("\nConnected to the WiFi network");
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
mg_mgr_init(&mgr);
|
||||
mg_log_set(MG_LL_DEBUG);
|
||||
mg_log_set_fn([](char ch, void *) { Serial.print(ch); }, NULL);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
mg_mgr_poll(&mgr, 1); // Process network events
|
||||
reconnect_if_not_connected(); // Reconnect to MQTT server if needed
|
||||
}
|
||||
|
||||
extern "C" int lwip_hook_ip6_input(struct pbuf *p, struct netif *inp)
|
||||
__attribute__((weak));
|
||||
extern "C" int lwip_hook_ip6_input(struct pbuf *p, struct netif *inp) {
|
||||
if (ip6_addr_isany_val(inp->ip6_addr[0].u_addr.ip6)) {
|
||||
pbuf_free(p);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
../../../../../mongoose.c
|
||||
@@ -0,0 +1 @@
|
||||
../../../../../mongoose.h
|
||||
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define MG_ARCH MG_ARCH_ESP32
|
||||
@@ -0,0 +1 @@
|
||||
../../../../../mongoose.c
|
||||
@@ -0,0 +1 @@
|
||||
../../../../../mongoose.h
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#define MG_ARCH MG_ARCH_CUSTOM
|
||||
#define MG_ENABLE_SOCKET 0
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_ENABLE_DRIVER_PPP 1
|
||||
#define MG_ENABLE_TCPIP_DRIVER_INIT 0
|
||||
#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0
|
||||
#define MG_ENABLE_CUSTOM_MILLIS 1
|
||||
#define MG_ENABLE_CUSTOM_RANDOM 1
|
||||
#define MG_TLS MG_TLS_BUILTIN
|
||||
#define MG_IO_SIZE 128
|
||||
@@ -0,0 +1,122 @@
|
||||
#include <SoftwareSerial.h>
|
||||
#include "mongoose.h"
|
||||
|
||||
#define MQTT_SERVER "mqtt://broker.hivemq.com:8883"
|
||||
#define MQTT_SUB_TOPIC "mg/rx" // Subscribe to this topic
|
||||
#define MQTT_PUB_TOPIC "mg/tx" // Publish to this topic
|
||||
|
||||
static const char *script[] = {
|
||||
"AT\r\n", "*OK\r\n",
|
||||
"ATZ\r\n", "*OK\r\n",
|
||||
"AT+CPIN?\r\n", "*OK\r\n",
|
||||
"AT+CNMI=0,0,0,0,0\r\n", "*OK\r\n",
|
||||
"AT+CGDCONT=1,\"IP\",\"iot.1nce.net\"\r\n", "*OK\r\n",
|
||||
"AT+CGDATA=\"PPP\",1\r\n", "*CONNECT\r\n",
|
||||
NULL,
|
||||
};
|
||||
|
||||
// Visit https://mongoose.ws/tls/ and copy-paste CA certificate for your server
|
||||
#define TLS_CA \
|
||||
""
|
||||
|
||||
// We use Serial1 or software serial to communicate with the modem
|
||||
#define LED_PIN LED_BUILTIN
|
||||
#define RST_PIN 10
|
||||
#define ModemSerial Serial1
|
||||
//#define RX_PIN 9
|
||||
//#define TX_PIN 8
|
||||
//SoftwareSerial ModemSerial(RX_PIN, TX_PIN);
|
||||
|
||||
struct mg_connection *mqtt_connection;
|
||||
struct mg_tcpip_driver_ppp_data driver_data;
|
||||
struct mg_mgr mgr; // Mongoose event manager
|
||||
struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}}; // Network interface
|
||||
|
||||
uint64_t mg_millis(void) {
|
||||
return millis();
|
||||
}
|
||||
bool mg_random(void *buf, size_t len) { // For TLS
|
||||
uint8_t *p = (uint8_t *) buf;
|
||||
while (len--) *p++ = (unsigned char) (rand() & 255);
|
||||
return true;
|
||||
}
|
||||
|
||||
void mqtt_publish(const char *message) {
|
||||
struct mg_mqtt_opts opts = {};
|
||||
opts.topic = mg_str(MQTT_PUB_TOPIC);
|
||||
opts.message = mg_str(message);
|
||||
if (mqtt_connection) mg_mqtt_pub(mqtt_connection, &opts);
|
||||
}
|
||||
|
||||
void handle_command(struct mg_str msg) {
|
||||
if (msg.len == 3 && memcmp(msg.buf, "off", 3) == 0) {
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
mqtt_publish("done - off");
|
||||
} else if (msg.len == 2 && memcmp(msg.buf, "on", 2) == 0) {
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
mqtt_publish("done - on");
|
||||
}
|
||||
}
|
||||
|
||||
static void mqtt_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_CONNECT && mg_url_is_ssl(MQTT_SERVER)) {
|
||||
struct mg_tls_opts opts = {};
|
||||
opts.ca = mg_str(TLS_CA);
|
||||
mg_tls_init(c, &opts);
|
||||
}
|
||||
if (ev == MG_EV_MQTT_OPEN) {
|
||||
MG_INFO(("%lu CONNECTED to %s", c->id, MQTT_SERVER));
|
||||
struct mg_mqtt_opts opts = {};
|
||||
opts.topic = mg_str(MQTT_SUB_TOPIC);
|
||||
mg_mqtt_sub(c, &opts);
|
||||
MG_INFO(("%lu SUBSCRIBED to %s", c->id, MQTT_SUB_TOPIC));
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
// Received MQTT message
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||
MG_INFO(("%lu RECEIVED %.*s <- %.*s", c->id, (int) mm->data.len,
|
||||
mm->data.buf, (int) mm->topic.len, mm->topic.buf));
|
||||
handle_command(mm->data);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
MG_INFO(("%lu CLOSED", c->id));
|
||||
mqtt_connection = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void reconnect_if_not_connected(void) {
|
||||
if (mif.state == MG_TCPIP_STATE_READY && mqtt_connection == NULL) {
|
||||
struct mg_mqtt_opts opts = {};
|
||||
opts.clean = true;
|
||||
mqtt_connection =
|
||||
mg_mqtt_connect(&mgr, MQTT_SERVER, &opts, mqtt_ev_handler, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200); // Initialise serial for logs
|
||||
while (!Serial) delay(50); // for debug output
|
||||
|
||||
pinMode(LED_PIN, OUTPUT); // Initialise pins
|
||||
pinMode(RST_PIN, OUTPUT);
|
||||
digitalWrite(RST_PIN, HIGH);
|
||||
//pinMode(RX_PIN, INPUT); // Only required for
|
||||
//pinMode(TX_PIN, OUTPUT); // software serial
|
||||
ModemSerial.begin(115200); // Serial for modem communication
|
||||
|
||||
mg_mgr_init(&mgr); // Initialise Mongoose event manager
|
||||
mg_log_set(MG_LL_DEBUG); // Set debug log level
|
||||
mg_log_set_fn([](char c, void *) { Serial.print(c); }, NULL); // Log serial
|
||||
|
||||
mif.driver = &mg_tcpip_driver_ppp; // Initialise built-in TCP/IP stack
|
||||
mif.driver_data = &driver_data; // with the cellular driver
|
||||
driver_data.script = script;
|
||||
driver_data.reset = [](void *) { digitalWrite(RST_PIN, LOW); delay(1); digitalWrite(RST_PIN, HIGH); },
|
||||
driver_data.tx = [](void *, uint8_t c) { ModemSerial.write(c); },
|
||||
driver_data.rx = [](void *) { return ModemSerial.available() ? ModemSerial.read() : -1; },
|
||||
mg_tcpip_init(&mgr, &mif);
|
||||
mif.enable_dhcp_client = false;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
mg_mgr_poll(&mgr, 1); // Process network events
|
||||
reconnect_if_not_connected(); // Reconnect to MQTT server if needed
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
../../../../../mongoose.c
|
||||
@@ -0,0 +1 @@
|
||||
../../../../../mongoose.h
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#define MG_ARCH MG_ARCH_CUSTOM
|
||||
#define MG_ENABLE_SOCKET 0
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_ENABLE_DRIVER_W5500 1
|
||||
#define MG_ENABLE_TCPIP_DRIVER_INIT 0
|
||||
#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0
|
||||
#define MG_ENABLE_CUSTOM_MILLIS 1
|
||||
#define MG_ENABLE_CUSTOM_RANDOM 1
|
||||
// #define MG_TLS MG_TLS_BUILTIN
|
||||
#define MG_IO_SIZE 128
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
#include <SPI.h>
|
||||
#include "mongoose.h"
|
||||
|
||||
#define MQTT_SERVER "mqtt://broker.hivemq.com:1883"
|
||||
#define MQTT_SUB_TOPIC "mg/rx" // Subscribe to this topic
|
||||
#define MQTT_PUB_TOPIC "mg/tx" // Publish to this topic
|
||||
#define SS_PIN 2 // Slave select pin
|
||||
#define LED_PIN 21 // LED pin
|
||||
|
||||
struct mg_connection *mqtt_connection;
|
||||
struct mg_tcpip_spi spi = {
|
||||
NULL, // SPI metadata
|
||||
[](void *) { digitalWrite(SS_PIN, LOW); SPI.beginTransaction(SPISettings()); },
|
||||
[](void *) { digitalWrite(SS_PIN, HIGH); SPI.endTransaction(); },
|
||||
[](void *, uint8_t c) { return SPI.transfer(c); }, // Execute transaction
|
||||
};
|
||||
struct mg_mgr mgr; // Mongoose event manager
|
||||
struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}}; // Network interface
|
||||
|
||||
uint64_t mg_millis(void) {
|
||||
return millis();
|
||||
}
|
||||
bool mg_random(void *buf, size_t len) { // For TLS
|
||||
uint8_t *p = (uint8_t *) buf;
|
||||
while (len--) *p++ = (unsigned char) (rand() & 255);
|
||||
return true;
|
||||
}
|
||||
|
||||
void mqtt_publish(const char *message) {
|
||||
struct mg_mqtt_opts opts = {};
|
||||
opts.topic = mg_str(MQTT_PUB_TOPIC);
|
||||
opts.message = mg_str(message);
|
||||
if (mqtt_connection) mg_mqtt_pub(mqtt_connection, &opts);
|
||||
}
|
||||
|
||||
// Crude function to get available RAM, for quick profiling
|
||||
size_t getFreeRAM(void) {
|
||||
size_t size = 0, increment = 100;
|
||||
void *p;
|
||||
while ((p = malloc(size)) != NULL) free(p), size += increment;
|
||||
return size;
|
||||
}
|
||||
|
||||
// Implement LED control over MQTT: "on" and "off" commands
|
||||
void handle_command(struct mg_str msg) {
|
||||
if (msg.len == 3 && memcmp(msg.buf, "off", 3) == 0) {
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
mqtt_publish("done - off");
|
||||
} else if (msg.len == 2 && memcmp(msg.buf, "on", 2) == 0) {
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
mqtt_publish("done - on");
|
||||
}
|
||||
MG_INFO(("Free RAM: %u bytes", getFreeRAM()));
|
||||
}
|
||||
|
||||
static void mqtt_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_MQTT_OPEN) {
|
||||
MG_INFO(("%lu CONNECTED to %s", c->id, MQTT_SERVER));
|
||||
struct mg_mqtt_opts opts = {};
|
||||
opts.topic = mg_str(MQTT_SUB_TOPIC);
|
||||
mg_mqtt_sub(c, &opts);
|
||||
MG_INFO(("%lu SUBSCRIBED to %s", c->id, MQTT_SUB_TOPIC));
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
// Received MQTT message
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||
MG_INFO(("%lu RECEIVED %.*s <- %.*s", c->id, (int) mm->data.len,
|
||||
mm->data.buf, (int) mm->topic.len, mm->topic.buf));
|
||||
handle_command(mm->data);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
MG_INFO(("%lu CLOSED", c->id));
|
||||
mqtt_connection = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void reconnect_if_not_connected(void) {
|
||||
if (mif.state == MG_TCPIP_STATE_READY && mqtt_connection == NULL) {
|
||||
struct mg_mqtt_opts opts = {};
|
||||
opts.clean = true;
|
||||
mqtt_connection =
|
||||
mg_mqtt_connect(&mgr, MQTT_SERVER, &opts, mqtt_ev_handler, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200); // Initialise serial
|
||||
while (!Serial) delay(50); // for debug output
|
||||
|
||||
SPI.begin(); // Iniitialise SPI
|
||||
pinMode(SS_PIN, OUTPUT); // to communicate with W5500 Ethernet module
|
||||
pinMode(LED_PIN, OUTPUT); // Initialise LED
|
||||
|
||||
mg_mgr_init(&mgr); // Initialise Mongoose event manager
|
||||
mg_log_set(MG_LL_DEBUG); // Set debug log level
|
||||
mg_log_set_fn([](char ch, void *) { Serial.print(ch); }, NULL); // Log serial
|
||||
mif.driver = &mg_tcpip_driver_w5500; // Use W5500 built-in driver
|
||||
mif.driver_data = &spi; // Pass SPI interface to W5500 driver
|
||||
mg_tcpip_init(&mgr, &mif); // Initialise built-in TCP/IP stack
|
||||
}
|
||||
|
||||
void loop() {
|
||||
mg_mgr_poll(&mgr, 1); // Process network events
|
||||
reconnect_if_not_connected(); // Reconnect to MQTT server if needed
|
||||
}
|
||||
Reference in new issue
Block a user