I'm building an individually addressable led lamp, as the controller I'm using an ESP-01 (ESP8266). As the primary "controlling environment", I want to use a mobile app (or website), and secondary an IR controller. With IR it's no problem, but the mobile app...
I've got it to receive commands from MIT AppInventor2 by connecting a WiFi element to ESP's local address with a command behind a slash (and optionally a value) http://x.x.x.x/command/value/
and read it using String request = client.readStringUntil('\n');
. But I find the MIT AI2 very limiting. I've found FlutterFlow, which would be way better, but when I try to make an API call there, it returns an error.
Any suggestions on what might be wrong? Does it need to be https
for FlutterFlow? Is it a problem with it being only local?
Here is the code (I wrote it into a custom library, which might have been a waste of time...):
- As an PlatformIO project: myWiFiAPI_test.zip (2.7 MB)
- As code:
main.cpp:
//Include libraries
#include <Arduino.h>
#include <myWifi.h>
//Pin Config
#define PIN_STATUS_LED 2 // Builtin led used to display status
//CONFIG
#define BAUDRATE 74880 // Baudrate for Hardware Serial
#define LOOP_TIME 100 // Run loop() every [x] ms
#define BEHIND_LOOP_WARN 40 // If target loop time is [x] ms behind millis(), print warning to Serial
#define MY_SSID "O2-Internet-826" // SSID of the wifi network
#define MY_PASS "bA6RfeFT" // Password of the wifi network
//Define variables, etc
std::map<String, std::function<void(int)>> callbacks = {
{"/def/", [](int value){ Serial.println(F("Connected to def")); }},
};
myWiFi wifi(MY_SSID, MY_PASS, IPAddress(10,0,0,58), callbacks);
void setup()
{
Serial.begin(BAUDRATE);
delay(100);
Serial.println('\n');
wifi.connect();
}
void loop(){
wifi.update();
}
myWiFi.h:
#ifndef WIFI_H
#define WIFI_H
#include "Arduino.h"
#include "map"
#include "ESP8266WiFi/src/ESP8266WiFi.h"
/*Usage:
myWiFi wifi(SSID, PASS, IPAddress, ipCallbacks, connectionUpdateCallback);
wifi.connect(); once in setup
wifi.update(); often in loop*/
class myWiFi {
public:
myWiFi(String SSID, String PASS, IPAddress IP,
std::map<String, std::function<void(int)>> ipCallbacks);
~myWiFi();
void connect();
void update();
protected:
String SSID;
String PASS;
IPAddress IP;
IPAddress GATEWAY;
IPAddress SUBNET;
std::map<String, std::function<void(int)>> ipCallbacks;
};
#endif //WIFI_H
myWiFi.cpp:
#include "myWifi.h"
WiFiServer ESPserver(80); // Service Port
myWiFi::myWiFi(String SSID, String PASS, IPAddress IP,
std::map<String, std::function<void(int)>> ipCallbacks) {
this->SSID = SSID;
this->PASS = PASS;
this->IP = IP;
GATEWAY = IPAddress(192,168,1,1);
SUBNET = IPAddress(255,255,255,0);
this->ipCallbacks = ipCallbacks;
}
myWiFi::~myWiFi() {
}
void myWiFi::connect() {
Serial.printf_P((const char *)F("Connecting to: %s with a static IP: %s\n"), SSID.c_str(), IP.toString());
WiFi.config(IP, GATEWAY, SUBNET);
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(150);
Serial.print(F("*"));
}
ESPserver.begin();
Serial.printf_P((const char *)F("\nWifi connected, server started at http://%s/\n\n"), WiFi.localIP().toString());
}
void myWiFi::update() {
if (WiFi.status() != WL_CONNECTED) {
return;
}
// Check if a client has connected
WiFiClient client = ESPserver.accept();
if (!client) {
return;
}
// Wait until the client sends some data
int i = 0;
while(!client.available()) {
i++;
delay(50);
if(i > 10) {
break;
}
}
if (!client.available()) {
return;
};
// Read the first line of the request
String request = client.readStringUntil('\n');
client.flush();
Serial.println(request);
// Match the request
request.remove(0,4);
request.remove(request.length()-10,10);
int value = 0;
int index = request.lastIndexOf("/")+1;
try {
value = request.substring(index).toInt();
} catch (const std::exception& e) { value = 0; }
request.remove(index, request.length()-index);
if (this->ipCallbacks.find(request) != this->ipCallbacks.end()) {
this->ipCallbacks[request](value);
}
// Return the response
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println(""); // IMPORTANT
client.print(F("Request recieved!"));
delay(1);
}
Thanks a lot for your help, Viktor