Bonjour à tous ,
je reviens vers vous car je Progress en Arduino mais pas encore assez apparemment.
je bloque pour un projet en gros je veut transmettre en 232 l'adresse IP ,que mon esp32 doit prendre pour se connecté a une serveur et le sauvegarder en mémoire .tous fonctionne Nikel a l'exception que l'esp32 ne se connecte plus a mon serveur.
//#include "WiFi.h"
#include <WiFi.h>
#include <Preferences.h>
#include <AM2320.h>
#include <Arduino.h>
WiFiClient client;
// the fix IP address of the server
Preferences preferences;
AM2320 th(&Wire);
//#define TFT_GREY 0x5AEB // New colour
#define ADC_EN 14 //ADC_EN is the ADC detection enable port
#define ADC_PIN 34
#define BUTTON_1 35
#define BUTTON_2 0
// identfication du réseau wifi local
String msg = " ";
int vref = 1100;
int compteurwifi=0;
WiFiClient wifiClient;
float temptemp=0;
String wifitemp="";
String mdptemp="";
int temppause=10000000 ;
byte ip[] = {192,168,0,40 }; //adresse par defaut de mon serveur
float showVoltage()
{
// static uint64_t timeStamp = 0;
// if (millis() - timeStamp > 1000) {
// timeStamp = millis();
uint16_t v = analogRead(ADC_PIN);
float battery_voltage = ((float)v / 4095.0) * 2.0 * 3.3 * (vref / 1000.0);
return battery_voltage;
// }
}
void setup(void) {
delay(100);
Wire.begin();
preferences.begin("myfile", false);
Serial.begin(115200);
Serial.print(preferences.getUInt("IP1",192));
Serial.print(preferences.getUInt("IP2",192));
Serial.print(preferences.getUInt("IP3",192));
Serial.print(preferences.getUInt("IP4",192));
ip[1]=byte(preferences.getUInt("IP1",192));
ip[2]=byte(preferences.getUInt("IP2",168));
ip[3]=byte(preferences.getUInt("IP3",0));
ip[4]=byte(preferences.getUInt("IP4",22));
String wifitemp = preferences.getString("nomwifi","non trouvé");
String mdptemp=preferences.getString("mdpwifi","non trouvé");
temppause=preferences.getUInt("temppause",1)*1000000;
#define WIFI_SSID wifitemp.c_str()
#define WIFI_PASSWORD mdptemp.c_str()
String WifiSTR="";
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
WifiSTR = WifiSTR + ".";
Serial.print(WifiSTR);
++compteurwifi;
if (compteurwifi>20){
break;
}
delay(200);
}
if(WiFi.status() == WL_CONNECTED){
Serial.println("connecté");
}
String nomwifi="";
String mdpwifi="";
preferences.end();
delay(200);
}
void loop() {
temptemp=showVoltage();
if (temptemp<4.7) {
switch(th.Read()) {
case 2:
//tft.println(" CRC failed");
break;
case 1:
//tft.println(" Sensor offline");
break;
case 0:
client.connect(ip,2000); // Connection to the server
client.print(WiFi.macAddress());
Serial.println(WiFi.macAddress());
client.print("@@@");
Serial.println("@@@");
client.print(th.cTemp);
Serial.println(th.cTemp);
client.print("@@@");
Serial.println("@@@");
client.print(th.Humidity);
Serial.println(th.Humidity);
client.print("@@@");
Serial.println("@@@");
delay(100);
client.print(String(temptemp));
Serial.println(String(temptemp));
delay(100);
client.print("\r");
Serial.println("\r");
client.flush();
client.stop();
delay(100);
esp_sleep_enable_timer_wakeup(temppause);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
esp_deep_sleep_start();
break;
}
}
if (Serial.available() )
{
msg = Serial.readString(); //Stocke le message sous forme de String dans msg
int longueur=msg.length(); //On calcul la longueur
msg=msg.substring(0,longueur-2);
Serial.print("--");
Serial.print(msg); //Puis votre message
Serial.println("--");
////enregistre le SSID
if (msg.indexOf("@wifi")>0)
{
preferences.begin("myfile", false);
preferences.putString("nomwifi",msg.substring(6));//
preferences.end();
Serial.println("enregistrer");
}
///demande du SSID
if (msg.indexOf("@?wifi")>0)
{
preferences.begin("myfile", false);
Serial.print("''");
Serial.print(preferences.getString("nomwifi","non trouvé"));
Serial.println("''");
preferences.end();
}
if (msg.indexOf("@mdp")>0){
preferences.begin("myfile", false);
preferences.putString("mdpwifi",msg.substring(5));//
preferences.end();
Serial.println("enregistrer");
}
if (msg.indexOf("@?mdp")>0){
preferences.begin("myfile", false);
Serial.print("''");
Serial.print(preferences.getString("mdpwifi","non trouvé"));
Serial.println("''");
preferences.end();
}
if (msg.indexOf("@temppause")>0){
String tempmsg=msg.substring(11);
preferences.begin("myfile", false);
preferences.putUInt("temppause",tempmsg.toInt());//
preferences.end();
temppause=tempmsg.toInt()*1000000;
Serial.println("enregistrer");
}
if (msg.indexOf("@?temppause")>0){
preferences.begin("myfile", false);
Serial.print("''");
Serial.print(preferences.getUInt("temppause",0));
Serial.println("''");
preferences.end();
}
if (msg.indexOf("@?mac")>0){
Serial.print("''");
Serial.print(WiFi.macAddress());
Serial.println("''");
}
if (msg.indexOf("@serveur")>0){
String tempip=msg.substring(9);
preferences.begin("myfile", false);
String groupe1=tempip.substring(0,3);
String groupe2=tempip.substring(3,6);
String groupe3=tempip.substring(6,9);
String groupe4=tempip.substring(9,12);
preferences.putUInt("IP1",groupe1.toInt());
preferences.putUInt("IP2",groupe2.toInt());
preferences.putUInt("IP3",groupe3.toInt());
preferences.putUInt("IP4",groupe4.toInt());
preferences.end();
Serial.print(tempip);
Serial.println("enregistrer");
}
if (msg.indexOf("@?serveur")>0){
String tempip=msg.substring(10);
preferences.begin("myfile", false);
Serial.print(preferences.getUInt("IP1",0));
Serial.print(".");
Serial.print(preferences.getUInt("IP2",0));
Serial.print(".");
Serial.print(preferences.getUInt("IP3",0));
Serial.print(".");
Serial.println(preferences.getUInt("IP4",0));
preferences.end();
Serial.print(tempip);
Serial.println("enregistrer");
}
}
}
en gros il faudrait que le String que je reçoit sur mon port série (exemple de reception:192168000040=> correspond a 192.168.0.40) je puisse le sauvegarder et le mettre en paramètre dans le client.connect(ip,2000); car je pense que c'est le format de "ip" qui ne vas pas.