User Tools

Site Tools


maakplek_statusmelder

This is an old revision of the document!


De ESP8266 maakt verbinding met wifi en stuurt bij het omhalen van de schakelaar een bericht naar de twitter API van pushingbox. Zijn 3.3v voeding krijgt hij via een spanningsregelaar die is aangesloten op de vcc uitgang van de pro micro.
De arduino pro micro is via USB-OTG verbonden met een lader en de telefoon zodat de telefoon oplaad en via muis/keyboard te besturen is. De pro micro is net zoals de leonardo door zijn ATmega32U4 processor direct via USB te gebruiken en kan dus ook via USB een HID apparaat nadoen.
Via deze methode wordt via keyboard commando's een bericht naar whatsapp gestuurd. De telefoon is voorzien van een prepay simkaartje (5 euro voor 7,50 beltegoed, minimaal 1x per kwartaal een telefoontje of sms, 10 ct per sms) en stuurt via een app elke maand een sms om het nummer en beltegoed te behouden.

code voor de esp8266:

#include <ESP8266WiFi.h>

#define AP_SSID     "Maakplek"
#define AP_PASSWORD "ikbennietdeslimste@maakplek.nl"

#define API_SERVER  "api.pushingbox.com"
#define API_KEY     "bekendbijJasper"

#define ledPin 0
#define switchPin 2

int switchState = false;
int lastSwitchState = false;
int lastConnected = false;

void setup() {
  Serial.begin(115200);
  pinMode(switchPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  wifiConnect();
}

void loop() {
  switchState = digitalRead(switchPin);
  if (switchState != lastSwitchState) {
    if (switchState == HIGH) {
      WiFiClient client;
      if (!client.connect(API_SERVER, 80))
      {
        Serial.println("connection failed");
        return;
      }
      client.print("GET /pushingbox?devid=");
      client.print(API_KEY);
      client.print("&state=open");
      client.println(" HTTP/1.1");
      client.print("Host: ");
      client.println(API_SERVER);
      client.println();

      digitalWrite(ledPin, HIGH);

      Serial.println("open");

      delay(10);
    }
    else {
      WiFiClient client;
      if (!client.connect(API_SERVER, 80))
      {
        Serial.println("connection failed");
        return;
      }
      client.print("GET /pushingbox?devid=");
      client.print(API_KEY);
      client.print("&state=gesloten");
      client.println(" HTTP/1.1");
      client.print("Host: ");
      client.println(API_SERVER);
      client.println();

      digitalWrite(ledPin, LOW);

      Serial.println("gesloten");

      delay(10);
    }

  }

  lastSwitchState = switchState;

}
void wifiConnect()
{
  Serial.print( "Connecting to " );
  Serial.print( AP_SSID );
  WiFi.begin( AP_SSID, AP_PASSWORD );
  while ( WiFi.status() != WL_CONNECTED )
  {
    delay( 1000 );
    Serial.print( "." );
  }

  Serial.println( "" );
  Serial.println( "WiFi connected" );

  Serial.println( WiFi.localIP( ) );
}

Code voor de Arduino Pro Mini:

#include "Keyboard.h"

#define buttonPin 3    // the number of the pushbutton pin
#define enablePin 4    // the number of the enable switch

int buttonState = LOW;       // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
int maakplekState = LOW;

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(enablePin, INPUT_PULLUP);
  
Serial.begin(9600);
Serial.println("start");
Keyboard.begin();
}


void sendOpen(){
    Keyboard.press (KEY_ESC); Keyboard.releaseAll(); delay(1000);   
    Keyboard.press (KEY_ESC); Keyboard.releaseAll(); delay(1000);       
    
    Keyboard.press (KEY_LEFT_SHIFT); Keyboard.print("whatsapp");Keyboard.releaseAll(); delay(1000);
    Keyboard.press (KEY_RETURN); Keyboard.releaseAll(); delay(5000); 
      
    Keyboard.print("maakplek"); delay(2000);
    Keyboard.press (KEY_DOWN_ARROW); Keyboard.releaseAll(); delay(1000);   
    Keyboard.press (KEY_DOWN_ARROW); Keyboard.releaseAll(); delay(1000);   
    Keyboard.press (KEY_RETURN); Keyboard.releaseAll(); delay(5000);   
   
    Keyboard.print("Maakplek is geopend!");delay(1000);
    Keyboard.press (KEY_RIGHT_ARROW); Keyboard.releaseAll(); delay(1000);   
    Keyboard.press (KEY_RIGHT_ARROW); Keyboard.releaseAll(); delay(1000);   
    Keyboard.press (KEY_RETURN); Keyboard.releaseAll(); delay(5000);   
    Keyboard.press (KEY_ESC); Keyboard.releaseAll(); delay(1000);       
    Keyboard.press (KEY_ESC); Keyboard.releaseAll(); delay(1000);       
    Keyboard.press (KEY_ESC); Keyboard.releaseAll(); delay(1000);       
 }

void sendClosed(){
    Keyboard.press (KEY_ESC); Keyboard.releaseAll(); delay(1000);   
    Keyboard.press (KEY_ESC); Keyboard.releaseAll(); delay(1000);       
    
    Keyboard.press (KEY_LEFT_SHIFT); Keyboard.print("whatsapp");Keyboard.releaseAll(); delay(1000);
    Keyboard.press (KEY_RETURN); Keyboard.releaseAll(); delay(5000); 
      
    Keyboard.print("maakplek"); delay(2000);
    Keyboard.press (KEY_DOWN_ARROW); Keyboard.releaseAll(); delay(1000);   
    Keyboard.press (KEY_DOWN_ARROW); Keyboard.releaseAll(); delay(1000);   
    Keyboard.press (KEY_RETURN); Keyboard.releaseAll(); delay(5000);   
   
    Keyboard.print("Maakplek is gesloten!");delay(1000);
    Keyboard.press (KEY_RIGHT_ARROW); Keyboard.releaseAll(); delay(1000);   
    Keyboard.press (KEY_RIGHT_ARROW); Keyboard.releaseAll(); delay(1000);   
    Keyboard.press (KEY_RETURN); Keyboard.releaseAll(); delay(5000);   
    Keyboard.press (KEY_ESC); Keyboard.releaseAll(); delay(1000);       
    Keyboard.press (KEY_ESC); Keyboard.releaseAll(); delay(1000);       
    Keyboard.press (KEY_ESC); Keyboard.releaseAll(); delay(1000);       
 }


void loop() {
    int enableState = digitalRead(enablePin);
    if (enableState == 0) {
      Serial.println("enabled");
      int buttonState = digitalRead(buttonPin);
      if (buttonState == 1){
        if (maakplekState != buttonState){
        Serial.println("Maakplek Open");
        sendOpen();  
        maakplekState = HIGH;  
        }
      }
      else
      {
        if (maakplekState != buttonState){
        Serial.println("Maakplek Dicht");
        sendClosed();  
        maakplekState = LOW;  
        }
      }
    }
    else {
    Serial.println("disabled");
    }  
}
maakplek_statusmelder.1552486494.txt.gz · Last modified: 2022/09/29 21:25 (external edit)