Interface and Application Programming¶
Group assignment:
- Compare as many tool options as possible.
- Document your work on the group work page and reflect on your individual page what you learned.
Individual assignment:
- Write an application that interfaces a user with an input and/or output device(s) on a board that you made.
Learning outcomes¶
- Implement a User Interface (UI) using programming and explore protocols to communicate with an embedded board.
As usual here is my timetable:
Group Assignment¶
Reflection
This week, we learned how to create apps and interfaces for microcontroller boards. Interfaces help hardware and software talk to each other smoothly. We tried out tools like web servers and MIT App Inventor in our group project. With an ESP32 board, LEDs, and a servo, we made a web server that works on phones and lets you control things from a browser. I figured out how to set up networks, put code on the board, and fix problems. It’s cool to see how tech can make things easy to use and work together nicely.
Click here to view our group assignment
Individual Assignment¶
Hero shot!!
This week’s assignment was to make an application that interfaces a user with an input and/ or output device that we made. So I will be doing a part of my final project for this week’s asssignment and also control a servo motor through the webserver I will be hosting.
SO let’s start with taking in inputs from the webserver and displaying a random topic upon pressing the button. I only learnt this week that I cannot store data from the webserver so I will be adding a sd card module for my final project.
SO this code is basically an edited version from the previous display I did on the serial monitor. Here is the code:
#include <Arduino.h>
#include <WiFi.h> // Use WiFi.h library for ESP32-C3
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
AsyncWebServer server(80);
// REPLACE WITH YOUR NETWORK CREDENTIALS
const char* ssid = "your-ssid";
const char* password = "your-ssid-password";
const char* PARAM_INPUT = "input";
// OLED settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Button pin
#define BUTTON_PIN 3
// Define maximum number of input messages to store
#define MAX_MESSAGES 10
// Array to store input messages
String messages[MAX_MESSAGES];
// Variables to track number of stored messages
int numMessages = 0;
bool buttonPressed = false;
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
void setup() {
Serial.begin(115200);
Wire.begin();
pinMode(BUTTON_PIN, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.display();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult()!= WL_CONNECTED) {
Serial.println("WiFi Failed!");
return;
}
Serial.println();
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Send web page with input fields to client
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/html", "<form action='/get' method='get'>"
"Input: <input type='text' name='input'><br>"
"<input type='submit' value='Submit'></form>");
});
// Send a GET request to <ESP_IP>/get?input=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
// Get input message and store it
if (request->hasParam(PARAM_INPUT)) {
storeMessage(request->getParam(PARAM_INPUT)->value());
}
// Send response to client without displaying the message
request->send(200, "text/html", "Data received. Press the button to display.");
});
server.onNotFound(notFound);
server.begin();
}
void loop() {
// Check if the button is pressed
if (digitalRead(BUTTON_PIN) == LOW) {
buttonPressed = false;
delay(1000); // Add debounce delay if needed
}
// Display random message only if the button is pressed
if (digitalRead(BUTTON_PIN) == HIGH) {
displayRandomMessage();
buttonPressed = true; // Reset buttonPressed flag after displaying message
}
}
void storeMessage(String message) {
// Check if there's still space in the array to store the message
if (numMessages < MAX_MESSAGES) {
// Store the message in the array
messages[numMessages++] = message;}
}
void displayRandomMessage() {
if (buttonPressed && numMessages > 0) {
int randomIndex = random(0, numMessages);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Message:");
display.println(messages[randomIndex]);
display.display();
buttonPressed = true; // Reset buttonPressed flag after displaying message
} else {
Serial.println("No messages to display.");
}
}
Code Explanation:
SO the webserver was setup using basic HTML coding. It inlcudes a form (‘