Advertisement
ununununun

PLDUINO test + AiThinker v1.1.1 + WiFiEsp

Sep 28th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.23 KB | None | 0 0
  1. #include <PLDuino.h>
  2. #include <PLDuinoGUI.h>
  3. #include <TMRpcm_PLDuino.h>
  4. #include <SPI.h>
  5. #include <SD.h>
  6. #include <Adafruit_GFX.h>
  7. #include <Adafruit_ILI9341.h>
  8. #include <PLDuino.h>
  9. #include <PLDTouch.h>
  10. #include <PLDuinoGUI.h>
  11. #include <using_namespace_PLDuinoGUI.h>
  12. #include <DS3232RTC.h>
  13. #include <TimeLib.h>
  14. #include <Wire.h>
  15. #include <avr/io.h>
  16. #include <SPI.h>
  17. #include <SD.h>
  18. #include "WiFiEsp.h"
  19. #include <WiFiEspUdp.h>
  20. #include <EEPROM.h>
  21.  
  22.  
  23. #define FILENAME "xtre.cfg"
  24. File f;
  25. WiFiEspUDP udp;
  26. int status = WL_IDLE_STATUS;
  27. String s = "";
  28. String is = "" ;
  29. boolean sc = false;
  30. bool CFG_OK = false;
  31.   String ssid, pass;
  32. unsigned int localPort = 9999;  // local port to listen on
  33. unsigned int rPort = 9996;  // local port to listen on
  34. IPAddress Ip(192, 168, 100, 255);
  35.  
  36. char packetBuffer[255];          // buffer to hold incoming packet
  37. char ReplyBuffer[] = "___";      // a string to send back
  38.  
  39.  
  40.  
  41. Label l("", ILI9341_WHITE);
  42.  
  43.  
  44. Adafruit_ILI9341 tft = Adafruit_ILI9341(PLDuino::LCD_CS, PLDuino::LCD_DC);
  45. PLDTouch touch(PLDuino::TOUCH_CS, PLDuino::TOUCH_IRQ);
  46.  
  47.  
  48. char* s2c(String command){
  49.     if(command.length()!=0){
  50.         char *p = const_cast<char*>(command.c_str());
  51.         return p;
  52.     }
  53. }
  54.  
  55. void showl(String s)
  56. {
  57.  
  58.   l.updateText(s, tft);
  59. }
  60.  
  61. void UpdStaticLog(String s, bool visible, uint16_t color)
  62. {
  63. //showl(is);
  64.   tft.setCursor(0, 0);
  65.   tft.setTextColor(0x0000);
  66.   tft.println(s);
  67.   if (!visible) return;
  68.   tft.setTextColor(color);
  69.   tft.setCursor(0, 0);
  70.   tft.println(s);
  71. }
  72.  
  73. const char* c_str0(String str) {
  74.   char* buf = new char[str.length()];
  75.   str.getBytes(buf, str.length()+1, 0);
  76.   return reinterpret_cast<const char*>(buf);
  77. }
  78.  
  79. const char* c_str1(String str) {
  80.   char* buf = new char[str.length()];
  81.   str.getBytes(buf, str.length()+1, 0);
  82.   strcpy(reinterpret_cast<const char*>(buf),buf);
  83.   return buf;
  84. }
  85.  
  86. void setup() {
  87.   PLDuino::init();
  88.   PLDuino::enableLCD();
  89.   PLDuino::enableESP();
  90.  
  91.   tft.begin();
  92.   tft.fillScreen(ILI9341_BLACK);
  93.   tft.setRotation(3);
  94.   tft.setTextSize(1);
  95. // l.setPositionAndSize(0,10,280,20);
  96. //l.setTextSize(3);
  97. // l.draw(tft);
  98.  
  99. // initialize serial:
  100.   Serial.begin(115200);
  101.   Serial2.begin(115200);
  102.   WiFi.init(&Serial2);
  103.  
  104.  
  105.   while (!SD.begin(PLDuino::SD_CS))
  106.   {
  107.  
  108.     UpdStaticLog("Failed to initialize SD card. Please check it.", true, 0xF800);
  109.     delay(1000);
  110.     UpdStaticLog("Failed to initialize SD card. Please check it.", false, 0x0000);
  111.     delay(100);
  112.   }
  113.   UpdStaticLog("Failed to initialize SD card. Please check it.", false, 0x0000);
  114.   UpdStaticLog("SD card init OK. Loading config...", true, 0xFFFF);
  115.   File f = SD.open(FILENAME, FILE_READ);
  116.   String fs = "SHITTTTTTTTTTT";
  117.  
  118.   if (!f)
  119.   {
  120. // Stop on error
  121.     UpdStaticLog("Failed to open " FILENAME ". Stop.", true, 0xF800);
  122.     return;
  123.   }
  124.   while (f.available() > 0)
  125.   {
  126.     fs = f.readStringUntil('\n');
  127.  
  128.     if (fs.indexOf("_apc]") > 0)
  129.     {
  130.       ssid = f.readStringUntil('\n');
  131.       pass = f.readStringUntil('\n');
  132.       break;
  133.     }
  134.  
  135.   }
  136.   f.close();
  137.   delay(1000);
  138.   Serial.println(pass.c_str());
  139.   UpdStaticLog("SD card init OK. Loading config...", false, 0x0000);
  140.   UpdStaticLog("SSID=" + String(ssid) + "\nPASS=" + String(pass), true, 0xFFFF);
  141.   delay(1000);
  142.   UpdStaticLog("SSID=" + String(ssid) + "\nPASS=" + String(pass), false, 0x0000);
  143.    byte l0 = ssid.length()+1;
  144.    byte l1 = pass.length()+1;
  145.    char ssid0[l0];
  146.    char pass0[l1];
  147.     ssid.toCharArray(ssid0,l0);
  148.     pass.toCharArray(pass0,l1);
  149.     if (WiFi.status() == WL_NO_SHIELD) {
  150.     Serial.println("WiFi shield not present");
  151.     // don't continue:
  152.     while (true);
  153.   }
  154.      ssid.replace(0xd,'\0');
  155.      ssid.replace(' ','\0');
  156.      pass.replace(' ','\0');
  157.      pass.replace(0xd,'\0');
  158.      
  159.    String s0 = "Attempting to connect to WPA SSID: "+String(ssid.c_str());
  160.     while ( status != WL_CONNECTED) {
  161.       UpdStaticLog(s0,false,0x0000);
  162.       UpdStaticLog(s0,true,0xFFFF);
  163.        Serial.println(s0);
  164.     // Connect to WPA/WPA2 network
  165.      // ssid.remove(ssid.length());
  166.    //   pass.remove(pass.length());
  167.  
  168.       status = WiFi.begin(c_str1(ssid),c_str1(pass));
  169.       delay(1000);
  170.   }
  171.     char bufIP[16];
  172.     sprintf(bufIP, "IP:%d.%d.%d.%d", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3] );
  173.     UpdStaticLog(s0,false,0x0000);
  174.     UpdStaticLog("Connected. " +String(bufIP),true,0xFFFF);
  175.  
  176.     udp.begin(localPort);
  177.     delay(2000);
  178.     UpdStaticLog("Connected. " +String(bufIP),false,0x0000);
  179.  
  180. // reserve 200 bytes for the inputString:
  181. }
  182.  
  183. void quadro_read()
  184. {
  185.  // se1();
  186.   udp_read1();
  187.   if (sc)
  188.   {
  189. //showl(is);
  190.     tft.setTextColor(0xFFFF);
  191.     tft.setCursor(0, 0);
  192. //setlen(is,is.indexOf('\n'));
  193.   //  is = is.substring(is.indexOf(':'));
  194.     tft.print(is);
  195.     Serial.print(is);
  196. // delay(10);
  197.     tft.setCursor(0, 0);
  198.     tft.setTextColor(0x0000);
  199.     tft.print(is);
  200.     is = "";
  201.     sc = false;
  202.     udp.beginPacket(Ip, rPort);
  203.     udp.write(String("A"+String(random(67))+'\n'+String("S"+String(random(333))+'\n')).c_str())  ;
  204.     udp.endPacket();
  205.  
  206.   }
  207. }
  208.  
  209. void loop() {
  210.   quadro_read() ;
  211.  
  212. }
  213.  
  214.  
  215. void udp_read1()
  216. {
  217.  
  218.   int packetSize = udp.parsePacket();
  219.   if (packetSize)
  220.   {
  221.    sc = true;
  222.     is = udp.readStringUntil('\n')+'\n';
  223.     //is = is.substring(is.indexOf(':')+1);
  224.     sc = true;
  225.   }
  226.  
  227.   // memset(data, 0, sizeof(incomingPacket));
  228. }
  229.  
  230.  
  231. void udp_read()
  232. {
  233.   char data[255];
  234.   int packetSize = udp.parsePacket();
  235.   if (packetSize)
  236.   {
  237.    sc = true;
  238.    int len = udp.read(data, 255);
  239.  
  240.     char* is_udp = new char[len];
  241.   // is_udp = data;
  242.    memcpy(is_udp,String(data).c_str(),len);
  243.    is = String(is_udp);
  244.   is_udp = NULL;
  245.   delete is_udp;
  246.   }
  247.  
  248.    delay(10);
  249.   // memset(data, 0, sizeof(incomingPacket));
  250. }
  251.  
  252. void se1() {
  253.   while (Serial2.available()) {
  254.     is = Serial2.readStringUntil('\n')+'\n';
  255.     is = is.substring(is.indexOf(':')+1);
  256.     sc = true;
  257.   }
  258. }
  259.  
  260.  
  261. void se() {
  262.   while (Serial2.available()) {
  263. // get the new byte:
  264.     char inChar = (char)Serial2.read();
  265. // add it to the inputString:
  266.     is += inChar;
  267. // if the incoming character is a newline, set a flag
  268. // so the main loop can do something about it:
  269.     if (inChar == '\n') {
  270.       sc = true;
  271.     }
  272.   }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement