echo '' ;

Archives

ESP8266 Mongoose-OS Interface – Button

Code

 

// https://www.aruneworld.com/
// Tested By  : Arun(20170430)
// Example Name : AEW_Button_Message.js
// Firmware  : Mongoose OS for ESP32
//*******************************//

// This example demonstrates how to react on a button press
// by printing a message on a console.
//
// To try this example,
//   1. Download <code>{{EJS0}}</code> tool from https://mongoose-os.com/software.html
//   2. Run <code>{{EJS1}}</code> tool and install Mongoose OS
//   3. In the UI, navigate to the <code>{{EJS2}}</code> tab and load this example

// Load Mongoose OS API
load('api_gpio.js');

let pin = 0;   // GPIO 0 is typically a 'Flash' button
GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function(x) {
  print('Button press, pin: ', x);
}, true);

print('Flash button is configured on GPIO pin ', pin);
print('Press the flash button now!');

 


Next :

Previous :


 

ESP8266 Mongoose-OS Tutorials – TCP Web Server

Welcome to the ESP8266 Mongoose-OS Tutorials on the TCP Web Server. In this series of tutorials, we will explore how to create a TCP Web Server using the ESP8266 microcontroller and the Mongoose-OS firmware.

The ESP8266 is a powerful microcontroller known for its built-in WiFi capability, making it suitable for IoT (Internet of Things) projects. Mongoose-OS is an open-source firmware for microcontrollers that provides a development environment for building IoT applications. “ESP8266 Mongoose OS Tutorials TCP Web Server”

In this tutorial series, we will demonstrate how to create a TCP Web Server using Mongoose-OS on the ESP8266. We will cover topics such as setting up the development environment, writing code to create a TCP server, handling incoming connections, and sending data over the network.

Whether you’re a beginner or an experienced developer, these tutorials will provide you with the knowledge and skills to create your own TCP Web Server on the ESP8266 using Mongoose-OS.

Let’s get start!

TCP Web Server Uses

UseDescription
Data LoggingLogging data received from clients, such as sensor readings or system status updates, for later analysis or archival purposes.
Remote MonitoringProviding remote access to real-time data, allowing users to monitor and control devices or systems from anywhere with an internet connection.
Home AutomationControlling smart home devices remotely, such as lights, thermostats, or security cameras, to manage home environments more conveniently.
Industrial ControlMonitoring and controlling machinery, production processes, or environmental conditions in industrial settings, enabling remote access for maintenance.
IoT ApplicationsExchanging data between connected IoT devices, gathering sensor data from remote locations, or controlling IoT devices remotely.
CommunicationFacilitating communication between devices on a network, allowing them to exchange data, synchronize operations, or coordinate tasks.
Remote ConfigurationRemotely configuring devices or systems, updating settings, adjusting parameters, or performing firmware upgrades without physical access to the device.
Real-Time NotificationsSending real-time notifications to connected clients, such as alerts, alarms, or status updates, to keep users informed of important events.
Custom ApplicationsBuilding custom applications tailored to specific needs, such as remote control interfaces, monitoring dashboards, or interactive user interfaces.
Distributed SystemsIntegrating into distributed systems or networked applications to enable communication and data exchange between multiple nodes or components.

Code

// https://www.aruneworld.com/embedded/espressif/esp8266/esp8266_mongoose-os/
// Tested By : Arun(20170430)
// Example Name : AEW_TCP_Web_Server.js
// Firmware : Mongoose OS for ESP32

//*******************************//

// This example demonstrates how to create a TCP echo server.
//
// To try this example,
// 1. Download mos tool from https://mongoose-os.com/software.html
// 2. Run mos tool and install Mongoose OS
// 3. In the UI, navigate to the Examples tab and load this example

// Load Mongoose OS API
load('api_net.js');

let port = '1234';

Net.bind(port, function(conn, ev, ev_data) {
    if (ev !== Net.EV_ACCEPT) return;
    Net.send(conn, JSON.stringify({a: 1, b: 'hey!'}));
    Net.close(conn);
}, true);

print('TCP server is listening on port ', port);

Code Explanation

Line(s)Explanation
1-4Comments providing information about the source of the code, the tester, example name, and firmware used.
7Load the Mongoose OS API module to enable network functionality.
9Define the port number (e.g., ‘1234’) on which the TCP server will listen for incoming connections.
11-18Bind a TCP server to the specified port. When a connection is accepted, send a JSON-encoded message {a: 1, b: 'hey!'} to the client and close the connection.
15Check if the event received is an accept event.
16Send a JSON-encoded message {a: 1, b: 'hey!'} to the client.
17Close the connection after sending the message.
19Print a message indicating that the TCP server is listening on the specified port.

NEXT

MongooseOS
Mongoose-OS Interface
ESP8266 MongooseOS Interface LED
ESP8266 MongooseOS Interface Button
Mongoose OS Tutorials
ESP8266 MongooseOS Tutorials Web Server
Others
ESP8266 MongooseOS All Post

ESP8266 Mongoose-OS Interface – LED

To turn on the led you need to load the gpio_api.js file.


LED Turn ON/OFF

Note : This below example was tested mongoose os with java script app demo-js in ESP8266 NodeMCU Dev Board(ESP-12E)

  • This below example turn on the LED GPIO-4 (NodeMCU Dev Board digital pin 2)
  • Set the GPIO pin mode as OUPUT uisng gpio set mode function ex: GPIO.set_mode(led, GPIO.MODE_OUTPUT)
  • Turn off the LED call the gpio write function with low-0 ex : GPIO.write(led, 0) .
  • Turn on the LED call the gpio write function with low-1 ex : GPIO.write(led, 1) .

Code

load('api_gpio.js');
 
// Configure LED
let led = 4; //lde pin GPIO04
GPIO.set_mode(led, GPIO.MODE_OUTPUT);
GPIO.write(led,1);

 


LED turn ON/OFF using button

  • Use Boot button(GPIO-0)
  • The example code used blink in-build LED, It’s connected to GPIO-10.
  • You can change any other pin Ex : led = 4; // Get LED GPIO-04 pin; 
  • Note : Code 1 and Two are same application and functionality. Main difference is calling function.

Code 1 :

  • Used GPIO-0 pin (Boot button in NodeMCU Dev Board), as interrupt and used Button interrupt handler function.
// http://www.ArunEworld.com/Embedded/ESPressif/ESP8266/ESP8266_Mongoose-os/
// Tested By  : Arun(20170430)
// Example Name : AEW_LED-Blink_Using_Button.js
// Firmware  : Mongoose OS for ESP32
//*******************************//
// This example demonstrates how to react on a button press by toggling LED.
//
// To try this example,
//   1. Download <code>{{EJS7}}</code> tool from https://mongoose-os.com/software.html
//   2. Run <code>{{EJS8}}</code> tool and install Mongoose OS
//   3. In the UI, navigate to the <code>{{EJS9}}</code> tab and load this example

// Load Mongoose OS API
load('api_gpio.js');

// Configure LED
let led = ffi('int get_led_gpio_pin()')();  // Get built-in LED GPIO pin
GPIO.set_mode(led, GPIO.MODE_OUTPUT);

let pin = 0;   // GPIO 0 is typically a 'Flash' button
GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function(x) {
  let value = GPIO.toggle(led);
  print('Button press, pin:', x, 'LED pin:', led, ' Value: ', value);
}, true);

print('Flash button is configured on GPIO pin ', pin);
print('Press the flash button now!');

 

Code 2:

  • In this code i was used GPIO-0 pin (Boot button in NodeMCU dev Kit) as interrupt and used interrupt handler function.
  • If your using interruot handler function then you need o enable the interrupt using GPIO.enable_int(pin)  function
load('api_gpio.js');

GPIO.set_mode(4, 1);
//GPIO.write(10, 0);


GPIO.set_int_handler(0, GPIO.INT_EDGE_NEG, function(pin) {
   print('Pin', 0, 'got interrupt');
   GPIO.toggle(4);
   
}, null);
GPIO.enable_int(0);