Left for archival purposes.

User avatar
By Mikejstb
#12289 Sure - here it is. The usual apologies for poor coding - I'm learning Lua as I go along.
I don't know yet how to clear the display, the ug8 world is kind of odd, i'm trying to learn
how to work with it.

My overall environment is a bunch of ESP modules sending mqtt messages with temperature, humidity, barometer, battery level, sometimes light level - all sent to a mosquitto broker running on a Raspberry Pi, which also runs Node Red to process the messages, save data to a mysql database, shoot data out to ThingSpeak to graph, etc.

I'm trying to use Node Red for the heavy processing to keep the ESPs from having to do too much more than collecting data and also controlling outlets & lights. Lots of fun!
Learning how to work with the ESPs, Lua, Node Red, and all is a fun challange!

I'd love to see how you get this into the flashmod setup - I've been following that thread but I just don't understand it yet.

Code: Select all --Init 
DeviceID="ESP-12_Light_Control" 
Broker="192.168.2.177" 
mTopic = "home/#"
topic = mTopic
myStr0 = "  mqtt Message Center"
myStr1 = "   topic = "..topic
myStr2 = "Current Date/Time-UTC ="
myStr3 = "" -- place holders for the other lines
myStr4 = ""
myStr5 = ""
myStr6 = ""
myStr7 = ""
gotEm = 0

function init_i2c_display()
     -- SDA and SCL can be assigned freely to available GPIOs
     sda = 5 -- GPIO14
     scl = 6 -- GPIO12
     sla = 0x3c
     i2c.setup(0, sda, scl, i2c.SLOW)
     disp = u8g.ssd1306_128x64_i2c(sla)
end

-- graphic test components
function prepare()
     disp:setFont(u8g.font_6x10)
     disp:setFontRefHeightExtendedText()
     disp:setDefaultForegroundColor()
     disp:setFontPosTop()
end


function draw(void)
--this gets called in an alarm loop to keep the display refreshed.
-- it prints the global strings myStr - so we just change those strings
-- to change what is displayed
    prepare()
    disp:drawStr(0,0,myStr0)
    disp:drawStr(0,8,myStr1)
    disp:drawStr(0,16,myStr2)
    disp:drawStr(0,24,myStr3)
    disp:drawStr(0,32,myStr4)
    disp:drawStr(0,40,myStr5)
    disp:drawStr(0,48,myStr6)
    disp:drawStr(0,56,myStr7)
end


function loop(void)
-- this is the loop that u8g apparently needs for display
  disp:firstPage()
  repeat
    draw()
  until disp:nextPage() == false
 end

 function listen()
 -- this is where the ESP listens for mqtt messages and deals with displaying them
 m:on("message", function(conn, topic, data)
 
   print(topic .. ":" ..data)
   if (topic == "home/time") then -- my node-red server sends out the date/time every minute
      myStr3 = data
   elseif gotEm == 0 then   -- this is for shuffling the messages on
      myStr4 = topic      -- the display. First message topic goes to
      myStr5 = "  "..data  -- line 4, message to line 5
      gotEm = 1
   elseif gotEm == 1 then -- when the second message comes in put it below
      myStr6 = topic     -- the first
      myStr7 = "  "..data
      gotEm = 2
   elseif gotEm == 2 then -- after the first two when another comes in shuffle
      myStr4 = myStr6 --   everything  up a line
      myStr5 = myStr7
      myStr6 = topic
      myStr7 = "  "..data -- if memory was plentiful we could store more and maybe scroll
   end                  -- thru them...
end)
 end -- of listen function
 
 function mqtt_sub()     -- here's where we subscribe to the topic we want to follow
    m:subscribe("home/#",0, function(conn)   
      print("Mqtt Subscribed home/# ") 
      listen() --run the subscription function 
 
    end) 
 end 

 m = mqtt.Client("ESP8266", 180, "uuuuu", "pppppp")  -- stuff to deal
 m:lwt("/lwt", "ESP8266", 0, 0)           -- with mqtt reconnects
 m:on("offline", function(con)   
    print ("Mqtt Reconnecting...")   
    tmr.alarm(1, 5000, 0, function()
print ("should connect here...")   
      m:connect(Broker, 1883, 0, function(conn)   
        print("Mqtt Connected to:" .. Broker) 
        mqtt_sub() --run the subscription function 
      end) 
    end) 
 end) 

 -- below is initial set up of the mqtt connection
 tmr.alarm(0, 1000, 1, function() 
  if wifi.sta.status() == 5 and wifi.sta.getip() ~= nil then 
    tmr.stop(0) 
    m:connect(Broker, 1883, 0, function(conn)   
      print("Mqtt Connected to:" .. Broker) 
     mqtt_sub()
   end) 
  end 
 end) 
 
-- below is to set up the timer to keep hitting the display
init_i2c_display()
tmr.alarm(3,1000,1,function()   
   disp:firstPage()
   repeat
      draw()
   until disp:nextPage() == false
end)


User avatar
By Mikejstb
#12363 further size reduction - replaced the -201 with a -12.
Still on protoboard.
Next step will be make a PC to let me stack the ESP, the oled, and a lipo with mini charger.
I'm waiting for some 1.2" displays to arrive, my old eyes are struggling with the .96"!
Maybe some acrylic, make a nice little clock-sized cube unit.
mqtt-2.jpg
mqtt-2.jpg (30.34 KiB) Viewed 5753 times


I may play and see if u8g sleepOn and sleepOff is implemented - I've heard that these displays can burn in if you display static data for too long.

Although the final project will be battery powered, so I think I'll just have a push button that runs for 5 minutes or so then shuts down.
I think it is awesome that with two tiny modules, four or five wires, and some programming such a fun little thing can be made. Compared to the old 8051 times - it's a wonderful time to be alive!

Thanks again for the terrific library!
User avatar
By alon24
#12570 I thought I would contribute back.

While following the code form mqtt_oled
I saw that the init was not the same as in
https://github.com/adafruit/Adafruit_SS ... SD1306.cpp

so I changed it in the c code (I am dont with lua), and this displays correctly (not upside down)now.
I am thinking that if you had the code from that project , this would solve the isssue.
Code: Select all // Init sequence for 128x64 OLED module
       OLED_writeCmd(0xAE);                    // 0xAE
       OLED_writeCmd(0xD5);            // 0xD5
       OLED_writeCmd(0x80);                                  // the suggested ratio 0x80
       OLED_writeCmd(0xA8);                  // 0xA8
       OLED_writeCmd(0x3F);
       OLED_writeCmd(0xD3);              // 0xD3
       OLED_writeCmd(0x0);                                   // no offset
       OLED_writeCmd(0x40 | 0x0);            // line #0
       OLED_writeCmd(0x8D);                    // 0x8D

//       if (vccstate == SSD1306_EXTERNALVCC)
//         { OLED_writeCmd(0x10); }
//       else
//         { OLED_writeCmd(0x14); }
       OLED_writeCmd(0x14);


       OLED_writeCmd(0x20);                    // 0x20
       OLED_writeCmd(0x00);                                  // 0x0 act like ks0108
       OLED_writeCmd(0xA0 | 0x1);
       OLED_writeCmd(0xC8);
       OLED_writeCmd(0xDA);                    // 0xDA
       OLED_writeCmd(0x12);
       OLED_writeCmd(0x81);                   // 0x81
//       if (vccstate == SSD1306_EXTERNALVCC)
//         { OLED_writeCmd(0x9F); }
//       else
//         { OLED_writeCmd(0xCF); }
       OLED_writeCmd(0xCF);

       OLED_writeCmd(0xd9);                  // 0xd9
//       if (vccstate == SSD1306_EXTERNALVCC)
//         { OLED_writeCmd(0x22); }
//       else
//         { OLED_writeCmd(0xF1); }
       OLED_writeCmd(0xF1);
       OLED_writeCmd(0xDB);                 // 0xDB
       OLED_writeCmd(0x40);
       OLED_writeCmd(0xA4);           // 0xA4
       OLED_writeCmd(0xA6);                 // 0xA6
       OLED_writeCmd(0xaf); // turn on oled panel
       OLED_Fill(0x00);  //OLED CLS
User avatar
By Toshi Bass
#12591 [quote="pracas"]Dear All,
Thanks for all the replies. I've been on a holiday and just back. Let me check the issues raised.
Reset -> Last i had the display running for 2 hours and there was no reset issue. I tested with a ESP12 module[quote]
I have had you code successfully running for about 2 weeks now displaying the state of 2 relays and a ds18b20 and it works great much easier to use than u8g , but free memory is the problem, my code is stripped down to the bare minimum and its on the edge - the biggest issue with the code is its based on nodeMCU build 20150127 which means that I cannot use node.compile() on my modules to save memory, it would be really useful if you could recompile your code using nodeMCU build 20150318 .. thanks..