Good afternoon all
hopefully this is a very simple problem, I’m using a ESP 32 what I’m trying to achieve is send a command (192.168.1.210/slider1?value=29) to my fan controlle also another (ESP32)
if I use a web browser and just type the command in this works fine.
But if I try and use the program nothing appears to happen it doesn’t appear to be sending anything?
Here is the loop part of the code that is sending the data.
void loop() {
ts.execute();
watchdog();
// Read Joystick values and map to values of 0 - 255
joystick[0] = map(analogRead(JoyStick_X_PIN), 0, 4095, 0, 64);
joystick[1] = map(analogRead(JoyStick_Y_PIN), 0, 4095, 0, 64);
joystick[2] = digitalRead(Button_Switch) ;
//Display the joystick values in the serial monitor.
/* Serial.println("-----------");
Serial.print("x:");
Serial.print(joystick[0]);
Serial.print(" : y:");
Serial.print(joystick[1]);
Serial.print(" : s:");
Serial.println(joystick[2]);
*/
delay(500);
if(x<=10){
x++;
}
else {
x=0;
/*UploadData="";
UploadData= String("/slider1?value=");
UploadData+=String(joystick[0]);
SendHttpPOST();
*/
UploadData="";
UploadData= String("192.168.1.210");
UploadData+= String("/slider1?value=");
UploadData+=String(joystick[0]);
Serial.println(UploadData);
if (client.connect(ServerHost1,80)){
client.println("GET /"+UploadData+" HTTP/1.1\r\nConnection: close\r\n\r\n");
client.println("Host: "+String("192.168.1.210"));
delay(500); // Essential delay for ESP32
client.println F("Connection: close");
Serial.println F("Connection: close");
client.stop();
}
}
}
Any suggestions would be much appreciated
if you would like to look at the full code please see below
Thank you
#include <Arduino.h>
#include "soc/timer_group_struct.h"
#include "soc/timer_group_reg.h"
#define _TASK_INLINE
#include <TaskScheduler.h>
#include <WiFi.h>
#include "WebServer.h"
#include <Preferences.h>
#include <esp_task_wdt.h>
// Connect to the server with http://fanserver.local/ e.g. name = "myserver" then use address http://myserver.local/
IPAddress local_IP(192, 168, 1, 205);
IPAddress gateway(192, 168, 1, 254); // Set your network Gateway usually your Router base address
IPAddress subnet(255, 255, 255, 0);
IPAddress dns1(192,168,1,254); // Set your network DNS usually your Router base address
IPAddress dns2(8,8,8,8);
WebServer server(80);
WiFiClient client;
Scheduler ts;
Preferences preferences;
const char* hostname = "Joystick";
const char* ServerHost1 = "192.168.1.210";
const char* ServerHost2 = "192.168.1.118";
#define WDT_TIMEOUT 4
// Define Joystick Connections
#define JoyStick_X_PIN 36
#define JoyStick_Y_PIN 39
#define Button_Switch 17
// Declare unsigned 8-bit joystick array
uint16_t joystick[3];
const char* ssid = "BTHub6-******";
const char* password = "JL488888888888";
int Rssi=0;
String UploadData;
unsigned int x=0;
//################# Voids
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info);
void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
void motor_print();
uint8_t StartWiFi();
void watchdog_setup();
void watchdog();
void Web_Listen();
void Rssi_Update();
void SendHttpPOST();
void SendHttpPOST();
//#####################
Task Web_Listen1(305,TASK_FOREVER , &Web_Listen,&ts,true);
Task watchdog1(220, TASK_FOREVER, &watchdog,&ts,true);
//#############################################################################################
void setup() {
Serial.begin(115200);
Serial.println();
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
TIMERG0.wdt_feed=1; TIMERG0.wdt_wprotect=0;
preferences.begin("credentials", false);
pinMode(Button_Switch,INPUT_PULLUP );
if (StartWiFi() == WL_CONNECTED) { // Start the WiFi service
Serial.println("WiFi Started...");
}
watchdog_setup(); //Setup watchdog
}
//#############################################################################################
void loop() {
ts.execute();
watchdog();
// Read Joystick values and map to values of 0 - 255
joystick[0] = map(analogRead(JoyStick_X_PIN), 0, 4095, 0, 64);
joystick[1] = map(analogRead(JoyStick_Y_PIN), 0, 4095, 0, 64);
joystick[2] = digitalRead(Button_Switch) ;
//Display the joystick values in the serial monitor.
/* Serial.println("-----------");
Serial.print("x:");
Serial.print(joystick[0]);
Serial.print(" : y:");
Serial.print(joystick[1]);
Serial.print(" : s:");
Serial.println(joystick[2]);
*/
delay(500);
if(x<=10){
x++;
}
else {
x=0;
/*UploadData="";
UploadData= String("/slider1?value=");
UploadData+=String(joystick[0]);
SendHttpPOST();
*/
UploadData="";
UploadData= String("192.168.1.210");
UploadData+= String("/slider1?value=");
UploadData+=String(joystick[0]);
Serial.println(UploadData);
if (client.connect(ServerHost1,80)){
client.println("GET /"+UploadData+" HTTP/1.1\r\nConnection: close\r\n\r\n");
client.println("Host: "+String("192.168.1.210"));
delay(500); // Essential delay for ESP32
client.println F("Connection: close");
Serial.println F("Connection: close");
client.stop();
}
}
}
//#############################################################################################
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//#############################################################################################
void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("Disconnected from WiFi access point");
Serial.print("WiFi lost connection. Reason: ");
Serial.println(info.disconnected.reason);
Serial.println("Trying to Reconnect");
WiFi.begin(ssid, password);
}
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("Connected to AP successfully!");
}
//#############################################################################################
uint8_t StartWiFi() {
Serial.print("\r\nConnecting to: "); Serial.println(String(ssid));
WiFi.disconnect();
delay(1200);
WiFi.mode(WIFI_STA); // switch off AP
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.config(local_IP, gateway, subnet, dns1 ,dns2);
WiFi.setHostname(hostname); //define hostname
WiFi.begin(ssid, password);
unsigned long start = millis();
uint8_t connectionStatus;
bool AttemptConnection = true;
while (AttemptConnection) {
connectionStatus = WiFi.status();
if (millis() > start + 15000) { // Wait 15-secs maximum
AttemptConnection = false;
}
if (connectionStatus == WL_CONNECTED || connectionStatus == WL_CONNECT_FAILED) {
AttemptConnection = false;
}
delay(50);
}
if (connectionStatus == WL_CONNECTED) {
Serial.println("WiFi connected at: " + WiFi.localIP().toString());
WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_STA_CONNECTED);
WiFi.onEvent(WiFiGotIP, SYSTEM_EVENT_STA_GOT_IP);
WiFi.onEvent(WiFiStationDisconnected, SYSTEM_EVENT_STA_DISCONNECTED);
}
server.on("/test", []() {server.send(200, "text/plain", "Server status is OK"); }); // Simple server test by providing a status response
server.begin();
return connectionStatus;
}
//#############################################################################################
void Web_Listen(){
server.handleClient();
}
//###################################### Watchdog ###########################################
void watchdog(){
esp_task_wdt_reset(); //Feed the watchdog
}
//#############################################################################################
void watchdog_setup(){
Serial.print("Configuring Watchdog Timeout for ");
Serial.print(WDT_TIMEOUT);
Serial.println(" Seconds");
esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
esp_task_wdt_add(NULL); //add current thread to WDT watch
}
//#############################################################################################
void Rssi_Update(){
int xx=0;
Rssi = WiFi.RSSI();
Rssi = 90 / 40.0 * Rssi + 212.5;
if (Rssi > 100) Rssi = 100;
//RssiS= Rssi + String(Rssi, 0) + " %";
Serial.print("RSSI:");
Serial.println(Rssi);
//ThingSpeak.setField(2,Rssi );
//xx=ThingSpeak.writeFields(myChannelNumber1, myWriteAPIKey1);
if(xx == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(xx));
//Rssi_Fail();
}
}
//#############################################################################################
/*
void CollectUploadData() {
Serial.println("...Submitting Upload request");
UploadData = "sensor?Sensor="+String(SensorNumber);
UploadData += "&temperature="+String(Temperature);
UploadData += "&humidity="+String(Humidity);
UploadData += "&BMP-temperature="+String(Temperature);
UploadData += "&pressure="+String(Humidity);
UploadData += "&spare="+String(Lightv);
UploadData += "&sensortype="+String("SHTC3");
Serial.println("...Information to be uploaded: "+UploadData);
SendHttpPOST();
//Thingspeak_Send();
}
*/
//############################################################################################
void SendHttpPOST() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print F("Attempting connection...");
if (client.connect(ServerHost1,80)){
Serial.println F("connected");
client.println("PUT /"+UploadData+" HTTP/1.1\r\nConnection: close\r\n\r\n");
client.println("Host: "+String("192.168.1.210"));
delay(500); // Essential delay for ESP32
client.println F("Connection: close");
Serial.println F("Connection: close");
client.stop();
return;
}
else {
Serial.println F("try again in 2 Seconds");
//Thingspeak_Send_Fail();
}
}
}