Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



a toggle button tha...
 
Share:
Notifications
Clear all

[Solved] a toggle button that updates a webserver status

34 Posts
2 Users
0 Reactions
13.1 K Views
(@murad)
Eminent Member
Joined: 4 years ago
Posts: 24
Topic starter  

@hans

its fine brother i thank you for ur help and dedication ,,, 

isn't it a little bit ghosty over here? since when u have this forum ? 


   
ReplyQuote
(@murad)
Eminent Member
Joined: 4 years ago
Posts: 24
Topic starter  

so i revamped the code to get better switching parameters ... and actually, the code to push to the server didn't work at alll ,,, 

i tried it again and nothing happened 

i never had that much of obstacles lol  i even made it live .... 

here is the functonal part of my code that im struggling to make it function when i push the physical button ,,,, that changes the status of the webserver code and the data it get sent back to my ESP

http://zeint.net/IOT/esp-outputs-action.php?action=output_update&id=8&state= 1

 

u can see the results at : 

http://zeint.net/IOT/esp-outputs.php

this is the front end 

😫


   
ReplyQuote
(@murad)
Eminent Member
Joined: 4 years ago
Posts: 24
Topic starter  

still cant post long


   
ReplyQuote
(@murad)
Eminent Member
Joined: 4 years ago
Posts: 24
Topic starter  

anyways i added this ... 

const char* Pushserver = "http://zeint.net/IOT/esp-outputs-action.php";

and these ... 

void relayOnOff(int relay){

switch(relay){
case 1:
if(toggleState_1 == 0){
digitalWrite(RelayPin1, LOW); // turn on relay 1
toggleState_1 = 1;
Serial.println("Device1 ON");
}


else{
digitalWrite(RelayPin1, HIGH); // turn off relay 1
toggleState_1 = 0;
Serial.println("Device1 OFF");

HTTPClient http;
http.begin(Pushserver);
http.addHeader("Content-Type", "application/json");
String httpRequestData = "{\"action\":\"output_update\",\"id\":\"8\",\"state\":\"1\"}";///// this is to turn it on ,,, when i toggle the physical button
// String httpRequestData = "{\"action\":\"output_update\",\"id\":\"8\",\"state\":\"0\"}";///// this is to turn it off
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
delay(1000);
break;
}
default : break;
}
}

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 
Posted by: @murad

since when u have this forum

Ehm, I'm using this forum for about a year or maybe 2 years now. I used a different forum before that.

Posted by: @murad

http://zeint.net/IOT/esp-outputs-action.php?action=output_update&id=8&state= 1

I see there is a space before the "1" - is that accidental (there should not be a space there).
When using the link without a space, I see the last switch change (1=on, 0=off) correctly.

http://zeint.net/IOT/esp-outputs-action.php?action=output_update&id=8&state=1

http://zeint.net/IOT/esp-outputs-action.php?action=output_update&id=8&state=0

I'm not sure what is going wrong .. seems to work, right?


   
ReplyQuote
(@murad)
Eminent Member
Joined: 4 years ago
Posts: 24
Topic starter  

@hansyes it does work on link but not when i try to push it in the arduino side 

it just dont send the data 

i even tried to make post commands 

i was not successful the only way iy works is on the links

Posted by: @murad
Posted by: @murad

http://zeint.net/IOT/esp-outputs-action.php?action=output_update&id=8&state= 1

I see there is a space before the "1" - is that accidental (there should not be a space there).
When using the link without a space, I see the last switch change (1=on, 0=off) correctly.

I think that accorded due to coloring i colored it in ur forum but when i push the actual button and the last script run it does nothing at all 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 
Posted by: @murad

yes it does work on link but not when i try to push it in the arduino side 

The link is the GET format, where as your code is using the POST format.

Maybe you have to start over, try this code.
It only sets the switch to ON every 10 seconds, up to you to add the relay code mlater, once this works,
Test is with state=0 as well - to make sure it works, before adding the relay functions in it.

This is code from the link I previous gave you (paragraph called "Code ESP8266 HTTP GET with Arduino IDE"), just slightly adjust for the links and parameters you gave me in your previous post.

#include < ESP8266WiFi.h >
#include < ESP8266HTTPClient.h >
#include < WiFiClient.h >

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

//Your Domain name with URL path or IP address with path
String serverName = "http://zeint.net/IOT/esp-outputs-action.php";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

void setup() {
  Serial.begin(115200); 

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;

      String serverPath = serverName + "action=output_update&id=8&state=1";
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverPath.c_str());
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

I only modified the red lines.

 

May I ask what operating system and browser you're using?
I'm not experiencing any of the issues you seem to be running into in this forum.


   
ReplyQuote
(@murad)
Eminent Member
Joined: 4 years ago
Posts: 24
Topic starter  
Posted by: @hans

May I ask what operating system and browser you're using?
I'm not experiencing any of the issues you seem to be running into in this forum.

im using windows 10 and google chrome upated .... 

18:16:33.992 -> Connected to WiFi network with IP Address: 192.168.1.24
18:16:33.992 -> Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.
18:16:34.742 -> HTTP Response code: 404
18:16:34.742 -> <html><head><title>Error 404 - Not Found</title><head><body><h1>Error 404 - Not Found</h1><p>The document you are looking for may have been removed or re-named. Please contact the web site owner for further assistance.</p></body></html>
18:16:40.183 -> HTTP Response code: 404

still isnt working 

i dont know what to do anymore lol 


   
ReplyQuote
(@murad)
Eminent Member
Joined: 4 years ago
Posts: 24
Topic starter  

OHHHHHH

i messed up with the code and it worked like this ,,,, 

now im gonna try to implement it in my code :D 

ty a lot buddy 

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Arduino_JSON.h>

#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;
const char* ssid = "MoKa";
const char* password = "mokamoka";

//Your Domain name with URL path or IP address with path
String serverName = "http://zeint.net/IOT/esp-outputs-action.php?action=output_update&id=8";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

void setup() {
Serial.begin(115200);

WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());

Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;

String serverPath = serverName + "&state=1";

// Your Domain name with URL path or IP address with path
http.begin(serverPath.c_str());

// Send HTTP GET request
int httpResponseCode = http.GET();

if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}

   
ReplyQuote
(@murad)
Eminent Member
Joined: 4 years ago
Posts: 24
Topic starter  

ahhh cant post long again :S

 


   
ReplyQuote
(@murad)
Eminent Member
Joined: 4 years ago
Posts: 24
Topic starter  

for some reason, i didn't allow me to use this method unless its in the main loop .... its about the "+" thingy and i worked it around using ur help

like this ,,,,  😊 


// Your IP address or domain name with URL path
const char* serverName = "http://zeint.net/IOT/esp-outputs-action.php?action=outputs_state&board=3";
////////////// Obstacle code//////////////
String Pushon = "http://zeint.net/IOT/esp-outputs-action.php?action=output_update&id=8&state=1";
String Pushoff = "http://zeint.net/IOT/esp-outputs-action.php?action=output_update&id=8&state=0";
////////////// Obstacle code//////////////

http.begin(Pushon.c_str());


http.begin(Pushoff.c_str());

void loop(){
buttons();
http();
}

it didnt accept this code .... so i had to work around ,,  it is working finally i wish i can share some stuff here ,,,  each time i paste long ,,, it dissapears 

String serverPath = serverName + "&state=1";

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

Hi Murad,

well, I'm happy to see we're making progress 😊 
Excellent!

If you have a very long code, then please consider attaching the INO file, instead of pasting the code here.
Code can trigger a security error (a false positive) - at least that is my best guess why it keeps failing.
I couldn't find anything in the firewall log, but then again, the log is huge and quite cryptic at times.

It is a little weird that the "+" didn't work.
The official Arduino documentation states this should work. 🤔

Does it give an error message?
If not, try adding this:

Serial.println(serverPath);

After this line:

String serverPath = serverName + "&state=1";

so we can see what happens to the string.


   
ReplyQuote
(@murad)
Eminent Member
Joined: 4 years ago
Posts: 24
Topic starter  

@hans

hello friend ,,, 

i will try this also thank you.


   
ReplyQuote
(@murad)
Eminent Member
Joined: 4 years ago
Posts: 24
Topic starter  

@hans

it doesn't allow u to compile and upload at all 

C:\Users\moka\Desktop\Arduino DONE projects\=[[Personal cloud ]]=\touchless_PHP_webserver_send_and_get\touchless_PHP_webserver_send_and_get.ino: In function 'void relayOnOff(int)':
touchless_PHP_webserver_send_and_get:61:37: error: invalid operands of types 'const char*' and 'const char [9]' to binary 'operator+'
String serverPath = serverName + "&state=1";
^
exit status 1
invalid operands of types 'const char*' and 'const char [9]' to binary 'operator+'

i think it should be in the main loop to work ,,, the void loop anyways i attached the file ,,, thats how its finally done :D 

 

This post was modified 4 years ago by murad

   
ReplyQuote
(@murad)
Eminent Member
Joined: 4 years ago
Posts: 24
Topic starter  

later i will try to add autoconfiguration to the code 


   
ReplyQuote
Page 2 / 3
Share: