Wifi error with Board Uno + Wifi

I can't use wifi. I think my code is wrong but I can't find the error. I am autodicated. Can you help me? Tank you

enum {
  WAIT_LESS_THAN_100 = 0,
  WAIT_FOR_MORE_THAN_100,
  CHECK_TIME_ELAPSED_SINCE_CHANGE,
};

const int analogInPin = 9;
const int pinSensorInput = 0;
int sensorValue;
int b;

const int BATTERYPIN = A0; //pin de la batterie
const float TensionMin = 3.2; //tension min
const float TensionMax = 4.2; //tension max

const char* ssid = "XXXXXXXXX"; //--> wifi name or SSID.
const char* password = "XXXXXXXXXXX"; //--> wifi password.

#include <ESP8266WiFi.h>  // charge librairie wifi
#include <WiFiClientSecure.h> //// charge librairie wifi


#include <Adafruit_NeoPixel.h>                       // Charge la librairie Neo Pixel d'Adafruit utilisé pour piloter le ruban de LED
#define PIXEL_PIN 6                                  // On définit le pin où est connecté la patte DATA du bandeau
Adafruit_NeoPixel leds(5, 6, NEO_GRB + NEO_KHZ800);  // initialation ruban

//----------------------------------------Host & httpsPort
const char* host = "script.google.com";
const int httpsPort = 443;
//----------------------------------------

WiFiClientSecure client; //--> Create a WiFiClientSecure object.

String GAS_ID = "AKfycby8Lfzwu-5SBj9tHxbw3_j8ZPZy6P6_3e_BBISC0Jg7SstXLUed"; //--> spreadsheet script ID

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);

  // initialize l'entrée capteur
  pinMode(9, INPUT_PULLUP);

  // initialize le bandeau LED
  leds.begin();  // Lance la connection
  leds.show();   // Initialise toute les led à 'off'

  /* Définition des couleurs */
  int GREEN[3] = { 0, 255, 0 };      // Couleur Verte
  int RED[3] = { 255, 0, 0 };        // Couleur Rouge
  int CYAN[3] = { 0, 255, 255 };     // Couleur Cyan
  int YELLOW[3] = { 255, 125, 0 };   // Couleur Jaune
  int ORANGE[3] = { 255, 40, 0 };    // Couleur Orange
  int PURPLE[3] = { 255, 0, 255 };   // Couleur Violette
  int PINK[3] = { 255, 0, 100 };     // Couleur Rose
  int BLUE[3] = { 0, 0, 255 };       // Couleur Bleu
  int WHITE[3] = { 255, 255, 255 };  // Couleur Blanche

  // initialize la sortie led
  Serial.begin(9600);

  // initialize le Wifi
  WiFi.begin(ssid, password); //--> Connect to your WiFi router
  Serial.println("");

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
  Serial.print(".");

  client.setInsecure();

  // initialize la fonction batterie
  int levelbat ()
{
  float b = analogRead(BATTERYPIN); //valeur analogique

  int minValue = (1023 * TensionMin) / 5; //Arduino
  int maxValue = (1023 * TensionMax) / 5; //Arduino

  //int minValue = (4095 * TensionMin) / 3; //ESP32
  //int maxValue = (4095 * TensionMax) / 3; //ESP32

  b = ((b - minValue) / (maxValue - minValue)) * 100; //mettre en pourcentage

  if (b > 100) //max is 100%
    b = 100;

  else if (b < 0) //min is 0%
    b = 0;
  int valeur = b;
  return b;
}

void loop() {

  Serial.println(levelbat()); //Donne info sur le niveau de batterie

  leds.setBrightness(60);  // Règle la luminosité à 100 % de la luminosité maximale


  static uint8_t
    state = WAIT_LESS_THAN_100;
  static uint32_t
    timeStateChange,
    timeReadSensor = 0;
  uint32_t
    timeNow = millis();

  // si detection 10x/sec
  if ((timeNow - timeReadSensor) >= 1000ul) {
    timeReadSensor = timeNow;
    sensorValue = analogRead(analogInPin);
    Serial.println(sensorValue);

  }  //if

  switch (state) {
    case WAIT_LESS_THAN_100:
      //we wait here for the sensor to read < 30 led 
      if (sensorValue < 30) {
        //If my sensor is less than 30: it is the RED LED ON
        leds.fill(leds.Color(255, 0, 0), 0, 5);
        leds.show();

        //then go to the state waiting for the sensor to read >= 100
        state = WAIT_FOR_MORE_THAN_100;

      }  //if

      break;

    case WAIT_FOR_MORE_THAN_100:
      //sensor is less than 30 now; here we wait for it to go >= 100 led RED
      if (sensorValue >= 30) {
        //If my sensor is greater than 30 for less than 10s since the change of state of my sensor: the RED LED lights up
        //
        //this is same LED state as <100
        leds.fill(leds.Color(255, 0, 0), 0, 5);
        leds.show();

        //get the time we saw this transition to >= 100
        timeStateChange = timeNow;
        //and move to time the 10-sec period
        state = CHECK_TIME_ELAPSED_SINCE_CHANGE;

      }  //if

      break;

    case CHECK_TIME_ELAPSED_SINCE_CHANGE:
      //value had increased to >=100; if it falls below 100 again, go back to the first state
      if (sensorValue < 30)
        state = WAIT_LESS_THAN_100;
      //otherwise check to see if 10-second has elapsed...
      else if ((timeNow - timeStateChange) >= 15000ul) {
        //If my sensor is greater than 30 for more than 5s since
        //the change of state of my sensor: the GREEN LED lights up
        leds.fill(leds.Color(0, 255, 0), 0, 5);
        leds.show();

        //I'm guessing you can go back to wait for <100 again
        state = WAIT_LESS_THAN_100;

      }  //else

      break;

String led = "Colorled : " + String(c) ;
  String bat = "Levelbat : " + String(l) + " %";
  Serial.println(led);
  Serial.println(bat);
  
  sendData(c, l); //--> Calls the sendData Subroutine
}

// Subroutine for sending data to Google Sheets
void sendData(float led, int bat) {
  Serial.println("==========");
  Serial.print("connecting to ");
  Serial.println(host);
  
  //----------------------------------------Connect to Google host
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }
  //----------------------------------------

  //----------------------------------------Processing data and sending data
  // Only if led change ?????
  String string_colorled =  String(led);
  String string_levelbat =  String(bat, DEC); 
  String url = "/macros/s/" + GAS_ID + "/exec?colorled=" + string_colorled + "levelbat =" + string_levelbat²;
  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
         "Host: " + host + "\r\n" +
         "User-Agent: BuildFailureDetectorESP8266\r\n" +
         "Connection: close\r\n\r\n");

  Serial.println("request sent");
  //----------------------------------------

  //----------------------------------------Checking whether the data was sent successfully or not
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  String line = client.readStringUntil('\n');
  if (line.startsWith("{\"state\":\"success\"")) {
    Serial.println("esp8266/Arduino CI successfull!");
  } else {
    Serial.println("esp8266/Arduino CI has failed");
  }
  Serial.print("reply was : ");
  Serial.println(line);
  Serial.println("closing connection");
  Serial.println("==========");
  Serial.println();
  //----------------------------------------


  }  //switch

}  //loop

and basic WiFi examples work?

@louloul,

Your topic was moved to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

It will help you get the best out of the forum in the future.

Thank you

Do you have one ? For test.

in IDE Examples menu for ESP8266WiFi library the WiFiClient example

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.