Étape 2: Le Web côté des choses
Écureuil d’imp (adapté d’une source en ligne que je peux trouver n’est plus...) :
================================================
Server.Show("") ;
télécommande pour rover
ledState < - 0 ;
fonction blink()
{
Changer l’État
ledState = ledState? 0:1 ;
Server.log (« ledState val: "+ ledState) ;
Reflètent l’état de la broche
Hardware.PIN9.Write(ledState) ;
}
classe d’entrée pour le canal de commande LED
inputHTTP classe étend InputPort
{
nom = « réglage de la puissance »
type = « nombre »
fonction set(httpVal)
{
Server.log ("reçu val:" + httpVal) ;
if(httpVal == 1) {}
Hardware.PIN9.Write(1) ;
IMP.Sleep(0.1) ;
Hardware.PIN9.Write(0) ;
}
d’autre if(httpVal == 2) {}
Hardware.PIN8.Write(1) ;
IMP.Sleep(0.1) ;
Hardware.PIN8.Write(0) ;
}
d’autre if(httpVal == 3) {}
Hardware.PIN2.Write(1) ;
IMP.Sleep(0.1) ;
Hardware.PIN2.Write(0) ;
}
d’autre if(httpVal == 4) {}
Hardware.PIN1.Write(1) ;
IMP.Sleep(0.1) ;
Hardware.PIN1.Write(0) ;
}
else {}
;
}
}
}
function watchdog() {}
IMP.Wakeup(60,Watchdog) ;
Server.log(httpVal) ;
}
démarrer l’écriture de chien de garde toutes les 60 secondes
Watchdog() ;
Configurer les broches comme une sortie de drain ouvert avec arrachage vers le haut
Hardware.PIN9.configure(DIGITAL_OUT_OD_PULLUP) ;
Hardware.PIN8.configure(DIGITAL_OUT_OD_PULLUP) ;
Hardware.PIN2.configure(DIGITAL_OUT_OD_PULLUP) ;
Hardware.PIN1.configure(DIGITAL_OUT_OD_PULLUP) ;
Enregistrer avec le serveur
IMP.configure (« Reomote Control pour Rover », [inputHTTP()], []) ;
================================================
Code de l’Arduino (Merci Chris Martin!) …
================================================
/*
AnalogReadSerial
Lit une broche d’entrée sur analogique 0, imprime le résultat dans le moniteur de la série.
Fixer la goupille de centrage d’un potentiomètre à broches A0 et l’extérieur épingles à + 5V et le sol.
Cet exemple de code est dans le domaine public.
*/
pinf int = 2 ;
pinl int = 12 ;
pinr int = 10 ;
pinb int = 9 ;
#define LmotorA 3 / / gauche pont moteur H, entrée A
#define LmotorB 11 / / gauche pont moteur H, entrée B
#define RmotorA 5 / / droite pont moteur H, entrée A
#define RmotorB 6 / / Right motor H bridge, entrée B
#define v 255
#include < Servo.h >
Servo myservo ;
int mené = 12 ;
int pos = 0 ;
la routine d’installation s’exécute une fois que lorsque vous appuyez sur reset :
void setup() {}
myservo.Attach(9) ;
pinMode (led, sortie) ;
pinMode(pinf,INPUT) ; initialiser une communication série 9600 bits par seconde :
pinMode(pinl,INPUT) ;
pinMode(pinr,INPUT) ;
pinMode(pinb,INPUT) ;
Serial.Begin(9600) ;
digitalWrite(pinf,LOW) ;
digitalWrite(pinl,LOW) ;
digitalWrite(pinr,LOW) ;
digitalWrite(pinb,LOW) ;
288000
C’est différent sur le serial monitor ne sais pas si c’est vers le haut ou vers le bas
Serial.Begin(14400) ;
}
lls int = 0 ;
rls int = 0 ;
al int = 0 ;
la routine de la boucle s’exécute maintes et maintes fois pour toujours :
void loop() {}
lire l’entrée sur la broche analogique 0 :
int sensorValue1 = digitalRead(pinf) ;
int sensorValue2 = digitalRead(pinl) ;
int sensorValue3 = digitalRead(pinr) ;
int sensorValue4 = digitalRead(pinb) ;
afficher la valeur que vous lisez :
Serial.Print(sensorValue1) ;
Serial.Print(":") ;
Serial.Print(sensorValue2) ;
Serial.Print(":") ;
Serial.Print(sensorValue3) ;
Serial.Print(":") ;
Serial.println(sensorValue4) ;
Delay(25) ; délai entre les lectures de stabilité
Si (sensorValue1 == 1) {}
analogWrite(RmotorA,0) ;
analogWrite(RmotorB,120) ;
analogWrite(LmotorA,0) ;
analogWrite(LmotorB,120) ;
retard (500) ;
analogWrite(RmotorA,0) ;
analogWrite(RmotorB,0) ;
analogWrite(LmotorA,0) ;
analogWrite(LmotorB,0) ;
myservo.Write(10) ;
retard (500) ;
}
else {}
analogWrite(RmotorA,0) ;
analogWrite(RmotorB,0) ;
analogWrite(LmotorA,0) ;
analogWrite(LmotorB,0) ;
}
Si (sensorValue2 == 1) {}
digitalWrite (conduit, en haut) ;
analogWrite(RmotorA,0) ;
analogWrite(RmotorB,250) ;
analogWrite(LmotorA,250) ;
analogWrite(LmotorB,0) ;
retard (100) ;
analogWrite(RmotorA,0) ;
analogWrite(RmotorB,0) ;
analogWrite(LmotorA,0) ;
analogWrite(LmotorB,0) ;
myservo.Write(10) ;
retard (500) ;
}
d’autre
{
analogWrite(RmotorA,0) ;
analogWrite(RmotorB,0) ;
analogWrite(LmotorA,0) ;
analogWrite(LmotorB,0) ;
}
Si (sensorValue4 == 1) {}
digitalWrite (conduit, en haut) ;
analogWrite(RmotorA,250) ;
analogWrite(RmotorB,0) ;
analogWrite(LmotorA,0) ;
analogWrite(LmotorB,250) ;
retard (100) ;
analogWrite(RmotorA,0) ;
analogWrite(RmotorB,0) ;
analogWrite(LmotorA,0) ;
analogWrite(LmotorB,0) ;
myservo.Write(10) ;
retard (500) ;
}
d’autre
{
analogWrite(RmotorA,0) ;
analogWrite(RmotorB,0) ;
analogWrite(LmotorA,0) ;
analogWrite(LmotorB,0) ;
}
Si (sensorValue3 == 1) {}
digitalWrite (conduit, en haut) ;
analogWrite(RmotorA,120) ;
analogWrite(RmotorB,0) ;
analogWrite(LmotorA,120) ;
analogWrite(LmotorB,0) ;
retard (500) ;
analogWrite(RmotorA,0) ;
analogWrite(RmotorB,0) ;
analogWrite(LmotorA,0) ;
analogWrite(LmotorB,0) ;
myservo.Write(10) ;
retard (500) ;
}
d’autre
{
analogWrite(RmotorA,0) ;
analogWrite(RmotorB,0) ;
analogWrite(LmotorA,0) ;
analogWrite(LmotorB,0) ;
}
}
================================================
Maintenant pour l’interface utilisateur. Décompressez le dossier attaché ; tout le code nécessaire devrait être là. Tout ce que vous devez faire est de coller dans votre API d’Imp électrique unique dans le fichier index.html dans l’espace approprié. J’ai marqué cela dans le fichier.