Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <PLDuino.h>
- #include <PLDuinoGUI.h>
- #include <TMRpcm_PLDuino.h>
- #include <SPI.h>
- #include <SD.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_ILI9341.h>
- #include <PLDuino.h>
- #include <PLDTouch.h>
- #include <PLDuinoGUI.h>
- #include <using_namespace_PLDuinoGUI.h>
- #include <DS3232RTC.h>
- #include <TimeLib.h>
- #include <Wire.h>
- #include <avr/io.h>
- #include <SPI.h>
- #include <SD.h>
- #include "WiFiEsp.h"
- #include <WiFiEspUdp.h>
- #include <EEPROM.h>
- #define FILENAME "xtre.cfg"
- File f;
- WiFiEspUDP udp;
- int status = WL_IDLE_STATUS;
- String s = "";
- String is = "" ;
- boolean sc = false;
- bool CFG_OK = false;
- String ssid, pass;
- unsigned int localPort = 9999; // local port to listen on
- unsigned int rPort = 9996; // local port to listen on
- IPAddress Ip(192, 168, 100, 255);
- char packetBuffer[255]; // buffer to hold incoming packet
- char ReplyBuffer[] = "___"; // a string to send back
- Label l("", ILI9341_WHITE);
- Adafruit_ILI9341 tft = Adafruit_ILI9341(PLDuino::LCD_CS, PLDuino::LCD_DC);
- PLDTouch touch(PLDuino::TOUCH_CS, PLDuino::TOUCH_IRQ);
- char* s2c(String command){
- if(command.length()!=0){
- char *p = const_cast<char*>(command.c_str());
- return p;
- }
- }
- void showl(String s)
- {
- l.updateText(s, tft);
- }
- void UpdStaticLog(String s, bool visible, uint16_t color)
- {
- //showl(is);
- tft.setCursor(0, 0);
- tft.setTextColor(0x0000);
- tft.println(s);
- if (!visible) return;
- tft.setTextColor(color);
- tft.setCursor(0, 0);
- tft.println(s);
- }
- const char* c_str0(String str) {
- char* buf = new char[str.length()];
- str.getBytes(buf, str.length()+1, 0);
- return reinterpret_cast<const char*>(buf);
- }
- const char* c_str1(String str) {
- char* buf = new char[str.length()];
- str.getBytes(buf, str.length()+1, 0);
- strcpy(reinterpret_cast<const char*>(buf),buf);
- return buf;
- }
- void setup() {
- PLDuino::init();
- PLDuino::enableLCD();
- PLDuino::enableESP();
- tft.begin();
- tft.fillScreen(ILI9341_BLACK);
- tft.setRotation(3);
- tft.setTextSize(1);
- // l.setPositionAndSize(0,10,280,20);
- //l.setTextSize(3);
- // l.draw(tft);
- // initialize serial:
- Serial.begin(115200);
- Serial2.begin(115200);
- WiFi.init(&Serial2);
- while (!SD.begin(PLDuino::SD_CS))
- {
- UpdStaticLog("Failed to initialize SD card. Please check it.", true, 0xF800);
- delay(1000);
- UpdStaticLog("Failed to initialize SD card. Please check it.", false, 0x0000);
- delay(100);
- }
- UpdStaticLog("Failed to initialize SD card. Please check it.", false, 0x0000);
- UpdStaticLog("SD card init OK. Loading config...", true, 0xFFFF);
- File f = SD.open(FILENAME, FILE_READ);
- String fs = "SHITTTTTTTTTTT";
- if (!f)
- {
- // Stop on error
- UpdStaticLog("Failed to open " FILENAME ". Stop.", true, 0xF800);
- return;
- }
- while (f.available() > 0)
- {
- fs = f.readStringUntil('\n');
- if (fs.indexOf("_apc]") > 0)
- {
- ssid = f.readStringUntil('\n');
- pass = f.readStringUntil('\n');
- break;
- }
- }
- f.close();
- delay(1000);
- Serial.println(pass.c_str());
- UpdStaticLog("SD card init OK. Loading config...", false, 0x0000);
- UpdStaticLog("SSID=" + String(ssid) + "\nPASS=" + String(pass), true, 0xFFFF);
- delay(1000);
- UpdStaticLog("SSID=" + String(ssid) + "\nPASS=" + String(pass), false, 0x0000);
- byte l0 = ssid.length()+1;
- byte l1 = pass.length()+1;
- char ssid0[l0];
- char pass0[l1];
- ssid.toCharArray(ssid0,l0);
- pass.toCharArray(pass0,l1);
- if (WiFi.status() == WL_NO_SHIELD) {
- Serial.println("WiFi shield not present");
- // don't continue:
- while (true);
- }
- ssid.replace(0xd,'\0');
- ssid.replace(' ','\0');
- pass.replace(' ','\0');
- pass.replace(0xd,'\0');
- String s0 = "Attempting to connect to WPA SSID: "+String(ssid.c_str());
- while ( status != WL_CONNECTED) {
- UpdStaticLog(s0,false,0x0000);
- UpdStaticLog(s0,true,0xFFFF);
- Serial.println(s0);
- // Connect to WPA/WPA2 network
- // ssid.remove(ssid.length());
- // pass.remove(pass.length());
- status = WiFi.begin(c_str1(ssid),c_str1(pass));
- delay(1000);
- }
- char bufIP[16];
- sprintf(bufIP, "IP:%d.%d.%d.%d", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3] );
- UpdStaticLog(s0,false,0x0000);
- UpdStaticLog("Connected. " +String(bufIP),true,0xFFFF);
- udp.begin(localPort);
- delay(2000);
- UpdStaticLog("Connected. " +String(bufIP),false,0x0000);
- // reserve 200 bytes for the inputString:
- }
- void quadro_read()
- {
- // se1();
- udp_read1();
- if (sc)
- {
- //showl(is);
- tft.setTextColor(0xFFFF);
- tft.setCursor(0, 0);
- //setlen(is,is.indexOf('\n'));
- // is = is.substring(is.indexOf(':'));
- tft.print(is);
- Serial.print(is);
- // delay(10);
- tft.setCursor(0, 0);
- tft.setTextColor(0x0000);
- tft.print(is);
- is = "";
- sc = false;
- udp.beginPacket(Ip, rPort);
- udp.write(String("A"+String(random(67))+'\n'+String("S"+String(random(333))+'\n')).c_str()) ;
- udp.endPacket();
- }
- }
- void loop() {
- quadro_read() ;
- }
- void udp_read1()
- {
- int packetSize = udp.parsePacket();
- if (packetSize)
- {
- sc = true;
- is = udp.readStringUntil('\n')+'\n';
- //is = is.substring(is.indexOf(':')+1);
- sc = true;
- }
- // memset(data, 0, sizeof(incomingPacket));
- }
- void udp_read()
- {
- char data[255];
- int packetSize = udp.parsePacket();
- if (packetSize)
- {
- sc = true;
- int len = udp.read(data, 255);
- char* is_udp = new char[len];
- // is_udp = data;
- memcpy(is_udp,String(data).c_str(),len);
- is = String(is_udp);
- is_udp = NULL;
- delete is_udp;
- }
- delay(10);
- // memset(data, 0, sizeof(incomingPacket));
- }
- void se1() {
- while (Serial2.available()) {
- is = Serial2.readStringUntil('\n')+'\n';
- is = is.substring(is.indexOf(':')+1);
- sc = true;
- }
- }
- void se() {
- while (Serial2.available()) {
- // get the new byte:
- char inChar = (char)Serial2.read();
- // add it to the inputString:
- is += inChar;
- // if the incoming character is a newline, set a flag
- // so the main loop can do something about it:
- if (inChar == '\n') {
- sc = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement