SOLVED**
Background: HTTPclient http; was in a loop. It had to be global.
I wanted to have a program listen for UDP packet of a certain code and then GET a webpage (to sound an alarm). I pieced together almost everything from NodeMCUs "Authorization" sketch into my UDP sketch. It says 'http' is not declared. It's not my includes, I ruled those out already.
What is wrong?
#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 = "";
int ix;
ESP8266WiFiMulti WiFiMulti;
WiFiUDP Udp;
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", "crystal2965");
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) {
int codei = atoi(code);
switch (codei) {
case 7004: openWeb(); //magnet break
}
rPacket = "R";
rPacket = strcat(rPacket, code);
// send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(rPacket);
Udp.endPacket();
}
void loop()
{
char* pack;
char* token;
if ((WiFiMulti.run() == WL_CONNECTED)) {
HTTPClient http;
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://mat00gs@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();
}