Post topics, source code that relate to the Arduino Platform

User avatar
By Lucas neill
#63530
martinayotte wrote:It is not clear what is your question.
Is it only to add gpio code to turn on relay ?
if yes, is it according to the temperature send to handle_feed() callback ?
Maybe something like this from your AP :

Code: Select all// Handling the /feed page from my server
void handle_feed() {
  String t = server.arg("temp");
  String h = server.arg("hum");
 
  server.send(200, "text/plain", "This is response to client");
  setupStMode(t, h);

  // new code added here for the relay
  long temp = t.toFloat();
  if (temp < 20.0)
    digitalWrite(2, HIGH); // Turn ON Relay on GPIO2
  else
    digitalWrite(2, LOW); // Turn OFF Relay on GPIO2
}

You also need to add in setup :

Code: Select all  pinMode(2, OUTPUT); // Make GPIO2 as an OUTPUT to drive Relay MOSFET

Ok the project is now working but I don't know why? Because when I try the code below using a led it is working as supposed to. When the temperature is below 20 GPIO2 lights up the led and when it is above 20 degrees it turns it off. But what surprises me happens when I connect a relay instead of led. When the temp is below 20 it turns off the relay but when it is above 20 it turns out on, I realized this by chance I just changed the low into high and high into low and it works vice versa. Why this happens like this?

void handle_feed() {
 String t = server.arg("temp");
 String h = server.arg("hum");

 server.send(200, "text/plain", "This is response to client");
 setupStMode(t, h);

 long temp = t.toFloat();
 if (temp < 20.0)
   digitalWrite(2, HIGH); // Turn ON Relay on GPIO2
 else
   digitalWrite(2, LOW); // Turn OFF Relay on GPIO2
}
}
User avatar
By Lucas neill
#63557
martinayotte wrote:This is because GPIOs can drive LED or something else in 2 modes : sink or source modes.
This means a digitalWrite(pin, LOW) or digitalWrite(pin, HIGH) will provide either a GND or VCC.
So, it is inverting the logic ...

Image


i wouldnt have found this even if i think for years. i just tried and found by luck
User avatar
By Lucas neill
#63606
martinayotte wrote:This is because GPIOs can drive LED or something else in 2 modes : sink or source modes.
This means a digitalWrite(pin, LOW) or digitalWrite(pin, HIGH) will provide either a GND or VCC.
So, it is inverting the logic ...

Image


i have one more problem martin. my server side sometimes disappears from wifi connections list so client cant connect to it and system breaks down. why is that? as you can see in the picture i soldered rst pin to gpio16 using a 220 ohm resistor(i couldnt find 470 ohm, i dont know if that matters) and relay is connected to arduino 5v and esp also arduino 3.3v. relay's GND and ESP's GND are on the same line. before i soldered the resistor sometimes it used to stuck on blue led constantly flashing(i mean always on) . After i soldered i didnt see this happens but again as a result AP ends for a reason i dont know why. this is the last version of my code and i changed esp.reset to esp.restart( i dont know if i had to actually and i dont know the difference between them) and nothing good happened. so my question is what can i do to have a stable Access Point? Thanks

Code: Select all/*
Geekstips.com
IoT project - Communication between two ESP8266 - Talk with Each Other
ESP8266 Arduino code example
*/
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
int relay = 2;
// IoT platform Credentials
String apiKey = "7VIJ3X18MG7POOKI";
const char* logServer = "api.thingspeak.com";
 
// Internet router credentials
const char* ssid = "ESP8266";
const char* password = "12345678";

 
float h;
float t;
ESP8266WebServer server(80);
 
void setup() {
  Serial.begin(74880);
  WiFi.mode(WIFI_AP_STA);
  setupAccessPoint();
  pinMode(2, OUTPUT); // Make GPIO2 as an OUTPUT to drive Relay MOSFET
}
// Handling the / root web page from my server
void handle_index() {
  server.send(200, "text/plain", "Get the f**k out from my server!");
}
 
// Handling the /feed page from my server
void handle_feed() {
  String t = server.arg("temp");
  String h = server.arg("hum");
   long temp = t.toFloat();
   if (temp < 22.0)
    digitalWrite(relay, LOW); // Turn ON Relay on GPIO2
  else
    digitalWrite(relay, HIGH); // Turn OFF Relay on GPIO2
  server.send(200, "text/plain", "This is response to client");
  setupStMode(t, h);
   
 
}

void setupAccessPoint(){
  Serial.println("** SETUP ACCESS POINT **");
  Serial.println("- disconnect from any other modes");
  WiFi.disconnect();
  Serial.println("- start ap with SID: "+ String(ssid));
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("- AP IP address is :");
  Serial.print(myIP);
  setupServer();
}
 
void setupServer(){
  Serial.println("** SETUP SERVER **");
  Serial.println("- starting server :");
  server.on("/", handle_index);
  server.on("/feed", handle_feed);
  server.begin();
};
 
void setupStMode(String t, String v){
  Serial.println("** SETUP STATION MODE **");
  Serial.println("- disconnect from any other modes");
  WiFi.disconnect();
  Serial.println();
  Serial.println("- connecting to Home Router SID: **********");
  WiFi.begin("SUPERONLINE_WiFi_1074", "LYW7HFEFLCVH");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("- succesfully connected");
  Serial.println("- starting client");
 
  WiFiClient client;
 
  Serial.println("- connecting to Database server: " + String(logServer));
  if (client.connect(logServer, 80)) {
    Serial.println("- succesfully connected");
    String postStr = apiKey;
    postStr += "&field1=";
    postStr += String(t);
    postStr += "&field2=";
    postStr += String(v);
    postStr += "\r\n\r\n";
    Serial.println("- sending data...");
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
  }
  client.stop();
  Serial.println("- stopping the client");
  Serial.println("- trying reset");
  ESP.restart(); // If your ESP does not respond you can just *** reset after each request sending
 
 
}
 
void loop() {
   server.handleClient();
      }


before i forget, i also pulled GPIO0 high to 3.3v and also no good. And there is diode from arduino 5v to relay VCC.
You do not have the required permissions to view the files attached to this post.