Bonjours monsieur j'ai un problème avec le codage de code que j'ai récupéré sur Internet voici le code
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h>
#include <ArduinoJson.h>
#include <StreamString.h>
#define API_KEY "" // TODO: Passez à votre clé API sinrique. Votre clé API est affichée sur le tableau de bord sinric.com
#define SSID_NAME "" // TODO: Changez le SSID de votre réseau Wifi
#define WIFI_PASSWORD "" // TODO: Changez le mot de passe de votre réseau Wifi
#define SERVER_URL "iot.sinric.com"
#define SERVER_PORT 80
#define HEARTBEAT_INTERVAL 300000 // 5 minutes
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
Client WiFiClient;
uint64_t heartbeatTimestamp = 0;
booléen isConnected = false;
void setPowerStateOnServer (String deviceId, String value);
void setTargetTemperatureOnServer (String deviceId, String value, String scale);
#define PIN_RED 5
#define PIN_GREEN 6
#define PIN_BLUE 3
void setup () {
Serial.begin (115200);
pinMode (PIN_RED, OUTPUT);
pinMode (PIN_GREEN, OUTPUT);
pinMode (PIN_BLUE, OUTPUT);
WiFiMulti.addAP (SSID_NAME, WIFI_PASSWORD);
Serial.println ();
Serial.print ("Connexion au Wifi:");
Serial.println (SSID_NAME);
// En attente de connexion Wifi
pendant (WiFiMulti.run ()! = WL_CONNECTED) {
delay (500);
Serial.print (".");
}
if (WiFiMulti.run () == WL_CONNECTED) {
Serial.println ("");
Serial.print ("WiFi connecté.");
Serial.print ("adresse IP:");
Serial.println (WiFi.localIP ());
}
// adresse du serveur, port et URL
webSocket.begin (SERVER_URL, SERVER_PORT, "/");
// gestionnaire d'événements
webSocket.onEvent (webSocketEvent);
webSocket.setAuthorization ("apikey", API_KEY);
// réessayez toutes les 5000 ms si la connexion a échoué
webSocket.setReconnectInterval (5000); // Si vous voyez que 'class WebSocketsClient' n'a aucun membre nommé 'setReconnectInterval' erreur de mise à jour arduinoWebSockets
}
boucle void () {
webSocket.loop ();
if (isConnected) {
uint64_t now = millis ();
// Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
heartbeatTimestamp = now;
webSocket.sendTXT("H");
}
}
}
void turnOn (String deviceId) {
if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID du premier périphérique
{
Serial.print ("Turn on device id:");
Serial.println (deviceId);
}
}
void turnOff (String deviceId) {
if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID du premier périphérique
{
Serial.print ("Turn off Device ID:");
Serial.println (deviceId);
}
}
void webSocketEvent (WStype_t type, uint8_t * payload, size_t length) {
switch (type) {
case WStype_DISCONNECTED:
isConnected = false;
Serial.printf ("[webSocketEvent] Webservice déconnecté du serveur! \ N");
Pause;
case WStype_CONNECTED: {
isConnected = true;
Serial.printf ("[webSocketEvent] Service connecté au serveur à l'url:% s \ n", charge utile);
Serial.printf ("[webSocketEvent] En attente de commandes du serveur ... \ n");
}
pause;
case WStype_TEXT: {
Serial.printf ("[webSocketEvent] obtenir le texte:% s \ n", charge utile);
DynamicJsonBuffer jsonBuffer;
JsonObject & json = jsonBuffer.parseObject ((char *) payload);
Chaîne deviceId = json ["
String action = json ["action"];
if(action == "setPowerState") {
// alexa, turn on tv ==> {"deviceId":"xx","action":"setPowerState","value":"ON"}
String value = json ["value"];
if(value == "ON") {
turnOn(deviceId);
} else {
turnOff(deviceId);
}
}
else if(action == "AdjustBrightness") {
// alexa, dim lights ==>{"deviceId":"xxx","action":"AdjustBrightness","value":-25}
}
else if(action == "AdjustBrightness") {
// alexa, dim lights ==>{"deviceId":"xx","action":"AdjustBrightness","value":-25}
}
else if(action == "SetBrightness") {
//alexa, set the lights to 50% ==> {"deviceId":"xx","action":"SetBrightness","value":50}
}
else if(action == "SetColor") {
//alexa, set the lights to red ==> {"deviceId":"xx","action":"SetColor","value":{"hue":0,"saturation":1,"brightness":1}}
String hue = json ["value"]["hue"];
String saturation = json ["value"]["saturation"];
String brightness = json ["value"]["brightness"];
Serial.println("[WSc] hue: " + hue);
Serial.println("[WSc] saturation: " + saturation);
Serial.println("[WSc] brightness: " + brightness);
long color = HSBtoRGB(atof(hue), atof(saturation), atof(brightness));
// Get the red, blue and green parts from generated color
int red = color >> 16 & 255;
int green = color >> 8 & 255;
int blue = color & 255;
setColor(red, green, blue);
}
else if(action == "IncreaseColorTemperature") {
//alexa, set the lights softer ==> {"deviceId":"xxx","action":"IncreaseColorTemperature"}
}
else if(action == "IncreaseColorTemperature") {
//alexa, set the lights softer ==> {"deviceId":"xxx","action":"IncreaseColorTemperature"}
}
else if(action == "SetColorTemperature") {
//alexa, set the lights softer ==> {"deviceId":"xxx","action":"SetColorTemperature","value":2200}
}
}
break;
case WStype_BIN:
Serial.printf("[webSocketEvent] get binary length: %u\n", length);
break;
}
}
// Si vous comptez utiliser un bouton-poussoir pour activer / désactiver manuellement le commutateur, utilisez cette fonction pour mettre à jour l'état sur le serveur
// afin qu'il se reflète sur l'application Alexa.
// par exemple: setPowerStateOnServer ("deviceid", "ON")
// Appel UNIQUEMENT si l'état a changé. NE PAS APPELER CECI IN loop () et surcharger le serveur.
void setPowerStateOnServer (String deviceId, String value) {
DynamicJsonBuffer jsonBuffer;
JsonObject & root = jsonBuffer.createObject ();
root ["deviceId"] = deviceId;
root ["action"] = "setPowerState";
racine ["valeur"] = valeur;
StreamString databuf;
root.printTo (databuf);
webSocket.sendTXT (databuf);
}
void setColor (caractère non signé rouge, vert caractère non signé, bleu char non signé)
{
analogWrite (PIN_RED, rouge);
analogWrite (PIN_GREEN, vert);
analogWrite (PIN_BLUE, bleu);
}
long HSBtoRGB (float _hue, float _sat, float _brightness) {
float red = 0.0;
float green = 0,0;
bleu flottant = 0,0;
if (_sat == 0.0) {
red = _brightness;
green = _brightness;
blue = _brightness;
} else {
if (_hue == 360.0) {
_hue = 0;
}
int slice = _hue / 60.0;
float hue_frac = (_hue / 60.0) - slice;
float aa = _brightness * (1.0 - _sat);
float bb = _brightness * (1.0 - _sat * hue_frac);
float cc = _brightness * (1.0 - _sat * (1.0 - hue_frac));
switch(slice) {
case 0:
red = _brightness;
green = cc;
blue = aa;
break;
case 1:
red = bb;
green = _brightness;
blue = aa;
break;
case 2:
red = aa;
green = _brightness;
blue = cc;
break;
case 3:
red = aa;
green = bb;
blue = _brightness;
break;
case 4:
red = cc;
green = aa;
blue = _brightness;
break;
case 5:
red = _brightness;
green = aa;
blue = bb;
break;
default:
red = 0.0;
green = 0.0;
blue = 0.0;
break;
}
}
long ired = red * 255.0;
long igreen = green * 255.0;
long iblue = blue * 255.0;
return long((ired << 16) | (igreen << 8) | (iblue));
}
Pouvez vous m'aidez merci d'avance