Advertisement
greenmikey

Untitled

Sep 29th, 2022
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. /*
  2. * ESP8266 NodeMCU AJAX Demo
  3. * Updates and Gets data from webpage without page refresh
  4. */
  5. #include <ESP8266WiFi.h>
  6. #include <WiFiClient.h>
  7. #include <ESP8266WebServer.h>
  8. #ifndef UNIT_TEST
  9. #include <Arduino.h>
  10. #endif
  11. #include <IRremoteESP8266.h>
  12. #include <IRsend.h>
  13. #define LED 2 //On board LED
  14. #define IR_LED 4 // ESP8266 GPIO pin to use. Recommended: 4 (D2).
  15. #include "index.h" //Our HTML webpage contents with javascripts
  16.  
  17. IRsend irsend(IR_LED); // (pin, not inverted, and use modulation) Set the GPIO to be used to sending the message.
  18.  
  19. String readText;
  20. unsigned int khz = 38; // 38kHz carrier frequency for infoglobe
  21. unsigned char header[] = {0x04}; //0x01: static, 0x04: scrolling
  22. unsigned char message[35] = {"Thank you very much Crankyoldgit"};
  23.  
  24. //SSID and Password of your WiFi router
  25. const char* ssid = "xxxxx";
  26. const char* password = "xxxxx";
  27.  
  28. ESP8266WebServer server(80); //Server on port 80
  29.  
  30. //===============================================================
  31. // This routine is executed when you open its IP in browser
  32. //===============================================================
  33. void handleRoot() {
  34. String s = MAIN_page; //Read HTML contents
  35. server.send(200, "text/html", s); //Send web page
  36. }
  37.  
  38. void handleReadMessage() {
  39. server.send(200, "text/plane", readText); //Send ADC value only to client ajax request
  40. }
  41.  
  42. void handleMessage() {
  43. readText = server.arg("textbox1"); //Refer xhttp.open("GET", "setLED?LEDstate="+led, true);
  44. Serial.print("Current Message: ");
  45. Serial.println(readText);
  46. server.send(200, "text/plane", readText); //Send web page
  47. memset(&message[0], 0, sizeof(message));
  48. for (int j = 0; j < readText.length(); ++j) {
  49. message[j] = readText[j];
  50. }
  51. for (int i = 0; i < sizeof(message); ++i) {
  52. if (message[i] == 0x00) {
  53. Serial.println("null char");
  54. }
  55. else {
  56. Serial.println(message[i]);
  57. }
  58. }
  59. sendHexRaw(header, 1, 38); //header at 38kHz
  60. sendHexRaw(message, readText.length(), 38); // Send a raw data capture at 38kHz.
  61. digitalWrite(IR_LED, LOW);
  62. delay(100);
  63. }
  64. //==============================================================
  65. // SETUP
  66. //==============================================================
  67. void setup(void){
  68. readText = "Thank you very much Crankyoldgit";
  69.  
  70. Serial.begin(115200);
  71.  
  72. WiFi.begin(ssid, password); //Connect to your WiFi router
  73. Serial.println("");
  74.  
  75. //Onboard LED port Direction output
  76. pinMode(LED,OUTPUT);
  77.  
  78. // Wait for connection
  79. while (WiFi.status() != WL_CONNECTED) {
  80. delay(500);
  81. Serial.print(".");
  82. }
  83.  
  84. //If connection successful show IP address in serial monitor
  85. Serial.println("");
  86. Serial.print("Connected to ");
  87. Serial.println(ssid);
  88. Serial.print("IP address: ");
  89. Serial.println(WiFi.localIP()); //IP address assigned to your ESP
  90.  
  91. server.on("/", handleRoot); //Which routine to handle at root location. This is display page
  92. server.on("/setMessage", handleMessage);
  93. server.on("/readMessage", handleReadMessage);
  94.  
  95. server.begin(); //Start server
  96. Serial.println("HTTP server started");
  97. irsend.begin();
  98. Serial.begin(115200, SERIAL_8N1);
  99. readText = "Thank you very much Crankyoldgit"; //current implementation has this match 'message'
  100.  
  101. }
  102. //==============================================================
  103. // LOOP
  104. //==============================================================
  105. void loop(void){
  106. server.handleClient(); //Handle client requests
  107. }
  108.  
  109. void sendHexRaw(unsigned char *sigArray, unsigned int sizeArray, unsigned int khz) {
  110. /* HEADER - the byte determines the transition effect
  111. When debugging note that first four bits of header should always be '0'
  112. 00 - loads message to buffer for transition effect
  113. 01 - Static message - blanks without message
  114. 02 - Flashing static message
  115. 03 - matches static/scroll of previous message
  116. 04 - scrolling (blanks without message)
  117. 05 - Overwrite portion of existing message
  118. 06 - Toggles scrolling when sent without a message
  119.  
  120. MESSAGE - 35 characters max - if sending a message each byte is an ASCII character.
  121. characters not supported: % & + ; @ [ \ ] ^ _ ` { | } ~
  122.  
  123. see http://hanixdiy.blogspot.com/2010/10/hacking-infoglobe-part-3.html for more details
  124. */
  125. uint32_t sigTime = micros();
  126. uint32_t delayTime;
  127. irsend.enableIROut(khz,33);
  128. for (unsigned int i = 0; i < sizeArray; i++) { //iterate thru each byte in sigArray
  129. register uint8_t bitMask = 0x80; //starting value of bitmask fo each Hex byte
  130.  
  131. while (bitMask) { //do 8 times for each bit of the 8 bit byte
  132. sigTime += 1000; //Time 1ms after our last operation (or start)
  133. delayTime = sigTime - micros(); //The difference between current time and 1ms after our last bit
  134. if (bitMask & sigArray[i]) { //its a One bit
  135. irsend.space(delayTime); // LED off for 1000 usec = 1msec
  136. //Serial.print('1'); //for debug - ruins timing but ensures you use the correct bits
  137. }
  138. else { // its a Zero bit
  139. irsend.mark(delayTime);
  140. //Serial.print('0'); //for debug - ruins timing but ensures you use the correct bits
  141. }
  142.  
  143. bitMask = (unsigned char) bitMask >> 1; // shift mask bit along until it reaches zero > exit the loop
  144. }
  145. }
  146. }
  147.  
Tags: infoglobe
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement