Use Wifi with arduino

Hi,
I created software for object detection.
Now I would like to send the information by wifi on doc Excel but I have no idea how to develop it.

I would like to send the information only when there is a change in the state of the LED (Red or Green).

Could you help me ?

Thank you.

Here is my code :

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;

#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

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);
}

void loop() {

  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 15x/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
      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
      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;

  }  //switch

}  //loop

Which Arduino board are you using ?

Now "Flora" but I just bought "Uno" with wifi

which Uno with WiFi? there are several different boards

SP8266, ATMEIncome 16U2, WiFi R3

Please post a link to exactly the device that you have

have you considered getting and esp32 or similar board rather than adding a WiFi board to an Arduino AVR board

Yes :+1:

No, I bought this

When it comes to WiFi-connected projects, you're likely to get much more help here when using an ESP32.

"ESP8266, ATMEIncome 16U2, WiFi R3"

that looks like 3 boards in a pack

I confirme it's the Uno R3 ATmega328 et WiFi ESP8266.

It's possible the use Wifi ? or it's more complicated ?

This is like pulling teeth

Please post a link to what you bought

HaiMa Wifi R3 Atmega328P Esp8266 Module Pour Arduino Uno - Noir : Amazon.fr: High-Tech

You have to connect a Wifi module to your Arduino. Either you chose ARduino UNO R4 wifi or connect an ESP01 with your existing board. You can also chose an ESP8266 or ESP32. Then you can send your data through Wifi . Here is an example using the ESP modules.

so it is the" Uno+WiFi" board with the DIP switches

Thank to the tutoriel.
I do this but i have error. Can you help me ?

`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`

Did you forget to use code tags when you posted your sketch ?

Which particular board are you using? ESP32-WROOM or anything else?

I dont know, it s important ?