hello
I have two programs on two adruino
I would like to combine the two programs am only 1 arduino but I can't do it
can help me please
#include <ESP8266WiFi.h>
String apiKey = "xxxxxxxxxx"; // Enter your Write API key from ThingSpeak
const char* resource = "/update?api_key=";
const char *ssid = "wifi-orbi-mams"; // replace with your wifi ssid and wpa2 key
const char *pass = "0019041966";
const char* server = "api.thingspeak.com";
const int AnalogIn = A0;
float suhu;
int dataadc;
WiFiClient client;
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
dataadc = analogRead(AnalogIn);
suhu = dataadc * (3.2/1023.0) * 100.0;
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(suhu);
postStr += "\r\n\r\n";
//client.print(String("GET ") + resource + apiKey + "&field1=" + temperatureTemp + "&field2=" + humidityTemp + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: close\r\n\r\n");
client.print(String("GET ") + resource + apiKey + "&field1=" + suhu + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: close\r\n\r\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);
Serial.print("Capteur: ");
Serial.print(suhu);
Serial.println(" Valeur Capteur. Send to Thingspeak.");
}
client.stop();
Serial.println("Waiting...");
// thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
delay(10000);
}
TWO PROGRAMME
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SimpleDHT.h>
#define ONE_WIRE_BUS 15
int pinDHT11 = 13;
SimpleDHT11 dht11;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
MDNSResponder mdns;
// Wi-Fi
const char* ssid = "wifi_new";
const char* password = "0019041966";
byte arduino_mac[] = { 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192,168,1,50);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
ESP8266WebServer server(80);
int D0_pin = 16;
int D2_pin = 2;//d4
int D1_pin = 5;
int D6_pin = 12;
void setup(void){
// preparing GPIOs
pinMode(D0_pin, OUTPUT);
digitalWrite(D0_pin, LOW);
pinMode(D2_pin, OUTPUT);
digitalWrite(D2_pin, LOW);
pinMode(D1_pin, OUTPUT);
digitalWrite(D1_pin, LOW);
sensors.begin();
delay(100);
Serial.begin(115200);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
//+++++++++++++++++++++++ START LED-1 ++++++++++++++++++++
server.on("/", [](){
server.send(200, "text/html", webPage());
});
server.on("/socket1On", [](){
digitalWrite(D0_pin, HIGH);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket1Off", [](){
digitalWrite(D0_pin, LOW);
server.send(200, "text/html", webPage());
delay(100);
});
//+++++++++++++++++++++++ END LED-1 ++++++++++++++++++++
//+++++++++++++++++++++++ START LED-2 ++++++++++++++++++++
server.on("/socket2On", [](){
digitalWrite(D2_pin, HIGH);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket2Off", [](){
digitalWrite(D2_pin, LOW);
server.send(200, "text/html", webPage());
delay(100);
});
// +++++++++++++++++++++++ END LED-2 ++++++++++++++++++++
//+++++++++++++++++++++++ START LED-3 ++++++++++++++++++++
server.on("/socket3On", [](){
digitalWrite(D1_pin, HIGH);
server.send(200, "text/html", webPage());
delay(100);
});
server.on("/socket3Off", [](){
digitalWrite(D1_pin, LOW);
server.send(200, "text/html", webPage());
delay(100);
});
// +++++++++++++++++++++++ END LED-3 ++++++++++++++++++++
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}
String webPage()
{
sensors.requestTemperatures();
byte temperature = 0;
byte humidity = 0;
dht11.read(pinDHT11, &temperature, &humidity, NULL);
String web;
web += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/> <meta charset=\"utf-8\"><title>ESP 8266</title><style>button{color:red;padding: 10px 27px;}</style></head>";
web += "<h1 style=\"text-align: center;font-family: Open sans;font-weight: 100;font-size: 20px;\"> SERVEUR WEB (La Mare de Gégé) </h1><div>";
//++++++++++ LED-1 +++++++++++++
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">(Remplissage en Mode Automatique)</p>";
if (digitalRead(D0_pin) == 1)
{
web += "<div style=\"text-align: center;width: 98px;color:black ;padding: 10px 30px;background-color: #43a209;margin: 0 auto;\">Marche AUTO</div>";
}
else
{
web += "<div style=\"text-align: center;width: 98px;color:black ;padding: 10px 30px;background-color: #ec1212;margin: 0 auto;\">Mare à l'Arrêt</div>";
}
web += "<div style=\"text-align: center;margin: 5px 0px;\"> <a href=\"socket1On\"><button>Mise en Marche </button></a> <a href=\"socket1Off\"><button> Mise à l'Arrêt </button></a></div>";
// ++++++++ LED-1 +++++++++++++
//++++++++++ LED-2 +++++++++++++
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">(Remplissage en Mode Continu)</p>";
if (digitalRead(D2_pin) == 1)
{
web += "<div style=\"text-align: center;width: 98px;color:black ;padding: 10px 30px;background-color: #43a209;margin: 0 auto;\"> Remplissage en Continu </div>";
}
else
{
web += "<div style=\"text-align: center;width: 98px;color:black ;padding: 10px 30px;background-color: #ec1212;margin: 0 auto;\">Arrêt</div>";
}
web += "<div style=\"text-align: center;margin: 5px 0px;\"> <a href=\"socket2On\"><button> Marche Forçé </button></a> <a href=\"socket2Off\"><button> Arrêt Forçage</button></a></div>";
// ++++++++ LED-2 +++++++++++++
//++++++++++ LED-3 +++++++++++++
web += "<p style=\"text-align: center;margin-top: 30px;margin-bottom: 5px;\">(Sonde de Niveau)</p>";
if (digitalRead(D6_pin) == 0)
{
web += "<div style=\"text-align: center;width: 98px;color:black ;padding: 10px 30px;background-color: #43a209;margin: 0 auto;\">Niveau à 100%</div>";
}
else
{
web += "<div style=\"text-align: center;width: 98px;color:black ;padding: 10px 30px;background-color: #ec1212;margin: 0 auto;\">Niveau inf à 90%</div>";
}
// web += "<div style=\"text-align: center;margin: 5px 0px;\"> <a href=\"socket3On\"><button>ON</button></a> <a href=\"socket3Off\"><button>OFF</button></a></div>";
// ++++++++ LED-3 +++++++++++++
//++++++++++ DS18B20 TEMP +++++++++++++
//web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">----DS18B20 TEMP----</p>";
//web += "<div style=\"text-align: center;width: 98px;color:white ;padding: 10px 30px;background-color: #5191e4;margin: 0 auto;\">"+ String(sensors.getTempCByIndex(0))+"</div>";
// ++++++++ DS18B20 TEMP +++++++++++++
//++++++++++ DHT11 TEMP +++++++++++++
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">(Température en C°)</p>";
web += "<div style=\"text-align: center;width: 98px;color:black ;padding: 10px 30px;background-color: #5191e4;margin: 0 auto;\">"+ String((int)temperature)+"</div>";
// ++++++++ DHT11 TEMP +++++++++++++
//++++++++++ DHT11 H +++++++++++++
web += "<p style=\"text-align: center;margin-top: 0px;margin-bottom: 5px;\">(Taux d'Humidité en %)</p>";
web += "<div style=\"text-align: center;width: 98px;color:black ;padding: 10px 30px;background-color: #5191e4;margin: 0 auto;\">"+ String((int)humidity)+"</div>";
// ++++++++ DHT11 H +++++++++++++
// ========REFRESH=============
web += "<div style=\"text-align:center;margin-top: 20px;\"><a href=\"/\"><button style=\"width:158px;\">REFRESH</button></a></div>";
// ========REFRESH=============
web += "</div>";
return(web);
}
`