Post topics, source code that relate to the Arduino Platform

User avatar
By Lucas neill
#62594 As I said I found all the things I need buy still can't find the code how to trigger the relay. There are different codes but in Lua language and either for triggerring relay or dht server there isn't a combined one so could you have a look at the code below and help me to insert a code to activate the relay connected to AP side on pin gpio 2 of esp?

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 <ESP8266HTTPClient.h>
#define DHTPIN 2
 
// AP Wi-Fi credentials
const char* ssid = "***********";
const char* password = "**********";
 
// Local ESP web-server address
String serverHost = "http://192.168.4.1/feed";
String data;
// DEEP_SLEEP Timeout interval
int sleepInterval = 5;
// DEEP_SLEEP Timeout interval when connecting to AP fails
int failConnectRetryInterval = 2;
int counter = 0;
 
float h;
float t;
// Static network configuration
IPAddress ip(192, 168, 4, 4);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
 
DHT dht(DHTPIN, DHT22);
WiFiClient client;
 
void setup() {
  ESP.eraseConfig();
  WiFi.persistent(false);
  Serial.begin(74880);
  Serial.println();
  Serial.println("**************************");
  Serial.println("**************************");
  Serial.println("******** BEGIN ***********");
  Serial.println("- start DHT sensor");
  dht.begin();
  delay(500);
  Serial.println("- set ESP STA mode");
  WiFi.mode(WIFI_STA);
  Serial.println("- connecting to wifi");
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);
  Serial.println("");
  while (WiFi.status() != WL_CONNECTED) {
    if(counter > 20){
       Serial.println("- can't connect, going to sleep");   
       hibernate(failConnectRetryInterval);
    }
    delay(500);
    Serial.print(".");
    counter++;
  }
 
  Serial.println("- wifi connected");
  Serial.println("- read DHT sensor");
  readDHTSensor();
  Serial.println("- build DATA stream string");
  buildDataStream();
  Serial.println("- send GET request");
  sendHttpRequest();
  Serial.println();
  Serial.println("- got back to sleep");
  Serial.println("**************************");
  Serial.println("**************************");
  hibernate(sleepInterval);
}
 
void sendHttpRequest() {
  HTTPClient http;
  http.begin(serverHost);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  http.POST(data);
  http.writeToStream(&Serial);
  http.end();
}
 
void readDHTSensor() {
  delay(200);
  h = dht.readHumidity();
  t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    t = 0.00;
    h = 0.00;
  }
  Serial.println("- temperature read : "+String(t));
  Serial.println("- humidity read : "+String(h));
}
 
void buildDataStream() {
  data = "temp=";
  data += String(t);
  data += "&hum=";
  data += String(h);
  Serial.println("- data stream: "+data);
}
 
 
void hibernate(int pInterval) {
  WiFi.disconnect();
  ESP.deepSleep(10 * 600000 * pInterval, WAKE_RFCAL);
  delay(100);
}
 
void loop() {}


This is AP side in which I need to insert a code. If temp is below 20 turn on relay otherwise turn off that's it

Code: Select all/*
Geekstips.com
IoT project - Communication between two ESP8266 - Talk with Each Other
ESP8266 Arduino code example
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// IoT platform Credentials
String apiKey = "*************";
const char* logServer = "api.thingspeak.com";
 
// Internet router credentials
const char* ssid = "***********";
const char* password = "***********";
 
ESP8266WebServer server(80);
 
void setup() {
  Serial.begin(74880);
  WiFi.mode(WIFI_AP_STA);
  setupAccessPoint();
}
// 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");
 
  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("*********", "*********");
  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");
  /** If your ESP does not respond you can just
  *** reset after each request sending
  Serial.println("- trying reset");
  ESP.reset();
  **/
}
 
void loop() {
  server.handleClient();
}
User avatar
By martinayotte
#62612 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
User avatar
By Lucas neill
#62681
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


Is handle_feed() about the data coming from client? If so yes I want to turn the relay on if the temperature is below 20 centigrade if above 20 I want to turn it off. So where should I insert what code?