Hi again, sorry I'm asking so many questions
I'm trying to get a status update (provided by my Arduino Mega via softSerial) to appear on a web page run from my Arduino Uno WiFi rev2. This is my current code:
#include <SPI.h>
#include <WiFiNINA.h>
#include <SoftwareSerial.h>
char ssid[] = "REDACTED";
char pass[] = "REDACTED";
int keyIndex = 0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
SoftwareSerial softSerial(9, 8);
int LED = 2;
void printWifiStatus() {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
server.begin();
printWifiStatus();
softSerial.begin(9600);
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
WiFiClient client = server.available();
if (softSerial.available()) {
char number = softSerial.read();
Serial.println(number);
if (number == '0') {
digitalWrite(LED, LOW);
char status[9] = "activated";
Serial.println(status);
}
else if (number == '1') {
digitalWrite(LED, HIGH);
char status[11] = "deactivated";
Serial.println(status);
}
else
char status[7] = "offline";
Serial.println(status);
}
if (client) {
Serial.println("new client");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("Current Status:");
client.print(status);
client.print("<br>");
client.print("Click <a href=\"/H\">here</a> to activate the alarm<br>");
client.print("Click <a href=\"/L\">here</a> to deactivate the alarm<br>");
client.println();
break;
}
else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
if (currentLine.endsWith("GET /H")) {
Serial.println("H");
}
if (currentLine.endsWith("GET /L")) {
Serial.println("L");
}
}
}
}
}
When I run it, the Status update does not appear on either the serial monitor or the web page however instead the webpage says Current Status:3
I have no idea where this is coming from or why I'm not getting a status update so if anyone has any ideas please let me know.
Many Thanks
the latter does not survive the if() statement in which it's defined, so outside the if, you go back to the global status which (if WiFi was connected successfuly) is WL_CONNECTED (which is 3)