Test de programmation / code

Hello Marine

code de test de validation de la programmation et de la communication série

explication baud / vitesse / com.

void setup() {
  Serial.begin(115200); // attention baud a selectionner
}

void loop() {
  Serial.println("Bonjour Marine");
  delay(1000);
  Serial.println("");
}

Avant de compiler il faut choisir la bonne carte

Copier coller le code dans l'ide Arduino ensuite -> utiliser le V pour compiler -> et la flèche pour envoyer le code. ( l'usb doit bien être branché ainsi que le port COM)

Servo

// 150 bas complet 
// 110 plat 
// 60 haut 

#include <Servo.h>

Servo myservo;
unsigned int val = 60; // defaut 
int pos; 

void setup() {
  myservo.attach(13);  // PIN13 - IO13 // rouge vin // gnd brun // signal orange 
  Serial.begin(115200);
}

void loop() {

  Serial.println("90");
  myservo.write(90);
  delay(3000);

  Serial.println("0");
  myservo.write(0);
  delay(10000);

  /* valeur via la console */

  /*
  if (Serial.available() > 0) {
    int tmp = Serial.parseInt();
    if ( tmp != 0 ) val = tmp;
    Serial.println();
    Serial.print("val : ");
    Serial.print(val);
    Serial.println();
  } 
  myservo.write(val);
  delay(1000);
  */

  /* boucle up ou down */
  /*
  for (pos = 60; pos <= 150; pos += 1) { // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
  }
  
  for (pos = 150; pos >= 60; pos -= 1) { // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
  }*/
}


void upbt(){
  Serial.println("servo up");
  for (pos = 150; pos >= 60; pos -= 1){
      myservo.write(pos);              
      delay(15);                      
  }
}

void downbt(int delaymot){
  Serial.println("servo down");
   for (pos = 60; pos <= 150; pos += 1){
    myservo.write(pos);
    delay(delaymot);
   }
}

valider les valeurs du servo / magnétisation

code 60 150 

Bouton

valider les états du bouton ( haut / bas )

#include <AceButton.h>
using namespace ace_button;
const int BUTTON_PIN = 21;

AceButton btn(BUTTON_PIN);

void setup() {
  Serial.begin(115200);
  Serial.println("SSSTTTARRTTTT !!!! ");
  pinMode(BUTTON_PIN, INPUT_PULLUP);  // pin GND - IO21 
  btn.setEventHandler(handleEvent);
}

void loop() {
    btn.check();
    delay(10);
}

void handleEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) {
  switch (eventType) {
    case AceButton::kEventPressed:
      Serial.println("Bt pressé bas");
      break;
    case AceButton::kEventReleased:
      Serial.println("Bt relaché");
      break;
  }
}

Code de poke poke

création d 'un compte chez Flespi

récupération de l'id.

Modification des variables de bases : dans le code :

// librairie 
#include <WiFi.h>
#include <PubSubClient.h>
#include <AceButton.h> // gestion des boutons 
#include <Servo.h> // gestion des servo moteur 

using namespace ace_button; // gestion du boutton
Servo myservo; 
int pos ; // position du servo

#define DEV_TYPE 0    // "0" pour le ESP32 maitre et "1" pour le ESP32 client   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

const int BUTTON_PIN = 21; // pin switch "end type" 

// Replace the next variables with your SSID/Password combination
const char* ssid = "SSID_WIFI"; /// VOTRE WIFI !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* password = "MOT_DE_PASSE_DU_WIFI"; /// VOTRE MOT DE PASSE WIFI !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* mqtt_server = "mqtt.flespi.io"; // TOUCHE PAS A CA 
const char* topicsperso0 = "poke/IciVotreSpeudo/0"; // a changer avec speudo!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* topicsperso1 = "poke/IciVotreSpeudo/1"; // a changer avec speudo!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* HostName0 = "POKEBT01IciVotreSpeudo"; // NOM DU PREMIERE BLOC !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* HostName1 = "POKEBT02IciVotreSpeudo"; // NOM DU DEUXIEME BLOC !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* mqttUser = "ab6WsP*"; // a laisser ou changer en fct de votre compte cloud. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* mqttPassword = "";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
long lastBt = 0;

AceButton btn(BUTTON_PIN);

void setup() {
  Serial.begin(115200);
  Serial.println("\n\r\n\r\n\r\n\r\n\r\n\r\n\r");

  myservo.attach(13);  // pin 13 pour le servo

#if DEV_TYPE == 0
  Serial.print("---------------------- ");
  Serial.print(HostName0);
  Serial.println(" ----------------------");
#elif DEV_TYPE == 1
  Serial.print("---------------------- ");
  Serial.print(HostName1);
  Serial.println("---------------------- ");
#endif

  downbt(15);
  upbt();

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  btn.setEventHandler(handleEvent);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

#if DEV_TYPE == 0
      client.publish(topicsperso0, "push");
      Serial.println("poke/votre_topic/0 - push");
#elif DEV_TYPE == 1
      client.publish(topicsperso1, "push");
      Serial.println("poke/votre_topic/1 - push");
#endif

  Serial.println("en attente ...");
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to wifi : ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;

  if (millis() - lastMsg > 2000) {
  lastMsg = millis();
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();
#if DEV_TYPE == 0
   if (String(topic) == topicsperso1) {
      Serial.print("poke/votre_topic/1 ");
      Serial.println(messageTemp);
      if (messageTemp == "push") {
        upbt();
      }
   }
#elif DEV_TYPE == 1
    if (String(topic) == topicsperso0) {
      Serial.print("poke/votre_topic/0 ");
      Serial.println(messageTemp);
      if (messageTemp == "push") {
        upbt();
      }
   }
#endif
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
#if DEV_TYPE == 0
    if (client.connect(HostName0, mqttUser, mqttPassword)) {
      Serial.println("connected");
      client.subscribe(topicsperso1);
#elif DEV_TYPE == 1
    if (client.connect(HostName1, mqttUser, mqttPassword)) {
      Serial.println("connected");
      client.subscribe(topicsperso0);
#endif
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop() {
  btn.check();
  
  if (!client.connected()) {
    reconnect();
  }
  
  client.loop();
}

void handleEvent(AceButton* button, uint8_t eventType,
                 uint8_t buttonState) {
  switch (eventType) {
    case AceButton::kEventPressed:
      if (millis() - lastBt > 500) {
        lastBt = millis();
        Serial.println("switch bas");
        downbt(1);
#if DEV_TYPE == 0
        client.publish(topicsperso0, "push");
        Serial.println("poke/votre_topic/0 - push");
        }
#elif DEV_TYPE == 1
        client.publish(topicsperso1, "push");
        Serial.println("poke/votre_topic/1 - push");
        }
#endif
      break;
    case AceButton::kEventReleased:
      Serial.println("switch haut");
      break;
  }
}

void upbt(){
  Serial.println("servo up");
  for (pos = 150; pos >= 60; pos -= 1){
      myservo.write(pos);              
      delay(15);                      
  }
}

void downbt(int delaymot){
  Serial.println("servo down");
   for (pos = 60; pos <= 150; pos += 1){
    myservo.write(pos);
    delay(delaymot);
   }
}

upload du code -> test avec les id fournis

Attention les valeurs a changer !

// Replace the next variables with your SSID/Password combination
const char* ssid = "SSID_WIFI"; /// VOTRE WIFI !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* password = "MOT_DE_PASSE_DU_WIFI"; /// VOTRE MOT DE PASSE WIFI !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* mqtt_server = "mqtt.flespi.io"; // TOUCHE PAS A CA 
const char* topicsperso0 = "poke/IciVotreSpeudo/0"; // a changer avec speudo!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* topicsperso1 = "poke/IciVotreSpeudo/1"; // a changer avec speudo!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* HostName0 = "POKEBT01IciVotreSpeudo"; // NOM DU PREMIERE BLOC !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* HostName1 = "POKEBT02IciVotreSpeudo"; // NOM DU DEUXIEME BLOC !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* mqttUser = "ab6WsP*"; // a laisser ou changer en fct de votre compte cloud. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const char* mqttPassword = "";

changement des valeurs pour le 2ᵉ poke.

Dernière mise à jour