Hi.
I made a topic a few days ago with a code I made many years ago that stopped working suddenly, instead of fixing it, I tried to make a new one now based on another webserver example I found.
Basic premise:
Webserver hooked to a radio transmitter, when you press the links, it broadcasts a signal to a wireless powerplug.
What I am trying to do now:
Add more if statements to the code, but when I add "if statements" I get an error, so I think my formatting is wrong.
The base code (which works without errors:) (But only the first 2 links (1-on/1-off) work
#include "SPI.h"
#include "Ethernet.h"
#include <NewRemoteTransmitter.h>
const unsigned long TRANSMITTER_ADDRESS = 27612394; //My personal transmitter address
const int PIN_TRANSMITTER = 8;
NewRemoteTransmitter transmitter(TRANSMITTER_ADDRESS, PIN_TRANSMITTER, 260, 3);
uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
uint8_t ip[] = { 192,168,1,50 }; // IP Address
EthernetServer server(80); // Server Port 80
// RemoteTransmitter configuration
// Connect Transmitter to Pin 11 and receiver to pin 2 (all digital)
// FAN 1 ON/OFF:
// Addr 27612394 unit 0 on, period: 267us.
// Addr 27612394 unit 0 off, period: 267us.
// FAN 2 ON/OFF:
// Addr 27612394 unit 1 on, period: 266us.
// Addr 27612394 unit 2 off, period: 267us.
// FAN 3 ON/OFF:
// Addr 27612394 unit 2 on, period: 267us.
// Addr 27612394 unit 1 off, period: 266us.
// ALL POWER OFF:
// Addr 27612394 group off, period: 267us.
String controlString; // Captures out URI querystring;;
int blueLEDPin = 2; // pin where our blue LED is connected
void setup(){
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(3, HIGH);
Ethernet.begin(mac, ip);
server.begin();
pinMode(PIN_TRANSMITTER, OUTPUT);
Serial.begin(9600);
Serial.print("Serial Write turned on");
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read the HTTP request
if (controlString.length() < 100) {
// write characters to string
controlString += c;
}
//if HTTP request has ended– 0x0D is Carriage Return \n ASCII
if (c == 0x0D) {
digitalWrite(4, HIGH);
httpResponseHome(client);
delay(10);
//stopping client
client.stop();
// control arduino pin
if(controlString.indexOf("?1-on") > -1) //checks for switch1on
{
transmitter.sendGroup(true);
httpResponseRedirect(client);
}
else{
if(controlString.indexOf("?1-off") > -1) //checks for switch1off
{
transmitter.sendGroup(false);
httpResponseRedirect(client);
}
}
//clearing string for next read
controlString="";
}
}
}
}
}
void httpResponseHome(EthernetClient c) {
c.println("HTTP/1.1 200 OK");
c.println("Content-Type: text/html");
c.println();
c.println("<html>");
c.println("<head>");
c.println( "<title>MPC NEXA Webserver</title>");
c.println( "<style>");
c.println( "body { font-family: Arial, sans-serif; font-size:12px; }");
c.println( "</style>");
c.println("</head>");
c.println("<body>");
c.println( "<h1>MPC NEXA Webserver</h1>");
c.println( "<ul>");
c.println( "<li><a href=\"./?1-on\">Turn on power</a></li>");
c.println( "<li><a href=\"./?1-off\">Turn off power</a></li>");
c.println( "</ul>");
c.println( "<ul>");
c.println( "<li><a href=\"./?2-on\">Turn on power 1</a></li>");
c.println( "<li><a href=\"./?2-off\">turn off power 1</a></li>");
c.println( "</ul>");
c.println( "<ul>");
c.println( "<li><a href=\"./?3-on\">Turn on power 2</a></li>");
c.println( "<li><a href=\"./?3-off\">turn off power 2</a></li>");
c.println( "</ul>");
c.println( "<ul>");
c.println( "<li><a href=\"./?4-on\">Turn on power 3</a></li>");
c.println( "<li><a href=\"./?4-off\">turn off power 3</a></li>");
c.println( "</ul>");
c.println( "<hr>");
c.println("</body>");
c.println("</html>");
}
void httpResponseRedirect(EthernetClient c) {
c.println("HTTP/1.1 301 Found");
c.println("Location: /");
c.println();
}
What I am trying to add, but I get errors when I try to add it under my last "else" statement, even if I add an else if behind it, but I am probably doing something wrong?
else{
if(controlString.indexOf("?2-on") > -1) //checks for switchon
{
transmitter.sendUnit(0, true);
httpResponseRedirect(client);
}
}
else{
if(controlString.indexOf("?2-off") > -1) //checks for switchoff
{
transmitter.sendUnit(0, false);
httpResponseRedirect(client);
}
}
else{
if(controlString.indexOf("?3-on") > -1) //checks for switchon
{
transmitter.sendUnit(1, true);
httpResponseRedirect(client);
}
}
else{
if(controlString.indexOf("?3-off") > -1) //checks for switchoff
{
transmitter.sendUnit(1, false);
httpResponseRedirect(client);
}
}
else{
if(controlString.indexOf("?4-on") > -1) //checks for switchon
{
transmitter.sendUnit(2, true);
httpResponseRedirect(client);
}
}
else{
if(controlString.indexOf("?4-off") > -1) //checks for switchoff
{
transmitter.sendUnit(2, false);
httpResponseRedirect(client);
}
}