mirror of
https://github.com/digistump/DigistumpArduino.git
synced 2025-04-28 15:49:02 -07:00
128 lines
4.6 KiB
Arduino
128 lines
4.6 KiB
Arduino
![]() |
#include <DigiKeyboard.h>
|
||
|
void setup() {
|
||
|
// put your setup code here, to run once:
|
||
|
wifiDelay(10000); //wait a good amount of time for moduel to connect to wifi - this script assumes it can connect and doesn't check
|
||
|
Serial.begin(600);
|
||
|
|
||
|
/*==========================================================
|
||
|
= NOTE: This assumes you have used the wifi module =
|
||
|
= web interface to set it up as a client and 600 baud =
|
||
|
= See the wifi shield page for more info. =
|
||
|
= =
|
||
|
= NOTE YOU MAY NEED TO CHANGE THE begin STATEMENT AND =
|
||
|
= THE MODULE TO A LOWER BAUD IF YOU EXPERIENCE SCRAMBLED=
|
||
|
= OUTPUT - see notes in the wiki on how to set these =
|
||
|
= =
|
||
|
= If not useing DigiKeyboard change wifiDelay as noted =
|
||
|
==========================================================*/
|
||
|
|
||
|
if(wifiConnect(F("requestb.in"))){ //host wrapped in F() to save ram
|
||
|
|
||
|
if(wifiSendPost(F("requestb.in"),F("/12tefnq1"),F("test=test123"))){ //host and path and parameters wrapped in F() to save ram
|
||
|
|
||
|
//uncomment one of the three test methods below to try one out, only one will work at a time
|
||
|
//you could also just ignore all of this - we know we got headers back so we're probably good and out data was sent
|
||
|
//TODO: read the status code to be sure
|
||
|
|
||
|
/*
|
||
|
//YOU CAN READ IT OUT ONE CHAR AT A TIME AND PRINT IT
|
||
|
while(!Serial.available()){wifiDelay(10);} //wait for some data
|
||
|
while(Serial.available()){ //as long as there is data read it and do something with it
|
||
|
DigiKeyboard.write(Serial.read()); //in this case we just type it out
|
||
|
}
|
||
|
*/
|
||
|
|
||
|
/*
|
||
|
//OR YOU CAN FIND SOMETHING AND THEN READ AFTER THAT
|
||
|
//try this with ip.jsontest.com
|
||
|
//See also: Serial.findUntil, Serial.readBytesUntil, Serial.readStringUntil, Serial.find, etc
|
||
|
Serial.find(": \""); //find the start of where the ip is shown
|
||
|
String ip = Serial.readStringUntil('"'); //read until the end of the ip which is the "
|
||
|
DigiKeyboard.print(ip); //type it out for the demo
|
||
|
*/
|
||
|
|
||
|
/*
|
||
|
//OR YOU CAN GRAB IT ALL AT ONCE
|
||
|
//ASSUMING THE BODY ISN'T LARGER THAN AVAILABLE RAME
|
||
|
String body = Serial.readString(); //read it all to a string
|
||
|
DigiKeyboard.print(body.trim()); //type it out for the demo - but first trim the whitespace off the start and end
|
||
|
*/
|
||
|
}
|
||
|
else{
|
||
|
//we didn't get a response to our GET - something failed
|
||
|
DigiKeyboard.write('1');
|
||
|
}
|
||
|
|
||
|
}
|
||
|
else{
|
||
|
//we couldn't talk to the wifi module properly - see wifiConnect for possible break points
|
||
|
DigiKeyboard.write('2');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
void loop() {
|
||
|
// put your main code here, to run repeatedly:
|
||
|
|
||
|
}
|
||
|
|
||
|
void wifiDelay(long time){
|
||
|
/*
|
||
|
If you are not using DigiKeyboard in you sketch
|
||
|
change this by commenting out the DigiKeyboard delay and
|
||
|
uncommenting the regular delay
|
||
|
Also remove the DigiKeyboard.h include at the top
|
||
|
Note: This sketch will not run with DigiKeyboard in use
|
||
|
unless connected to a computer
|
||
|
*/
|
||
|
DigiKeyboard.delay(time);
|
||
|
//delay(time);
|
||
|
}
|
||
|
|
||
|
|
||
|
bool wifiConnect(String host){
|
||
|
wifiDelay(50);
|
||
|
Serial.write("+++");
|
||
|
if (!Serial.find("a")) return false; //error in entering AT mode - try restoring module and then setting settings again
|
||
|
Serial.write("a");
|
||
|
if (!wifiWaitForOK()) return false; //error in entering AT mode
|
||
|
Serial.print(F("AT+NETP=TCP,CLIENT,80,"));
|
||
|
Serial.print(host);
|
||
|
Serial.print(F("\r"));
|
||
|
if (!wifiWaitForOK()) return false; //error in setting host
|
||
|
Serial.print(F("AT+ENTM\r"));
|
||
|
if (!wifiWaitForOK()) return false; //error in going back to transparent mode
|
||
|
wifiDelay(1000); //instead of calling TCPLK to check for a link we just wait a good amount of time for a link, because TCPLK can be buggy
|
||
|
while(Serial.read()!=-1); //empty the read buffer so we are ready for a clean GET OR POST
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool wifiWaitForOK(){
|
||
|
return Serial.find("k");
|
||
|
}
|
||
|
|
||
|
|
||
|
bool wifiSendPost(String host, String path, String data){
|
||
|
Serial.print(F("POST "));
|
||
|
Serial.print(path);
|
||
|
Serial.println(F(" HTTP/1.1"));
|
||
|
Serial.print(F("Host: "));
|
||
|
Serial.println(host);
|
||
|
Serial.println(F("Content-Type: application/x-www-form-urlencoded"));
|
||
|
Serial.print(F("Content-Length: "));
|
||
|
Serial.println(data.length());
|
||
|
Serial.println();
|
||
|
Serial.println(data);
|
||
|
Serial.println();
|
||
|
Serial.setTimeout(5000);
|
||
|
if(!Serial.find("\r\n\r")) //skip the header - if this fail then the GET probably did too
|
||
|
return false;
|
||
|
Serial.setTimeout(1000);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|