I want to find an authorization sketch I can add into an existing sketch that handles UDP packets and prints them to serial. The auth type is user:pass@http.com style.
Did you look at the "SimpleAuthentification" example that comes with the ESP8266 core?
Yeah I think, Authorization under HTTP Client. When I tried to merge it into my UDP stuff it threw an exception and I can’t decode it, tried but decoder just sits there. Tried the Auth client alone and it doesn’t crash. HERE is the code for that.
#include <string.h>
#include <stdio.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#define USE_SERIAL Serial
char* rPacket;
const char* ssid = "JMR";
const char* password = "crystal2965";
int ix;
ESP8266WiFiMulti WiFiMulti;
WiFiUDP Udp;
HTTPClient http;
long delayMS;
unsigned int localUdpPort = 8888; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char replyPacket[] = "Hi there! Got the message :-)"; // a reply string to send back
const char newLine[2] = "\n";
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for (uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("JMR", "crl2965");
IPAddress ip(192, 168, 1, 139);
IPAddress gw(192, 168, 1, 254);
IPAddress dns(192, 168, 1, 254);
IPAddress sn(255, 255, 255, 0);
WiFi.config(ip, gw, sn, dns);
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}
void switchCode2(char* code) {
Serial.println(code);
int codei = atoi(code);
switch (codei) {
case 7004: openWeb(); //magnet break
}
}
void loop()
{
char* pack;
char* token;
if ((WiFiMulti.run() == WL_CONNECTED)) {
USE_SERIAL.print("[HTTP] begin...\n");
int packetSize = Udp.parsePacket();
if (packetSize)
{
// receive incoming UDP packets
//Serial.println("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
pack = incomingPacket;
Serial.println(incomingPacket);
//Packet Parse
//Split packet into codes
token = strtok(pack, newLine);
switchCode2(token);
while ( token != NULL ) {
token = strtok(NULL, newLine);
switchCode2(token);
}
if (millis() - delayMS > 1000) Serial.println(9102);
}
}
}
void openWeb() {
USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
http.begin("http://matt:0gs@192.168.1.71:15080/admin?profile=6");
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
I don't know why it is showcasing multiwifi as well. Think point of an example is to focus on one thing. Wifi.h is much more simpler.